File: real_time_url_checks_allowlist.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 (199 lines) | stat: -rw-r--r-- 7,611 bytes parent folder | download | duplicates (11)
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
// Copyright 2022 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/android/real_time_url_checks_allowlist.h"

#include "base/functional/bind.h"
#include "base/memory/singleton.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "components/grit/components_resources.h"
#include "components/safe_browsing/android/proto/realtimeallowlist.pb.h"
#include "components/safe_browsing/core/browser/db/v4_protocol_manager_util.h"
#include "ui/base/resource/resource_bundle.h"

namespace safe_browsing {

namespace {

constexpr char kAllowlistPopulateUmaPrefix[] =
    "SafeBrowsing.Android.RealTimeAllowlist.Populate.";
constexpr char kAllowlistIsInAllowlistUmaPrefix[] =
    "SafeBrowsing.Android.RealTimeAllowlist.IsInAllowlist.";
constexpr char kPopulateResourceFileElapsed[] =
    "SafeBrowsing.Android.RealTimeAllowlist.PopulateResourceFileElapsed";
constexpr char kIsInAllowlistElapsed[] =
    "SafeBrowsing.Android.RealTimeAllowlist.IsInAllowlistElapsed";
constexpr char kDynamicUpdate[] = "DynamicUpdate";
constexpr char kResourceBundle[] = "ResourceBundle";
const int kHashSizeInBytes = 16;
const int kInvalidVersion = -1;
const int kValidSchemeId = 0;
const size_t kMinimumHashEntryCount = 100;

}  // namespace

using base::AutoLock;

struct RealTimeUrlChecksAllowlistSingletonTrait
    : public base::DefaultSingletonTraits<RealTimeUrlChecksAllowlist> {
  static RealTimeUrlChecksAllowlist* New() {
    RealTimeUrlChecksAllowlist* instance = new RealTimeUrlChecksAllowlist();
    instance->PopulateFromResourceBundle();
    return instance;
  }
};

// static
RealTimeUrlChecksAllowlist* RealTimeUrlChecksAllowlist::instance_for_testing_ =
    nullptr;

RealTimeUrlChecksAllowlist* RealTimeUrlChecksAllowlist::GetInstance() {
  if (instance_for_testing_)
    return instance_for_testing_;
  return base::Singleton<RealTimeUrlChecksAllowlist,
                         RealTimeUrlChecksAllowlistSingletonTrait>::get();
}
void RealTimeUrlChecksAllowlist::SetInstanceForTesting(
    RealTimeUrlChecksAllowlist* instance_for_testing) {
  instance_for_testing_ = instance_for_testing;
}

RealTimeUrlChecksAllowlist::RealTimeUrlChecksAllowlist()
    : version_id_(kInvalidVersion),
      scheme_id_(kValidSchemeId),
      minimum_hash_entry_count_(kMinimumHashEntryCount) {}
RealTimeUrlChecksAllowlist::~RealTimeUrlChecksAllowlist() {
  AutoLock lock(lock_);  // DCHECK fail if the lock is held.
  instance_for_testing_ = nullptr;
}

void RealTimeUrlChecksAllowlist::PopulateFromResourceBundle() {
  AutoLock lock(lock_);
  SCOPED_UMA_HISTOGRAM_TIMER(kPopulateResourceFileElapsed);
  ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
  std::string binary_pb =
      bundle.LoadDataResourceString(IDR_REAL_TIME_URL_CHECKS_ALLOWLIST_PB);
  RealTimeUrlChecksAllowlist::PopulateResult result =
      PopulateAllowlistFromBinaryPb(binary_pb);
  RecordPopulateMetrics(result, kResourceBundle);
  is_using_component_updater_version_ = false;
}

void RealTimeUrlChecksAllowlist::PopulateFromDynamicUpdate(
    const std::string& binary_pb) {
  AutoLock lock(lock_);
  RealTimeUrlChecksAllowlist::PopulateResult result =
      PopulateAllowlistFromBinaryPb(binary_pb);
  RecordPopulateMetrics(result, kDynamicUpdate);
  if (result == PopulateResult::kSuccess)
    is_using_component_updater_version_ = true;
}

RealTimeUrlChecksAllowlist::PopulateResult
RealTimeUrlChecksAllowlist::PopulateAllowlistFromBinaryPb(
    std::string binary_pb) {
  lock_.AssertAcquired();
  if (binary_pb.empty())
    return PopulateResult::kFailedEmpty;

  HighConfidenceAllowlist parsed_pb;
  if (!parsed_pb.ParseFromString(binary_pb))
    return PopulateResult::kFailedProtoParse;
  if (!parsed_pb.has_scheme_id())
    return PopulateResult::kFailedMissingSchemeId;
  if (parsed_pb.scheme_id() != scheme_id_)
    return PopulateResult::kSkippedInvalidSchemeId;
  if (!parsed_pb.has_version_id())
    return PopulateResult::kFailedMissingVersionId;
  if (parsed_pb.version_id() < version_id_)
    return PopulateResult::kSkippedOldVersionId;
  if (parsed_pb.version_id() == version_id_)
    return PopulateResult::kSkippedEqualVersionId;
  if (!parsed_pb.has_url_hashes())
    return PopulateResult::kFailedMissingUrlHashes;
  auto hashes_length = parsed_pb.url_hashes().length();
  if (hashes_length == 0)
    return PopulateResult::kFailedEmptyUrlHashes;
  if (hashes_length < minimum_hash_entry_count_ * kHashSizeInBytes)
    return PopulateResult::kFailedTooFewAllowlistEntries;
  if (hashes_length % kHashSizeInBytes != 0)
    return PopulateResult::kFailedDanglingHash;
  if (hashes_length >= UINT_MAX)
    return PopulateResult::kFailedHashLengthExceedsMax;

  std::set<std::string> new_allowlist_patterns;
  for (uint i = 0; i + kHashSizeInBytes <= hashes_length;
       i += kHashSizeInBytes) {
    std::string url_hash = parsed_pb.url_hashes().substr(i, kHashSizeInBytes);
    new_allowlist_patterns.insert(url_hash);
  }
  allowlist_patterns_ = new_allowlist_patterns;
  version_id_ = parsed_pb.version_id();
  return PopulateResult::kSuccess;
}

RealTimeUrlChecksAllowlist::IsInAllowlistResult
RealTimeUrlChecksAllowlist::IsInAllowlist(const GURL& url) {
  AutoLock lock(lock_);
  SCOPED_UMA_HISTOGRAM_TIMER(kIsInAllowlistElapsed);
  RealTimeUrlChecksAllowlist::IsInAllowlistResult result =
      IsInAllowlistInternal(url);
  RecordAllowlistUrlCheckMetrics(result, is_using_component_updater_version_
                                             ? kDynamicUpdate
                                             : kResourceBundle);
  return result;
}

RealTimeUrlChecksAllowlist::IsInAllowlistResult
RealTimeUrlChecksAllowlist::IsInAllowlistInternal(const GURL& url) {
  lock_.AssertAcquired();
  // If allowlist is empty, then the allowlist is unavailable
  if (allowlist_patterns_.empty()) {
    return IsInAllowlistResult::kAllowlistUnavailable;
  }

  std::vector<FullHashStr> full_hashes;
  V4ProtocolManagerUtil::UrlToFullHashes(url, &full_hashes);
  for (auto fh : full_hashes) {
    auto truncated_hash = fh.substr(0, kHashSizeInBytes);
    if (allowlist_patterns_.find(truncated_hash) != allowlist_patterns_.end()) {
      return IsInAllowlistResult::kInAllowlist;
    }
  }
  return IsInAllowlistResult::kNotInAllowlist;
}

void RealTimeUrlChecksAllowlist::SetMinimumEntryCountForTesting(
    size_t new_hash_entry_count) {
  minimum_hash_entry_count_ = new_hash_entry_count;
}

void RealTimeUrlChecksAllowlist::RecordPopulateMetrics(
    RealTimeUrlChecksAllowlist::PopulateResult result,
    const std::string& src_name) {
  lock_.AssertAcquired();
  DCHECK(src_name == kResourceBundle || src_name == kDynamicUpdate);
  base::UmaHistogramEnumeration(
      kAllowlistPopulateUmaPrefix + src_name + "Result", result);

  if (result == RealTimeUrlChecksAllowlist::PopulateResult::kSuccess) {
    base::UmaHistogramSparse(kAllowlistPopulateUmaPrefix + src_name + "Version",
                             version_id_);
    base::UmaHistogramCounts10000(
        kAllowlistPopulateUmaPrefix + src_name + "Size",
        allowlist_patterns_.size());
  }
}

void RealTimeUrlChecksAllowlist::RecordAllowlistUrlCheckMetrics(
    RealTimeUrlChecksAllowlist::IsInAllowlistResult result,
    const std::string& src_name) {
  lock_.AssertAcquired();
  DCHECK(src_name == kResourceBundle || src_name == kDynamicUpdate);
  base::UmaHistogramEnumeration(
      kAllowlistIsInAllowlistUmaPrefix + src_name + "Result", result);
}

}  // namespace safe_browsing