| 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 "net/base/sdch_manager.h" | 5 #include "net/base/sdch_manager.h" |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 | 21 |
| 22 // static | 22 // static |
| 23 const size_t SdchManager::kMaxDictionaryCount = 20; | 23 const size_t SdchManager::kMaxDictionaryCount = 20; |
| 24 | 24 |
| 25 // static | 25 // static |
| 26 SdchManager* SdchManager::global_ = NULL; | 26 SdchManager* SdchManager::global_ = NULL; |
| 27 | 27 |
| 28 // static | 28 // static |
| 29 bool SdchManager::g_sdch_enabled_ = true; | 29 bool SdchManager::g_sdch_enabled_ = true; |
| 30 | 30 |
| 31 // static |
| 32 bool SdchManager::g_secure_scheme_supported_ = false; |
| 33 |
| 31 //------------------------------------------------------------------------------ | 34 //------------------------------------------------------------------------------ |
| 32 SdchManager::Dictionary::Dictionary(const std::string& dictionary_text, | 35 SdchManager::Dictionary::Dictionary(const std::string& dictionary_text, |
| 33 size_t offset, | 36 size_t offset, |
| 34 const std::string& client_hash, | 37 const std::string& client_hash, |
| 35 const GURL& gurl, | 38 const GURL& gurl, |
| 36 const std::string& domain, | 39 const std::string& domain, |
| 37 const std::string& path, | 40 const std::string& path, |
| 38 const base::Time& expiration, | 41 const base::Time& expiration, |
| 39 const std::set<int>& ports) | 42 const std::set<int>& ports) |
| 40 : text_(dictionary_text, offset), | 43 : text_(dictionary_text, offset), |
| (...skipping 15 matching lines...) Expand all Loading... |
| 56 Avail-Dictionary header are modeled after the rules for cookie scoping. The | 59 Avail-Dictionary header are modeled after the rules for cookie scoping. The |
| 57 terms "domain-match" and "pathmatch" are defined in RFC 2965 [6]. A | 60 terms "domain-match" and "pathmatch" are defined in RFC 2965 [6]. A |
| 58 dictionary may be advertised in the Avail-Dictionaries header exactly when | 61 dictionary may be advertised in the Avail-Dictionaries header exactly when |
| 59 all of the following are true: | 62 all of the following are true: |
| 60 1. The server's effective host name domain-matches the Domain attribute of | 63 1. The server's effective host name domain-matches the Domain attribute of |
| 61 the dictionary. | 64 the dictionary. |
| 62 2. If the dictionary has a Port attribute, the request port is one of the | 65 2. If the dictionary has a Port attribute, the request port is one of the |
| 63 ports listed in the Port attribute. | 66 ports listed in the Port attribute. |
| 64 3. The request URI path-matches the path header of the dictionary. | 67 3. The request URI path-matches the path header of the dictionary. |
| 65 4. The request is not an HTTPS request. | 68 4. The request is not an HTTPS request. |
| 69 We can override (ignore) item (4) only when we have explicitly enabled |
| 70 HTTPS support AND dictionary has been acquired over HTTPS. |
| 66 */ | 71 */ |
| 67 if (!DomainMatch(target_url, domain_)) | 72 if (!DomainMatch(target_url, domain_)) |
| 68 return false; | 73 return false; |
| 69 if (!ports_.empty() && 0 == ports_.count(target_url.EffectiveIntPort())) | 74 if (!ports_.empty() && 0 == ports_.count(target_url.EffectiveIntPort())) |
| 70 return false; | 75 return false; |
| 71 if (path_.size() && !PathMatch(target_url.path(), path_)) | 76 if (path_.size() && !PathMatch(target_url.path(), path_)) |
| 72 return false; | 77 return false; |
| 73 if (target_url.SchemeIsSecure()) | 78 if (!SdchManager::secure_scheme_supported() && target_url.SchemeIsSecure()) |
| 79 return false; |
| 80 if (target_url.SchemeIsSecure() && !url_.SchemeIsSecure()) |
| 74 return false; | 81 return false; |
| 75 if (base::Time::Now() > expiration_) | 82 if (base::Time::Now() > expiration_) |
| 76 return false; | 83 return false; |
| 77 return true; | 84 return true; |
| 78 } | 85 } |
| 79 | 86 |
| 80 //------------------------------------------------------------------------------ | 87 //------------------------------------------------------------------------------ |
| 81 // Security functions restricting loads and use of dictionaries. | 88 // Security functions restricting loads and use of dictionaries. |
| 82 | 89 |
| 83 // static | 90 // static |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 bool SdchManager::Dictionary::CanUse(const GURL& referring_url) { | 152 bool SdchManager::Dictionary::CanUse(const GURL& referring_url) { |
| 146 if (!SdchManager::Global()->IsInSupportedDomain(referring_url)) | 153 if (!SdchManager::Global()->IsInSupportedDomain(referring_url)) |
| 147 return false; | 154 return false; |
| 148 /* | 155 /* |
| 149 1. The request URL's host name domain-matches the Domain attribute of the | 156 1. The request URL's host name domain-matches the Domain attribute of the |
| 150 dictionary. | 157 dictionary. |
| 151 2. If the dictionary has a Port attribute, the request port is one of the | 158 2. If the dictionary has a Port attribute, the request port is one of the |
| 152 ports listed in the Port attribute. | 159 ports listed in the Port attribute. |
| 153 3. The request URL path-matches the path attribute of the dictionary. | 160 3. The request URL path-matches the path attribute of the dictionary. |
| 154 4. The request is not an HTTPS request. | 161 4. The request is not an HTTPS request. |
| 162 We can override (ignore) item (4) only when we have explicitly enabled |
| 163 HTTPS support AND dictionary has been acquired over HTTPS. |
| 155 */ | 164 */ |
| 156 if (!DomainMatch(referring_url, domain_)) { | 165 if (!DomainMatch(referring_url, domain_)) { |
| 157 SdchErrorRecovery(DICTIONARY_FOUND_HAS_WRONG_DOMAIN); | 166 SdchErrorRecovery(DICTIONARY_FOUND_HAS_WRONG_DOMAIN); |
| 158 return false; | 167 return false; |
| 159 } | 168 } |
| 160 if (!ports_.empty() | 169 if (!ports_.empty() |
| 161 && 0 == ports_.count(referring_url.EffectiveIntPort())) { | 170 && 0 == ports_.count(referring_url.EffectiveIntPort())) { |
| 162 SdchErrorRecovery(DICTIONARY_FOUND_HAS_WRONG_PORT_LIST); | 171 SdchErrorRecovery(DICTIONARY_FOUND_HAS_WRONG_PORT_LIST); |
| 163 return false; | 172 return false; |
| 164 } | 173 } |
| 165 if (path_.size() && !PathMatch(referring_url.path(), path_)) { | 174 if (path_.size() && !PathMatch(referring_url.path(), path_)) { |
| 166 SdchErrorRecovery(DICTIONARY_FOUND_HAS_WRONG_PATH); | 175 SdchErrorRecovery(DICTIONARY_FOUND_HAS_WRONG_PATH); |
| 167 return false; | 176 return false; |
| 168 } | 177 } |
| 169 if (referring_url.SchemeIsSecure()) { | 178 if (!SdchManager::secure_scheme_supported() && |
| 179 referring_url.SchemeIsSecure()) { |
| 180 SdchErrorRecovery(DICTIONARY_FOUND_HAS_WRONG_SCHEME); |
| 181 return false; |
| 182 } |
| 183 if (referring_url.SchemeIsSecure() && !url_.SchemeIsSecure()) { |
| 170 SdchErrorRecovery(DICTIONARY_FOUND_HAS_WRONG_SCHEME); | 184 SdchErrorRecovery(DICTIONARY_FOUND_HAS_WRONG_SCHEME); |
| 171 return false; | 185 return false; |
| 172 } | 186 } |
| 173 | 187 |
| 174 // TODO(jar): Remove overly restrictive failsafe test (added per security | 188 // TODO(jar): Remove overly restrictive failsafe test (added per security |
| 175 // review) when we have a need to be more general. | 189 // review) when we have a need to be more general. |
| 176 if (!referring_url.SchemeIs("http")) { | 190 if (!referring_url.SchemeIsHTTPOrHTTPS()) { |
| 177 SdchErrorRecovery(ATTEMPT_TO_DECODE_NON_HTTP_DATA); | 191 SdchErrorRecovery(ATTEMPT_TO_DECODE_NON_HTTP_DATA); |
| 178 return false; | 192 return false; |
| 179 } | 193 } |
| 180 | 194 |
| 181 return true; | 195 return true; |
| 182 } | 196 } |
| 183 | 197 |
| 184 bool SdchManager::Dictionary::PathMatch(const std::string& path, | 198 bool SdchManager::Dictionary::PathMatch(const std::string& path, |
| 185 const std::string& restriction) { | 199 const std::string& restriction) { |
| 186 /* Must be either: | 200 /* Must be either: |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 DCHECK(CalledOnValidThread()); | 259 DCHECK(CalledOnValidThread()); |
| 246 fetcher_.reset(fetcher); | 260 fetcher_.reset(fetcher); |
| 247 } | 261 } |
| 248 | 262 |
| 249 // static | 263 // static |
| 250 void SdchManager::EnableSdchSupport(bool enabled) { | 264 void SdchManager::EnableSdchSupport(bool enabled) { |
| 251 g_sdch_enabled_ = enabled; | 265 g_sdch_enabled_ = enabled; |
| 252 } | 266 } |
| 253 | 267 |
| 254 // static | 268 // static |
| 269 void SdchManager::EnableSecureSchemeSupport(bool enabled) { |
| 270 g_secure_scheme_supported_ = enabled; |
| 271 } |
| 272 |
| 273 // static |
| 255 void SdchManager::BlacklistDomain(const GURL& url) { | 274 void SdchManager::BlacklistDomain(const GURL& url) { |
| 256 if (!global_ ) | 275 if (!global_ ) |
| 257 return; | 276 return; |
| 258 global_->SetAllowLatencyExperiment(url, false); | 277 global_->SetAllowLatencyExperiment(url, false); |
| 259 | 278 |
| 260 std::string domain(StringToLowerASCII(url.host())); | 279 std::string domain(StringToLowerASCII(url.host())); |
| 261 int count = global_->blacklisted_domains_[domain]; | 280 int count = global_->blacklisted_domains_[domain]; |
| 262 if (count > 0) | 281 if (count > 0) |
| 263 return; // Domain is already blacklisted. | 282 return; // Domain is already blacklisted. |
| 264 | 283 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 if (SdchManager::Global()->CanFetchDictionary(request_url, dictionary_url) && | 356 if (SdchManager::Global()->CanFetchDictionary(request_url, dictionary_url) && |
| 338 fetcher_.get()) | 357 fetcher_.get()) |
| 339 fetcher_->Schedule(dictionary_url); | 358 fetcher_->Schedule(dictionary_url); |
| 340 } | 359 } |
| 341 | 360 |
| 342 bool SdchManager::CanFetchDictionary(const GURL& referring_url, | 361 bool SdchManager::CanFetchDictionary(const GURL& referring_url, |
| 343 const GURL& dictionary_url) const { | 362 const GURL& dictionary_url) const { |
| 344 DCHECK(CalledOnValidThread()); | 363 DCHECK(CalledOnValidThread()); |
| 345 /* The user agent may retrieve a dictionary from the dictionary URL if all of | 364 /* The user agent may retrieve a dictionary from the dictionary URL if all of |
| 346 the following are true: | 365 the following are true: |
| 347 1 The dictionary URL host name matches the referrer URL host name | 366 1 The dictionary URL host name matches the referrer URL host name and |
| 367 scheme. |
| 348 2 The dictionary URL host name domain matches the parent domain of the | 368 2 The dictionary URL host name domain matches the parent domain of the |
| 349 referrer URL host name | 369 referrer URL host name |
| 350 3 The parent domain of the referrer URL host name is not a top level | 370 3 The parent domain of the referrer URL host name is not a top level |
| 351 domain | 371 domain |
| 352 4 The dictionary URL is not an HTTPS URL. | 372 4 The dictionary URL is not an HTTPS URL. |
| 353 */ | 373 */ |
| 354 // Item (1) above implies item (2). Spec should be updated. | 374 // Item (1) above implies item (2). Spec should be updated. |
| 355 // I take "host name match" to be "is identical to" | 375 // I take "host name match" to be "is identical to" |
| 356 if (referring_url.host() != dictionary_url.host()) { | 376 if (referring_url.host() != dictionary_url.host() || |
| 377 referring_url.scheme() != dictionary_url.scheme()) { |
| 357 SdchErrorRecovery(DICTIONARY_LOAD_ATTEMPT_FROM_DIFFERENT_HOST); | 378 SdchErrorRecovery(DICTIONARY_LOAD_ATTEMPT_FROM_DIFFERENT_HOST); |
| 358 return false; | 379 return false; |
| 359 } | 380 } |
| 360 if (referring_url.SchemeIs("https")) { | 381 if (!secure_scheme_supported() && referring_url.SchemeIsSecure()) { |
| 361 SdchErrorRecovery(DICTIONARY_SELECTED_FOR_SSL); | 382 SdchErrorRecovery(DICTIONARY_SELECTED_FOR_SSL); |
| 362 return false; | 383 return false; |
| 363 } | 384 } |
| 364 | 385 |
| 365 // TODO(jar): Remove this failsafe conservative hack which is more restrictive | 386 // TODO(jar): Remove this failsafe conservative hack which is more restrictive |
| 366 // than current SDCH spec when needed, and justified by security audit. | 387 // than current SDCH spec when needed, and justified by security audit. |
| 367 if (!referring_url.SchemeIs("http")) { | 388 if (!referring_url.SchemeIsHTTPOrHTTPS()) { |
| 368 SdchErrorRecovery(DICTIONARY_SELECTED_FROM_NON_HTTP); | 389 SdchErrorRecovery(DICTIONARY_SELECTED_FROM_NON_HTTP); |
| 369 return false; | 390 return false; |
| 370 } | 391 } |
| 371 | 392 |
| 372 return true; | 393 return true; |
| 373 } | 394 } |
| 374 | 395 |
| 375 bool SdchManager::AddSdchDictionary(const std::string& dictionary_text, | 396 bool SdchManager::AddSdchDictionary(const std::string& dictionary_text, |
| 376 const GURL& dictionary_url) { | 397 const GURL& dictionary_url) { |
| 377 DCHECK(CalledOnValidThread()); | 398 DCHECK(CalledOnValidThread()); |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 557 case '/': | 578 case '/': |
| 558 (*output)[i] = '_'; | 579 (*output)[i] = '_'; |
| 559 continue; | 580 continue; |
| 560 default: | 581 default: |
| 561 continue; | 582 continue; |
| 562 } | 583 } |
| 563 } | 584 } |
| 564 } | 585 } |
| 565 | 586 |
| 566 } // namespace net | 587 } // namespace net |
| OLD | NEW |