license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
| 5 | #include "chrome/browser/safe_browsing/protocol_manager.h" |
| 6 | |
[email protected] | 978df34 | 2009-11-24 06:21:53 | [diff] [blame] | 7 | #include "base/base64.h" |
[email protected] | 484fce4 | 2008-10-01 00:37:18 | [diff] [blame] | 8 | #include "base/file_version_info.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 9 | #include "base/histogram.h" |
| 10 | #include "base/logging.h" |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 11 | #include "base/rand_util.h" |
[email protected] | 80720414 | 2009-05-05 03:31:44 | [diff] [blame] | 12 | #include "base/stl_util-inl.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 13 | #include "base/string_util.h" |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 14 | #include "base/sys_info.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 15 | #include "base/task.h" |
| 16 | #include "base/timer.h" |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 17 | #include "chrome/browser/chrome_thread.h" |
[email protected] | d11f566 | 2009-11-12 20:52:56 | [diff] [blame] | 18 | #include "chrome/browser/net/url_request_context_getter.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 19 | #include "chrome/browser/profile.h" |
| 20 | #include "chrome/browser/safe_browsing/protocol_parser.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 21 | #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 22 | #include "chrome/common/env_vars.h" |
[email protected] | dfdb0de7 | 2009-02-19 21:58:14 | [diff] [blame] | 23 | #include "net/base/escape.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 24 | #include "net/base/load_flags.h" |
[email protected] | 3c3f4ac5 | 2009-12-15 20:22:17 | [diff] [blame] | 25 | #include "net/url_request/url_request_status.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 26 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 27 | using base::Time; |
| 28 | using base::TimeDelta; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 29 | |
| 30 | // Maximum time, in seconds, from start up before we must issue an update query. |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 31 | static const int kSbTimerStartIntervalSec = 5 * 60; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 32 | |
[email protected] | a11c2c6 | 2009-08-07 22:47:56 | [diff] [blame] | 33 | // The maximum time, in seconds, to wait for a response to an update request. |
| 34 | static const int kSbMaxUpdateWaitSec = 10; |
| 35 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 36 | // Update URL for querying about the latest set of chunk updates. |
| 37 | static const char* const kSbUpdateUrl = |
[email protected] | d321644 | 2009-03-05 21:07:27 | [diff] [blame] | 38 | "http://safebrowsing.clients.google.com/safebrowsing/downloads?client=%s" |
| 39 | "&appver=%s&pver=2.2"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 40 | |
| 41 | // GetHash request URL for retrieving full hashes. |
| 42 | static const char* const kSbGetHashUrl = |
[email protected] | d321644 | 2009-03-05 21:07:27 | [diff] [blame] | 43 | "http://safebrowsing.clients.google.com/safebrowsing/gethash?client=%s" |
| 44 | "&appver=%s&pver=2.2"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 45 | |
| 46 | // New MAC client key requests URL. |
| 47 | static const char* const kSbNewKeyUrl = |
[email protected] | d321644 | 2009-03-05 21:07:27 | [diff] [blame] | 48 | "https://sb-ssl.google.com/safebrowsing/newkey?client=%s&appver=%s" |
| 49 | "&pver=2.2"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 50 | |
[email protected] | dfdb0de7 | 2009-02-19 21:58:14 | [diff] [blame] | 51 | // URL for reporting malware pages. |
| 52 | static const char* const kSbMalwareReportUrl = |
[email protected] | d321644 | 2009-03-05 21:07:27 | [diff] [blame] | 53 | "http://safebrowsing.clients.google.com/safebrowsing/report?evts=malblhit" |
| 54 | "&evtd=%s&evtr=%s&evhr=%s&client=%s&appver=%s"; |
[email protected] | dfdb0de7 | 2009-02-19 21:58:14 | [diff] [blame] | 55 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 56 | // Maximum back off multiplier. |
| 57 | static const int kSbMaxBackOff = 8; |
| 58 | |
| 59 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 60 | // SafeBrowsingProtocolManager implementation ---------------------------------- |
| 61 | |
| 62 | SafeBrowsingProtocolManager::SafeBrowsingProtocolManager( |
| 63 | SafeBrowsingService* sb_service, |
[email protected] | 1a87151 | 2009-11-06 06:11:18 | [diff] [blame] | 64 | const std::string& client_name, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 65 | const std::string& client_key, |
[email protected] | d11f566 | 2009-11-12 20:52:56 | [diff] [blame] | 66 | const std::string& wrapped_key, |
| 67 | URLRequestContextGetter* request_context_getter) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 68 | : sb_service_(sb_service), |
| 69 | request_type_(NO_REQUEST), |
| 70 | update_error_count_(0), |
| 71 | gethash_error_count_(0), |
| 72 | update_back_off_mult_(1), |
| 73 | gethash_back_off_mult_(1), |
| 74 | next_update_sec_(-1), |
| 75 | update_state_(FIRST_REQUEST), |
| 76 | initial_request_(true), |
| 77 | chunk_pending_to_write_(false), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 78 | client_key_(client_key), |
[email protected] | 6e3b12ff | 2009-01-06 22:17:57 | [diff] [blame] | 79 | wrapped_key_(wrapped_key), |
[email protected] | 1a87151 | 2009-11-06 06:11:18 | [diff] [blame] | 80 | update_size_(0), |
[email protected] | d11f566 | 2009-11-12 20:52:56 | [diff] [blame] | 81 | client_name_(client_name), |
| 82 | request_context_getter_(request_context_getter) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 83 | // Set the backoff multiplier fuzz to a random value between 0 and 1. |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 84 | back_off_fuzz_ = static_cast<float>(base::RandDouble()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 85 | |
[email protected] | efbb6048 | 2009-11-12 21:38:55 | [diff] [blame] | 86 | // The first update must happen between 1-5 minutes of start up. |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 87 | next_update_sec_ = base::RandInt(60, kSbTimerStartIntervalSec); |
[email protected] | 484fce4 | 2008-10-01 00:37:18 | [diff] [blame] | 88 | |
| 89 | scoped_ptr<FileVersionInfo> version_info( |
| 90 | FileVersionInfo::CreateFileVersionInfoForCurrentModule()); |
| 91 | if (!version_info.get()) |
| 92 | version_ = "0.1"; |
| 93 | else |
| 94 | version_ = WideToASCII(version_info->product_version()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | SafeBrowsingProtocolManager::~SafeBrowsingProtocolManager() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 98 | // Delete in-progress SafeBrowsing requests. |
| 99 | STLDeleteContainerPairFirstPointers(hash_requests_.begin(), |
| 100 | hash_requests_.end()); |
| 101 | hash_requests_.clear(); |
[email protected] | dfdb0de7 | 2009-02-19 21:58:14 | [diff] [blame] | 102 | |
| 103 | // Delete in-progress malware reports. |
| 104 | STLDeleteContainerPointers(malware_reports_.begin(), malware_reports_.end()); |
| 105 | malware_reports_.clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | // Public API used by the SafeBrowsingService ---------------------------------- |
| 109 | |
| 110 | // We can only have one update or chunk request outstanding, but there may be |
| 111 | // multiple GetHash requests pending since we don't want to serialize them and |
| 112 | // slow down the user. |
| 113 | void SafeBrowsingProtocolManager::GetFullHash( |
| 114 | SafeBrowsingService::SafeBrowsingCheck* check, |
| 115 | const std::vector<SBPrefix>& prefixes) { |
| 116 | // If we are in GetHash backoff, we need to check if we're past the next |
| 117 | // allowed time. If we are, we can proceed with the request. If not, we are |
| 118 | // required to return empty results (i.e. treat the page as safe). |
| 119 | if (gethash_error_count_ && Time::Now() <= next_gethash_time_) { |
| 120 | std::vector<SBFullHashResult> full_hashes; |
[email protected] | 200abc3 | 2008-09-05 01:44:33 | [diff] [blame] | 121 | sb_service_->HandleGetHashResults(check, full_hashes, false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 122 | return; |
| 123 | } |
| 124 | |
| 125 | std::string url = StringPrintf(kSbGetHashUrl, |
[email protected] | 1a87151 | 2009-11-06 06:11:18 | [diff] [blame] | 126 | client_name_.c_str(), |
[email protected] | 484fce4 | 2008-10-01 00:37:18 | [diff] [blame] | 127 | version_.c_str()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 128 | if (!client_key_.empty()) { |
| 129 | url.append("&wrkey="); |
| 130 | url.append(wrapped_key_); |
| 131 | } |
| 132 | |
| 133 | GURL gethash_url(url); |
| 134 | URLFetcher* fetcher = new URLFetcher(gethash_url, URLFetcher::POST, this); |
| 135 | hash_requests_[fetcher] = check; |
| 136 | |
| 137 | std::string get_hash; |
| 138 | SafeBrowsingProtocolParser parser; |
| 139 | parser.FormatGetHash(prefixes, &get_hash); |
| 140 | |
| 141 | fetcher->set_load_flags(net::LOAD_DISABLE_CACHE); |
[email protected] | d11f566 | 2009-11-12 20:52:56 | [diff] [blame] | 142 | fetcher->set_request_context(request_context_getter_); |
[email protected] | d36e3c8e | 2008-08-29 23:42:20 | [diff] [blame] | 143 | fetcher->set_upload_data("text/plain", get_hash); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 144 | fetcher->Start(); |
| 145 | } |
| 146 | |
| 147 | void SafeBrowsingProtocolManager::GetNextUpdate() { |
| 148 | if (initial_request_) { |
| 149 | if (client_key_.empty() || wrapped_key_.empty()) { |
| 150 | IssueKeyRequest(); |
| 151 | return; |
| 152 | } else { |
| 153 | initial_request_ = false; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | if (!request_.get()) |
| 158 | IssueUpdateRequest(); |
| 159 | } |
| 160 | |
| 161 | // URLFetcher::Delegate implementation ----------------------------------------- |
| 162 | |
| 163 | // All SafeBrowsing request responses are handled here. |
| 164 | // TODO(paulg): Clarify with the SafeBrowsing team whether a failed parse of a |
| 165 | // chunk should retry the download and parse of that chunk (and |
| 166 | // what back off / how many times to try), and if that effects the |
| 167 | // update back off. For now, a failed parse of the chunk means we |
| 168 | // drop it. This isn't so bad because the next UPDATE_REQUEST we |
| 169 | // do will report all the chunks we have. If that chunk is still |
| 170 | // required, the SafeBrowsing servers will tell us to get it again. |
| 171 | void SafeBrowsingProtocolManager::OnURLFetchComplete( |
| 172 | const URLFetcher* source, |
| 173 | const GURL& url, |
| 174 | const URLRequestStatus& status, |
| 175 | int response_code, |
| 176 | const ResponseCookies& cookies, |
| 177 | const std::string& data) { |
| 178 | scoped_ptr<const URLFetcher> fetcher; |
| 179 | bool parsed_ok = true; |
| 180 | bool must_back_off = false; // Reduce SafeBrowsing service query frequency. |
| 181 | |
[email protected] | dfdb0de7 | 2009-02-19 21:58:14 | [diff] [blame] | 182 | // See if this is a malware report fetcher. We don't take any action for |
| 183 | // the response to those. |
| 184 | std::set<const URLFetcher*>::iterator mit = malware_reports_.find(source); |
| 185 | if (mit != malware_reports_.end()) { |
| 186 | const URLFetcher* report = *mit; |
| 187 | malware_reports_.erase(mit); |
| 188 | delete report; |
| 189 | return; |
| 190 | } |
| 191 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 192 | HashRequests::iterator it = hash_requests_.find(source); |
| 193 | if (it != hash_requests_.end()) { |
| 194 | // GetHash response. |
| 195 | fetcher.reset(it->first); |
| 196 | SafeBrowsingService::SafeBrowsingCheck* check = it->second; |
| 197 | std::vector<SBFullHashResult> full_hashes; |
[email protected] | 200abc3 | 2008-09-05 01:44:33 | [diff] [blame] | 198 | bool can_cache = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 199 | if (response_code == 200 || response_code == 204) { |
[email protected] | 682343d | 2009-04-17 19:51:40 | [diff] [blame] | 200 | // For tracking our GetHash false positive (204) rate, compared to real |
| 201 | // (200) responses. |
| 202 | if (response_code == 200) |
| 203 | UMA_HISTOGRAM_COUNTS("SB2.GetHash200", 1); |
| 204 | else |
| 205 | UMA_HISTOGRAM_COUNTS("SB2.GetHash204", 1); |
[email protected] | 200abc3 | 2008-09-05 01:44:33 | [diff] [blame] | 206 | can_cache = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 207 | gethash_error_count_ = 0; |
| 208 | gethash_back_off_mult_ = 1; |
| 209 | bool re_key = false; |
| 210 | SafeBrowsingProtocolParser parser; |
| 211 | parsed_ok = parser.ParseGetHash(data.data(), |
| 212 | static_cast<int>(data.length()), |
| 213 | client_key_, |
| 214 | &re_key, |
| 215 | &full_hashes); |
| 216 | if (!parsed_ok) { |
| 217 | // If we fail to parse it, we must still inform the SafeBrowsingService |
| 218 | // so that it doesn't hold up the user's request indefinitely. Not sure |
| 219 | // what to do at that point though! |
| 220 | full_hashes.clear(); |
| 221 | } else { |
| 222 | if (re_key) |
| 223 | HandleReKey(); |
| 224 | } |
[email protected] | 3c3f4ac5 | 2009-12-15 20:22:17 | [diff] [blame] | 225 | } else { |
[email protected] | 7bdc1bf | 2009-07-28 15:48:03 | [diff] [blame] | 226 | HandleGetHashError(Time::Now()); |
[email protected] | 3c3f4ac5 | 2009-12-15 20:22:17 | [diff] [blame] | 227 | if (status.status() == URLRequestStatus::FAILED) { |
| 228 | SB_DLOG(INFO) << "SafeBrowsing GetHash request for: " << source->url() |
| 229 | << " failed with os error: " << status.os_error(); |
| 230 | } else { |
| 231 | SB_DLOG(INFO) << "SafeBrowsing GetHash request for: " << source->url() |
| 232 | << " failed with error: " << response_code; |
| 233 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | // Call back the SafeBrowsingService with full_hashes, even if there was a |
| 237 | // parse error or an error response code (in which case full_hashes will be |
| 238 | // empty). We can't block the user regardless of the error status. |
[email protected] | 200abc3 | 2008-09-05 01:44:33 | [diff] [blame] | 239 | sb_service_->HandleGetHashResults(check, full_hashes, can_cache); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 240 | |
| 241 | hash_requests_.erase(it); |
| 242 | } else { |
| 243 | // Update, chunk or key response. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 244 | fetcher.reset(request_.release()); |
| 245 | |
[email protected] | a11c2c6 | 2009-08-07 22:47:56 | [diff] [blame] | 246 | if (request_type_ == UPDATE_REQUEST) { |
| 247 | if (!fetcher.get()) { |
| 248 | // We've timed out waiting for an update response, so we've cancelled |
| 249 | // the update request and scheduled a new one. Ignore this response. |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | // Cancel the update response timeout now that we have the response. |
| 254 | update_timer_.Stop(); |
| 255 | } |
| 256 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 257 | if (response_code == 200) { |
| 258 | // We have data from the SafeBrowsing service. |
| 259 | parsed_ok = HandleServiceResponse(source->url(), |
| 260 | data.data(), |
| 261 | static_cast<int>(data.length())); |
| 262 | if (!parsed_ok) { |
| 263 | SB_DLOG(INFO) << "SafeBrowsing request for: " << source->url() |
| 264 | << "failed parse."; |
[email protected] | 22717d1e | 2008-10-15 21:55:32 | [diff] [blame] | 265 | must_back_off = true; |
| 266 | chunk_request_urls_.clear(); |
[email protected] | 6e3b12ff | 2009-01-06 22:17:57 | [diff] [blame] | 267 | UpdateFinished(false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 268 | } |
| 269 | |
[email protected] | cb1cdf49 | 2009-01-16 23:51:44 | [diff] [blame] | 270 | switch (request_type_) { |
| 271 | case CHUNK_REQUEST: |
| 272 | if (parsed_ok) |
| 273 | chunk_request_urls_.pop_front(); |
| 274 | break; |
| 275 | case GETKEY_REQUEST: |
| 276 | if (initial_request_) { |
| 277 | // This is the first request we've made this session. Now that we |
| 278 | // have the keys, do the regular update request. |
| 279 | initial_request_ = false; |
| 280 | GetNextUpdate(); |
| 281 | return; |
| 282 | } |
| 283 | break; |
| 284 | case UPDATE_REQUEST: |
| 285 | if (chunk_request_urls_.empty() && parsed_ok) { |
| 286 | // We are up to date since the servers gave us nothing new, so we |
| 287 | // are done with this update cycle. |
| 288 | UpdateFinished(true); |
| 289 | } |
| 290 | break; |
[email protected] | 7e242b5 | 2009-02-05 12:31:02 | [diff] [blame] | 291 | default: |
| 292 | NOTREACHED(); |
| 293 | break; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 294 | } |
[email protected] | 3c3f4ac5 | 2009-12-15 20:22:17 | [diff] [blame] | 295 | } else { |
| 296 | // The SafeBrowsing service error, or very bad response code: back off. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 297 | must_back_off = true; |
| 298 | if (request_type_ == CHUNK_REQUEST) |
| 299 | chunk_request_urls_.clear(); |
[email protected] | 6e3b12ff | 2009-01-06 22:17:57 | [diff] [blame] | 300 | UpdateFinished(false); |
[email protected] | 3c3f4ac5 | 2009-12-15 20:22:17 | [diff] [blame] | 301 | if (status.status() == URLRequestStatus::FAILED) { |
| 302 | SB_DLOG(INFO) << "SafeBrowsing request for: " << source->url() |
| 303 | << " failed with os error: " << status.os_error(); |
| 304 | } else { |
| 305 | SB_DLOG(INFO) << "SafeBrowsing request for: " << source->url() |
| 306 | << " failed with error: " << response_code; |
| 307 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
| 311 | // Schedule a new update request if we've finished retrieving all the chunks |
| 312 | // from the previous update. We treat the update request and the chunk URLs it |
| 313 | // contains as an atomic unit as far as back off is concerned. |
| 314 | if (chunk_request_urls_.empty() && |
| 315 | (request_type_ == CHUNK_REQUEST || request_type_ == UPDATE_REQUEST)) |
| 316 | ScheduleNextUpdate(must_back_off); |
| 317 | |
| 318 | // Get the next chunk if available. |
| 319 | IssueChunkRequest(); |
| 320 | } |
| 321 | |
| 322 | bool SafeBrowsingProtocolManager::HandleServiceResponse(const GURL& url, |
| 323 | const char* data, |
| 324 | int length) { |
| 325 | SafeBrowsingProtocolParser parser; |
| 326 | |
| 327 | switch (request_type_) { |
| 328 | case UPDATE_REQUEST: { |
| 329 | int next_update_sec = -1; |
| 330 | bool re_key = false; |
| 331 | bool reset = false; |
[email protected] | 7b1e3710 | 2010-03-08 21:43:16 | [diff] [blame^] | 332 | scoped_ptr<std::vector<SBChunkDelete> > chunk_deletes( |
| 333 | new std::vector<SBChunkDelete>); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 334 | std::vector<ChunkUrl> chunk_urls; |
| 335 | if (!parser.ParseUpdate(data, length, client_key_, |
| 336 | &next_update_sec, &re_key, |
[email protected] | 7b1e3710 | 2010-03-08 21:43:16 | [diff] [blame^] | 337 | &reset, chunk_deletes.get(), &chunk_urls)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 338 | return false; |
| 339 | } |
| 340 | |
| 341 | last_update_ = Time::Now(); |
| 342 | |
| 343 | if (update_state_ == FIRST_REQUEST) |
| 344 | update_state_ = SECOND_REQUEST; |
| 345 | else if (update_state_ == SECOND_REQUEST) |
| 346 | update_state_ = NORMAL_REQUEST; |
| 347 | |
| 348 | // New time for the next update. |
| 349 | if (next_update_sec > 0) { |
| 350 | next_update_sec_ = next_update_sec; |
| 351 | } else if (update_state_ == SECOND_REQUEST) { |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 352 | next_update_sec_ = base::RandInt(15 * 60, 45 * 60); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | // We need to request a new set of keys for MAC. |
| 356 | if (re_key) |
| 357 | HandleReKey(); |
| 358 | |
| 359 | // New chunks to download. |
| 360 | if (!chunk_urls.empty()) { |
[email protected] | 553dba6 | 2009-02-24 19:08:23 | [diff] [blame] | 361 | UMA_HISTOGRAM_COUNTS("SB2.UpdateUrls", chunk_urls.size()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 362 | for (size_t i = 0; i < chunk_urls.size(); ++i) |
| 363 | chunk_request_urls_.push_back(chunk_urls[i]); |
| 364 | } |
| 365 | |
| 366 | // Handle the case were the SafeBrowsing service tells us to dump our |
| 367 | // database. |
| 368 | if (reset) { |
| 369 | sb_service_->ResetDatabase(); |
| 370 | return true; |
| 371 | } |
| 372 | |
[email protected] | 7b1e3710 | 2010-03-08 21:43:16 | [diff] [blame^] | 373 | // Chunks to delete from our storage. Pass ownership of |
| 374 | // |chunk_deletes|. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 375 | if (!chunk_deletes->empty()) |
[email protected] | 7b1e3710 | 2010-03-08 21:43:16 | [diff] [blame^] | 376 | sb_service_->HandleChunkDelete(chunk_deletes.release()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 377 | |
| 378 | break; |
| 379 | } |
| 380 | case CHUNK_REQUEST: { |
[email protected] | 484c57a | 2009-03-21 01:24:01 | [diff] [blame] | 381 | UMA_HISTOGRAM_TIMES("SB2.ChunkRequest", |
| 382 | base::Time::Now() - chunk_request_start_); |
[email protected] | 2257382 | 2008-11-14 00:40:47 | [diff] [blame] | 383 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 384 | const ChunkUrl chunk_url = chunk_request_urls_.front(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 385 | bool re_key = false; |
[email protected] | 7b1e3710 | 2010-03-08 21:43:16 | [diff] [blame^] | 386 | scoped_ptr<SBChunkList> chunks(new SBChunkList); |
[email protected] | 553dba6 | 2009-02-24 19:08:23 | [diff] [blame] | 387 | UMA_HISTOGRAM_COUNTS("SB2.ChunkSize", length); |
[email protected] | 6e3b12ff | 2009-01-06 22:17:57 | [diff] [blame] | 388 | update_size_ += length; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 389 | if (!parser.ParseChunk(data, length, |
| 390 | client_key_, chunk_url.mac, |
[email protected] | 7b1e3710 | 2010-03-08 21:43:16 | [diff] [blame^] | 391 | &re_key, chunks.get())) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 392 | #ifndef NDEBUG |
| 393 | std::string data_str; |
| 394 | data_str.assign(data, length); |
| 395 | std::string encoded_chunk; |
[email protected] | 978df34 | 2009-11-24 06:21:53 | [diff] [blame] | 396 | base::Base64Encode(data, &encoded_chunk); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 397 | SB_DLOG(INFO) << "ParseChunk error for chunk: " << chunk_url.url |
| 398 | << ", client_key: " << client_key_ |
| 399 | << ", wrapped_key: " << wrapped_key_ |
| 400 | << ", mac: " << chunk_url.mac |
| 401 | << ", Base64Encode(data): " << encoded_chunk |
| 402 | << ", length: " << length; |
| 403 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 404 | return false; |
| 405 | } |
| 406 | |
| 407 | if (re_key) |
| 408 | HandleReKey(); |
| 409 | |
[email protected] | 7b1e3710 | 2010-03-08 21:43:16 | [diff] [blame^] | 410 | // Chunks to add to storage. Pass ownership of |chunks|. |
| 411 | if (!chunks->empty()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 412 | chunk_pending_to_write_ = true; |
[email protected] | 7b1e3710 | 2010-03-08 21:43:16 | [diff] [blame^] | 413 | sb_service_->HandleChunk(chunk_url.list_name, chunks.release()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | break; |
| 417 | } |
| 418 | case GETKEY_REQUEST: { |
| 419 | std::string client_key, wrapped_key; |
| 420 | if (!parser.ParseNewKey(data, length, &client_key, &wrapped_key)) |
| 421 | return false; |
| 422 | |
| 423 | client_key_ = client_key; |
| 424 | wrapped_key_ = wrapped_key; |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 425 | ChromeThread::PostTask( |
| 426 | ChromeThread::UI, FROM_HERE, |
| 427 | NewRunnableMethod( |
| 428 | sb_service_, &SafeBrowsingService::OnNewMacKeys, client_key_, |
| 429 | wrapped_key_)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 430 | break; |
| 431 | } |
| 432 | |
| 433 | default: |
| 434 | return false; |
| 435 | } |
| 436 | |
| 437 | return true; |
| 438 | } |
| 439 | |
| 440 | void SafeBrowsingProtocolManager::Initialize() { |
| 441 | // Don't want to hit the safe browsing servers on build/chrome bots. |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 442 | if (base::SysInfo::HasEnvVar(env_vars::kHeadless)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 443 | return; |
| 444 | |
| 445 | ScheduleNextUpdate(false /* no back off */); |
| 446 | } |
| 447 | |
| 448 | void SafeBrowsingProtocolManager::ScheduleNextUpdate(bool back_off) { |
| 449 | DCHECK(next_update_sec_ > 0); |
| 450 | |
[email protected] | 2d31666 | 2008-09-03 18:18:14 | [diff] [blame] | 451 | // Unschedule any current timer. |
| 452 | update_timer_.Stop(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 453 | |
| 454 | // Reschedule with the new update. |
| 455 | const int next_update = GetNextUpdateTime(back_off); |
[email protected] | 2d31666 | 2008-09-03 18:18:14 | [diff] [blame] | 456 | update_timer_.Start(TimeDelta::FromMilliseconds(next_update), this, |
| 457 | &SafeBrowsingProtocolManager::GetNextUpdate); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | // According to section 5 of the SafeBrowsing protocol specification, we must |
| 461 | // back off after a certain number of errors. We only change 'next_update_sec_' |
| 462 | // when we receive a response from the SafeBrowsing service. |
| 463 | int SafeBrowsingProtocolManager::GetNextUpdateTime(bool back_off) { |
| 464 | int next = next_update_sec_; |
| 465 | if (back_off) { |
| 466 | next = GetNextBackOffTime(&update_error_count_, &update_back_off_mult_); |
| 467 | } else { |
| 468 | // Successful response means error reset. |
| 469 | update_error_count_ = 0; |
| 470 | update_back_off_mult_ = 1; |
| 471 | } |
| 472 | return next * 1000; // milliseconds |
| 473 | } |
| 474 | |
| 475 | int SafeBrowsingProtocolManager::GetNextBackOffTime(int* error_count, |
| 476 | int* multiplier) { |
| 477 | DCHECK(multiplier && error_count); |
| 478 | (*error_count)++; |
| 479 | if (*error_count > 1 && *error_count < 6) { |
| 480 | int next = static_cast<int>(*multiplier * (1 + back_off_fuzz_) * 30 * 60); |
| 481 | *multiplier *= 2; |
| 482 | if (*multiplier > kSbMaxBackOff) |
| 483 | *multiplier = kSbMaxBackOff; |
| 484 | return next; |
| 485 | } |
| 486 | |
| 487 | if (*error_count >= 6) |
| 488 | return 60 * 60 * 8; // 8 hours |
| 489 | |
| 490 | return 60; // 1 minute |
| 491 | } |
| 492 | |
| 493 | // This request requires getting a list of all the chunks for each list from the |
| 494 | // database asynchronously. The request will be issued when we're called back in |
| 495 | // OnGetChunksComplete. |
| 496 | // TODO(paulg): We should get this at start up and maintain a ChunkRange cache |
| 497 | // to avoid hitting the database with each update request. On the |
| 498 | // otherhand, this request will only occur ~20-30 minutes so there |
| 499 | // isn't that much overhead. Measure! |
| 500 | void SafeBrowsingProtocolManager::IssueUpdateRequest() { |
| 501 | request_type_ = UPDATE_REQUEST; |
[email protected] | 57119c3f | 2008-12-04 00:33:04 | [diff] [blame] | 502 | sb_service_->UpdateStarted(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | void SafeBrowsingProtocolManager::IssueChunkRequest() { |
| 506 | // We are only allowed to have one request outstanding at any time. Also, |
| 507 | // don't get the next url until the previous one has been written to disk so |
| 508 | // that we don't use too much memory. |
| 509 | if (request_.get() || chunk_request_urls_.empty() || chunk_pending_to_write_) |
| 510 | return; |
| 511 | |
| 512 | ChunkUrl next_chunk = chunk_request_urls_.front(); |
| 513 | DCHECK(!next_chunk.url.empty()); |
| 514 | if (!StartsWithASCII(next_chunk.url, "http://", false) && |
| 515 | !StartsWithASCII(next_chunk.url, "https://", false)) |
| 516 | next_chunk.url = "http://" + next_chunk.url; |
| 517 | GURL chunk_url(next_chunk.url); |
| 518 | request_type_ = CHUNK_REQUEST; |
| 519 | request_.reset(new URLFetcher(chunk_url, URLFetcher::GET, this)); |
| 520 | request_->set_load_flags(net::LOAD_DISABLE_CACHE); |
[email protected] | d11f566 | 2009-11-12 20:52:56 | [diff] [blame] | 521 | request_->set_request_context(request_context_getter_); |
[email protected] | 2257382 | 2008-11-14 00:40:47 | [diff] [blame] | 522 | chunk_request_start_ = base::Time::Now(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 523 | request_->Start(); |
| 524 | } |
| 525 | |
| 526 | void SafeBrowsingProtocolManager::IssueKeyRequest() { |
| 527 | GURL key_url(StringPrintf(kSbNewKeyUrl, |
[email protected] | 1a87151 | 2009-11-06 06:11:18 | [diff] [blame] | 528 | client_name_.c_str(), |
[email protected] | 484fce4 | 2008-10-01 00:37:18 | [diff] [blame] | 529 | version_.c_str())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 530 | request_type_ = GETKEY_REQUEST; |
| 531 | request_.reset(new URLFetcher(key_url, URLFetcher::GET, this)); |
| 532 | request_->set_load_flags(net::LOAD_DISABLE_CACHE); |
[email protected] | d11f566 | 2009-11-12 20:52:56 | [diff] [blame] | 533 | request_->set_request_context(request_context_getter_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 534 | request_->Start(); |
| 535 | } |
| 536 | |
| 537 | void SafeBrowsingProtocolManager::OnGetChunksComplete( |
| 538 | const std::vector<SBListChunkRanges>& lists, bool database_error) { |
| 539 | DCHECK(request_type_ == UPDATE_REQUEST); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 540 | if (database_error) { |
[email protected] | a11c2c6 | 2009-08-07 22:47:56 | [diff] [blame] | 541 | UpdateFinished(false); |
[email protected] | 3c3f4ac5 | 2009-12-15 20:22:17 | [diff] [blame] | 542 | ScheduleNextUpdate(false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 543 | return; |
| 544 | } |
| 545 | |
| 546 | const bool use_mac = !client_key_.empty(); |
| 547 | |
| 548 | // Format our stored chunks: |
| 549 | std::string list_data; |
| 550 | bool found_malware = false; |
| 551 | bool found_phishing = false; |
| 552 | for (size_t i = 0; i < lists.size(); ++i) { |
| 553 | list_data.append(FormatList(lists[i], use_mac)); |
[email protected] | c3ff8949 | 2008-11-11 02:17:51 | [diff] [blame] | 554 | if (lists[i].name == safe_browsing_util::kPhishingList) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 555 | found_phishing = true; |
| 556 | |
[email protected] | c3ff8949 | 2008-11-11 02:17:51 | [diff] [blame] | 557 | if (lists[i].name == safe_browsing_util::kMalwareList) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 558 | found_malware = true; |
| 559 | } |
| 560 | |
| 561 | // If we have an empty database, let the server know we want data for these |
| 562 | // lists. |
| 563 | if (!found_phishing) |
[email protected] | c3ff8949 | 2008-11-11 02:17:51 | [diff] [blame] | 564 | list_data.append(FormatList( |
| 565 | SBListChunkRanges(safe_browsing_util::kPhishingList), use_mac)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 566 | |
| 567 | if (!found_malware) |
[email protected] | c3ff8949 | 2008-11-11 02:17:51 | [diff] [blame] | 568 | list_data.append(FormatList( |
| 569 | SBListChunkRanges(safe_browsing_util::kMalwareList), use_mac)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 570 | |
| 571 | std::string url = StringPrintf(kSbUpdateUrl, |
[email protected] | 1a87151 | 2009-11-06 06:11:18 | [diff] [blame] | 572 | client_name_.c_str(), |
[email protected] | 484fce4 | 2008-10-01 00:37:18 | [diff] [blame] | 573 | version_.c_str()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 574 | if (use_mac) { |
| 575 | url.append("&wrkey="); |
| 576 | url.append(wrapped_key_); |
| 577 | } |
| 578 | |
| 579 | GURL update_url(url); |
| 580 | request_.reset(new URLFetcher(update_url, URLFetcher::POST, this)); |
| 581 | request_->set_load_flags(net::LOAD_DISABLE_CACHE); |
[email protected] | d11f566 | 2009-11-12 20:52:56 | [diff] [blame] | 582 | request_->set_request_context(request_context_getter_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 583 | request_->set_upload_data("text/plain", list_data); |
| 584 | request_->Start(); |
[email protected] | a11c2c6 | 2009-08-07 22:47:56 | [diff] [blame] | 585 | |
| 586 | // Begin the update request timeout. |
| 587 | update_timer_.Start(TimeDelta::FromSeconds(kSbMaxUpdateWaitSec), this, |
| 588 | &SafeBrowsingProtocolManager::UpdateResponseTimeout); |
| 589 | } |
| 590 | |
| 591 | // If we haven't heard back from the server with an update response, this method |
| 592 | // will run. Close the current update session and schedule another update. |
| 593 | void SafeBrowsingProtocolManager::UpdateResponseTimeout() { |
| 594 | DCHECK(request_type_ == UPDATE_REQUEST); |
| 595 | request_.reset(); |
[email protected] | a11c2c6 | 2009-08-07 22:47:56 | [diff] [blame] | 596 | UpdateFinished(false); |
[email protected] | 3c3f4ac5 | 2009-12-15 20:22:17 | [diff] [blame] | 597 | ScheduleNextUpdate(false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | void SafeBrowsingProtocolManager::OnChunkInserted() { |
| 601 | chunk_pending_to_write_ = false; |
| 602 | |
| 603 | if (chunk_request_urls_.empty()) { |
[email protected] | 484c57a | 2009-03-21 01:24:01 | [diff] [blame] | 604 | UMA_HISTOGRAM_LONG_TIMES("SB2.Update", Time::Now() - last_update_); |
[email protected] | 6e3b12ff | 2009-01-06 22:17:57 | [diff] [blame] | 605 | UpdateFinished(true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 606 | } else { |
| 607 | IssueChunkRequest(); |
| 608 | } |
| 609 | } |
| 610 | |
[email protected] | dfdb0de7 | 2009-02-19 21:58:14 | [diff] [blame] | 611 | void SafeBrowsingProtocolManager::ReportMalware(const GURL& malware_url, |
| 612 | const GURL& page_url, |
| 613 | const GURL& referrer_url) { |
| 614 | std::string report_str = StringPrintf( |
| 615 | kSbMalwareReportUrl, |
[email protected] | 0d2e6a6 | 2010-01-15 20:09:19 | [diff] [blame] | 616 | EscapeQueryParamValue(malware_url.spec(), true).c_str(), |
| 617 | EscapeQueryParamValue(page_url.spec(), true).c_str(), |
| 618 | EscapeQueryParamValue(referrer_url.spec(), true).c_str(), |
[email protected] | 1a87151 | 2009-11-06 06:11:18 | [diff] [blame] | 619 | client_name_.c_str(), |
[email protected] | dfdb0de7 | 2009-02-19 21:58:14 | [diff] [blame] | 620 | version_.c_str()); |
| 621 | GURL report_url(report_str); |
| 622 | URLFetcher* report = new URLFetcher(report_url, URLFetcher::GET, this); |
| 623 | report->set_load_flags(net::LOAD_DISABLE_CACHE); |
[email protected] | d11f566 | 2009-11-12 20:52:56 | [diff] [blame] | 624 | report->set_request_context(request_context_getter_); |
[email protected] | dfdb0de7 | 2009-02-19 21:58:14 | [diff] [blame] | 625 | report->Start(); |
| 626 | malware_reports_.insert(report); |
| 627 | } |
| 628 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 629 | // static |
| 630 | std::string SafeBrowsingProtocolManager::FormatList( |
| 631 | const SBListChunkRanges& list, bool use_mac) { |
| 632 | std::string formatted_results; |
| 633 | formatted_results.append(list.name); |
| 634 | formatted_results.append(";"); |
| 635 | if (!list.adds.empty()) { |
| 636 | formatted_results.append("a:" + list.adds); |
| 637 | if (!list.subs.empty() || use_mac) |
| 638 | formatted_results.append(":"); |
| 639 | } |
| 640 | if (!list.subs.empty()) { |
| 641 | formatted_results.append("s:" + list.subs); |
| 642 | if (use_mac) |
| 643 | formatted_results.append(":"); |
| 644 | } |
| 645 | if (use_mac) |
| 646 | formatted_results.append("mac"); |
| 647 | formatted_results.append("\n"); |
| 648 | |
| 649 | return formatted_results; |
| 650 | } |
| 651 | |
| 652 | void SafeBrowsingProtocolManager::HandleReKey() { |
| 653 | client_key_.clear(); |
| 654 | wrapped_key_.clear(); |
| 655 | IssueKeyRequest(); |
| 656 | } |
| 657 | |
[email protected] | 7bdc1bf | 2009-07-28 15:48:03 | [diff] [blame] | 658 | void SafeBrowsingProtocolManager::HandleGetHashError(const Time& now) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 659 | int next = GetNextBackOffTime(&gethash_error_count_, &gethash_back_off_mult_); |
[email protected] | 7bdc1bf | 2009-07-28 15:48:03 | [diff] [blame] | 660 | next_gethash_time_ = now + TimeDelta::FromSeconds(next); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 661 | } |
[email protected] | 6e3b12ff | 2009-01-06 22:17:57 | [diff] [blame] | 662 | |
| 663 | void SafeBrowsingProtocolManager::UpdateFinished(bool success) { |
[email protected] | 553dba6 | 2009-02-24 19:08:23 | [diff] [blame] | 664 | UMA_HISTOGRAM_COUNTS("SB2.UpdateSize", update_size_); |
[email protected] | 6e3b12ff | 2009-01-06 22:17:57 | [diff] [blame] | 665 | update_size_ = 0; |
| 666 | sb_service_->UpdateFinished(success); |
[email protected] | 7e242b5 | 2009-02-05 12:31:02 | [diff] [blame] | 667 | } |