diff --git a/browser/net/brave_request_handler.cc b/browser/net/brave_request_handler.cc index 4ec37033befe..9ba7905c50a6 100644 --- a/browser/net/brave_request_handler.cc +++ b/browser/net/brave_request_handler.cc @@ -25,6 +25,8 @@ #include "components/prefs/pref_service.h" #include "content/public/browser/browser_task_traits.h" #include "content/public/browser/browser_thread.h" +#include "content/public/common/url_constants.h" +#include "extensions/common/constants.h" #if BUILDFLAG(ENABLE_BRAVE_REFERRALS) #include "brave/browser/net/brave_referrals_network_delegate_helper.h" @@ -42,6 +44,12 @@ #include "brave/browser/net/brave_translate_redirect_network_delegate_helper.h" #endif +static bool IsInternalScheme(std::shared_ptr ctx) { + DCHECK(ctx); + return ctx->request_url.SchemeIs(extensions::kExtensionScheme) || + ctx->request_url.SchemeIs(content::kChromeUIScheme); +} + BraveRequestHandler::BraveRequestHandler() { DCHECK_CURRENTLY_ON(content::BrowserThread::UI); SetupCallbacks(); @@ -125,7 +133,7 @@ int BraveRequestHandler::OnBeforeURLRequest( std::shared_ptr ctx, net::CompletionOnceCallback callback, GURL* new_url) { - if (before_url_request_callbacks_.empty()) { + if (before_url_request_callbacks_.empty() || IsInternalScheme(ctx)) { return net::OK; } SCOPED_UMA_HISTOGRAM_TIMER("Brave.OnBeforeURLRequest_Handler"); @@ -140,7 +148,7 @@ int BraveRequestHandler::OnBeforeStartTransaction( std::shared_ptr ctx, net::CompletionOnceCallback callback, net::HttpRequestHeaders* headers) { - if (before_start_transaction_callbacks_.empty()) { + if (before_start_transaction_callbacks_.empty() || IsInternalScheme(ctx)) { return net::OK; } ctx->event_type = brave::kOnBeforeStartTransaction; @@ -163,7 +171,9 @@ int BraveRequestHandler::OnHeadersReceived( original_response_headers, override_response_headers); } - if (headers_received_callbacks_.empty()) { + if (headers_received_callbacks_.empty() && + !ctx->request_url.SchemeIs(content::kChromeUIScheme)) { + // Extension scheme not excluded since brave_webtorrent needs it. return net::OK; } diff --git a/renderer/brave_content_settings_agent_impl_browsertest.cc b/renderer/brave_content_settings_agent_impl_browsertest.cc index 9f82eeeee536..c08faf6a07d0 100644 --- a/renderer/brave_content_settings_agent_impl_browsertest.cc +++ b/renderer/brave_content_settings_agent_impl_browsertest.cc @@ -89,8 +89,18 @@ class BraveContentSettingsAgentImplBrowserTest : public InProcessBrowserTest { ASSERT_TRUE(embedded_test_server()->Start()); url_ = embedded_test_server()->GetURL("a.com", "/iframe.html"); - iframe_url_ = embedded_test_server()->GetURL("b.com", "/simple.html"); - image_url_ = embedded_test_server()->GetURL("b.com", "/logo.png"); + cross_site_url_ = embedded_test_server()->GetURL("b.com", "/simple.html"); + cross_site_image_url_ = + embedded_test_server()->GetURL("b.com", "/logo.png"); + link_url_ = embedded_test_server()->GetURL("a.com", "/simple_link.html"); + redirect_to_cross_site_url_ = embedded_test_server()->GetURL( + "a.com", "/cross-site/b.com/simple.html"); + redirect_to_cross_site_image_url_ = + embedded_test_server()->GetURL("a.com", "/cross-site/b.com/logo.png"); + same_site_url_ = + embedded_test_server()->GetURL("sub.a.com", "/simple.html"); + same_site_image_url_ = + embedded_test_server()->GetURL("sub.a.com", "/logo.png"); top_level_page_url_ = embedded_test_server()->GetURL("a.com", "/"); top_level_page_pattern_ = ContentSettingsPattern::FromString("http://a.com/*"); @@ -136,17 +146,26 @@ class BraveContentSettingsAgentImplBrowserTest : public InProcessBrowserTest { } const GURL& url() { return url_; } - const GURL& iframe_url() { return iframe_url_; } - const GURL& image_url() { return image_url_; } + const GURL& cross_site_url() { return cross_site_url_; } + const GURL& cross_site_image_url() { return cross_site_image_url_; } + const GURL& link_url() { return link_url_; } + const GURL& redirect_to_cross_site_url() { + return redirect_to_cross_site_url_; + } + const GURL& redirect_to_cross_site_image_url() { + return redirect_to_cross_site_image_url_; + } + const GURL& same_site_url() { return same_site_url_; } + const GURL& same_site_image_url() { return same_site_image_url_; } - std::string create_image_script() { + std::string create_image_script(const GURL& url) { std::string s; s = " var img = document.createElement('img');" " img.onload = function () {" " domAutomationController.send(img.src);" " };" " img.src = '" + - image_url().spec() + + url.spec() + "';" " document.body.appendChild(img);"; return s; @@ -228,7 +247,8 @@ class BraveContentSettingsAgentImplBrowserTest : public InProcessBrowserTest { void BlockThirdPartyFingerprinting() { brave_shields::SetFingerprintingControlType(browser()->profile(), - ControlType::BLOCK_THIRD_PARTY, top_level_page_url()); + ControlType::BLOCK_THIRD_PARTY, + top_level_page_url()); } void SetFingerprintingDefault() { @@ -261,6 +281,18 @@ class BraveContentSettingsAgentImplBrowserTest : public InProcessBrowserTest { return value; } + void NavigateDirectlyToPageWithLink(const GURL& url) { + NavigateToPageWithLink(url); + content::RenderFrameHost* main_frame = contents()->GetMainFrame(); + EXPECT_EQ(main_frame->GetLastCommittedURL(), url); + } + + void RedirectToPageWithLink(const GURL& url, const GURL& final_url) { + NavigateToPageWithLink(url); + content::RenderFrameHost* main_frame = contents()->GetMainFrame(); + EXPECT_EQ(main_frame->GetLastCommittedURL(), final_url); + } + void NavigateToPageWithIframe() { ui_test_utils::NavigateToURL(browser(), url()); ASSERT_EQ(contents()->GetAllFrames().size(), 2u) @@ -275,9 +307,15 @@ class BraveContentSettingsAgentImplBrowserTest : public InProcessBrowserTest { embedded_test_server()->GetURL(origin, path)); } - void NavigateIframe() { - ASSERT_TRUE(NavigateIframeToURL(contents(), kIframeID, iframe_url())); - ASSERT_EQ(child_frame()->GetLastCommittedURL(), iframe_url()); + void NavigateIframe(const GURL& url) { + ASSERT_TRUE(NavigateIframeToURL(contents(), kIframeID, url)); + ASSERT_EQ(child_frame()->GetLastCommittedURL(), url); + } + + void NavigateCrossSiteRedirectIframe() { + ASSERT_TRUE(NavigateIframeToURL(contents(), kIframeID, + redirect_to_cross_site_url())); + ASSERT_EQ(child_frame()->GetLastCommittedURL(), cross_site_url()); } template @@ -287,8 +325,13 @@ class BraveContentSettingsAgentImplBrowserTest : public InProcessBrowserTest { private: GURL url_; - GURL iframe_url_; - GURL image_url_; + GURL cross_site_url_; + GURL cross_site_image_url_; + GURL link_url_; + GURL redirect_to_cross_site_url_; + GURL redirect_to_cross_site_image_url_; + GURL same_site_url_; + GURL same_site_image_url_; GURL top_level_page_url_; ContentSettingsPattern top_level_page_pattern_; ContentSettingsPattern first_party_pattern_; @@ -299,6 +342,20 @@ class BraveContentSettingsAgentImplBrowserTest : public InProcessBrowserTest { std::map last_referrers_; base::ScopedTempDir temp_user_data_dir_; + + void NavigateToPageWithLink(const GURL& url) { + ui_test_utils::NavigateToURL(browser(), link_url()); + content::RenderFrameHost* main_frame = contents()->GetMainFrame(); + EXPECT_EQ(main_frame->GetLastCommittedURL(), link_url()); + + std::string clickLink = + "domAutomationController.send(clickLink('" + url.spec() + "'));"; + bool success = false; + EXPECT_TRUE( + ExecuteScriptAndExtractBool(contents(), clickLink.c_str(), &success)); + EXPECT_TRUE(success); + EXPECT_TRUE(WaitForLoadStop(contents())); + } }; // See https://github.com/brave/brave-browser/issues/8937 @@ -321,7 +378,7 @@ IN_PROC_BROWSER_TEST_F(BraveContentSettingsAgentImplBrowserTest, // The iframe should have the same result as the top frame because farbling is // based on the top frame's session token. hash = -1; - NavigateIframe(); + NavigateIframe(cross_site_url()); EXPECT_TRUE( ExecuteScriptAndExtractInt(child_frame(), kGetImageDataScript, &hash)); EXPECT_EQ(kExpectedImageDataHashFarblingBalanced, hash); @@ -427,17 +484,74 @@ IN_PROC_BROWSER_TEST_F(BraveContentSettingsAgentImplBrowserTest, EXPECT_EQ(ExecScriptGetStr(kReferrerScript, contents()), ""); EXPECT_TRUE(GetLastReferrer(url()).empty()); - // Sub-resources loaded within the page get their referrer spoofed. - EXPECT_EQ(ExecScriptGetStr(create_image_script(), contents()), - image_url().spec()); - EXPECT_EQ(GetLastReferrer(image_url()), iframe_url().GetOrigin().spec()); + // Same-site sub-resources within the page get the page URL as referrer. + EXPECT_EQ( + ExecScriptGetStr(create_image_script(same_site_image_url()), contents()), + same_site_image_url().spec()); + EXPECT_EQ(GetLastReferrer(same_site_image_url()), url().spec()); + + // Cross-site sub-resources within the page get their referrer spoofed. + EXPECT_EQ( + ExecScriptGetStr(create_image_script(cross_site_image_url()), contents()), + cross_site_image_url().spec()); + EXPECT_EQ(GetLastReferrer(cross_site_image_url()), + cross_site_url().GetOrigin().spec()); + + // Same-site iframe navigations get the page get the page URL as referrer. + NavigateIframe(same_site_url()); + EXPECT_EQ(ExecScriptGetStr(kReferrerScript, child_frame()), url().spec()); + EXPECT_EQ(GetLastReferrer(same_site_url()), url().spec()); + + // Cross-site iframe navigations get their referrer spoofed. + NavigateIframe(cross_site_url()); + EXPECT_EQ(ExecScriptGetStr(kReferrerScript, child_frame()), + cross_site_url().GetOrigin().spec()); + EXPECT_EQ(GetLastReferrer(cross_site_url()), + cross_site_url().GetOrigin().spec()); + + // Same-site navigations get the original page URL as the referrer. + NavigateDirectlyToPageWithLink(same_site_url()); + EXPECT_EQ(ExecScriptGetStr(kReferrerScript, contents()), link_url().spec()); + EXPECT_EQ(GetLastReferrer(same_site_url()), link_url().spec()); + + // Cross-site navigations get no referrer. + NavigateDirectlyToPageWithLink(cross_site_url()); + EXPECT_EQ(ExecScriptGetStr(kReferrerScript, contents()), ""); + EXPECT_EQ(GetLastReferrer(cross_site_url()), ""); +} + +IN_PROC_BROWSER_TEST_F(BraveContentSettingsAgentImplBrowserTest, + DISABLED_BlockReferrerByDefaultRedirects) { + ContentSettingsForOneType settings; + content_settings()->GetSettingsForOneType( + ContentSettingsType::PLUGINS, brave_shields::kReferrers, &settings); + EXPECT_EQ(settings.size(), 0u) + << "There should not be any visible referrer rules."; + + // The initial navigation doesn't have a referrer. + NavigateToPageWithIframe(); + EXPECT_EQ(ExecScriptGetStr(kReferrerScript, contents()), ""); + EXPECT_TRUE(GetLastReferrer(url()).empty()); - // Cross-origin iframe navigations get their referrer spoofed. - NavigateIframe(); + // Cross-site sub-resources within the page get their referrer spoofed. + EXPECT_EQ( + ExecScriptGetStr(create_image_script(redirect_to_cross_site_image_url()), + contents()), + redirect_to_cross_site_image_url().spec()); + EXPECT_EQ(GetLastReferrer(cross_site_image_url()), + cross_site_url().GetOrigin().spec()); + // Cross-site iframe navigations get their referrer spoofed. + NavigateCrossSiteRedirectIframe(); EXPECT_EQ(ExecScriptGetStr(kReferrerScript, child_frame()), - iframe_url().GetOrigin().spec()); - EXPECT_EQ(GetLastReferrer(iframe_url()), iframe_url().GetOrigin().spec()); + cross_site_url().GetOrigin().spec()); + EXPECT_EQ(GetLastReferrer(redirect_to_cross_site_url()), + cross_site_url().GetOrigin().spec()); + + // Cross-site navigations get no referrer. + RedirectToPageWithLink(redirect_to_cross_site_url(), cross_site_url()); + EXPECT_EQ(ExecScriptGetStr(kReferrerScript, contents()), ""); + EXPECT_EQ(GetLastReferrer(redirect_to_cross_site_url()), ""); } IN_PROC_BROWSER_TEST_F(BraveContentSettingsAgentImplBrowserTest, @@ -449,17 +563,77 @@ IN_PROC_BROWSER_TEST_F(BraveContentSettingsAgentImplBrowserTest, EXPECT_EQ(ExecScriptGetStr(kReferrerScript, contents()), ""); EXPECT_TRUE(GetLastReferrer(url()).empty()); - // Sub-resources loaded within the page get their referrer spoofed. - EXPECT_EQ(ExecScriptGetStr(create_image_script(), contents()), - image_url().spec()); - EXPECT_EQ(GetLastReferrer(image_url()), image_url().GetOrigin().spec()); + // Same-site sub-resources within the page get the page URL as referrer. + EXPECT_EQ( + ExecScriptGetStr(create_image_script(same_site_image_url()), contents()), + same_site_image_url().spec()); + EXPECT_EQ(GetLastReferrer(same_site_image_url()), url().spec()); + + // Cross-site sub-resources within the page get their referrer spoofed. + EXPECT_EQ( + ExecScriptGetStr(create_image_script(cross_site_image_url()), contents()), + cross_site_image_url().spec()); + EXPECT_EQ(GetLastReferrer(cross_site_image_url()), + cross_site_image_url().GetOrigin().spec()); + + // Same-site iframe navigations get the page get the page URL as referrer. + NavigateIframe(same_site_url()); + EXPECT_EQ(ExecScriptGetStr(kReferrerScript, child_frame()), url().spec()); + EXPECT_EQ(GetLastReferrer(same_site_url()), url().spec()); + + // Cross-site iframe navigations get their referrer spoofed. + NavigateIframe(cross_site_url()); + EXPECT_EQ(ExecScriptGetStr(kReferrerScript, child_frame()), + cross_site_url().GetOrigin().spec()); + EXPECT_EQ(GetLastReferrer(cross_site_url()), + cross_site_url().GetOrigin().spec()); + + // Same-site navigations get the original page URL as the referrer. + NavigateDirectlyToPageWithLink(same_site_url()); + EXPECT_EQ(ExecScriptGetStr(kReferrerScript, contents()), link_url().spec()); + EXPECT_EQ(GetLastReferrer(same_site_url()), link_url().spec()); + + // Cross-site navigations get no referrer. + NavigateDirectlyToPageWithLink(cross_site_url()); + EXPECT_EQ(ExecScriptGetStr(kReferrerScript, contents()), ""); + EXPECT_EQ(GetLastReferrer(cross_site_url()), ""); +} + +IN_PROC_BROWSER_TEST_F(BraveContentSettingsAgentImplBrowserTest, + DISABLED_BlockReferrerRedirects) { + BlockReferrers(); + + // The initial navigation doesn't have a referrer. + NavigateToPageWithIframe(); + EXPECT_EQ(ExecScriptGetStr(kReferrerScript, contents()), ""); + EXPECT_TRUE(GetLastReferrer(url()).empty()); + + // Cross-site sub-resources within the page get their referrer spoofed. + EXPECT_EQ( + ExecScriptGetStr(create_image_script(redirect_to_cross_site_image_url()), + contents()), + redirect_to_cross_site_image_url().spec()); + EXPECT_EQ(GetLastReferrer(cross_site_image_url()), + cross_site_url().GetOrigin().spec()); - // Cross-origin iframe navigations get their referrer spoofed. - NavigateIframe(); + // Cross-site iframe navigations get their referrer spoofed. + NavigateCrossSiteRedirectIframe(); + EXPECT_EQ(ExecScriptGetStr(kReferrerScript, child_frame()), + cross_site_url().GetOrigin().spec()); + EXPECT_EQ(GetLastReferrer(redirect_to_cross_site_url()), + cross_site_url().GetOrigin().spec()); + // Cross-site iframe navigations get their referrer spoofed. + NavigateCrossSiteRedirectIframe(); EXPECT_EQ(ExecScriptGetStr(kReferrerScript, child_frame()), - iframe_url().GetOrigin().spec()); - EXPECT_EQ(GetLastReferrer(iframe_url()), iframe_url().GetOrigin().spec()); + cross_site_url().GetOrigin().spec()); + EXPECT_EQ(GetLastReferrer(redirect_to_cross_site_url()), + cross_site_url().GetOrigin().spec()); + + // Cross-site navigations get no referrer. + RedirectToPageWithLink(redirect_to_cross_site_url(), cross_site_url()); + EXPECT_EQ(ExecScriptGetStr(kReferrerScript, contents()), ""); + EXPECT_EQ(GetLastReferrer(redirect_to_cross_site_url()), ""); } IN_PROC_BROWSER_TEST_F(BraveContentSettingsAgentImplBrowserTest, @@ -471,16 +645,16 @@ IN_PROC_BROWSER_TEST_F(BraveContentSettingsAgentImplBrowserTest, EXPECT_EQ(ExecScriptGetStr(kReferrerScript, contents()), ""); EXPECT_TRUE(GetLastReferrer(url()).empty()); - // Sub-resources loaded within the page get the page URL as referrer. - EXPECT_EQ(ExecScriptGetStr(create_image_script(), contents()), - image_url().spec()); - EXPECT_EQ(GetLastReferrer(image_url()), url().spec()); + // Cross-site sub-resources within the page get the page URL as referrer. + EXPECT_EQ( + ExecScriptGetStr(create_image_script(cross_site_image_url()), contents()), + cross_site_image_url().spec()); + EXPECT_EQ(GetLastReferrer(cross_site_image_url()), url().spec()); - // A cross-origin iframe navigation gets the URL of the first one as + // A cross-site iframe navigation gets the URL of the first one as // referrer. - NavigateIframe(); - - EXPECT_EQ(GetLastReferrer(iframe_url()), url()); + NavigateIframe(cross_site_url()); + EXPECT_EQ(GetLastReferrer(cross_site_url()), url()); EXPECT_EQ(ExecScriptGetStr(kReferrerScript, child_frame()), url().spec()); } @@ -494,16 +668,16 @@ IN_PROC_BROWSER_TEST_F(BraveContentSettingsAgentImplBrowserTest, EXPECT_EQ(ExecScriptGetStr(kReferrerScript, contents()), ""); EXPECT_TRUE(GetLastReferrer(url()).empty()); - // Sub-resources loaded within the page get the page URL as referrer. - EXPECT_EQ(ExecScriptGetStr(create_image_script(), contents()), - image_url().spec()); - EXPECT_EQ(GetLastReferrer(image_url()), url().spec()); + // Cross-site sub-resources within the page get the page URL as referrer. + EXPECT_EQ( + ExecScriptGetStr(create_image_script(cross_site_image_url()), contents()), + cross_site_image_url().spec()); + EXPECT_EQ(GetLastReferrer(cross_site_image_url()), url().spec()); // A cross-origin iframe navigation gets the URL of the first one as // referrer. - NavigateIframe(); - - EXPECT_EQ(GetLastReferrer(iframe_url()), url()); + NavigateIframe(cross_site_url()); + EXPECT_EQ(GetLastReferrer(cross_site_url()), url()); EXPECT_EQ(ExecScriptGetStr(kReferrerScript, child_frame()), url().spec()); } @@ -512,7 +686,7 @@ IN_PROC_BROWSER_TEST_F(BraveContentSettingsAgentImplBrowserTest, NavigateToPageWithIframe(); CheckCookie(child_frame(), kTestCookie); - NavigateIframe(); + NavigateIframe(cross_site_url()); CheckCookie(child_frame(), kEmptyCookie); } @@ -523,7 +697,7 @@ IN_PROC_BROWSER_TEST_F(BraveContentSettingsAgentImplBrowserTest, NavigateToPageWithIframe(); CheckCookie(child_frame(), kTestCookie); - NavigateIframe(); + NavigateIframe(cross_site_url()); CheckCookie(child_frame(), kEmptyCookie); } @@ -533,7 +707,7 @@ IN_PROC_BROWSER_TEST_F(BraveContentSettingsAgentImplBrowserTest, BlockCookies) { NavigateToPageWithIframe(); CheckCookie(contents(), kEmptyCookie); - NavigateIframe(); + NavigateIframe(cross_site_url()); CheckCookie(child_frame(), kEmptyCookie); } @@ -543,7 +717,7 @@ IN_PROC_BROWSER_TEST_F(BraveContentSettingsAgentImplBrowserTest, AllowCookies) { NavigateToPageWithIframe(); CheckCookie(contents(), kTestCookie); - NavigateIframe(); + NavigateIframe(cross_site_url()); CheckCookie(child_frame(), kTestCookie); } @@ -559,7 +733,7 @@ IN_PROC_BROWSER_TEST_F(BraveContentSettingsAgentImplBrowserTest, NavigateToPageWithIframe(); CheckCookie(contents(), kEmptyCookie); - NavigateIframe(); + NavigateIframe(cross_site_url()); CheckCookie(child_frame(), kTestCookie); } @@ -575,7 +749,7 @@ IN_PROC_BROWSER_TEST_F(BraveContentSettingsAgentImplBrowserTest, NavigateToPageWithIframe(); CheckCookie(contents(), kTestCookie); - NavigateIframe(); + NavigateIframe(cross_site_url()); CheckCookie(child_frame(), kEmptyCookie); } @@ -587,7 +761,7 @@ IN_PROC_BROWSER_TEST_F(BraveContentSettingsAgentImplBrowserTest, NavigateToPageWithIframe(); CheckCookie(contents(), kTestCookie); - NavigateIframe(); + NavigateIframe(cross_site_url()); CheckCookie(child_frame(), kTestCookie); } @@ -598,7 +772,7 @@ IN_PROC_BROWSER_TEST_F(BraveContentSettingsAgentImplBrowserTest, NavigateToPageWithIframe(); CheckCookie(contents(), kTestCookie); - NavigateIframe(); + NavigateIframe(cross_site_url()); CheckCookie(child_frame(), kTestCookie); } @@ -610,7 +784,7 @@ IN_PROC_BROWSER_TEST_F(BraveContentSettingsAgentImplBrowserTest, NavigateToPageWithIframe(); CheckCookie(contents(), kEmptyCookie); - NavigateIframe(); + NavigateIframe(cross_site_url()); CheckCookie(child_frame(), kEmptyCookie); } diff --git a/test/data/simple_link.html b/test/data/simple_link.html new file mode 100644 index 000000000000..67a95f53a18f --- /dev/null +++ b/test/data/simple_link.html @@ -0,0 +1,22 @@ + + + + Link navigation test + + + + Navigate away + +