File: site_policy.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; 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 (193 lines) | stat: -rw-r--r-- 7,169 bytes parent folder | download
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
// 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 <string>
#include <string_view>
#include <vector>

#include "base/containers/contains.h"
#include "base/feature_list.h"
#include "base/functional/callback.h"
#include "base/notimplemented.h"
#include "base/strings/string_split.h"
#include "base/task/sequenced_task_runner.h"
#include "chrome/browser/actor/actor_features.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/actor/actor_logging.h"
#include "components/optimization_guide/core/optimization_guide_decision.h"
#include "components/optimization_guide/core/optimization_guide_util.h"
#include "components/optimization_guide/proto/hints.pb.h"
#include "components/safe_browsing/buildflags.h"
#include "components/tabs/public/tab_interface.h"
#include "components/variations/service/variations_service.h"
#include "content/public/browser/web_contents.h"
#include "net/base/url_util.h"
#include "url/gurl.h"
#include "url/url_constants.h"

#if BUILDFLAG(SAFE_BROWSING_AVAILABLE)
#include "chrome/browser/safe_browsing/user_interaction_observer.h"
#endif

namespace actor {

namespace {

void ResolveDecision(DecisionCallback callback, bool decision) {
  // Some decisions are made asynchronously, so always invoke the callback
  // asynchronously for consistency.
  ACTOR_LOG() << __func__ << ": Decided to " << (decision ? "allow" : "block")
              << " for actions";
  base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE, base::BindOnce(std::move(callback), decision));
}

// Returns true if `url`'s host is in the `allowlist`. If `include_subdomains`
// is true, subdomains also match if the parent domain is in the list.
bool IsHostInAllowList(const std::vector<std::string_view>& allowlist,
                       const GURL& url,
                       bool include_subdomains) {
  if (!include_subdomains) {
    return base::Contains(allowlist, url.host_piece());
  }

  std::string host = url.host();
  while (!host.empty()) {
    if (base::Contains(allowlist, host)) {
      return true;
    }
    host = net::GetSuperdomain(host);
  }
  return false;
}

void OnOptimizationGuideDecision(
    DecisionCallback callback,
    optimization_guide::OptimizationGuideDecision decision,
    const optimization_guide::OptimizationMetadata& metadata) {
  ACTOR_LOG() << __func__ << ": OptimizationGuideDecision is "
              << optimization_guide::GetStringForOptimizationGuideDecision(
                     decision);
  ResolveDecision(
      std::move(callback),
      decision == optimization_guide::OptimizationGuideDecision::kTrue);
}

void MayActOnUrl(const GURL& url, Profile* profile, DecisionCallback callback) {
  ACTOR_LOG() << __func__ << ": Considering for eligibility \"" << url.spec()
              << "\"";
  if (net::IsLocalhost(url) || url.IsAboutBlank()) {
    ResolveDecision(std::move(callback), true);
    return;
  }

  if (!url.SchemeIs(url::kHttpsScheme) || url.HostIsIPAddress()) {
    ACTOR_LOG() << __func__ << ": Wrong scheme";
    ResolveDecision(std::move(callback), false);
    return;
  }

  if (base::FeatureList::IsEnabled(kGlicActionAllowlist)) {
    const std::string allowlist_joined = kAllowlist.Get();
    const std::vector<std::string_view> allowlist =
        base::SplitStringPiece(allowlist_joined, ",", base::TRIM_WHITESPACE,
                               base::SPLIT_WANT_NONEMPTY);
    if (IsHostInAllowList(allowlist, url, /*include_subdomains=*/true)) {
      ResolveDecision(std::move(callback), true);
      return;
    }

    const std::string allowlist_exact_joined = kAllowlistExact.Get();
    const std::vector<std::string_view> allowlist_exact =
        base::SplitStringPiece(allowlist_exact_joined, ",",
                               base::TRIM_WHITESPACE,
                               base::SPLIT_WANT_NONEMPTY);
    if (IsHostInAllowList(allowlist_exact, url, /*include_subdomains=*/false)) {
      ResolveDecision(std::move(callback), true);
      return;
    }

    if (kAllowlistOnly.Get()) {
      if (allowlist.empty() && allowlist_exact.empty()) {
        ACTOR_LOG() << __func__ << ": Allowlist is empty";
        if (variations::VariationsService* variations_service =
                g_browser_process->variations_service()) {
          if (!variations_service->IsLikelyDogfoodClient()) {
            ACTOR_LOG() << __func__ << ": Non-dogfood client";
          }
          if (variations_service->GetClientFilterableStateForVersion()
                  ->GoogleGroups()
                  .empty()) {
            ACTOR_LOG() << __func__ << ": No Google groups";
          }
        }
      } else {
        ACTOR_LOG() << __func__ << ": URL not in allowlist";
      }
      ResolveDecision(std::move(callback), false);
      return;
    }
  }

  if (auto* optimization_guide_decider =
          OptimizationGuideKeyedServiceFactory::GetForProfile(profile);
      optimization_guide_decider &&
      base::FeatureList::IsEnabled(kGlicActionUseOptimizationGuide)) {
    optimization_guide_decider->CanApplyOptimization(
        url, optimization_guide::proto::GLIC_ACTION_PAGE_BLOCK,
        base::BindOnce(&OnOptimizationGuideDecision, std::move(callback)));
    return;
  }

  // Fail closed.
  ResolveDecision(std::move(callback), false);
}

}  // namespace

void InitActionBlocklist(Profile* profile) {
  if (auto* optimization_guide_decider =
          OptimizationGuideKeyedServiceFactory::GetForProfile(profile);
      optimization_guide_decider &&
      base::FeatureList::IsEnabled(kGlicActionUseOptimizationGuide)) {
    optimization_guide_decider->RegisterOptimizationTypes(
        {optimization_guide::proto::GLIC_ACTION_PAGE_BLOCK});
  }
}

// TODO(mcnee): Add UMA for the outcomes.
void MayActOnTab(const tabs::TabInterface& tab, DecisionCallback callback) {
  content::WebContents& web_contents = *tab.GetContents();

  if (web_contents.GetPrimaryMainFrame()->IsErrorDocument()) {
    ACTOR_LOG() << __func__ << ": Tab is an error document";
    ResolveDecision(std::move(callback), false);
    return;
  }

#if BUILDFLAG(SAFE_BROWSING_AVAILABLE)
  // SafeBrowsing Delayed Warnings experiment can delay some SafeBrowsing
  // warnings until user interaction. If the current page has a delayed warning,
  // it'll have a user interaction observer attached.
  // Do not act on such a page.
  if (safe_browsing::SafeBrowsingUserInteractionObserver::FromWebContents(
          &web_contents)) {
    ACTOR_LOG() << __func__ << ": Blocked by safebrowsing";
    ResolveDecision(std::move(callback), false);
    return;
  }
#endif

  const GURL& url = web_contents.GetPrimaryMainFrame()->GetLastCommittedURL();
  MayActOnUrl(url,
              Profile::FromBrowserContext(web_contents.GetBrowserContext()),
              std::move(callback));
}

}  // namespace actor