Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 825b166 | 2012-03-12 19:07:31 | [diff] [blame] | 5 | #include "content/browser/browser_url_handler_impl.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
Peter Kasting | 1557e5f | 2025-01-28 01:14:08 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | |
[email protected] | 10994d13 | 2013-06-11 07:16:18 | [diff] [blame] | 11 | #include "base/strings/string_util.h" |
danakj | e94b7c84 | 2020-09-16 18:47:43 | [diff] [blame] | 12 | #include "content/browser/renderer_host/debug_urls.h" |
[email protected] | d235345 | 2012-01-19 19:53:56 | [diff] [blame] | 13 | #include "content/browser/webui/web_ui_impl.h" |
[email protected] | 87f3c08 | 2011-10-19 18:07:44 | [diff] [blame] | 14 | #include "content/public/browser/content_browser_client.h" |
Hans Wennborg | 5ffd139 | 2019-10-16 11:00:02 | [diff] [blame] | 15 | #include "content/public/common/content_client.h" |
[email protected] | a1d2916 | 2011-10-14 17:14:03 | [diff] [blame] | 16 | #include "content/public/common/url_constants.h" |
clamy | 7fced7b | 2017-11-16 19:52:43 | [diff] [blame] | 17 | #include "content/public/common/url_utils.h" |
Gyuyoung Kim | 107c2a0 | 2021-04-13 01:49:30 | [diff] [blame] | 18 | #include "third_party/blink/public/common/chrome_debug_urls.h" |
[email protected] | 707e1c4 | 2013-07-09 21:18:58 | [diff] [blame] | 19 | #include "url/gurl.h" |
[email protected] | cd3d789 | 2009-03-04 23:55:06 | [diff] [blame] | 20 | |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 21 | namespace content { |
[email protected] | 825b166 | 2012-03-12 19:07:31 | [diff] [blame] | 22 | |
[email protected] | cd3d789 | 2009-03-04 23:55:06 | [diff] [blame] | 23 | // Handles rewriting view-source URLs for what we'll actually load. |
[email protected] | 4654bfe | 2013-08-26 03:36:58 | [diff] [blame] | 24 | static bool HandleViewSource(GURL* url, BrowserContext* browser_context) { |
Tom Sepez | 442e88e | 2024-08-01 18:38:16 | [diff] [blame] | 25 | if (!url->SchemeIs(kViewSourceScheme)) { |
| 26 | return false; |
[email protected] | cd3d789 | 2009-03-04 23:55:06 | [diff] [blame] | 27 | } |
Tom Sepez | 442e88e | 2024-08-01 18:38:16 | [diff] [blame] | 28 | |
| 29 | // Load the inner URL instead. |
| 30 | *url = GURL(url->GetContent()); |
| 31 | |
| 32 | // https://crbug.com/40077794: limit view-source to view the content and |
| 33 | // not any other kind of 'active' url scheme like 'javascript' or 'data'. |
| 34 | std::vector<std::string> all_allowed_sub_schemes({ |
| 35 | url::kHttpScheme, |
| 36 | url::kHttpsScheme, |
| 37 | kChromeUIScheme, |
| 38 | url::kFileScheme, |
| 39 | url::kFileSystemScheme, |
| 40 | }); |
| 41 | |
| 42 | // Merge all the schemes for which view-source is allowed by default, with |
| 43 | // the view-source schemes defined by the ContentBrowserClient. |
| 44 | GetContentClient()->browser()->GetAdditionalViewSourceSchemes( |
| 45 | &all_allowed_sub_schemes); |
| 46 | |
| 47 | for (const auto& allowed_sub_scheme : all_allowed_sub_schemes) { |
| 48 | if (url->SchemeIs(allowed_sub_scheme)) { |
| 49 | return true; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | *url = GURL(url::kAboutBlankURL); |
[email protected] | cd3d789 | 2009-03-04 23:55:06 | [diff] [blame] | 54 | return false; |
| 55 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 56 | |
[email protected] | 38178a4 | 2009-12-17 18:58:32 | [diff] [blame] | 57 | // Turns a non view-source URL into the corresponding view-source URL. |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 58 | static bool ReverseViewSource(GURL* url, BrowserContext* browser_context) { |
[email protected] | 38178a4 | 2009-12-17 18:58:32 | [diff] [blame] | 59 | // No action necessary if the URL is already view-source: |
[email protected] | dbdda540 | 2013-05-30 22:13:48 | [diff] [blame] | 60 | if (url->SchemeIs(kViewSourceScheme)) |
[email protected] | 38178a4 | 2009-12-17 18:58:32 | [diff] [blame] | 61 | return false; |
sgurun | 939a726c | 2014-10-15 20:21:18 | [diff] [blame] | 62 | // Recreate the url with the view-source scheme. |
| 63 | *url = GURL(kViewSourceScheme + std::string(":") + url->spec()); |
[email protected] | 38178a4 | 2009-12-17 18:58:32 | [diff] [blame] | 64 | return true; |
| 65 | } |
| 66 | |
[email protected] | 2abef1a | 2013-10-17 23:31:16 | [diff] [blame] | 67 | static bool DebugURLHandler(GURL* url, BrowserContext* browser_context) { |
[email protected] | 8bf104801 | 2012-02-08 01:22:18 | [diff] [blame] | 68 | // Circumvent processing URLs that the renderer process will handle. |
Gyuyoung Kim | 107c2a0 | 2021-04-13 01:49:30 | [diff] [blame] | 69 | return blink::IsRendererDebugURL(*url); |
[email protected] | 8bf104801 | 2012-02-08 01:22:18 | [diff] [blame] | 70 | } |
| 71 | |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 72 | // static |
| 73 | BrowserURLHandler* BrowserURLHandler::GetInstance() { |
[email protected] | 825b166 | 2012-03-12 19:07:31 | [diff] [blame] | 74 | return BrowserURLHandlerImpl::GetInstance(); |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 75 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 76 | |
| 77 | // static |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 78 | BrowserURLHandler::URLHandler BrowserURLHandler::null_handler() { |
[email protected] | f79f486 | 2012-02-16 16:52:42 | [diff] [blame] | 79 | // Required for VS2010: http://connect.microsoft.com/VisualStudio/feedback/details/520043/error-converting-from-null-to-a-pointer-type-in-std-pair |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 80 | return nullptr; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 81 | } |
| 82 | |
[email protected] | 825b166 | 2012-03-12 19:07:31 | [diff] [blame] | 83 | // static |
| 84 | BrowserURLHandlerImpl* BrowserURLHandlerImpl::GetInstance() { |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 85 | return base::Singleton<BrowserURLHandlerImpl>::get(); |
[email protected] | 825b166 | 2012-03-12 19:07:31 | [diff] [blame] | 86 | } |
| 87 | |
Lukasz Anforowicz | 7b07879 | 2020-10-20 17:04:31 | [diff] [blame] | 88 | BrowserURLHandlerImpl::BrowserURLHandlerImpl() { |
[email protected] | 2abef1a | 2013-10-17 23:31:16 | [diff] [blame] | 89 | AddHandlerPair(&DebugURLHandler, BrowserURLHandlerImpl::null_handler()); |
[email protected] | 8bf104801 | 2012-02-08 01:22:18 | [diff] [blame] | 90 | |
alexmos | 94875b3b | 2017-03-16 22:19:01 | [diff] [blame] | 91 | // view-source: should take precedence over other rewriters, so it's |
| 92 | // important to add it before calling up to the content client. |
[email protected] | b8148ac | 2011-07-13 22:03:25 | [diff] [blame] | 93 | AddHandlerPair(&HandleViewSource, &ReverseViewSource); |
alexmos | 94875b3b | 2017-03-16 22:19:01 | [diff] [blame] | 94 | |
| 95 | GetContentClient()->browser()->BrowserURLHandlerCreated(this); |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 96 | } |
| 97 | |
[email protected] | 825b166 | 2012-03-12 19:07:31 | [diff] [blame] | 98 | BrowserURLHandlerImpl::~BrowserURLHandlerImpl() { |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 99 | } |
| 100 | |
[email protected] | 825b166 | 2012-03-12 19:07:31 | [diff] [blame] | 101 | void BrowserURLHandlerImpl::AddHandlerPair(URLHandler handler, |
| 102 | URLHandler reverse_handler) { |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 103 | url_handlers_.push_back(HandlerPair(handler, reverse_handler)); |
| 104 | } |
| 105 | |
[email protected] | 825b166 | 2012-03-12 19:07:31 | [diff] [blame] | 106 | void BrowserURLHandlerImpl::RewriteURLIfNecessary( |
[email protected] | 3d7474ff | 2011-07-27 17:47:37 | [diff] [blame] | 107 | GURL* url, |
Lukasz Anforowicz | b2eb19b1 | 2020-01-25 00:40:42 | [diff] [blame] | 108 | BrowserContext* browser_context) { |
| 109 | DCHECK(url); |
| 110 | DCHECK(browser_context); |
| 111 | bool ignored_reverse_on_redirect; |
| 112 | RewriteURLIfNecessary(url, browser_context, &ignored_reverse_on_redirect); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 113 | } |
[email protected] | 38178a4 | 2009-12-17 18:58:32 | [diff] [blame] | 114 | |
Devlin Cronin | 9d570a2 | 2020-06-03 16:00:59 | [diff] [blame] | 115 | std::vector<GURL> BrowserURLHandlerImpl::GetPossibleRewrites( |
| 116 | const GURL& url, |
| 117 | BrowserContext* browser_context) { |
| 118 | std::vector<GURL> rewrites; |
| 119 | for (const auto& it : url_handlers_) { |
| 120 | const URLHandler& handler = it.first; |
| 121 | if (!handler) |
| 122 | continue; |
| 123 | |
| 124 | GURL mutable_url(url); |
| 125 | if (handler(&mutable_url, browser_context)) |
| 126 | rewrites.push_back(std::move(mutable_url)); |
| 127 | } |
| 128 | |
| 129 | return rewrites; |
| 130 | } |
| 131 | |
Lukasz Anforowicz | b2eb19b1 | 2020-01-25 00:40:42 | [diff] [blame] | 132 | void BrowserURLHandlerImpl::RewriteURLIfNecessary( |
| 133 | GURL* url, |
| 134 | BrowserContext* browser_context, |
| 135 | bool* reverse_on_redirect) { |
| 136 | DCHECK(url); |
| 137 | DCHECK(browser_context); |
| 138 | DCHECK(reverse_on_redirect); |
| 139 | |
Lukasz Anforowicz | 0a131d1 | 2020-09-22 22:28:13 | [diff] [blame] | 140 | if (!url->is_valid()) { |
| 141 | *reverse_on_redirect = false; |
| 142 | return; |
| 143 | } |
| 144 | |
Lukasz Anforowicz | b2eb19b1 | 2020-01-25 00:40:42 | [diff] [blame] | 145 | for (const auto& it : url_handlers_) { |
| 146 | const URLHandler& handler = it.first; |
| 147 | bool has_reverse_rewriter = it.second; |
| 148 | if (handler && handler(url, browser_context)) { |
| 149 | *reverse_on_redirect = has_reverse_rewriter; |
| 150 | return; |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
[email protected] | d9c2e51 | 2012-10-25 18:54:36 | [diff] [blame] | 155 | bool BrowserURLHandlerImpl::ReverseURLRewrite( |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 156 | GURL* url, const GURL& original, BrowserContext* browser_context) { |
Lukasz Anforowicz | b2eb19b1 | 2020-01-25 00:40:42 | [diff] [blame] | 157 | for (const auto& it : url_handlers_) { |
| 158 | const URLHandler& handler = it.first; |
| 159 | const URLHandler& reverse_rewriter = it.second; |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 160 | if (reverse_rewriter) { |
| 161 | GURL test_url(original); |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 162 | if (!handler) { |
[email protected] | 3d7474ff | 2011-07-27 17:47:37 | [diff] [blame] | 163 | if (reverse_rewriter(url, browser_context)) |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 164 | return true; |
[email protected] | 3d7474ff | 2011-07-27 17:47:37 | [diff] [blame] | 165 | } else if (handler(&test_url, browser_context)) { |
| 166 | return reverse_rewriter(url, browser_context); |
[email protected] | f1eb87a | 2011-05-06 17:49:41 | [diff] [blame] | 167 | } |
[email protected] | 38178a4 | 2009-12-17 18:58:32 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | return false; |
| 171 | } |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 172 | |
Lukasz Anforowicz | 7b07879 | 2020-10-20 17:04:31 | [diff] [blame] | 173 | void BrowserURLHandlerImpl::RemoveHandlerForTesting(URLHandler handler) { |
| 174 | const auto it = |
Peter Kasting | 1557e5f | 2025-01-28 01:14:08 | [diff] [blame] | 175 | std::ranges::find(url_handlers_, handler, &HandlerPair::first); |
Daniel Cheng | 4d54f0a | 2025-05-26 22:59:12 | [diff] [blame] | 176 | CHECK(url_handlers_.end() != it); |
Lukasz Anforowicz | 7b07879 | 2020-10-20 17:04:31 | [diff] [blame] | 177 | url_handlers_.erase(it); |
Camille Lamy | 2baa802 | 2018-10-19 16:43:17 | [diff] [blame] | 178 | } |
| 179 | |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 180 | } // namespace content |