Skip to content

Commit

Permalink
Merge pull request #19174 from brave/cosmetic-filtering-debug-logging
Browse files Browse the repository at this point in the history
Add `#brave-adblock-scriptlet-debug-logs` flag for filter authors
  • Loading branch information
antonok-edm authored Jul 8, 2023
2 parents 1e9250d + 7995de2 commit e29428b
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 4 deletions.
10 changes: 10 additions & 0 deletions browser/about_flags.cc
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,16 @@
FEATURE_VALUE_TYPE(brave_shields::features:: \
kBraveAdblockMobileNotificationsListDefault), \
}, \
{ \
"brave-adblock-scriptlet-debug-logs", \
"Enable debug logging for scriptlet injections", \
"Enable console debugging for scriptlets injected by cosmetic " \
"filtering, exposing additional information that can be useful for " \
"filter authors.", \
kOsDesktop, \
FEATURE_VALUE_TYPE( \
brave_shields::features::kBraveAdblockScriptletDebugLogs), \
}, \
{ \
"brave-dark-mode-block", \
"Enable dark mode blocking fingerprinting protection", \
Expand Down
42 changes: 42 additions & 0 deletions browser/brave_shields/ad_block_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ using brave_shields::features::kBraveAdblockCollapseBlockedElements;
using brave_shields::features::kBraveAdblockCookieListDefault;
using brave_shields::features::kBraveAdblockCosmeticFiltering;
using brave_shields::features::kBraveAdblockDefault1pBlocking;
using brave_shields::features::kBraveAdblockScriptletDebugLogs;
using brave_shields::features::kCosmeticFilteringJsPerformance;

AdBlockServiceTest::AdBlockServiceTest()
Expand Down Expand Up @@ -2255,6 +2256,47 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringWindowScriptlet) {
EXPECT_EQ(base::Value(true), result.value);
}

class ScriptletDebugLogsFlagEnabledTest : public AdBlockServiceTest {
public:
ScriptletDebugLogsFlagEnabledTest() {
feature_list_.InitAndEnableFeature(kBraveAdblockScriptletDebugLogs);
}

private:
base::test::ScopedFeatureList feature_list_;
};

// Test that scriptlet injection has access to `canDebug` inside of
// `scriptletGlobals`, and that it is set to `true`.
IN_PROC_BROWSER_TEST_F(ScriptletDebugLogsFlagEnabledTest, CanDebugSetToTrue) {
ASSERT_TRUE(InstallDefaultAdBlockExtension());
std::string scriptlet =
"(function() {"
" if (scriptletGlobals.get('canDebug')) {"
" window.success = true;"
" }"
"})();";
std::string scriptlet_base64;
base::Base64Encode(scriptlet, &scriptlet_base64);
UpdateAdBlockInstanceWithRules(
"b.com##+js(debuggable)",
"[{"
"\"name\": \"debuggable\","
"\"aliases\": [],"
"\"kind\": {\"mime\": \"application/javascript\"},"
"\"content\": \"" +
scriptlet_base64 + "\"}]");

GURL tab_url =
embedded_test_server()->GetURL("b.com", "/cosmetic_filtering.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), tab_url));

content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();

EXPECT_EQ(true, EvalJs(contents, R"(window.success)"));
}

// Test scriptlet injection with DeAMP enabled
IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CheckForDeAmpPref) {
std::string scriptlet =
Expand Down
3 changes: 3 additions & 0 deletions components/brave_shields/common/features.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ BASE_FEATURE(kBraveAdblockCookieListOptIn,
BASE_FEATURE(kBraveAdblockCosmeticFiltering,
"BraveAdblockCosmeticFiltering",
base::FEATURE_ENABLED_BY_DEFAULT);
BASE_FEATURE(kBraveAdblockScriptletDebugLogs,
"BraveAdblockScriptletDebugLogs",
base::FEATURE_DISABLED_BY_DEFAULT);
BASE_FEATURE(kBraveAdblockCspRules,
"BraveAdblockCspRules",
base::FEATURE_ENABLED_BY_DEFAULT);
Expand Down
1 change: 1 addition & 0 deletions components/brave_shields/common/features.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ BASE_DECLARE_FEATURE(kBraveAdblockCookieListOptIn);
BASE_DECLARE_FEATURE(kBraveAdblockCosmeticFiltering);
BASE_DECLARE_FEATURE(kBraveAdblockCspRules);
BASE_DECLARE_FEATURE(kBraveAdblockMobileNotificationsListDefault);
BASE_DECLARE_FEATURE(kBraveAdblockScriptletDebugLogs);
BASE_DECLARE_FEATURE(kBraveDomainBlock);
BASE_DECLARE_FEATURE(kBraveDomainBlock1PES);
BASE_DECLARE_FEATURE(kBraveExtensionNetworkBlocking);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const char kObservingScriptletEntryPoint[] =

const char kScriptletInitScript[] =
R"((function() {
let text = '(function() {\nconst scriptletGlobals = new Map();\nlet deAmpEnabled = %s;\n' + %s + '})()';
let text = '(function() {\nconst scriptletGlobals = new Map(%s);\nlet deAmpEnabled = %s;\n' + %s + '})()';
let script;
try {
script = document.createElement('script');
Expand Down Expand Up @@ -411,11 +411,16 @@ void CosmeticFiltersJSHandler::ApplyRules(bool de_amp_enabled) {

std::string scriptlet_script;
base::Value* injected_script = resources_dict_->Find("injected_script");

if (injected_script &&
base::JSONWriter::Write(*injected_script, &scriptlet_script)) {
scriptlet_script = base::StringPrintf(kScriptletInitScript,
de_amp_enabled ? "true" : "false",
scriptlet_script.c_str());
const bool scriptlet_debug_enabled = base::FeatureList::IsEnabled(
brave_shields::features::kBraveAdblockScriptletDebugLogs);

scriptlet_script = base::StringPrintf(
kScriptletInitScript,
scriptlet_debug_enabled ? "[[\"canDebug\", true]]" : "",
de_amp_enabled ? "true" : "false", scriptlet_script.c_str());
}
if (!scriptlet_script.empty()) {
web_frame->ExecuteScriptInIsolatedWorld(
Expand Down

0 comments on commit e29428b

Please sign in to comment.