OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/safe_browsing/protocol_manager.h" | 5 #include "chrome/browser/safe_browsing/protocol_manager.h" |
6 | 6 |
7 #include "base/base64.h" | |
7 #include "base/environment.h" | 8 #include "base/environment.h" |
8 #include "base/logging.h" | 9 #include "base/logging.h" |
9 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
10 #include "base/metrics/sparse_histogram.h" | 11 #include "base/metrics/sparse_histogram.h" |
11 #include "base/profiler/scoped_tracker.h" | 12 #include "base/profiler/scoped_tracker.h" |
12 #include "base/rand_util.h" | 13 #include "base/rand_util.h" |
13 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
14 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
15 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
16 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
82 static const int kSbTimerStartIntervalSecMax = 300; | 83 static const int kSbTimerStartIntervalSecMax = 300; |
83 | 84 |
84 // The maximum time, in seconds, to wait for a response to an update request. | 85 // The maximum time, in seconds, to wait for a response to an update request. |
85 static const int kSbMaxUpdateWaitSec = 30; | 86 static const int kSbMaxUpdateWaitSec = 30; |
86 | 87 |
87 // Maximum back off multiplier. | 88 // Maximum back off multiplier. |
88 static const size_t kSbMaxBackOff = 8; | 89 static const size_t kSbMaxBackOff = 8; |
89 | 90 |
90 const char kUmaHashResponseMetricName[] = "SB2.GetHashResponseOrErrorCode"; | 91 const char kUmaHashResponseMetricName[] = "SB2.GetHashResponseOrErrorCode"; |
91 | 92 |
93 // The V4 URL prefix where browser fetches hashes from the V4 server. | |
94 const char kSbV4URLPrefix[] = "https://safebrowsing.googleapis.com/v4"; | |
Nathan Parker
2016/01/05 22:44:00
kSbV4UrlPrefix
kcarattini
2016/01/05 23:30:57
Done.
| |
95 | |
92 // The default SBProtocolManagerFactory. | 96 // The default SBProtocolManagerFactory. |
93 class SBProtocolManagerFactoryImpl : public SBProtocolManagerFactory { | 97 class SBProtocolManagerFactoryImpl : public SBProtocolManagerFactory { |
94 public: | 98 public: |
95 SBProtocolManagerFactoryImpl() {} | 99 SBProtocolManagerFactoryImpl() {} |
96 ~SBProtocolManagerFactoryImpl() override {} | 100 ~SBProtocolManagerFactoryImpl() override {} |
97 SafeBrowsingProtocolManager* CreateProtocolManager( | 101 SafeBrowsingProtocolManager* CreateProtocolManager( |
98 SafeBrowsingProtocolManagerDelegate* delegate, | 102 SafeBrowsingProtocolManagerDelegate* delegate, |
99 net::URLRequestContextGetter* request_context_getter, | 103 net::URLRequestContextGetter* request_context_getter, |
100 const SafeBrowsingProtocolConfig& config) override { | 104 const SafeBrowsingProtocolConfig& config) override { |
101 return new SafeBrowsingProtocolManager(delegate, request_context_getter, | 105 return new SafeBrowsingProtocolManager(delegate, request_context_getter, |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
186 | 190 |
187 bool SafeBrowsingProtocolManager::IsUpdateScheduled() const { | 191 bool SafeBrowsingProtocolManager::IsUpdateScheduled() const { |
188 return update_timer_.IsRunning(); | 192 return update_timer_.IsRunning(); |
189 } | 193 } |
190 | 194 |
191 SafeBrowsingProtocolManager::~SafeBrowsingProtocolManager() { | 195 SafeBrowsingProtocolManager::~SafeBrowsingProtocolManager() { |
192 // Delete in-progress SafeBrowsing requests. | 196 // Delete in-progress SafeBrowsing requests. |
193 STLDeleteContainerPairFirstPointers(hash_requests_.begin(), | 197 STLDeleteContainerPairFirstPointers(hash_requests_.begin(), |
194 hash_requests_.end()); | 198 hash_requests_.end()); |
195 hash_requests_.clear(); | 199 hash_requests_.clear(); |
200 | |
201 STLDeleteContainerPairFirstPointers(v4_hash_requests_.begin(), | |
202 v4_hash_requests_.end()); | |
203 v4_hash_requests_.clear(); | |
196 } | 204 } |
197 | 205 |
198 // We can only have one update or chunk request outstanding, but there may be | 206 // We can only have one update or chunk request outstanding, but there may be |
199 // multiple GetHash requests pending since we don't want to serialize them and | 207 // multiple GetHash requests pending since we don't want to serialize them and |
200 // slow down the user. | 208 // slow down the user. |
201 void SafeBrowsingProtocolManager::GetFullHash( | 209 void SafeBrowsingProtocolManager::GetFullHash( |
202 const std::vector<SBPrefix>& prefixes, | 210 const std::vector<SBPrefix>& prefixes, |
203 FullHashCallback callback, | 211 FullHashCallback callback, |
204 bool is_download, | 212 bool is_download, |
205 bool is_extended_reporting) { | 213 bool is_extended_reporting) { |
(...skipping 15 matching lines...) Expand all Loading... | |
221 hash_requests_[fetcher] = FullHashDetails(callback, is_download); | 229 hash_requests_[fetcher] = FullHashDetails(callback, is_download); |
222 | 230 |
223 const std::string get_hash = FormatGetHash(prefixes); | 231 const std::string get_hash = FormatGetHash(prefixes); |
224 | 232 |
225 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE); | 233 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE); |
226 fetcher->SetRequestContext(request_context_getter_.get()); | 234 fetcher->SetRequestContext(request_context_getter_.get()); |
227 fetcher->SetUploadData("text/plain", get_hash); | 235 fetcher->SetUploadData("text/plain", get_hash); |
228 fetcher->Start(); | 236 fetcher->Start(); |
229 } | 237 } |
230 | 238 |
239 std::string SafeBrowsingProtocolManager::GetV4HashRequest( | |
240 const std::vector<SBPrefix>& prefixes, | |
241 ThreatType threat_type) { | |
242 // Build the request. Client info and client states are not added to the | |
243 // request protocol buffer. Client info is passed as params in the url. | |
244 FindFullHashesRequest req; | |
245 ThreatInfo* info = req.mutable_threat_info(); | |
246 info->add_threat_types(threat_type); | |
247 info->add_platform_types(CHROME_PLATFORM); | |
248 info->add_threat_entry_types(URL_EXPRESSION); | |
249 for (const SBPrefix& prefix : prefixes) { | |
250 std::string hash; | |
Nathan Parker
2016/01/05 22:44:00
nit: I think you can do
std::string hash(ptr, size
kcarattini
2016/01/05 23:30:57
Done.
| |
251 hash.append(reinterpret_cast<const char*>(&prefix), sizeof(SBPrefix)); | |
252 info->add_threat_entries()->set_hash(hash); | |
253 } | |
254 | |
255 // Serialize and Base64 encode. | |
256 std::string req_data, req_base64; | |
257 req.SerializeToString(&req_data); | |
258 base::Base64Encode(req_data, &req_base64); | |
259 | |
260 return req_base64; | |
261 } | |
262 | |
263 void SafeBrowsingProtocolManager::GetV4FullHashes( | |
264 const std::vector<SBPrefix>& prefixes, | |
265 ThreatType threat_type, | |
266 FullHashCallback callback) { | |
267 DCHECK(CalledOnValidThread()); | |
268 // TODO(kcarattini): Implement backoff behavior. | |
269 | |
270 std::string req_base64 = GetV4HashRequest(prefixes, threat_type); | |
271 GURL gethash_url = GetV4HashUrl(req_base64); | |
272 | |
273 net::URLFetcher* fetcher = | |
274 net::URLFetcher::Create(url_fetcher_id_++, gethash_url, | |
275 net::URLFetcher::GET, this) | |
kcarattini
2015/12/29 22:33:41
Changed to GET as per offline discussion with awoz
| |
276 .release(); | |
277 // TODO(kcarattini): Implement a new response processor. | |
278 v4_hash_requests_[fetcher] = FullHashDetails(callback, | |
279 false /* is_download */); | |
280 | |
281 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE); | |
282 fetcher->SetRequestContext(request_context_getter_.get()); | |
283 fetcher->Start(); | |
284 } | |
285 | |
286 void SafeBrowsingProtocolManager::GetFullHashesWithApis( | |
287 const std::vector<SBPrefix>& prefixes, | |
288 FullHashCallback callback) { | |
289 GetV4FullHashes(prefixes, API_ABUSE, callback); | |
290 } | |
291 | |
231 void SafeBrowsingProtocolManager::GetNextUpdate() { | 292 void SafeBrowsingProtocolManager::GetNextUpdate() { |
232 DCHECK(CalledOnValidThread()); | 293 DCHECK(CalledOnValidThread()); |
233 if (request_.get() || request_type_ != NO_REQUEST) | 294 if (request_.get() || request_type_ != NO_REQUEST) |
234 return; | 295 return; |
235 | 296 |
236 IssueUpdateRequest(); | 297 IssueUpdateRequest(); |
237 } | 298 } |
238 | 299 |
239 // net::URLFetcherDelegate implementation ---------------------------------- | 300 // net::URLFetcherDelegate implementation ---------------------------------- |
240 | 301 |
(...skipping 510 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
751 return GURL(url); | 812 return GURL(url); |
752 } | 813 } |
753 | 814 |
754 GURL SafeBrowsingProtocolManager::GetHashUrl(bool is_extended_reporting) const { | 815 GURL SafeBrowsingProtocolManager::GetHashUrl(bool is_extended_reporting) const { |
755 std::string url = SafeBrowsingProtocolManagerHelper::ComposeUrl( | 816 std::string url = SafeBrowsingProtocolManagerHelper::ComposeUrl( |
756 url_prefix_, "gethash", client_name_, version_, additional_query_, | 817 url_prefix_, "gethash", client_name_, version_, additional_query_, |
757 is_extended_reporting); | 818 is_extended_reporting); |
758 return GURL(url); | 819 return GURL(url); |
759 } | 820 } |
760 | 821 |
822 // The API hash call uses the pver4 Safe Browsing server. | |
823 GURL SafeBrowsingProtocolManager::GetV4HashUrl( | |
824 const std::string& request_base64) const { | |
825 std::string url = SafeBrowsingProtocolManagerHelper::ComposePver4Url( | |
826 kSbV4URLPrefix, "encodedFullHashes", | |
827 request_base64, client_name_, version_); | |
828 return GURL(url); | |
829 } | |
830 | |
761 GURL SafeBrowsingProtocolManager::NextChunkUrl(const std::string& url) const { | 831 GURL SafeBrowsingProtocolManager::NextChunkUrl(const std::string& url) const { |
762 DCHECK(CalledOnValidThread()); | 832 DCHECK(CalledOnValidThread()); |
763 std::string next_url; | 833 std::string next_url; |
764 if (!base::StartsWith(url, "http://", base::CompareCase::INSENSITIVE_ASCII) && | 834 if (!base::StartsWith(url, "http://", base::CompareCase::INSENSITIVE_ASCII) && |
765 !base::StartsWith(url, "https://", | 835 !base::StartsWith(url, "https://", |
766 base::CompareCase::INSENSITIVE_ASCII)) { | 836 base::CompareCase::INSENSITIVE_ASCII)) { |
767 // Use https if we updated via https, otherwise http (useful for testing). | 837 // Use https if we updated via https, otherwise http (useful for testing). |
768 if (base::StartsWith(url_prefix_, "https://", | 838 if (base::StartsWith(url_prefix_, "https://", |
769 base::CompareCase::INSENSITIVE_ASCII)) | 839 base::CompareCase::INSENSITIVE_ASCII)) |
770 next_url.append("https://"); | 840 next_url.append("https://"); |
(...skipping 20 matching lines...) Expand all Loading... | |
791 SafeBrowsingProtocolManager::FullHashDetails::FullHashDetails( | 861 SafeBrowsingProtocolManager::FullHashDetails::FullHashDetails( |
792 FullHashCallback callback, | 862 FullHashCallback callback, |
793 bool is_download) | 863 bool is_download) |
794 : callback(callback), is_download(is_download) {} | 864 : callback(callback), is_download(is_download) {} |
795 | 865 |
796 SafeBrowsingProtocolManager::FullHashDetails::~FullHashDetails() {} | 866 SafeBrowsingProtocolManager::FullHashDetails::~FullHashDetails() {} |
797 | 867 |
798 SafeBrowsingProtocolManagerDelegate::~SafeBrowsingProtocolManagerDelegate() {} | 868 SafeBrowsingProtocolManagerDelegate::~SafeBrowsingProtocolManagerDelegate() {} |
799 | 869 |
800 } // namespace safe_browsing | 870 } // namespace safe_browsing |
OLD | NEW |