Martin Verde | 777aea8 | 2025-04-29 14:12:04 | [diff] [blame] | 1 | // Copyright 2025 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <memory> |
| 6 | #include <tuple> |
| 7 | |
| 8 | #include "base/test/scoped_feature_list.h" |
| 9 | #include "base/test/values_test_util.h" |
| 10 | #include "chrome/browser/fingerprinting_protection/fingerprinting_protection_filter_browser_test_harness.h" |
| 11 | #include "chrome/browser/ui/browser.h" |
| 12 | #include "chrome/test/base/ui_test_utils.h" |
| 13 | #include "components/fingerprinting_protection_filter/common/fingerprinting_protection_filter_features.h" |
| 14 | #include "content/public/test/browser_test.h" |
| 15 | #include "content/public/test/browser_test_utils.h" |
| 16 | #include "content/public/test/content_browser_test_utils.h" |
| 17 | #include "content/public/test/test_devtools_protocol_client.h" |
| 18 | #include "url/gurl.h" |
| 19 | |
| 20 | namespace fingerprinting_protection_filter { |
| 21 | namespace { |
| 22 | |
| 23 | using testing::Eq; |
| 24 | using testing::Pointee; |
| 25 | |
| 26 | class UserReideintificationDevtoolsProtocolTest |
| 27 | : public content::TestDevToolsProtocolClient, |
| 28 | public FingerprintingProtectionFilterBrowserTest { |
| 29 | public: |
| 30 | UserReideintificationDevtoolsProtocolTest() = default; |
| 31 | |
| 32 | UserReideintificationDevtoolsProtocolTest( |
| 33 | const UserReideintificationDevtoolsProtocolTest&) = delete; |
| 34 | UserReideintificationDevtoolsProtocolTest& operator=( |
| 35 | const UserReideintificationDevtoolsProtocolTest&) = delete; |
| 36 | |
| 37 | ~UserReideintificationDevtoolsProtocolTest() override = default; |
| 38 | |
| 39 | void EnableAudits() { |
| 40 | Attach(); |
| 41 | SendCommandSync("Audits.enable"); |
| 42 | } |
| 43 | |
| 44 | void WaitForIssueAddedWithProperties(const std::string& type_enum_string, |
| 45 | const GURL& affected_url) { |
| 46 | auto matcher = [](const base::Value::Dict& params) { |
| 47 | const std::string* maybe_issue_code = |
| 48 | params.FindStringByDottedPath("issue.code"); |
| 49 | return maybe_issue_code && |
| 50 | *maybe_issue_code == "UserReidentificationIssue"; |
| 51 | }; |
| 52 | |
| 53 | base::Value::Dict notification = WaitForMatchingNotification( |
| 54 | "Audits.issueAdded", base::BindRepeating(matcher)); |
| 55 | |
| 56 | EXPECT_EQ(*notification.FindStringByDottedPath( |
| 57 | "issue.details.userReidentificationIssueDetails.type"), |
| 58 | type_enum_string); |
| 59 | EXPECT_EQ(*notification.FindStringByDottedPath( |
| 60 | "issue.details.userReidentificationIssueDetails.request.url"), |
| 61 | affected_url.possibly_invalid_spec()); |
| 62 | } |
| 63 | |
| 64 | protected: |
| 65 | void Attach() { AttachToWebContents(web_contents()); } |
| 66 | |
| 67 | // InProcessBrowserTest interface |
| 68 | void TearDownOnMainThread() override { DetachProtocolClient(); } |
| 69 | |
| 70 | base::test::ScopedFeatureList scoped_feature_list_; |
| 71 | }; |
| 72 | |
| 73 | class IncognitoUserReideintificationDevtoolsProtocolTest |
| 74 | : public UserReideintificationDevtoolsProtocolTest { |
| 75 | public: |
| 76 | IncognitoUserReideintificationDevtoolsProtocolTest() { |
| 77 | scoped_feature_list_.InitWithFeaturesAndParameters( |
| 78 | /*enabled_features=*/ |
| 79 | {{features::kEnableFingerprintingProtectionFilterInIncognito, |
| 80 | {{"activation_level", "enabled"}}}}, |
| 81 | /*disabled_features=*/ |
| 82 | {{features::kEnableFingerprintingProtectionFilter}}); |
| 83 | } |
| 84 | |
| 85 | IncognitoUserReideintificationDevtoolsProtocolTest( |
| 86 | const IncognitoUserReideintificationDevtoolsProtocolTest&) = delete; |
| 87 | IncognitoUserReideintificationDevtoolsProtocolTest& operator=( |
| 88 | const IncognitoUserReideintificationDevtoolsProtocolTest&) = delete; |
| 89 | |
| 90 | ~IncognitoUserReideintificationDevtoolsProtocolTest() override = default; |
| 91 | }; |
| 92 | |
| 93 | } // namespace |
| 94 | |
| 95 | IN_PROC_BROWSER_TEST_F(UserReideintificationDevtoolsProtocolTest, |
| 96 | Enabled_SubframeDocumentLoadIssueReported) { |
| 97 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 98 | EnableAudits(); |
| 99 | |
| 100 | // Navigate to a test page with multiple child frames and disallow loading |
| 101 | // child frame documents that in turn would end up loading |
| 102 | // included_script.html. |
| 103 | ASSERT_NO_FATAL_FAILURE( |
| 104 | SetRulesetToDisallowURLsWithSubstring("frame_with_included_script.html")); |
| 105 | GURL url(GetTestUrl(kMultiPlatformTestFrameSetPath)); |
| 106 | ASSERT_TRUE(NavigateToDestination(url)); |
| 107 | |
| 108 | // Navigate the first subframe to a disallowed URL and verify that an issue is |
| 109 | // reported. |
| 110 | GURL disallowed_subdocument_url( |
| 111 | GetCrossSiteTestUrl("/frame_with_included_script.html")); |
| 112 | NavigateFrame(kSubframeNames[0], disallowed_subdocument_url); |
| 113 | |
| 114 | WaitForIssueAddedWithProperties( |
| 115 | /*type_enum_string=*/"BlockedFrameNavigation", |
| 116 | /*affected_url=*/GetCrossSiteTestUrl("/frame_with_included_script.html")); |
| 117 | } |
| 118 | |
| 119 | IN_PROC_BROWSER_TEST_F(UserReideintificationDevtoolsProtocolTest, |
| 120 | Enabled_SubresourceLoadIssueReported) { |
| 121 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 122 | EnableAudits(); |
| 123 | GURL url = GetTestUrl("/frame_with_cross_origin_included_script.html"); |
| 124 | |
| 125 | ASSERT_NO_FATAL_FAILURE( |
| 126 | SetRulesetToDisallowURLsWithSubstring("included_script.js")); |
| 127 | ASSERT_TRUE(NavigateToDestination(url)); |
| 128 | EXPECT_FALSE( |
| 129 | WasParsedScriptElementLoaded(web_contents()->GetPrimaryMainFrame())); |
| 130 | WaitForIssueAddedWithProperties( |
| 131 | /*type_enum_string=*/"BlockedSubresource", |
| 132 | /*affected_url=*/GURL( |
| 133 | "https://cross-origin-site.com/included_script.js")); |
| 134 | } |
| 135 | |
| 136 | IN_PROC_BROWSER_TEST_F(IncognitoUserReideintificationDevtoolsProtocolTest, |
| 137 | Enabled_Incognito_SubframeDocumentLoadIssueReported) { |
| 138 | // Close normal browser and switch the test's browser instance to an incognito |
| 139 | // instance. |
| 140 | Browser* incognito = CreateIncognitoBrowser(browser()->profile()); |
| 141 | CloseBrowserSynchronously(browser()); |
| 142 | SelectFirstBrowser(); |
| 143 | ASSERT_EQ(browser(), incognito); |
| 144 | |
| 145 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 146 | EnableAudits(); |
| 147 | |
| 148 | // Navigate to a test page with multiple child frames and disallow loading |
| 149 | // child frame documents that in turn would end up loading |
| 150 | // included_script.html. |
| 151 | ASSERT_NO_FATAL_FAILURE( |
| 152 | SetRulesetToDisallowURLsWithSubstring("frame_with_included_script.html")); |
| 153 | GURL url(GetTestUrl(kMultiPlatformTestFrameSetPath)); |
| 154 | ASSERT_TRUE(NavigateToDestination(url)); |
| 155 | |
| 156 | // Navigate the first subframe to a disallowed URL and verify that an issue is |
| 157 | // reported. |
| 158 | GURL disallowed_subdocument_url( |
| 159 | GetCrossSiteTestUrl("/frame_with_included_script.html")); |
| 160 | NavigateFrame(kSubframeNames[0], disallowed_subdocument_url); |
| 161 | |
| 162 | WaitForIssueAddedWithProperties( |
| 163 | /*type_enum_string=*/"BlockedFrameNavigation", |
| 164 | /*affected_url=*/GetCrossSiteTestUrl("/frame_with_included_script.html")); |
| 165 | } |
| 166 | |
| 167 | } // namespace fingerprinting_protection_filter |