Chromium Code Reviews| OLD | NEW |
|---|---|
| 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_protocol_manager_util.h" | 5 #include "components/safe_browsing_db/v4_protocol_manager_util.h" |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/metrics/sparse_histogram.h" | 8 #include "base/metrics/sparse_histogram.h" |
| 9 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
| 10 #include "base/strings/string_util.h" | |
| 10 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "crypto/sha2.h" | |
| 11 #include "net/base/escape.h" | 13 #include "net/base/escape.h" |
| 12 #include "net/http/http_request_headers.h" | 14 #include "net/http/http_request_headers.h" |
| 15 #include "url/url_util.h" | |
| 13 | 16 |
| 14 using base::Time; | 17 using base::Time; |
| 15 using base::TimeDelta; | 18 using base::TimeDelta; |
| 16 | 19 |
| 17 namespace safe_browsing { | 20 namespace safe_browsing { |
| 18 | 21 |
| 22 namespace { | |
| 23 | |
|
Nathan Parker
2016/08/05 18:07:39
I assume these were just moved...
vakh (use Gerrit instead)
2016/08/05 18:25:47
Yes, from util.cc
| |
| 24 std::string Unescape(const std::string& url) { | |
| 25 std::string unescaped_str(url); | |
| 26 const int kMaxLoopIterations = 1024; | |
| 27 size_t old_size = 0; | |
| 28 int loop_var = 0; | |
| 29 do { | |
| 30 old_size = unescaped_str.size(); | |
| 31 unescaped_str = net::UnescapeURLComponent( | |
| 32 unescaped_str, | |
| 33 net::UnescapeRule::SPOOFING_AND_CONTROL_CHARS | | |
| 34 net::UnescapeRule::SPACES | net::UnescapeRule::PATH_SEPARATORS | | |
| 35 net::UnescapeRule::URL_SPECIAL_CHARS_EXCEPT_PATH_SEPARATORS); | |
| 36 } while (old_size != unescaped_str.size() && | |
| 37 ++loop_var <= kMaxLoopIterations); | |
| 38 | |
| 39 return unescaped_str; | |
| 40 } | |
| 41 | |
| 42 std::string Escape(const std::string& url) { | |
| 43 std::string escaped_str; | |
| 44 // The escaped string is larger so allocate double the length to reduce the | |
| 45 // chance of the string being grown. | |
| 46 escaped_str.reserve(url.length() * 2); | |
| 47 const char* kHexString = "0123456789ABCDEF"; | |
| 48 for (size_t i = 0; i < url.length(); i++) { | |
| 49 unsigned char c = static_cast<unsigned char>(url[i]); | |
| 50 if (c <= ' ' || c > '~' || c == '#' || c == '%') { | |
| 51 escaped_str += '%'; | |
| 52 escaped_str += kHexString[c >> 4]; | |
| 53 escaped_str += kHexString[c & 0xf]; | |
| 54 } else { | |
| 55 escaped_str += c; | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 return escaped_str; | |
| 60 } | |
| 61 | |
| 62 } // namespace | |
| 63 | |
| 19 std::ostream& operator<<(std::ostream& os, const UpdateListIdentifier& id) { | 64 std::ostream& operator<<(std::ostream& os, const UpdateListIdentifier& id) { |
| 20 os << "{hash: " << id.hash() << "; platform_type: " << id.platform_type | 65 os << "{hash: " << id.hash() << "; platform_type: " << id.platform_type |
| 21 << "; threat_entry_type: " << id.threat_entry_type | 66 << "; threat_entry_type: " << id.threat_entry_type |
| 22 << "; threat_type: " << id.threat_type << "}"; | 67 << "; threat_type: " << id.threat_type << "}"; |
| 23 return os; | 68 return os; |
| 24 } | 69 } |
| 25 | 70 |
| 26 // The Safe Browsing V4 server URL prefix. | 71 // The Safe Browsing V4 server URL prefix. |
| 27 const char kSbV4UrlPrefix[] = "https://safebrowsing.googleapis.com/v4"; | 72 const char kSbV4UrlPrefix[] = "https://safebrowsing.googleapis.com/v4"; |
| 28 | 73 |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 131 } | 176 } |
| 132 | 177 |
| 133 // static | 178 // static |
| 134 void V4ProtocolManagerUtil::UpdateHeaders(net::HttpRequestHeaders* headers) { | 179 void V4ProtocolManagerUtil::UpdateHeaders(net::HttpRequestHeaders* headers) { |
| 135 // NOTE(vakh): The following header informs the envelope server (which sits in | 180 // NOTE(vakh): The following header informs the envelope server (which sits in |
| 136 // front of Google's stubby server) that the received GET request should be | 181 // front of Google's stubby server) that the received GET request should be |
| 137 // interpreted as a POST. | 182 // interpreted as a POST. |
| 138 headers->SetHeaderIfMissing("X-HTTP-Method-Override", "POST"); | 183 headers->SetHeaderIfMissing("X-HTTP-Method-Override", "POST"); |
| 139 } | 184 } |
| 140 | 185 |
| 186 // static | |
| 187 void V4ProtocolManagerUtil::UrlToFullHashes( | |
| 188 const GURL& url, | |
| 189 base::hash_set<FullHash>* full_hashes) { | |
| 190 std::string canon_host, canon_path, canon_query; | |
| 191 CanonicalizeUrl(url, &canon_host, &canon_path, &canon_query); | |
| 192 | |
| 193 std::vector<std::string> hosts; | |
| 194 if (url.HostIsIPAddress()) { | |
| 195 hosts.push_back(url.host()); | |
| 196 } else { | |
| 197 GenerateHostVariantsToCheck(canon_host, &hosts); | |
| 198 } | |
| 199 | |
| 200 std::vector<std::string> paths; | |
| 201 GeneratePathVariantsToCheck(canon_path, canon_query, &paths); | |
| 202 for (const std::string& host : hosts) { | |
| 203 for (const std::string& path : paths) { | |
| 204 full_hashes->insert(crypto::SHA256HashString(host + path)); | |
| 205 } | |
| 206 } | |
| 207 } | |
| 208 | |
| 209 // static | |
| 210 void V4ProtocolManagerUtil::GenerateHostsToCheck( | |
| 211 const GURL& url, | |
| 212 std::vector<std::string>* hosts) { | |
| 213 std::string canon_host; | |
| 214 CanonicalizeUrl(url, &canon_host, NULL, NULL); | |
| 215 GenerateHostVariantsToCheck(canon_host, hosts); | |
| 216 } | |
| 217 | |
| 218 // static | |
| 219 void V4ProtocolManagerUtil::GeneratePathsToCheck( | |
| 220 const GURL& url, | |
| 221 std::vector<std::string>* paths) { | |
| 222 std::string canon_path; | |
| 223 std::string canon_query; | |
| 224 CanonicalizeUrl(url, NULL, &canon_path, &canon_query); | |
| 225 GeneratePathVariantsToCheck(canon_path, canon_query, paths); | |
| 226 } | |
| 227 | |
| 228 // static | |
| 229 void V4ProtocolManagerUtil::GeneratePatternsToCheck( | |
| 230 const GURL& url, | |
| 231 std::vector<std::string>* urls) { | |
| 232 std::string canon_host; | |
| 233 std::string canon_path; | |
| 234 std::string canon_query; | |
| 235 CanonicalizeUrl(url, &canon_host, &canon_path, &canon_query); | |
| 236 | |
| 237 std::vector<std::string> hosts, paths; | |
| 238 GenerateHostVariantsToCheck(canon_host, &hosts); | |
| 239 GeneratePathVariantsToCheck(canon_path, canon_query, &paths); | |
| 240 for (size_t h = 0; h < hosts.size(); ++h) { | |
| 241 for (size_t p = 0; p < paths.size(); ++p) { | |
| 242 urls->push_back(hosts[h] + paths[p]); | |
| 243 } | |
| 244 } | |
| 245 } | |
| 246 | |
| 247 // static | |
| 248 void V4ProtocolManagerUtil::CanonicalizeUrl(const GURL& url, | |
| 249 std::string* canonicalized_hostname, | |
| 250 std::string* canonicalized_path, | |
| 251 std::string* canonicalized_query) { | |
| 252 DCHECK(url.is_valid()); | |
| 253 | |
| 254 // We only canonicalize "normal" URLs. | |
| 255 if (!url.IsStandard()) | |
| 256 return; | |
| 257 | |
| 258 // Following canonicalization steps are excluded since url parsing takes care | |
| 259 // of those :- | |
| 260 // 1. Remove any tab (0x09), CR (0x0d), and LF (0x0a) chars from url. | |
| 261 // (Exclude escaped version of these chars). | |
| 262 // 2. Normalize hostname to 4 dot-seperated decimal values. | |
| 263 // 3. Lowercase hostname. | |
| 264 // 4. Resolve path sequences "/../" and "/./". | |
| 265 | |
| 266 // That leaves us with the following :- | |
| 267 // 1. Remove fragment in URL. | |
| 268 GURL url_without_fragment; | |
| 269 GURL::Replacements f_replacements; | |
| 270 f_replacements.ClearRef(); | |
| 271 f_replacements.ClearUsername(); | |
| 272 f_replacements.ClearPassword(); | |
| 273 url_without_fragment = url.ReplaceComponents(f_replacements); | |
| 274 | |
| 275 // 2. Do URL unescaping until no more hex encoded characters exist. | |
| 276 std::string url_unescaped_str(Unescape(url_without_fragment.spec())); | |
| 277 url::Parsed parsed; | |
| 278 url::ParseStandardURL(url_unescaped_str.data(), url_unescaped_str.length(), | |
| 279 &parsed); | |
| 280 | |
| 281 // 3. In hostname, remove all leading and trailing dots. | |
| 282 base::StringPiece host; | |
| 283 if (parsed.host.len > 0) | |
| 284 host.set(url_unescaped_str.data() + parsed.host.begin, parsed.host.len); | |
| 285 | |
| 286 base::StringPiece host_without_end_dots = | |
| 287 base::TrimString(host, ".", base::TrimPositions::TRIM_ALL); | |
| 288 | |
| 289 // 4. In hostname, replace consecutive dots with a single dot. | |
| 290 std::string host_without_consecutive_dots( | |
| 291 RemoveConsecutiveChars(host_without_end_dots, '.')); | |
| 292 | |
| 293 // 5. In path, replace runs of consecutive slashes with a single slash. | |
| 294 base::StringPiece path; | |
| 295 if (parsed.path.len > 0) | |
| 296 path.set(url_unescaped_str.data() + parsed.path.begin, parsed.path.len); | |
| 297 std::string path_without_consecutive_slash(RemoveConsecutiveChars(path, '/')); | |
| 298 | |
| 299 url::Replacements<char> hp_replacements; | |
| 300 hp_replacements.SetHost( | |
| 301 host_without_consecutive_dots.data(), | |
| 302 url::Component(0, host_without_consecutive_dots.length())); | |
| 303 hp_replacements.SetPath( | |
| 304 path_without_consecutive_slash.data(), | |
| 305 url::Component(0, path_without_consecutive_slash.length())); | |
| 306 | |
| 307 std::string url_unescaped_with_can_hostpath; | |
| 308 url::StdStringCanonOutput output(&url_unescaped_with_can_hostpath); | |
| 309 url::Parsed temp_parsed; | |
| 310 url::ReplaceComponents(url_unescaped_str.data(), url_unescaped_str.length(), | |
| 311 parsed, hp_replacements, NULL, &output, &temp_parsed); | |
| 312 output.Complete(); | |
| 313 | |
| 314 // 6. Step needed to revert escaping done in url::ReplaceComponents. | |
| 315 url_unescaped_with_can_hostpath = Unescape(url_unescaped_with_can_hostpath); | |
| 316 | |
| 317 // 7. After performing all above steps, percent-escape all chars in url which | |
| 318 // are <= ASCII 32, >= 127, #, %. Escapes must be uppercase hex characters. | |
| 319 std::string escaped_canon_url_str(Escape(url_unescaped_with_can_hostpath)); | |
| 320 url::Parsed final_parsed; | |
| 321 url::ParseStandardURL(escaped_canon_url_str.data(), | |
| 322 escaped_canon_url_str.length(), &final_parsed); | |
| 323 | |
| 324 if (canonicalized_hostname && final_parsed.host.len > 0) { | |
| 325 *canonicalized_hostname = escaped_canon_url_str.substr( | |
| 326 final_parsed.host.begin, final_parsed.host.len); | |
| 327 } | |
| 328 if (canonicalized_path && final_parsed.path.len > 0) { | |
| 329 *canonicalized_path = escaped_canon_url_str.substr(final_parsed.path.begin, | |
| 330 final_parsed.path.len); | |
| 331 } | |
| 332 if (canonicalized_query && final_parsed.query.len > 0) { | |
| 333 *canonicalized_query = escaped_canon_url_str.substr( | |
| 334 final_parsed.query.begin, final_parsed.query.len); | |
| 335 } | |
| 336 } | |
| 337 | |
| 338 // static | |
| 339 std::string V4ProtocolManagerUtil::RemoveConsecutiveChars(base::StringPiece str, | |
| 340 const char c) { | |
| 341 std::string output; | |
| 342 // Output is at most the length of the original string. | |
| 343 output.reserve(str.size()); | |
| 344 | |
| 345 size_t i = 0; | |
| 346 while (i < str.size()) { | |
| 347 output.append(1, str[i++]); | |
| 348 if (str[i - 1] == c) { | |
| 349 while (i < str.size() && str[i] == c) { | |
| 350 i++; | |
| 351 } | |
| 352 } | |
| 353 } | |
| 354 | |
| 355 return output; | |
| 356 } | |
| 357 | |
| 358 // static | |
| 359 void V4ProtocolManagerUtil::GenerateHostVariantsToCheck( | |
| 360 const std::string& host, | |
| 361 std::vector<std::string>* hosts) { | |
| 362 hosts->clear(); | |
| 363 | |
| 364 if (host.empty()) | |
| 365 return; | |
| 366 | |
| 367 // Per the Safe Browsing Protocol v2 spec, we try the host, and also up to 4 | |
| 368 // hostnames formed by starting with the last 5 components and successively | |
| 369 // removing the leading component. The last component isn't examined alone, | |
| 370 // since it's the TLD or a subcomponent thereof. | |
| 371 // | |
| 372 // Note that we don't need to be clever about stopping at the "real" eTLD -- | |
| 373 // the data on the server side has been filtered to ensure it will not | |
| 374 // blacklist a whole TLD, and it's not significantly slower on our side to | |
| 375 // just check too much. | |
| 376 // | |
| 377 // Also note that because we have a simple blacklist, not some sort of complex | |
| 378 // whitelist-in-blacklist or vice versa, it doesn't matter what order we check | |
| 379 // these in. | |
| 380 const size_t kMaxHostsToCheck = 4; | |
| 381 bool skipped_last_component = false; | |
| 382 for (std::string::const_reverse_iterator i(host.rbegin()); | |
| 383 i != host.rend() && hosts->size() < kMaxHostsToCheck; ++i) { | |
| 384 if (*i == '.') { | |
| 385 if (skipped_last_component) | |
| 386 hosts->push_back(std::string(i.base(), host.end())); | |
| 387 else | |
| 388 skipped_last_component = true; | |
| 389 } | |
| 390 } | |
| 391 hosts->push_back(host); | |
| 392 } | |
| 393 | |
| 394 // static | |
| 395 void V4ProtocolManagerUtil::GeneratePathVariantsToCheck( | |
| 396 const std::string& path, | |
| 397 const std::string& query, | |
| 398 std::vector<std::string>* paths) { | |
| 399 paths->clear(); | |
| 400 | |
| 401 if (path.empty()) | |
| 402 return; | |
| 403 | |
| 404 // Per the Safe Browsing Protocol v2 spec, we try the exact path with/without | |
| 405 // the query parameters, and also up to 4 paths formed by starting at the root | |
| 406 // and adding more path components. | |
| 407 // | |
| 408 // As with the hosts above, it doesn't matter what order we check these in. | |
| 409 const size_t kMaxPathsToCheck = 4; | |
| 410 for (std::string::const_iterator i(path.begin()); | |
| 411 i != path.end() && paths->size() < kMaxPathsToCheck; ++i) { | |
| 412 if (*i == '/') | |
| 413 paths->push_back(std::string(path.begin(), i + 1)); | |
| 414 } | |
| 415 | |
| 416 if (!paths->empty() && paths->back() != path) | |
| 417 paths->push_back(path); | |
| 418 | |
| 419 if (!query.empty()) | |
| 420 paths->push_back(path + "?" + query); | |
| 421 } | |
| 422 | |
| 141 } // namespace safe_browsing | 423 } // namespace safe_browsing |
| OLD | NEW |