File: url_lookup_service_browsertest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (188 lines) | stat: -rw-r--r-- 7,377 bytes parent folder | download | duplicates (4)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/safe_browsing/core/browser/realtime/url_lookup_service.h"

#include "base/test/metrics/histogram_tester.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/safe_browsing/url_lookup_service_factory.h"
#include "chrome/browser/signin/identity_test_environment_profile_adaptor.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/prefs/pref_service.h"
#include "components/safe_browsing/core/common/safe_browsing_prefs.h"
#include "components/safe_browsing/core/common/utils.h"
#include "components/variations/pref_names.h"
#include "content/public/test/browser_test.h"
#include "net/dns/mock_host_resolver.h"

namespace safe_browsing {

namespace {

constexpr std::string_view kRealtimeEndpoint = "/realtime_endpoint";

class SafeBrowsingUrlLookupServiceTest : public InProcessBrowserTest {
 public:
  SafeBrowsingUrlLookupServiceTest() {
    scoped_feature_list_.InitAndDisableFeature(
        kSafeBrowsingRemoveCookiesInAuthRequests);
  }
  SafeBrowsingUrlLookupServiceTest(const SafeBrowsingUrlLookupServiceTest&) =
      delete;
  SafeBrowsingUrlLookupServiceTest& operator=(
      const SafeBrowsingUrlLookupServiceTest&) = delete;

  void SetUp() override {
    secure_embedded_test_server_ = std::make_unique<net::EmbeddedTestServer>(
        net::EmbeddedTestServer::Type::TYPE_HTTPS);
    secure_embedded_test_server_->RegisterRequestHandler(base::BindRepeating(
        &SafeBrowsingUrlLookupServiceTest::UrlRequestHandler,
        base::Unretained(this)));
    ASSERT_TRUE(secure_embedded_test_server_->Start());
    InProcessBrowserTest::SetUp();
  }

  void SetUpOnMainThread() override {
    InProcessBrowserTest::SetUpOnMainThread();
    host_resolver()->AddRule("*", "127.0.0.1");
    identity_test_env_adaptor_ =
        std::make_unique<IdentityTestEnvironmentProfileAdaptor>(
            browser()->profile());
  }

  void SetUpInProcessBrowserTestFixture() override {
    create_services_subscription_ =
        BrowserContextDependencyManager::GetInstance()
            ->RegisterCreateServicesCallbackForTesting(
                base::BindRepeating(&SafeBrowsingUrlLookupServiceTest::
                                        OnWillCreateBrowserContextServices,
                                    base::Unretained(this)));
  }

  signin::IdentityTestEnvironment* identity_test_env() {
    return identity_test_env_adaptor_->identity_test_env();
  }

  net::EmbeddedTestServer* secure_embedded_test_server() {
    return secure_embedded_test_server_.get();
  }

  const net::test_server::HttpRequest last_realtime_request() {
    return last_realtime_request_;
  }

 private:
  std::unique_ptr<net::test_server::HttpResponse> UrlRequestHandler(
      const net::test_server::HttpRequest& request) {
    if (request.relative_url == kRealtimeEndpoint) {
      auto response = std::make_unique<net::test_server::BasicHttpResponse>();
      response->set_code(net::HTTP_OK);
      response->AddCustomHeader("Set-Cookie", "cookie=reset");
      last_realtime_request_ = request;
      return response;
    }
    return nullptr;
  }

  void OnWillCreateBrowserContextServices(content::BrowserContext* context) {
    IdentityTestEnvironmentProfileAdaptor::
        SetIdentityTestEnvironmentFactoriesOnBrowserContext(context);
  }

  base::test::ScopedFeatureList scoped_feature_list_;
  std::unique_ptr<net::EmbeddedTestServer> secure_embedded_test_server_;
  base::CallbackListSubscription create_services_subscription_;
  std::unique_ptr<IdentityTestEnvironmentProfileAdaptor>
      identity_test_env_adaptor_;
  net::test_server::HttpRequest last_realtime_request_;
};

IN_PROC_BROWSER_TEST_F(SafeBrowsingUrlLookupServiceTest,
                       ServiceRespectsLocationChanges) {
  safe_browsing::SetSafeBrowsingState(
      browser()->profile()->GetPrefs(),
      safe_browsing::SafeBrowsingState::ENHANCED_PROTECTION);
  auto* url_lookup_service =
      RealTimeUrlLookupServiceFactory::GetForProfile(browser()->profile());

  // By default for ESB, full URL lookups should be enabled.
  EXPECT_TRUE(url_lookup_service->CanPerformFullURLLookup());

  // Changing to CN should disable the lookups.
  g_browser_process->local_state()->SetString(
      variations::prefs::kVariationsCountry, "cn");
  EXPECT_FALSE(url_lookup_service->CanPerformFullURLLookup());

  // Changing to US should re-enable the lookups.
  g_browser_process->local_state()->SetString(
      variations::prefs::kVariationsCountry, "us");
  EXPECT_TRUE(url_lookup_service->CanPerformFullURLLookup());
}

IN_PROC_BROWSER_TEST_F(SafeBrowsingUrlLookupServiceTest, LookupWithToken) {
  identity_test_env()->SetPrimaryAccount("test@example.com",
                                         signin::ConsentLevel::kSync);
  identity_test_env()->SetRefreshTokenForPrimaryAccount();
  identity_test_env()->SetAutomaticIssueOfAccessTokens(true);

  RealTimeUrlLookupService::OverrideUrlForTesting(
      secure_embedded_test_server()->GetURL(kRealtimeEndpoint));

  safe_browsing::SetSafeBrowsingState(
      browser()->profile()->GetPrefs(),
      safe_browsing::SafeBrowsingState::ENHANCED_PROTECTION);
  auto* url_lookup_service =
      RealTimeUrlLookupServiceFactory::GetForProfile(browser()->profile());

  base::RunLoop run_loop;
  base::HistogramTester histogram_tester;
  url_lookup_service->StartLookup(
      secure_embedded_test_server()->GetURL("/"),
      base::IgnoreArgs<bool, bool, std::unique_ptr<RTLookupResponse>>(
          run_loop.QuitClosure()),
      base::SequencedTaskRunner::GetCurrentDefault(), SessionID::InvalidValue(),
      /*referring_app_info=*/std::nullopt);
  run_loop.Run();

  EXPECT_TRUE(base::Contains(last_realtime_request().headers,
                             net::HttpRequestHeaders::kAuthorization));

  histogram_tester.ExpectUniqueSample(
      "SafeBrowsing.AuthenticatedCookieResetEndpoint",
      SafeBrowsingAuthenticatedEndpoint::kRealtimeUrlLookup, 1);
}

IN_PROC_BROWSER_TEST_F(SafeBrowsingUrlLookupServiceTest, LookupWithoutToken) {
  RealTimeUrlLookupService::OverrideUrlForTesting(
      secure_embedded_test_server()->GetURL(kRealtimeEndpoint));

  safe_browsing::SetSafeBrowsingState(
      browser()->profile()->GetPrefs(),
      safe_browsing::SafeBrowsingState::ENHANCED_PROTECTION);
  auto* url_lookup_service =
      RealTimeUrlLookupServiceFactory::GetForProfile(browser()->profile());

  base::RunLoop run_loop;
  base::HistogramTester histogram_tester;
  url_lookup_service->StartLookup(
      secure_embedded_test_server()->GetURL("/"),
      base::IgnoreArgs<bool, bool, std::unique_ptr<RTLookupResponse>>(
          run_loop.QuitClosure()),
      base::SequencedTaskRunner::GetCurrentDefault(), SessionID::InvalidValue(),
      /*referring_app_info=*/std::nullopt);
  run_loop.Run();

  EXPECT_FALSE(base::Contains(last_realtime_request().headers,
                              net::HttpRequestHeaders::kAuthorization));

  histogram_tester.ExpectUniqueSample(
      "SafeBrowsing.AuthenticatedCookieResetEndpoint",
      SafeBrowsingAuthenticatedEndpoint::kRealtimeUrlLookup, 0);
}

}  // namespace

}  // namespace safe_browsing