From c745791190562c0a9985bfbc03c40f8b7f28470d Mon Sep 17 00:00:00 2001 From: Arthur Edelstein Date: Fri, 3 May 2024 11:12:27 -0700 Subject: [PATCH] don't perturb pixels on google maps (#23416) This is a temporary fix for Google Maps; we will revert it once a better fix is found. --- .../canvas2d/base_rendering_context_2d.cc | 29 ++++++++-- ...content_settings_agent_impl_browsertest.cc | 58 +++++++++++++++++++ 2 files changed, 81 insertions(+), 6 deletions(-) diff --git a/chromium_src/third_party/blink/renderer/modules/canvas/canvas2d/base_rendering_context_2d.cc b/chromium_src/third_party/blink/renderer/modules/canvas/canvas2d/base_rendering_context_2d.cc index 53c5c88cd3ec..e2f64f52ddf8 100644 --- a/chromium_src/third_party/blink/renderer/modules/canvas/canvas2d/base_rendering_context_2d.cc +++ b/chromium_src/third_party/blink/renderer/modules/canvas/canvas2d/base_rendering_context_2d.cc @@ -8,13 +8,30 @@ #include "base/notreached.h" #include "brave/third_party/blink/renderer/core/farbling/brave_session_cache.h" #include "third_party/blink/renderer/platform/graphics/image_data_buffer.h" +#include "third_party/blink/renderer/platform/weborigin/kurl.h" -#define BRAVE_GET_IMAGE_DATA \ - if (ExecutionContext* context = ExecutionContext::From(script_state)) { \ - SkPixmap image_data_pixmap = image_data->GetSkPixmap(); \ - brave::BraveSessionCache::From(*context).PerturbPixels( \ - static_cast(image_data_pixmap.writable_addr()), \ - image_data_pixmap.computeByteSize()); \ +namespace { + +bool IsGoogleMaps(const blink::KURL& url) { + const auto host = url.Host(); + if (!host.StartsWith("google.") && !host.Contains(".google.")) { + return false; + } + const auto path = url.GetPath(); + return path.StartsWith("/maps/") || path == "/maps"; +} + +} // namespace + +#define BRAVE_GET_IMAGE_DATA \ + if (ExecutionContext* context = ExecutionContext::From(script_state)) { \ + if (!IsGoogleMaps(context->Url())) { \ + SkPixmap image_data_pixmap = image_data->GetSkPixmap(); \ + brave::BraveSessionCache::From(*context).PerturbPixels( \ + static_cast( \ + image_data_pixmap.writable_addr()), \ + image_data_pixmap.computeByteSize()); \ + } \ } #define BRAVE_BASE_RENDERING_CONTEXT_2D_MEASURE_TEXT \ diff --git a/components/content_settings/renderer/brave_content_settings_agent_impl_browsertest.cc b/components/content_settings/renderer/brave_content_settings_agent_impl_browsertest.cc index d62ccf2b42c5..73c234a901fa 100644 --- a/components/content_settings/renderer/brave_content_settings_agent_impl_browsertest.cc +++ b/components/content_settings/renderer/brave_content_settings_agent_impl_browsertest.cc @@ -21,6 +21,7 @@ #include "components/content_settings/core/browser/host_content_settings_map.h" #include "components/content_settings/core/common/content_settings.h" #include "components/content_settings/core/common/content_settings_types.h" +#include "components/google/core/common/google_switches.h" #include "components/network_session_configurator/common/network_switches.h" #include "content/public/browser/render_frame_host.h" #include "content/public/test/browser_test.h" @@ -31,10 +32,13 @@ #include "net/http/http_request_headers.h" #include "net/test/embedded_test_server/default_handlers.h" #include "net/test/embedded_test_server/http_request.h" +#include "net/test/embedded_test_server/request_handler_util.h" #include "testing/gmock/include/gmock/gmock.h" #include "url/origin.h" using brave_shields::ControlType; +using net::test_server::HttpRequest; +using net::test_server::HttpResponse; namespace { @@ -74,6 +78,7 @@ const int kExpectedImageDataHashFarblingBalanced = 172; const int kExpectedImageDataHashFarblingOff = 0; const int kExpectedImageDataHashFarblingMaximum = kExpectedImageDataHashFarblingBalanced; +const int kExpectedImageDataHashFarblingBalancedGoogleCom = 182; const char kEmptyCookie[] = ""; @@ -97,6 +102,19 @@ GURL GetOriginURL(const GURL& url) { return url::Origin::Create(url).GetURL(); } +// Remaps requests from /maps/simple.html to /simple.html +std::unique_ptr HandleGoogleMapsFileRequest( + const base::FilePath& server_root, + const HttpRequest& request) { + HttpRequest new_request(request); + if (!new_request.relative_url.starts_with("/maps")) { + // This handler is only relevant for a Google Maps url. + return nullptr; + } + new_request.relative_url = new_request.relative_url.substr(5); + return HandleFileRequest(server_root, new_request); +} + } // namespace class BraveContentSettingsAgentImplBrowserTest : public InProcessBrowserTest { @@ -117,6 +135,8 @@ class BraveContentSettingsAgentImplBrowserTest : public InProcessBrowserTest { https_server_.SetSSLConfig(net::EmbeddedTestServer::CERT_TEST_NAMES); https_server_.ServeFilesFromDirectory(test_data_dir); https_server_.AddDefaultHandlers(GetChromeTestDataDir()); + https_server_.RegisterDefaultHandler( + base::BindRepeating(&HandleGoogleMapsFileRequest, test_data_dir)); content::SetupCrossSiteRedirector(&https_server_); https_server_.RegisterRequestMonitor(base::BindRepeating( &BraveContentSettingsAgentImplBrowserTest::SaveReferrer, @@ -143,6 +163,16 @@ class BraveContentSettingsAgentImplBrowserTest : public InProcessBrowserTest { ContentSettingsPattern::FromString("https://firstParty/*"); } + void SetUpCommandLine(base::CommandLine* command_line) override { + // Since the HTTPS server only serves a valid cert for localhost, + // this is needed to load pages from "www.google.*" without an interstitial. + command_line->AppendSwitch(switches::kIgnoreCertificateErrors); + + // The production code only allows known ports (80 for http and 443 for + // https), but the test server runs on a random port. + command_line->AppendSwitch(switches::kIgnoreGooglePortNumbers); + } + void SaveReferrer(const net::test_server::HttpRequest& request) { base::AutoLock auto_lock(last_referrers_lock_); @@ -430,6 +460,34 @@ IN_PROC_BROWSER_TEST_F(BraveContentSettingsAgentImplBrowserTest, content::EvalJs(contents(), kGetImageDataScript)); } +IN_PROC_BROWSER_TEST_F(BraveContentSettingsAgentImplBrowserTest, + FarbleGetImageDataGoogleMapsException) { + // Farbling should be disabled on Google Maps + SetFingerprintingDefault(); + ASSERT_TRUE(ui_test_utils::NavigateToURL( + browser(), https_server().GetURL("google.com", "/maps/simple.html"))); + EXPECT_EQ(kExpectedImageDataHashFarblingOff, + content::EvalJs(contents(), kGetImageDataScript)); + + // Farbling should not be disabled on other Google things + ASSERT_TRUE(ui_test_utils::NavigateToURL( + browser(), https_server().GetURL("google.com", "/simple.html"))); + EXPECT_EQ(kExpectedImageDataHashFarblingBalancedGoogleCom, + content::EvalJs(contents(), kGetImageDataScript)); + + // Farbling should be disabled on google.co.uk maps + ASSERT_TRUE(ui_test_utils::NavigateToURL( + browser(), https_server().GetURL("google.co.uk", "/maps/simple.html"))); + EXPECT_EQ(kExpectedImageDataHashFarblingOff, + content::EvalJs(contents(), kGetImageDataScript)); + + // Farbling should be disabled on google.de maps + ASSERT_TRUE(ui_test_utils::NavigateToURL( + browser(), https_server().GetURL("google.de", "/maps/simple.html"))); + EXPECT_EQ(kExpectedImageDataHashFarblingOff, + content::EvalJs(contents(), kGetImageDataScript)); +} + class BraveContentSettingsAgentImplV2BrowserTest : public BraveContentSettingsAgentImplBrowserTest { public: