Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(994)

Side by Side Diff: components/safe_browsing_db/v4_get_hash_protocol_manager.cc

Issue 2233103002: Move full hash caching logic to v4_get_hash_protocol_manager (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: unique_ptr for V4GetHasProtocolManager. Fix the lone failing unit test. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/safe_browsing_db/v4_get_hash_protocol_manager.h" 5 #include "components/safe_browsing_db/v4_get_hash_protocol_manager.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/base64url.h" 9 #include "base/base64url.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
(...skipping 30 matching lines...) Expand all
41 // expected. 41 // expected.
42 NO_METADATA_ERROR = 4, 42 NO_METADATA_ERROR = 4,
43 43
44 // A match in the response contained a ThreatType that was inconsistent 44 // A match in the response contained a ThreatType that was inconsistent
45 // with the other matches. 45 // with the other matches.
46 INCONSISTENT_THREAT_TYPE_ERROR = 5, 46 INCONSISTENT_THREAT_TYPE_ERROR = 5,
47 47
48 // A match in the response contained a metadata, but the metadata is invalid. 48 // A match in the response contained a metadata, but the metadata is invalid.
49 UNEXPECTED_METADATA_VALUE_ERROR = 6, 49 UNEXPECTED_METADATA_VALUE_ERROR = 6,
50 50
51 // A match in the response had no information in the threat field.
52 NO_THREAT_ERROR = 7,
53
51 // Memory space for histograms is determined by the max. ALWAYS 54 // Memory space for histograms is determined by the max. ALWAYS
52 // ADD NEW VALUES BEFORE THIS ONE. 55 // ADD NEW VALUES BEFORE THIS ONE.
53 PARSE_RESULT_TYPE_MAX = 7, 56 PARSE_RESULT_TYPE_MAX = 8,
57 };
58
59 // Enumerate full hash cache hits/misses for histogramming purposes.
60 // DO NOT CHANGE THE ORDERING OF THESE VALUES.
61 enum V4FullHashCacheResultType {
62 // Full hashes for which there is no cache hit.
63 FULL_HASH_CACHE_MISS = 0,
64
65 // Full hashes with a cache hit.
66 FULL_HASH_CACHE_HIT = 1,
67
68 // Full hashes with a negative cache hit.
69 FULL_HASH_NEGATIVE_CACHE_HIT = 2,
70
71 // Memory space for histograms is determined by the max. ALWAYS
72 // ADD NEW VALUES BEFORE THIS ONE.
73 FULL_HASH_CACHE_RESULT_MAX
54 }; 74 };
55 75
56 // Record parsing errors of a GetHash result. 76 // Record parsing errors of a GetHash result.
57 void RecordParseGetHashResult(ParseResultType result_type) { 77 void RecordParseGetHashResult(ParseResultType result_type) {
58 UMA_HISTOGRAM_ENUMERATION("SafeBrowsing.ParseV4HashResult", result_type, 78 UMA_HISTOGRAM_ENUMERATION("SafeBrowsing.ParseV4HashResult", result_type,
59 PARSE_RESULT_TYPE_MAX); 79 PARSE_RESULT_TYPE_MAX);
60 } 80 }
61 81
62 // Record a GetHash result. 82 // Record a GetHash result.
63 void RecordGetHashResult(safe_browsing::V4OperationResult result) { 83 void RecordGetHashResult(safe_browsing::V4OperationResult result) {
64 UMA_HISTOGRAM_ENUMERATION( 84 UMA_HISTOGRAM_ENUMERATION(
65 "SafeBrowsing.GetV4HashResult", result, 85 "SafeBrowsing.GetV4HashResult", result,
66 safe_browsing::V4OperationResult::OPERATION_RESULT_MAX); 86 safe_browsing::V4OperationResult::OPERATION_RESULT_MAX);
67 } 87 }
68 88
89 // Record a full hash cache hit result.
90 void RecordV4FullHashCacheResult(V4FullHashCacheResultType result_type) {
91 UMA_HISTOGRAM_ENUMERATION("SafeBrowsing.V4FullHashCacheResult", result_type,
92 FULL_HASH_CACHE_RESULT_MAX);
93 }
94
69 } // namespace 95 } // namespace
70 96
71 namespace safe_browsing { 97 namespace safe_browsing {
72 98
73 const char kUmaV4HashResponseMetricName[] = 99 const char kUmaV4HashResponseMetricName[] =
74 "SafeBrowsing.GetV4HashHttpResponseOrErrorCode"; 100 "SafeBrowsing.GetV4HashHttpResponseOrErrorCode";
75 101
76 // The default V4GetHashProtocolManagerFactory. 102 // The default V4GetHashProtocolManagerFactory.
77 class V4GetHashProtocolManagerFactoryImpl 103 class V4GetHashProtocolManagerFactoryImpl
78 : public V4GetHashProtocolManagerFactory { 104 : public V4GetHashProtocolManagerFactory {
79 public: 105 public:
80 V4GetHashProtocolManagerFactoryImpl() {} 106 V4GetHashProtocolManagerFactoryImpl() {}
81 ~V4GetHashProtocolManagerFactoryImpl() override {} 107 ~V4GetHashProtocolManagerFactoryImpl() override {}
82 V4GetHashProtocolManager* CreateProtocolManager( 108 std::unique_ptr<V4GetHashProtocolManager> CreateProtocolManager(
83 net::URLRequestContextGetter* request_context_getter, 109 net::URLRequestContextGetter* request_context_getter,
110 const base::hash_set<UpdateListIdentifier>& stores_to_request,
84 const V4ProtocolConfig& config) override { 111 const V4ProtocolConfig& config) override {
85 return new V4GetHashProtocolManager(request_context_getter, config); 112 return base::WrapUnique(new V4GetHashProtocolManager(
113 request_context_getter, stores_to_request, config));
86 } 114 }
87 115
88 private: 116 private:
89 DISALLOW_COPY_AND_ASSIGN(V4GetHashProtocolManagerFactoryImpl); 117 DISALLOW_COPY_AND_ASSIGN(V4GetHashProtocolManagerFactoryImpl);
90 }; 118 };
91 119
120 // ----------------------------------------------------------------
121
122 CachedHashPrefixInfo::CachedHashPrefixInfo() {}
123
124 CachedHashPrefixInfo::CachedHashPrefixInfo(const CachedHashPrefixInfo& other) =
125 default;
126
127 CachedHashPrefixInfo::~CachedHashPrefixInfo() {}
128
129 // ----------------------------------------------------------------
130
131 FullHashCallbackInfo::FullHashCallbackInfo() {}
132
133 FullHashCallbackInfo::FullHashCallbackInfo(
134 const std::vector<FullHashInfo>& cached_full_hash_infos,
135 const std::vector<HashPrefix>& prefixes_requested,
136 std::unique_ptr<net::URLFetcher> fetcher,
137 const FullHashToStoreAndHashPrefixesMap&
138 full_hash_to_store_and_hash_prefixes,
139 FullHashCallback callback)
140 : cached_full_hash_infos(cached_full_hash_infos),
141 callback(callback),
142 fetcher(std::move(fetcher)),
143 full_hash_to_store_and_hash_prefixes(
144 full_hash_to_store_and_hash_prefixes),
145 prefixes_requested(prefixes_requested) {}
146
147 FullHashCallbackInfo::~FullHashCallbackInfo() {}
148
149 // ----------------------------------------------------------------
150
151 FullHashInfo::FullHashInfo(const FullHash& full_hash,
152 const UpdateListIdentifier& list_id,
153 const base::Time& positive_ttl)
154 : full_hash(full_hash), list_id(list_id), positive_ttl(positive_ttl) {}
155
156 FullHashInfo::FullHashInfo(const FullHashInfo& other) = default;
157
158 FullHashInfo::~FullHashInfo() {}
159
160 bool FullHashInfo::operator==(const FullHashInfo& other) const {
161 return full_hash == other.full_hash && list_id == other.list_id &&
162 positive_ttl == other.positive_ttl && metadata == other.metadata;
163 }
164
165 bool FullHashInfo::operator!=(const FullHashInfo& other) const {
166 return !operator==(other);
167 }
168
92 // V4GetHashProtocolManager implementation -------------------------------- 169 // V4GetHashProtocolManager implementation --------------------------------
93 170
94 // static 171 // static
95 V4GetHashProtocolManagerFactory* V4GetHashProtocolManager::factory_ = NULL; 172 V4GetHashProtocolManagerFactory* V4GetHashProtocolManager::factory_ = NULL;
96 173
97 // static 174 // static
98 V4GetHashProtocolManager* V4GetHashProtocolManager::Create( 175 std::unique_ptr<V4GetHashProtocolManager> V4GetHashProtocolManager::Create(
99 net::URLRequestContextGetter* request_context_getter, 176 net::URLRequestContextGetter* request_context_getter,
177 const base::hash_set<UpdateListIdentifier>& stores_to_request,
100 const V4ProtocolConfig& config) { 178 const V4ProtocolConfig& config) {
101 if (!factory_) 179 if (!factory_)
102 factory_ = new V4GetHashProtocolManagerFactoryImpl(); 180 factory_ = new V4GetHashProtocolManagerFactoryImpl();
103 return factory_->CreateProtocolManager(request_context_getter, config); 181 return factory_->CreateProtocolManager(request_context_getter,
182 stores_to_request, config);
104 } 183 }
105 184
106 void V4GetHashProtocolManager::ResetGetHashErrors() { 185 // static
107 gethash_error_count_ = 0; 186 void V4GetHashProtocolManager::RegisterFactory(
108 gethash_back_off_mult_ = 1; 187 std::unique_ptr<V4GetHashProtocolManagerFactory> factory) {
188 if (factory_)
189 delete factory_;
190 factory_ = factory.release();
109 } 191 }
110 192
111 V4GetHashProtocolManager::V4GetHashProtocolManager( 193 V4GetHashProtocolManager::V4GetHashProtocolManager(
112 net::URLRequestContextGetter* request_context_getter, 194 net::URLRequestContextGetter* request_context_getter,
195 const base::hash_set<UpdateListIdentifier>& stores_to_request,
113 const V4ProtocolConfig& config) 196 const V4ProtocolConfig& config)
114 : gethash_error_count_(0), 197 : gethash_error_count_(0),
115 gethash_back_off_mult_(1), 198 gethash_back_off_mult_(1),
116 next_gethash_time_(Time::FromDoubleT(0)), 199 next_gethash_time_(Time::FromDoubleT(0)),
117 config_(config), 200 config_(config),
118 request_context_getter_(request_context_getter), 201 request_context_getter_(request_context_getter),
119 url_fetcher_id_(0), 202 url_fetcher_id_(0),
120 clock_(new base::DefaultClock()) {} 203 clock_(new base::DefaultClock()) {
121 204 for (const UpdateListIdentifier& store : stores_to_request) {
122 V4GetHashProtocolManager::~V4GetHashProtocolManager() { 205 platform_types_.insert(store.platform_type);
123 } 206 threat_entry_types_.insert(store.threat_entry_type);
124 207 threat_types_.insert(store.threat_type);
125 // static 208 }
126 void V4GetHashProtocolManager::RegisterFactory( 209 DCHECK(!platform_types_.empty());
127 std::unique_ptr<V4GetHashProtocolManagerFactory> factory) { 210 DCHECK(!threat_entry_types_.empty());
128 if (factory_) 211 DCHECK(!threat_types_.empty());
129 delete factory_; 212 }
130 factory_ = factory.release(); 213
214 V4GetHashProtocolManager::~V4GetHashProtocolManager() {}
215
216 void V4GetHashProtocolManager::ClearCache() {
217 DCHECK(CalledOnValidThread());
218 full_hash_cache_.clear();
219 }
220
221 void V4GetHashProtocolManager::GetFullHashes(
222 const FullHashToStoreAndHashPrefixesMap&
223 full_hash_to_store_and_hash_prefixes,
224 FullHashCallback callback) {
225 DCHECK(CalledOnValidThread());
226 DCHECK(!full_hash_to_store_and_hash_prefixes.empty());
227
228 std::vector<HashPrefix> prefixes_to_request;
229 std::vector<FullHashInfo> cached_full_hash_infos;
230 GetFullHashCachedResults(full_hash_to_store_and_hash_prefixes, Time::Now(),
231 &prefixes_to_request, &cached_full_hash_infos);
232
233 if (prefixes_to_request.empty()) {
234 // 100% cache hits (positive or negative) so we can call the callback right
235 // away.
236 callback.Run(cached_full_hash_infos);
237 return;
238 }
239
240 // We need to wait the minimum waiting duration, and if we are in backoff,
241 // we need to check if we're past the next allowed time. If we are, we can
242 // proceed with the request. If not, we are required to return empty results
243 // (i.e. treat the page as safe).
244 if (clock_->Now() <= next_gethash_time_) {
245 if (gethash_error_count_) {
246 RecordGetHashResult(V4OperationResult::BACKOFF_ERROR);
247 } else {
248 RecordGetHashResult(V4OperationResult::MIN_WAIT_DURATION_ERROR);
249 }
250 callback.Run(cached_full_hash_infos);
251 return;
252 }
253
254 std::string req_base64 = GetHashRequest(prefixes_to_request);
255 GURL gethash_url;
256 net::HttpRequestHeaders headers;
257 GetHashUrlAndHeaders(req_base64, &gethash_url, &headers);
258
259 std::unique_ptr<net::URLFetcher> owned_fetcher = net::URLFetcher::Create(
260 url_fetcher_id_++, gethash_url, net::URLFetcher::GET, this);
261 net::URLFetcher* fetcher = owned_fetcher.get();
262 pending_hash_requests_[fetcher].reset(new FullHashCallbackInfo(
263 cached_full_hash_infos, prefixes_to_request, std::move(owned_fetcher),
264 full_hash_to_store_and_hash_prefixes, callback));
265
266 fetcher->SetExtraRequestHeaders(headers.ToString());
267 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE);
268 fetcher->SetRequestContext(request_context_getter_.get());
269 fetcher->Start();
270 }
271
272 void V4GetHashProtocolManager::GetFullHashesWithApis(
273 const GURL& url,
274 ThreatMetadataForApiCallback api_callback) {
275 base::hash_set<FullHash> full_hashes;
276 V4ProtocolManagerUtil::UrlToFullHashes(url, &full_hashes);
277
278 FullHashToStoreAndHashPrefixesMap full_hash_to_store_and_hash_prefixes;
279 for (const FullHash& full_hash : full_hashes) {
280 HashPrefix prefix;
281 bool result =
282 V4ProtocolManagerUtil::FullHashToSmallestHashPrefix(full_hash, &prefix);
283 DCHECK(result);
284 full_hash_to_store_and_hash_prefixes[full_hash].push_back(
285 StoreAndHashPrefix(GetChromeUrlApiId(), prefix));
286 }
287
288 GetFullHashes(full_hash_to_store_and_hash_prefixes,
289 base::Bind(&V4GetHashProtocolManager::OnFullHashForApi,
290 base::Unretained(this), api_callback, full_hashes));
291 }
292
293 void V4GetHashProtocolManager::GetFullHashCachedResults(
294 const FullHashToStoreAndHashPrefixesMap&
295 full_hash_to_store_and_hash_prefixes,
296 const Time& now,
297 std::vector<HashPrefix>* prefixes_to_request,
298 std::vector<FullHashInfo>* cached_full_hash_infos) const {
299 DCHECK(!full_hash_to_store_and_hash_prefixes.empty());
300 DCHECK(prefixes_to_request);
301 DCHECK(prefixes_to_request->empty());
302 DCHECK(cached_full_hash_infos);
303 DCHECK(cached_full_hash_infos->empty());
304
305 // Caching behavior is documented here:
306 // https://developers.google.com/safe-browsing/v4/caching#about-caching
307 //
308 // The cache operates as follows:
309 // Lookup:
310 // Case 1: The prefix is in the cache.
311 // Case a: The full hash is in the cache.
312 // Case i : The positive full hash result has not expired.
313 // The result is unsafe and we do not need to send a new
314 // request.
315 // Case ii: The positive full hash result has expired.
316 // We need to send a request for full hashes.
317 // Case b: The full hash is not in the cache.
318 // Case i : The negative cache entry has not expired.
319 // The result is still safe and we do not need to send a
320 // new request.
321 // Case ii: The negative cache entry has expired.
322 // We need to send a request for full hashes.
323 // Case 2: The prefix is not in the cache.
324 // We need to send a request for full hashes.
325 //
326 // Note on eviction:
327 // CachedHashPrefixInfo entries can be removed from the cache only when
328 // the negative cache expire time and the cache expire time of all full
329 // hash results for that prefix have expired.
330 // Individual full hash results can be removed from the prefix's
331 // cache entry if they expire AND their expire time is after the negative
332 // cache expire time.
333
334 // TODO(vakh): Perform cache cleanup.
335 base::hash_set<HashPrefix> unique_prefixes_to_request;
336 for (const auto& it : full_hash_to_store_and_hash_prefixes) {
337 const FullHash& full_hash = it.first;
338 const StoreAndHashPrefixes& matched = it.second;
339 for (const StoreAndHashPrefix& matched_it : matched) {
340 const UpdateListIdentifier& list_id = matched_it.list_id;
341 const HashPrefix& prefix = matched_it.hash_prefix;
342 const auto& prefix_entry = full_hash_cache_.find(prefix);
343 if (prefix_entry != full_hash_cache_.end()) {
344 // Case 1.
345 const CachedHashPrefixInfo& cached_prefix_info = prefix_entry->second;
346 bool found_full_hash = false;
347 for (const FullHashInfo& full_hash_info :
348 cached_prefix_info.full_hash_infos) {
349 if (full_hash_info.full_hash == full_hash &&
350 full_hash_info.list_id == list_id) {
351 // Case a.
352 found_full_hash = true;
353 bool positive_ttl_unexpired = full_hash_info.positive_ttl > now;
354 if (positive_ttl_unexpired) {
355 // Case i.
356 cached_full_hash_infos->push_back(full_hash_info);
357 RecordV4FullHashCacheResult(FULL_HASH_CACHE_HIT);
358 } else {
359 // Case ii.
360 unique_prefixes_to_request.insert(prefix);
361 RecordV4FullHashCacheResult(FULL_HASH_CACHE_MISS);
362 }
363 break;
364 }
365 }
366
367 if (!found_full_hash) {
368 // Case b.
369 if (cached_prefix_info.negative_ttl > now) {
370 // Case i.
371 RecordV4FullHashCacheResult(FULL_HASH_NEGATIVE_CACHE_HIT);
372 } else {
373 // Case ii.
374 unique_prefixes_to_request.insert(prefix);
375 RecordV4FullHashCacheResult(FULL_HASH_CACHE_MISS);
376 }
377 }
378 } else {
379 // Case 2.
380 unique_prefixes_to_request.insert(prefix);
381 RecordV4FullHashCacheResult(FULL_HASH_CACHE_MISS);
382 }
383 }
384 }
385
386 prefixes_to_request->insert(prefixes_to_request->begin(),
387 unique_prefixes_to_request.begin(),
388 unique_prefixes_to_request.end());
131 } 389 }
132 390
133 std::string V4GetHashProtocolManager::GetHashRequest( 391 std::string V4GetHashProtocolManager::GetHashRequest(
134 const std::vector<SBPrefix>& prefixes, 392 const std::vector<HashPrefix>& prefixes_to_request) {
135 const std::vector<PlatformType>& platforms, 393 DCHECK(!prefixes_to_request.empty());
136 ThreatType threat_type) { 394
137 // Build the request. Client info and client states are not added to the
138 // request protocol buffer. Client info is passed as params in the url.
139 FindFullHashesRequest req; 395 FindFullHashesRequest req;
140 ThreatInfo* info = req.mutable_threat_info(); 396 ThreatInfo* info = req.mutable_threat_info();
141 info->add_threat_types(threat_type); 397 for (const PlatformType p : platform_types_) {
142 info->add_threat_entry_types(URL);
143 for (const PlatformType p : platforms) {
144 info->add_platform_types(p); 398 info->add_platform_types(p);
145 } 399 }
146 for (const SBPrefix& prefix : prefixes) { 400 for (const ThreatEntryType tet : threat_entry_types_) {
147 std::string hash(reinterpret_cast<const char*>(&prefix), sizeof(SBPrefix)); 401 info->add_threat_entry_types(tet);
148 info->add_threat_entries()->set_hash(hash); 402 }
403 for (const ThreatType tt : threat_types_) {
404 info->add_threat_types(tt);
405 }
406 for (const HashPrefix& prefix : prefixes_to_request) {
407 info->add_threat_entries()->set_hash(prefix);
149 } 408 }
150 409
151 // Serialize and Base64 encode. 410 // Serialize and Base64 encode.
152 std::string req_data, req_base64; 411 std::string req_data, req_base64;
153 req.SerializeToString(&req_data); 412 req.SerializeToString(&req_data);
154 base::Base64UrlEncode(req_data, base::Base64UrlEncodePolicy::INCLUDE_PADDING, 413 base::Base64UrlEncode(req_data, base::Base64UrlEncodePolicy::INCLUDE_PADDING,
155 &req_base64); 414 &req_base64);
156 return req_base64; 415 return req_base64;
157 } 416 }
158 417
418 void V4GetHashProtocolManager::GetHashUrlAndHeaders(
419 const std::string& req_base64,
420 GURL* gurl,
421 net::HttpRequestHeaders* headers) const {
422 V4ProtocolManagerUtil::GetRequestUrlAndHeaders(req_base64, "fullHashes:find",
423 config_, gurl, headers);
424 }
425
426 void V4GetHashProtocolManager::HandleGetHashError(const Time& now) {
427 DCHECK(CalledOnValidThread());
428 TimeDelta next = V4ProtocolManagerUtil::GetNextBackOffInterval(
429 &gethash_error_count_, &gethash_back_off_mult_);
430 next_gethash_time_ = now + next;
431 }
432
433 void V4GetHashProtocolManager::OnFullHashForApi(
434 ThreatMetadataForApiCallback api_callback,
435 const base::hash_set<FullHash>& full_hashes,
436 const std::vector<FullHashInfo>& full_hash_infos) {
437 ThreatMetadata md;
438 for (const FullHashInfo& full_hash_info : full_hash_infos) {
439 DCHECK_EQ(GetChromeUrlApiId(), full_hash_info.list_id);
440 DCHECK(full_hashes.find(full_hash_info.full_hash) != full_hashes.end());
441 md.api_permissions.insert(full_hash_info.metadata.api_permissions.begin(),
442 full_hash_info.metadata.api_permissions.end());
443 }
444
445 // TODO(vakh): Figure out what UMA metrics to report. This code was previously
446 // calling RecordV4GetHashCheckResult with appropriate values but that's not
447 // applicable anymore.
448 api_callback.Run(md);
449 }
450
159 bool V4GetHashProtocolManager::ParseHashResponse( 451 bool V4GetHashProtocolManager::ParseHashResponse(
160 const std::string& data, 452 const std::string& data,
161 std::vector<SBFullHashResult>* full_hashes, 453 std::vector<FullHashInfo>* full_hash_infos,
162 base::Time* negative_cache_expire) { 454 Time* negative_cache_expire) {
163 FindFullHashesResponse response; 455 FindFullHashesResponse response;
164 456
165 if (!response.ParseFromString(data)) { 457 if (!response.ParseFromString(data)) {
166 RecordParseGetHashResult(PARSE_FROM_STRING_ERROR); 458 RecordParseGetHashResult(PARSE_FROM_STRING_ERROR);
167 return false; 459 return false;
168 } 460 }
169 461
170 // negative_cache_duration should always be set. 462 // negative_cache_duration should always be set.
171 DCHECK(response.has_negative_cache_duration()); 463 DCHECK(response.has_negative_cache_duration());
464
172 // Seconds resolution is good enough so we ignore the nanos field. 465 // Seconds resolution is good enough so we ignore the nanos field.
173 *negative_cache_expire = 466 *negative_cache_expire =
174 clock_->Now() + base::TimeDelta::FromSeconds( 467 clock_->Now() +
175 response.negative_cache_duration().seconds()); 468 TimeDelta::FromSeconds(response.negative_cache_duration().seconds());
176 469
177 if (response.has_minimum_wait_duration()) { 470 if (response.has_minimum_wait_duration()) {
178 // Seconds resolution is good enough so we ignore the nanos field. 471 // Seconds resolution is good enough so we ignore the nanos field.
179 next_gethash_time_ = 472 next_gethash_time_ =
180 clock_->Now() + base::TimeDelta::FromSeconds( 473 clock_->Now() +
181 response.minimum_wait_duration().seconds()); 474 TimeDelta::FromSeconds(response.minimum_wait_duration().seconds());
182 } 475 }
183 476
184 // We only expect one threat type per request, so we make sure
185 // the threat types are consistent between matches.
186 ThreatType expected_threat_type = THREAT_TYPE_UNSPECIFIED;
187
188 // Loop over the threat matches and fill in full_hashes.
189 for (const ThreatMatch& match : response.matches()) { 477 for (const ThreatMatch& match : response.matches()) {
190 // Make sure the platform and threat entry type match. 478 if (!match.has_platform_type()) {
191 if (!(match.has_threat_entry_type() && match.threat_entry_type() == URL && 479 RecordParseGetHashResult(UNEXPECTED_PLATFORM_TYPE_ERROR);
192 match.has_threat())) { 480 return false;
481 }
482 if (!match.has_threat_entry_type()) {
193 RecordParseGetHashResult(UNEXPECTED_THREAT_ENTRY_TYPE_ERROR); 483 RecordParseGetHashResult(UNEXPECTED_THREAT_ENTRY_TYPE_ERROR);
194 return false; 484 return false;
195 } 485 }
196
197 if (!match.has_threat_type()) { 486 if (!match.has_threat_type()) {
198 RecordParseGetHashResult(UNEXPECTED_THREAT_TYPE_ERROR); 487 RecordParseGetHashResult(UNEXPECTED_THREAT_TYPE_ERROR);
199 return false; 488 return false;
200 } 489 }
201 490 if (!match.has_threat()) {
202 if (expected_threat_type == THREAT_TYPE_UNSPECIFIED) { 491 RecordParseGetHashResult(NO_THREAT_ERROR);
203 expected_threat_type = match.threat_type();
204 } else if (match.threat_type() != expected_threat_type) {
205 RecordParseGetHashResult(INCONSISTENT_THREAT_TYPE_ERROR);
206 return false; 492 return false;
207 } 493 }
208 494
209 // Fill in the full hash. 495 UpdateListIdentifier list_id(
210 SBFullHashResult result; 496 match.platform_type(), match.threat_entry_type(), match.threat_type());
211 result.hash = StringToSBFullHash(match.threat().hash()); 497 base::Time positive_ttl;
212
213 if (match.has_cache_duration()) { 498 if (match.has_cache_duration()) {
214 // Seconds resolution is good enough so we ignore the nanos field. 499 // Seconds resolution is good enough so we ignore the nanos field.
215 result.cache_expire_after = 500 positive_ttl = clock_->Now() +
216 clock_->Now() + 501 TimeDelta::FromSeconds(match.cache_duration().seconds());
217 base::TimeDelta::FromSeconds(match.cache_duration().seconds());
218 } else { 502 } else {
219 result.cache_expire_after = clock_->Now(); 503 positive_ttl = clock_->Now();
504 }
505 FullHashInfo full_hash_info(match.threat().hash(), list_id, positive_ttl);
506 if (!ParseMetadata(match, &full_hash_info.metadata)) {
507 return false;
220 } 508 }
221 509
222 // Different threat types will handle the metadata differently. 510 full_hash_infos->push_back(full_hash_info);
223 if (match.threat_type() == API_ABUSE) { 511 }
224 if (match.has_platform_type() && 512 return true;
225 match.platform_type() == CHROME_PLATFORM) { 513 }
226 if (match.has_threat_entry_metadata()) { 514
227 // For API Abuse, store a list of the returned permissions. 515 bool V4GetHashProtocolManager::ParseMetadata(const ThreatMatch& match,
228 for (const ThreatEntryMetadata::MetadataEntry& m : 516 ThreatMetadata* metadata) {
229 match.threat_entry_metadata().entries()) { 517 DCHECK(metadata);
230 if (m.key() == "permission") { 518 // Different threat types will handle the metadata differently.
231 result.metadata.api_permissions.insert(m.value()); 519 if (match.threat_type() == API_ABUSE) {
232 } else { 520 if (match.has_platform_type() && match.platform_type() == CHROME_PLATFORM) {
233 RecordParseGetHashResult(UNEXPECTED_METADATA_VALUE_ERROR); 521 if (match.has_threat_entry_metadata()) {
234 return false; 522 // For API Abuse, store a list of the returned permissions.
235 } 523 for (const ThreatEntryMetadata::MetadataEntry& m :
236 } 524 match.threat_entry_metadata().entries()) {
237 } else { 525 if (m.key() == "permission") {
238 RecordParseGetHashResult(NO_METADATA_ERROR); 526 metadata->api_permissions.insert(m.value());
239 return false;
240 }
241 } else {
242 RecordParseGetHashResult(UNEXPECTED_PLATFORM_TYPE_ERROR);
243 return false;
244 }
245 } else if (match.threat_type() == MALWARE_THREAT ||
246 match.threat_type() == POTENTIALLY_HARMFUL_APPLICATION) {
247 for (const ThreatEntryMetadata::MetadataEntry& m :
248 match.threat_entry_metadata().entries()) {
249 // TODO: Need to confirm the below key/value pairs with CSD backend.
250 if (m.key() == "pha_pattern_type" ||
251 m.key() == "malware_pattern_type") {
252 if (m.value() == "LANDING") {
253 result.metadata.threat_pattern_type =
254 ThreatPatternType::MALWARE_LANDING;
255 break;
256 } else if (m.value() == "DISTRIBUTION") {
257 result.metadata.threat_pattern_type =
258 ThreatPatternType::MALWARE_DISTRIBUTION;
259 break;
260 } else { 527 } else {
261 RecordParseGetHashResult(UNEXPECTED_METADATA_VALUE_ERROR); 528 RecordParseGetHashResult(UNEXPECTED_METADATA_VALUE_ERROR);
262 return false; 529 return false;
263 } 530 }
264 } 531 }
532 } else {
533 RecordParseGetHashResult(NO_METADATA_ERROR);
534 return false;
265 } 535 }
266 } else if (match.threat_type() == SOCIAL_ENGINEERING_PUBLIC) { 536 } else {
267 for (const ThreatEntryMetadata::MetadataEntry& m : 537 RecordParseGetHashResult(UNEXPECTED_PLATFORM_TYPE_ERROR);
268 match.threat_entry_metadata().entries()) { 538 return false;
269 if (m.key() == "se_pattern_type") { 539 }
270 if (m.value() == "SOCIAL_ENGINEERING_ADS") { 540 } else if (match.threat_type() == MALWARE_THREAT ||
271 result.metadata.threat_pattern_type = 541 match.threat_type() == POTENTIALLY_HARMFUL_APPLICATION) {
272 ThreatPatternType::SOCIAL_ENGINEERING_ADS; 542 for (const ThreatEntryMetadata::MetadataEntry& m :
273 break; 543 match.threat_entry_metadata().entries()) {
274 } else if (m.value() == "SOCIAL_ENGINEERING_LANDING") { 544 // TODO: Need to confirm the below key/value pairs with CSD backend.
275 result.metadata.threat_pattern_type = 545 if (m.key() == "pha_pattern_type" || m.key() == "malware_pattern_type") {
276 ThreatPatternType::SOCIAL_ENGINEERING_LANDING; 546 if (m.value() == "LANDING") {
277 break; 547 metadata->threat_pattern_type = ThreatPatternType::MALWARE_LANDING;
278 } else if (m.value() == "PHISHING") { 548 break;
279 result.metadata.threat_pattern_type = ThreatPatternType::PHISHING; 549 } else if (m.value() == "DISTRIBUTION") {
280 break; 550 metadata->threat_pattern_type =
281 } else { 551 ThreatPatternType::MALWARE_DISTRIBUTION;
282 RecordParseGetHashResult(UNEXPECTED_METADATA_VALUE_ERROR); 552 break;
283 return false; 553 } else {
284 } 554 RecordParseGetHashResult(UNEXPECTED_METADATA_VALUE_ERROR);
555 return false;
285 } 556 }
286 } 557 }
287 } else {
288 RecordParseGetHashResult(UNEXPECTED_THREAT_TYPE_ERROR);
289 return false;
290 } 558 }
559 } else if (match.threat_type() == SOCIAL_ENGINEERING_PUBLIC) {
560 for (const ThreatEntryMetadata::MetadataEntry& m :
561 match.threat_entry_metadata().entries()) {
562 if (m.key() == "se_pattern_type") {
563 if (m.value() == "SOCIAL_ENGINEERING_ADS") {
564 metadata->threat_pattern_type =
565 ThreatPatternType::SOCIAL_ENGINEERING_ADS;
566 break;
567 } else if (m.value() == "SOCIAL_ENGINEERING_LANDING") {
568 metadata->threat_pattern_type =
569 ThreatPatternType::SOCIAL_ENGINEERING_LANDING;
570 break;
571 } else if (m.value() == "PHISHING") {
572 metadata->threat_pattern_type = ThreatPatternType::PHISHING;
573 break;
574 } else {
575 RecordParseGetHashResult(UNEXPECTED_METADATA_VALUE_ERROR);
576 return false;
577 }
578 }
579 }
580 } else {
581 RecordParseGetHashResult(UNEXPECTED_THREAT_TYPE_ERROR);
582 return false;
583 }
291 584
292 full_hashes->push_back(result);
293 }
294 return true; 585 return true;
295 } 586 }
296 587
297 void V4GetHashProtocolManager::GetFullHashes( 588 void V4GetHashProtocolManager::ResetGetHashErrors() {
298 const std::vector<SBPrefix>& prefixes, 589 gethash_error_count_ = 0;
299 const std::vector<PlatformType>& platforms, 590 gethash_back_off_mult_ = 1;
300 ThreatType threat_type,
301 FullHashCallback callback) {
302 DCHECK(CalledOnValidThread());
303 // We need to wait the minimum waiting duration, and if we are in backoff,
304 // we need to check if we're past the next allowed time. If we are, we can
305 // proceed with the request. If not, we are required to return empty results
306 // (i.e. treat the page as safe).
307 if (clock_->Now() <= next_gethash_time_) {
308 if (gethash_error_count_) {
309 RecordGetHashResult(V4OperationResult::BACKOFF_ERROR);
310 } else {
311 RecordGetHashResult(V4OperationResult::MIN_WAIT_DURATION_ERROR);
312 }
313 std::vector<SBFullHashResult> full_hashes;
314 callback.Run(full_hashes, base::Time());
315 return;
316 }
317
318 std::string req_base64 = GetHashRequest(prefixes, platforms, threat_type);
319 GURL gethash_url;
320 net::HttpRequestHeaders headers;
321 GetHashUrlAndHeaders(req_base64, &gethash_url, &headers);
322
323 std::unique_ptr<net::URLFetcher> owned_fetcher = net::URLFetcher::Create(
324 url_fetcher_id_++, gethash_url, net::URLFetcher::GET, this);
325 net::URLFetcher* fetcher = owned_fetcher.get();
326 fetcher->SetExtraRequestHeaders(headers.ToString());
327 hash_requests_[fetcher] = std::make_pair(std::move(owned_fetcher), callback);
328
329 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE);
330 fetcher->SetRequestContext(request_context_getter_.get());
331 fetcher->Start();
332 }
333
334 void V4GetHashProtocolManager::GetFullHashesWithApis(
335 const std::vector<SBPrefix>& prefixes,
336 FullHashCallback callback) {
337 std::vector<PlatformType> platform = {CHROME_PLATFORM};
338 GetFullHashes(prefixes, platform, API_ABUSE, callback);
339 } 591 }
340 592
341 void V4GetHashProtocolManager::SetClockForTests( 593 void V4GetHashProtocolManager::SetClockForTests(
342 std::unique_ptr<base::Clock> clock) { 594 std::unique_ptr<base::Clock> clock) {
343 clock_ = std::move(clock); 595 clock_ = std::move(clock);
344 } 596 }
345 597
598 void V4GetHashProtocolManager::UpdateCache(
599 const std::vector<HashPrefix>& prefixes_requested,
600 const std::vector<FullHashInfo>& full_hash_infos,
601 const Time& negative_cache_expire) {
602 // If negative_cache_expire is null, don't cache the results it's not clear
603 // till what time they should be considered valid.
604 if (negative_cache_expire.is_null()) {
605 return;
606 }
607
608 for (const HashPrefix& prefix : prefixes_requested) {
609 // Create or reset the cached result for this prefix.
610 full_hash_cache_[prefix].full_hash_infos.clear();
611 full_hash_cache_[prefix].negative_ttl = negative_cache_expire;
612
613 for (const FullHashInfo& full_hash_info : full_hash_infos) {
614 if (V4ProtocolManagerUtil::FullHashMatchesHashPrefix(
615 full_hash_info.full_hash, prefix)) {
616 full_hash_cache_[prefix].full_hash_infos.push_back(full_hash_info);
617 }
618 }
619 }
620 }
621
622 void V4GetHashProtocolManager::MergeResults(
623 const FullHashToStoreAndHashPrefixesMap&
624 full_hash_to_store_and_hash_prefixes,
625 const std::vector<FullHashInfo>& full_hash_infos,
626 std::vector<FullHashInfo>* merged_full_hash_infos) {
627 for (const FullHashInfo& fhi : full_hash_infos) {
628 bool matched_full_hash =
629 full_hash_to_store_and_hash_prefixes.end() !=
630 full_hash_to_store_and_hash_prefixes.find(fhi.full_hash);
631 if (matched_full_hash) {
632 for (const StoreAndHashPrefix& sahp :
633 full_hash_to_store_and_hash_prefixes.at(fhi.full_hash)) {
634 if (fhi.list_id == sahp.list_id) {
635 merged_full_hash_infos->push_back(fhi);
636 break;
637 }
638 }
639 }
640 }
641 }
642
346 // net::URLFetcherDelegate implementation ---------------------------------- 643 // net::URLFetcherDelegate implementation ----------------------------------
347 644
348 // SafeBrowsing request responses are handled here. 645 // SafeBrowsing request responses are handled here.
349 void V4GetHashProtocolManager::OnURLFetchComplete( 646 void V4GetHashProtocolManager::OnURLFetchComplete(
350 const net::URLFetcher* source) { 647 const net::URLFetcher* source) {
351 DCHECK(CalledOnValidThread()); 648 DCHECK(CalledOnValidThread());
352 649
353 HashRequests::iterator it = hash_requests_.find(source); 650 PendingHashRequests::iterator it = pending_hash_requests_.find(source);
354 DCHECK(it != hash_requests_.end()) << "Request not found"; 651 DCHECK(it != pending_hash_requests_.end()) << "Request not found";
355 652
356 int response_code = source->GetResponseCode(); 653 int response_code = source->GetResponseCode();
357 net::URLRequestStatus status = source->GetStatus(); 654 net::URLRequestStatus status = source->GetStatus();
358 V4ProtocolManagerUtil::RecordHttpResponseOrErrorCode( 655 V4ProtocolManagerUtil::RecordHttpResponseOrErrorCode(
359 kUmaV4HashResponseMetricName, status, response_code); 656 kUmaV4HashResponseMetricName, status, response_code);
360 657
361 const FullHashCallback& callback = it->second.second; 658 std::vector<FullHashInfo> full_hash_infos;
362 std::vector<SBFullHashResult> full_hashes; 659 Time negative_cache_expire;
363 base::Time negative_cache_expire;
364 if (status.is_success() && response_code == net::HTTP_OK) { 660 if (status.is_success() && response_code == net::HTTP_OK) {
365 RecordGetHashResult(V4OperationResult::STATUS_200); 661 RecordGetHashResult(V4OperationResult::STATUS_200);
366 ResetGetHashErrors(); 662 ResetGetHashErrors();
367 std::string data; 663 std::string data;
368 source->GetResponseAsString(&data); 664 source->GetResponseAsString(&data);
369 if (!ParseHashResponse(data, &full_hashes, &negative_cache_expire)) { 665 if (!ParseHashResponse(data, &full_hash_infos, &negative_cache_expire)) {
370 full_hashes.clear(); 666 full_hash_infos.clear();
371 RecordGetHashResult(V4OperationResult::PARSE_ERROR); 667 RecordGetHashResult(V4OperationResult::PARSE_ERROR);
372 } 668 }
373 } else { 669 } else {
374 HandleGetHashError(clock_->Now()); 670 HandleGetHashError(clock_->Now());
375 671
376 DVLOG(1) << "SafeBrowsing GetEncodedFullHashes request for: " 672 DVLOG(1) << "SafeBrowsing GetEncodedFullHashes request for: "
377 << source->GetURL() << " failed with error: " << status.error() 673 << source->GetURL() << " failed with error: " << status.error()
378 << " and response code: " << response_code; 674 << " and response code: " << response_code;
379 675
380 if (status.status() == net::URLRequestStatus::FAILED) { 676 if (status.status() == net::URLRequestStatus::FAILED) {
381 RecordGetHashResult(V4OperationResult::NETWORK_ERROR); 677 RecordGetHashResult(V4OperationResult::NETWORK_ERROR);
382 } else { 678 } else {
383 RecordGetHashResult(V4OperationResult::HTTP_ERROR); 679 RecordGetHashResult(V4OperationResult::HTTP_ERROR);
384 } 680 }
385 } 681 }
386 682
387 // Invoke the callback with full_hashes, even if there was a parse error or 683 const std::unique_ptr<FullHashCallbackInfo>& fhci = it->second;
388 // an error response code (in which case full_hashes will be empty). The 684 UpdateCache(fhci->prefixes_requested, full_hash_infos, negative_cache_expire);
389 // caller can't be blocked indefinitely. 685 MergeResults(fhci->full_hash_to_store_and_hash_prefixes, full_hash_infos,
390 callback.Run(full_hashes, negative_cache_expire); 686 &fhci->cached_full_hash_infos);
391 687
392 hash_requests_.erase(it); 688 const FullHashCallback& callback = fhci->callback;
689 callback.Run(fhci->cached_full_hash_infos);
690
691 pending_hash_requests_.erase(it);
393 } 692 }
394 693
395 void V4GetHashProtocolManager::HandleGetHashError(const Time& now) { 694 #ifndef DEBUG
396 DCHECK(CalledOnValidThread()); 695 std::ostream& operator<<(std::ostream& os, const FullHashInfo& fhi) {
397 base::TimeDelta next = V4ProtocolManagerUtil::GetNextBackOffInterval( 696 os << "{full_hash: " << fhi.full_hash << "; list_id: " << fhi.list_id
398 &gethash_error_count_, &gethash_back_off_mult_); 697 << "; positive_ttl: " << fhi.positive_ttl
399 next_gethash_time_ = now + next; 698 << "; metadata.api_permissions.size(): "
699 << fhi.metadata.api_permissions.size() << "}";
700 return os;
400 } 701 }
401 702 #endif
402 void V4GetHashProtocolManager::GetHashUrlAndHeaders(
403 const std::string& req_base64,
404 GURL* gurl,
405 net::HttpRequestHeaders* headers) const {
406 V4ProtocolManagerUtil::GetRequestUrlAndHeaders(req_base64, "fullHashes:find",
407 config_, gurl, headers);
408 }
409 703
410 } // namespace safe_browsing 704 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698