Skip to content

Commit

Permalink
Merge pull request #20336 from brave/feature/psst
Browse files Browse the repository at this point in the history
PSST component and script injection logic
  • Loading branch information
bridiver authored Nov 3, 2023
2 parents fbfb3fb + 9c9c361 commit 886954a
Show file tree
Hide file tree
Showing 38 changed files with 1,348 additions and 3 deletions.
1 change: 1 addition & 0 deletions android/android_browser_tests.gni
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ android_test_exception_deps = [
"//brave/browser/net:browser_tests",
"//brave/browser/permissions:browser_tests",
"//brave/browser/playlist/test:browser_tests",
"//brave/browser/psst:browser_tests",
"//brave/browser/test:browser_tests",
"//brave/browser/ui:browser_tests",
"//brave/browser/ui/tabs/test:browser_tests",
Expand Down
8 changes: 8 additions & 0 deletions browser/about_flags.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "brave/components/ipfs/buildflags/buildflags.h"
#include "brave/components/ntp_background_images/browser/features.h"
#include "brave/components/playlist/common/buildflags/buildflags.h"
#include "brave/components/psst/common/features.h"
#include "brave/components/request_otr/common/buildflags/buildflags.h"
#include "brave/components/skus/common/features.h"
#include "brave/components/speedreader/common/buildflags/buildflags.h"
Expand Down Expand Up @@ -623,6 +624,13 @@
FEATURE_VALUE_TYPE( \
brave_shields::features::kBraveLocalhostAccessPermission), \
}, \
{ \
"brave-psst", \
"Enable PSST (Privacy Site Settings Tool) feature", \
"Enable PSST feature", \
kOsAll, \
FEATURE_VALUE_TYPE(psst::features::kBravePsst), \
}, \
{ \
"brave-extension-network-blocking", \
"Enable extension network blocking", \
Expand Down
6 changes: 4 additions & 2 deletions browser/brave_tab_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
#include "brave/components/greaselion/browser/buildflags/buildflags.h"
#include "brave/components/ipfs/buildflags/buildflags.h"
#include "brave/components/playlist/common/buildflags/buildflags.h"
#include "brave/components/psst/browser/content/psst_tab_helper.h"
#include "brave/components/request_otr/common/buildflags/buildflags.h"
#include "brave/components/speedreader/common/buildflags/buildflags.h"
#include "brave/components/tor/buildflags/buildflags.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/common/chrome_isolated_world_ids.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/web_contents.h"
#include "extensions/buildflags/buildflags.h"
Expand Down Expand Up @@ -146,9 +148,9 @@ void AttachTabHelpers(content::WebContents* web_contents) {
web_contents);

brave_ads::AdsTabHelper::CreateForWebContents(web_contents);

brave_ads::SearchResultAdTabHelper::MaybeCreateForWebContents(web_contents);

psst::PsstTabHelper::MaybeCreateForWebContents(
web_contents, ISOLATED_WORLD_ID_BRAVE_INTERNAL);
#if BUILDFLAG(ENABLE_EXTENSIONS)
WebDiscoveryTabHelper::MaybeCreateForWebContents(web_contents);
#endif
Expand Down
29 changes: 29 additions & 0 deletions browser/psst/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright (c) 2023 The Brave Authors. All rights reserved.
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at https://mozilla.org/MPL/2.0/.

source_set("browser_tests") {
testonly = true
defines = [ "HAS_OUT_OF_PROC_TEST_RUNNER" ]

sources = [ "psst_tab_helper_browsertest.cc" ]

deps = [
"//base",
"//brave/components/brave_shields/browser",
"//brave/components/cosmetic_filters/browser",
"//brave/components/psst/browser/core",
"//brave/components/psst/common",
"//chrome/test:test_support",
"//content/test:test_support",
"//net:test_support",
"//url",
]

if (is_android) {
deps += [ "//chrome/test:test_support_ui_android" ]
} else {
deps += [ "//chrome/test:test_support_ui" ]
}
}
184 changes: 184 additions & 0 deletions browser/psst/psst_tab_helper_browsertest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
// Copyright (c) 2023 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

#include <string>

#include "base/path_service.h"
#include "base/test/scoped_feature_list.h"
#include "brave/browser/brave_content_browser_client.h"
#include "brave/components/constants/brave_paths.h"
#include "brave/components/psst/browser/core/psst_rule_registry.h"
#include "brave/components/psst/common/features.h"
#include "chrome/test/base/chrome_test_utils.h"
#include "chrome/test/base/testing_browser_process.h"
#include "content/public/common/content_client.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_mock_cert_verifier.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"

#if BUILDFLAG(IS_ANDROID)
#include "chrome/test/base/android/android_browser_test.h"
#else
#include "chrome/test/base/in_process_browser_test.h"
#endif

namespace psst {

class PsstTabHelperBrowserTest : public PlatformBrowserTest {
public:
PsstTabHelperBrowserTest()
: https_server_(net::EmbeddedTestServer::TYPE_HTTPS) {
feature_list_.InitAndEnableFeature(psst::features::kBravePsst);
}

void SetUpOnMainThread() override {
PlatformBrowserTest::SetUpOnMainThread();
brave::RegisterPathProvider();
base::FilePath test_data_dir;
base::PathService::Get(brave::DIR_TEST_DATA, &test_data_dir);

content::SetBrowserClientForTesting(&test_content_browser_client_);

// Also called in Disabled test.
if (psst::PsstRuleRegistry::GetInstance()) {
psst::PsstRuleRegistry::GetInstance()->SetComponentPath(
test_data_dir.AppendASCII("psst-component-data"));
}
https_server_.ServeFilesFromDirectory(test_data_dir);

mock_cert_verifier_.mock_cert_verifier()->set_default_result(net::OK);
host_resolver()->AddRule("*", "127.0.0.1");
ASSERT_TRUE(https_server_.Start());
}

void SetUpCommandLine(base::CommandLine* command_line) override {
PlatformBrowserTest::SetUpCommandLine(command_line);
mock_cert_verifier_.SetUpCommandLine(command_line);
}

void SetUpInProcessBrowserTestFixture() override {
PlatformBrowserTest::SetUpInProcessBrowserTestFixture();
mock_cert_verifier_.SetUpInProcessBrowserTestFixture();
}

void TearDownInProcessBrowserTestFixture() override {
mock_cert_verifier_.TearDownInProcessBrowserTestFixture();
PlatformBrowserTest::TearDownInProcessBrowserTestFixture();
}

content::WebContents* web_contents() {
return chrome_test_utils::GetActiveWebContents(this);
}

protected:
net::EmbeddedTestServer https_server_;
base::test::ScopedFeatureList feature_list_;

private:
content::ContentMockCertVerifier mock_cert_verifier_;
BraveContentBrowserClient test_content_browser_client_;
};

// TESTS

IN_PROC_BROWSER_TEST_F(PsstTabHelperBrowserTest, RuleMatchTestScriptTrue) {
const GURL url = https_server_.GetURL("a.com", "/simple.html");

const char rules[] =
R"(
[
{
"include": [
"https://a.com/*"
],
"exclude": [
],
"version": 1,
"test_script": "a/test.js",
"policy_script": "a/policy.js"
}
]
)";
psst::PsstRuleRegistry::GetInstance()->OnLoadRules(rules);

std::u16string expected_title(u"testpolicy");
content::TitleWatcher watcher(web_contents(), expected_title);
ASSERT_TRUE(content::NavigateToURL(web_contents(), url));
EXPECT_EQ(expected_title, watcher.WaitAndGetTitle());
}

IN_PROC_BROWSER_TEST_F(PsstTabHelperBrowserTest, RuleMatchTestScriptFalse) {
const GURL url = https_server_.GetURL("b.com", "/simple.html");

const char rules[] =
R"(
[
{
"include": [
"https://b.com/*"
],
"exclude": [
],
"version": 1,
"test_script": "b/test.js",
"policy_script": "b/policy.js"
}
]
)";
psst::PsstRuleRegistry::GetInstance()->OnLoadRules(rules);

std::u16string expected_title(u"test");
content::TitleWatcher watcher(web_contents(), expected_title);
ASSERT_TRUE(content::NavigateToURL(web_contents(), url));
EXPECT_EQ(expected_title, watcher.WaitAndGetTitle());
}

IN_PROC_BROWSER_TEST_F(PsstTabHelperBrowserTest, NoMatch) {
const GURL url = https_server_.GetURL("a.com", "/simple.html");

const char rules[] =
R"(
[
{
"include": [
"https://c.com/*"
],
"exclude": [
],
"version": 1,
"test_script": "a/test.js",
"policy_script": "a/policy.js"
}
]
)";
psst::PsstRuleRegistry::GetInstance()->OnLoadRules(rules);

std::u16string expected_title(u"OK");
content::TitleWatcher watcher(web_contents(), expected_title);
ASSERT_TRUE(content::NavigateToURL(web_contents(), url));
EXPECT_EQ(expected_title, watcher.WaitAndGetTitle());
}

class PsstTabHelperBrowserTestDisabled : public PsstTabHelperBrowserTest {
public:
PsstTabHelperBrowserTestDisabled() {
feature_list_.Reset();
feature_list_.InitAndDisableFeature(psst::features::kBravePsst);
}
};

IN_PROC_BROWSER_TEST_F(PsstTabHelperBrowserTestDisabled, DoesNotInjectScript) {
const GURL url = https_server_.GetURL("a.com", "/simple.html");
ASSERT_FALSE(psst::PsstRuleRegistry::GetInstance());

std::u16string expected_title(u"OK");
content::TitleWatcher watcher(web_contents(), expected_title);
ASSERT_TRUE(content::NavigateToURL(web_contents(), url));
EXPECT_EQ(expected_title, watcher.WaitAndGetTitle());
}

} // namespace psst
3 changes: 3 additions & 0 deletions browser/sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ brave_chrome_browser_deps = [
"//brave/components/p3a:buildflags",
"//brave/components/playlist/common/buildflags",
"//brave/components/privacy_sandbox",
"//brave/components/psst/browser/content",
"//brave/components/psst/browser/core",
"//brave/components/psst/common",
"//brave/components/request_otr/common/buildflags",
"//brave/components/resources",
"//brave/components/skus/browser",
Expand Down
3 changes: 2 additions & 1 deletion chromium_src/chrome/browser/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ include_rules = [
"+brave/components/constants",
"+brave/components/content_settings/core",
"+brave/components/debounce",
"+brave/components/google_sign_in_permission",
"+brave/components/decentralized_dns",
"+brave/components/google_sign_in_permission",
"+brave/components/ipfs",
"+brave/components/l10n/common",
"+brave/components/ntp_background_images",
Expand All @@ -33,6 +33,7 @@ include_rules = [
"+brave/components/permissions",
"+brave/components/playlist",
"+brave/components/privacy_sandbox",
"+brave/components/psst/browser/core",
"+brave/components/request_otr",
"+brave/components/sidebar",
"+brave/components/speedreader",
Expand Down
5 changes: 5 additions & 0 deletions chromium_src/chrome/browser/component_updater/registration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "chrome/browser/component_updater/registration.h"
#include "base/functional/bind.h"
#include "brave/components/widevine/static_buildflags.h"
#include "chrome/browser/component_updater/widevine_cdm_component_installer.h"

Expand All @@ -23,14 +24,18 @@
#undef RegisterComponentsForUpdate

#include "brave/components/brave_wallet/browser/wallet_data_files_installer.h"
#include "brave/components/psst/browser/core/psst_component_installer.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/component_updater/component_updater_utils.h"

namespace component_updater {

void RegisterComponentsForUpdate() {
RegisterComponentsForUpdate_ChromiumImpl();
ComponentUpdateService* cus = g_browser_process->component_updater();
brave_wallet::RegisterWalletDataFilesComponent(cus);
psst::RegisterPsstComponent(
cus, base::BindOnce(&component_updater::BraveOnDemandUpdate));
}

} // namespace component_updater
25 changes: 25 additions & 0 deletions components/psst/browser/content/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright (c) 2023 The Brave Authors. All rights reserved.
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at https://mozilla.org/MPL/2.0/.

component("content") {
output_name = "psst_browser_content"

defines = [ "IS_PSST_BROWSER_CONTENT_IMPL" ]

sources = [
"psst_tab_helper.cc",
"psst_tab_helper.h",
]

deps = [
"//base",
"//brave/components/psst/browser/core",
"//brave/components/psst/common",
"//brave/components/script_injector/common/mojom",
"//components/sessions",
"//content/public/browser",
"//url",
]
}
4 changes: 4 additions & 0 deletions components/psst/browser/content/DEPS
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include_rules = [
"+content/public/browser",
"+third_party/blink/public"
]
Loading

0 comments on commit 886954a

Please sign in to comment.