File: site_policy_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (258 lines) | stat: -rw-r--r-- 9,748 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/actor/site_policy.h"

#include "base/base64.h"
#include "base/command_line.h"
#include "base/metrics/field_trial_params.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "chrome/browser/actor/actor_features.h"
#include "chrome/browser/actor/actor_keyed_service.h"
#include "chrome/browser/lookalikes/lookalike_test_helper.h"
#include "chrome/browser/optimization_guide/browser_test_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/optimization_guide/core/filters/bloom_filter.h"
#include "components/optimization_guide/core/optimization_guide_switches.h"
#include "components/optimization_guide/proto/hints.pb.h"
#include "components/safe_browsing/buildflags.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/browser_test.h"
#include "net/dns/mock_host_resolver.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

#if BUILDFLAG(SAFE_BROWSING_AVAILABLE)
#include "chrome/browser/safe_browsing/test_safe_browsing_service.h"
#include "components/safe_browsing/core/browser/db/fake_database_manager.h"
#include "components/safe_browsing/core/common/features.h"
#include "components/safe_browsing/core/common/safe_browsing_prefs.h"
#endif

namespace actor {

namespace {

// Hosts that will trigger lookalike warnings. One causes an interstitial and
// the other only a safety tip.
constexpr char kLookalikeHostInterstitial[] = "google.com.example.com";
constexpr char kLookalikeHostWarning[] = "accounts-google.com";

#if BUILDFLAG(SAFE_BROWSING_AVAILABLE)

class ActorSitePolicyBrowserTest : public InProcessBrowserTest {
 public:
  ActorSitePolicyBrowserTest() {
    base::FieldTrialParams params;
    params["allowlist"] = "a.com,b.com";
    params["allowlist_only"] = "false";
    scoped_feature_list_.InitAndEnableFeatureWithParameters(
        kGlicActionAllowlist, std::move(params));
  }

  ~ActorSitePolicyBrowserTest() override = default;

  void SetUpCommandLine(base::CommandLine* command_line) override {
    InProcessBrowserTest::SetUpCommandLine(command_line);

    constexpr std::string kBlockedHost = "bar.com";
    constexpr uint32_t kNumHashFunctions = 7;
    constexpr uint32_t kNumBits = 511;
    optimization_guide::BloomFilter blocklist_bloom_filter(kNumHashFunctions,
                                                           kNumBits);
    blocklist_bloom_filter.Add(kBlockedHost);
    std::string blocklist_bloom_filter_data(
        reinterpret_cast<const char*>(&blocklist_bloom_filter.bytes()[0]),
        blocklist_bloom_filter.bytes().size());

    optimization_guide::proto::Configuration config;
    optimization_guide::proto::OptimizationFilter*
        blocklist_optimization_filter = config.add_optimization_blocklists();
    blocklist_optimization_filter->set_optimization_type(
        optimization_guide::proto::GLIC_ACTION_PAGE_BLOCK);
    blocklist_optimization_filter->mutable_bloom_filter()
        ->set_num_hash_functions(kNumHashFunctions);
    blocklist_optimization_filter->mutable_bloom_filter()->set_num_bits(
        kNumBits);
    blocklist_optimization_filter->mutable_bloom_filter()->set_data(
        blocklist_bloom_filter_data);

    std::string encoded_config;
    config.SerializeToString(&encoded_config);
    encoded_config = base::Base64Encode(encoded_config);

    command_line->AppendSwitchASCII(
        optimization_guide::switches::kHintsProtoOverride, encoded_config);
  }

  void SetUpOnMainThread() override {
    InProcessBrowserTest::SetUpOnMainThread();
    host_resolver()->AddRule("*", "127.0.0.1");
    LookalikeTestHelper::SetUpLookalikeTestParams();
    embedded_https_test_server().SetCertHostnames(
        {"a.com", "b.com", "c.com", "bar.com", "*.bar.com",
         kLookalikeHostInterstitial, kLookalikeHostWarning});
    ASSERT_TRUE(embedded_https_test_server().Start());

    // Optimization guide uses this histogram to signal initialization in tests.
    optimization_guide::RetryForHistogramUntilCountReached(
        &histogram_tester_for_init_,
        "OptimizationGuide.HintsManager.HintCacheInitialized", 1);

    InitActionBlocklist(browser()->profile());
  }

  void TearDownOnMainThread() override {
    LookalikeTestHelper::TearDownLookalikeTestParams();
    InProcessBrowserTest::TearDownOnMainThread();
  }

 protected:
  void CheckUrl(const GURL& url, bool expected_allowed) {
    ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));

    base::test::TestFuture<bool> allowed;
    auto* actor_service = ActorKeyedService::Get(browser()->profile());
    MayActOnTab(*browser()->tab_strip_model()->GetActiveTab(),
                actor_service->GetJournal(), TaskId(), allowed.GetCallback());
    // The result should not be provided synchronously.
    EXPECT_FALSE(allowed.IsReady());
    EXPECT_EQ(expected_allowed, allowed.Get());
  }

 private:
  base::HistogramTester histogram_tester_for_init_;
  base::test::ScopedFeatureList scoped_feature_list_;
};

IN_PROC_BROWSER_TEST_F(ActorSitePolicyBrowserTest, Basic) {
  const GURL allowed_url =
      embedded_https_test_server().GetURL("a.com", "/title1.html");
  CheckUrl(allowed_url, true);
}

IN_PROC_BROWSER_TEST_F(ActorSitePolicyBrowserTest, AllowIfNotInBlocklist) {
  const GURL allowed_url =
      embedded_https_test_server().GetURL("c.com", "/title1.html");
  CheckUrl(allowed_url, true);
}

IN_PROC_BROWSER_TEST_F(ActorSitePolicyBrowserTest, BlockIfInBlocklist) {
  const GURL blocked_url =
      embedded_https_test_server().GetURL("bar.com", "/title1.html");
  CheckUrl(blocked_url, false);
}

IN_PROC_BROWSER_TEST_F(ActorSitePolicyBrowserTest,
                       BlockSubdomainIfInBlocklist) {
  const GURL blocked_url =
      embedded_https_test_server().GetURL("sub.bar.com", "/title1.html");
  CheckUrl(blocked_url, false);
}

IN_PROC_BROWSER_TEST_F(ActorSitePolicyBrowserTest, BlockLookalikes) {
  const GURL lookalike_url = embedded_https_test_server().GetURL(
      kLookalikeHostInterstitial, "/title1.html");
  CheckUrl(lookalike_url, false);
}

IN_PROC_BROWSER_TEST_F(ActorSitePolicyBrowserTest,
                       TreatLookalikeWarningsAsBlocking) {
  const GURL lookalike_url = embedded_https_test_server().GetURL(
      kLookalikeHostWarning, "/title1.html");
  CheckUrl(lookalike_url, false);
}

class ActorSitePolicySafeBrowsingBrowserTest
    : public ActorSitePolicyBrowserTest {
 public:
  ActorSitePolicySafeBrowsingBrowserTest() = default;
  ~ActorSitePolicySafeBrowsingBrowserTest() override = default;

 protected:
  void CreatedBrowserMainParts(
      content::BrowserMainParts* browser_main_parts) override {
    fake_safe_browsing_database_manager_ =
        base::MakeRefCounted<safe_browsing::FakeSafeBrowsingDatabaseManager>(
            content::GetUIThreadTaskRunner({}));
    safe_browsing_factory_.SetTestDatabaseManager(
        fake_safe_browsing_database_manager_.get());
    safe_browsing::SafeBrowsingService::RegisterFactory(
        &safe_browsing_factory_);
    ActorSitePolicyBrowserTest::CreatedBrowserMainParts(browser_main_parts);
  }

  void TearDown() override {
    safe_browsing::SafeBrowsingService::RegisterFactory(nullptr);
    ActorSitePolicyBrowserTest::TearDown();
  }

  void AddDangerousUrl(const GURL& dangerous_url) {
    fake_safe_browsing_database_manager_->AddDangerousUrl(
        dangerous_url, safe_browsing::SBThreatType::SB_THREAT_TYPE_URL_MALWARE);
  }

  void AddPhishingUrl(const GURL& phishing_url) {
    fake_safe_browsing_database_manager_->AddDangerousUrl(
        phishing_url, safe_browsing::SBThreatType::SB_THREAT_TYPE_URL_PHISHING);
  }

 private:
  scoped_refptr<safe_browsing::FakeSafeBrowsingDatabaseManager>
      fake_safe_browsing_database_manager_;
  safe_browsing::TestSafeBrowsingServiceFactory safe_browsing_factory_;
};

class ActorSitePolicyDelayedWarningBrowserTest
    : public ActorSitePolicySafeBrowsingBrowserTest {
 public:
  ActorSitePolicyDelayedWarningBrowserTest() {
    scoped_feature_list_.InitAndEnableFeature(safe_browsing::kDelayedWarnings);
  }
  ~ActorSitePolicyDelayedWarningBrowserTest() override = default;

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

IN_PROC_BROWSER_TEST_F(ActorSitePolicySafeBrowsingBrowserTest,
                       BlockDangerousSite) {
  const GURL dangerous_url =
      embedded_https_test_server().GetURL("c.com", "/title1.html");
  AddDangerousUrl(dangerous_url);
  CheckUrl(dangerous_url, false);
}

IN_PROC_BROWSER_TEST_F(ActorSitePolicyDelayedWarningBrowserTest,
                       BlockPhishingSiteWithDelayedWarning) {
  const GURL phishing_url =
      embedded_https_test_server().GetURL("c.com", "/title1.html");
  AddPhishingUrl(phishing_url);
  CheckUrl(phishing_url, false);
}

IN_PROC_BROWSER_TEST_F(ActorSitePolicySafeBrowsingBrowserTest,
                       RequireSafeBrowsing) {
  // Disable SafeBrowsing.
  safe_browsing::SetSafeBrowsingState(
      browser()->profile()->GetPrefs(),
      safe_browsing::SafeBrowsingState::NO_SAFE_BROWSING);

  // This would otherwise be allowed, but since we don't have SafeBrowsing to
  // check if it's dangerous, we assume it is unsafe.
  const GURL normally_allowed_url =
      embedded_https_test_server().GetURL("a.com", "/title1.html");
  CheckUrl(normally_allowed_url, false);
}

#endif  // BUILDFLAG(SAFE_BROWSING_AVAILABLE)

}  // namespace

}  // namespace actor