Skip to content

don't perturb pixels on google maps #23416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<const unsigned char*>(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<const unsigned char*>( \
image_data_pixmap.writable_addr()), \
image_data_pixmap.computeByteSize()); \
} \
}

#define BRAVE_BASE_RENDERING_CONTEXT_2D_MEASURE_TEXT \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {

Expand Down Expand Up @@ -74,6 +78,7 @@ const int kExpectedImageDataHashFarblingBalanced = 172;
const int kExpectedImageDataHashFarblingOff = 0;
const int kExpectedImageDataHashFarblingMaximum =
kExpectedImageDataHashFarblingBalanced;
const int kExpectedImageDataHashFarblingBalancedGoogleCom = 182;

const char kEmptyCookie[] = "";

Expand All @@ -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<HttpResponse> 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 {
Expand All @@ -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,
Expand All @@ -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_);

Expand Down Expand Up @@ -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:
Expand Down
Loading