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
3 // found in the LICENSE file.
5 #include "net/base/sdch_manager.h"
7 #include "base/base64.h"
8 #include "base/logging.h"
9 #include "base/metrics/histogram.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_util.h"
12 #include "crypto/sha2.h"
13 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
14 #include "net/url_request/url_request_http_job.h"
18 //------------------------------------------------------------------------------
21 // Adjust SDCH limits downwards for mobile.
22 #if defined(OS_ANDROID) || defined(OS_IOS)
24 const size_t SdchManager::kMaxDictionaryCount = 1;
25 const size_t SdchManager::kMaxDictionarySize = 150 * 1000;
28 const size_t SdchManager::kMaxDictionaryCount = 20;
29 const size_t SdchManager::kMaxDictionarySize = 1000 * 1000;
33 bool SdchManager::g_sdch_enabled_ = true;
36 bool SdchManager::g_secure_scheme_supported_ = false;
38 //------------------------------------------------------------------------------
39 SdchManager::Dictionary::Dictionary(const std::string& dictionary_text,
41 const std::string& client_hash,
43 const std::string& domain,
44 const std::string& path,
45 const base::Time& expiration,
46 const std::set<int>& ports)
47 : text_(dictionary_text, offset),
48 client_hash_(client_hash),
52 expiration_(expiration),
56 SdchManager::Dictionary::~Dictionary() {
59 bool SdchManager::Dictionary::CanAdvertise(const GURL& target_url) {
60 /* The specific rules of when a dictionary should be advertised in an
61 Avail-Dictionary header are modeled after the rules for cookie scoping. The
62 terms "domain-match" and "pathmatch" are defined in RFC 2965 [6]. A
63 dictionary may be advertised in the Avail-Dictionaries header exactly when
64 all of the following are true:
65 1. The server's effective host name domain-matches the Domain attribute of
67 2. If the dictionary has a Port attribute, the request port is one of the
68 ports listed in the Port attribute.
69 3. The request URI path-matches the path header of the dictionary.
70 4. The request is not an HTTPS request.
71 We can override (ignore) item (4) only when we have explicitly enabled
72 HTTPS support AND dictionary has been acquired over HTTPS.
74 if (!DomainMatch(target_url, domain_))
76 if (!ports_.empty() && 0 == ports_.count(target_url.EffectiveIntPort()))
78 if (path_.size() && !PathMatch(target_url.path(), path_))
80 if (!SdchManager::secure_scheme_supported() && target_url.SchemeIsSecure())
82 if (target_url.SchemeIsSecure() && !url_.SchemeIsSecure())
84 if (base::Time::Now() > expiration_)
89 //------------------------------------------------------------------------------
90 // Security functions restricting loads and use of dictionaries.
93 bool SdchManager::Dictionary::CanSet(const std::string& domain,
94 const std::string& path,
95 const std::set<int>& ports,
96 const GURL& dictionary_url) {
98 A dictionary is invalid and must not be stored if any of the following are
100 1. The dictionary has no Domain attribute.
101 2. The effective host name that derives from the referer URL host name does
102 not domain-match the Domain attribute.
103 3. The Domain attribute is a top level domain.
104 4. The referer URL host is a host domain name (not IP address) and has the
105 form HD, where D is the value of the Domain attribute, and H is a string
106 that contains one or more dots.
107 5. If the dictionary has a Port attribute and the referer URL's port was not
111 // TODO(jar): Redirects in dictionary fetches might plausibly be problematic,
112 // and hence the conservative approach is to not allow any redirects (if there
113 // were any... then don't allow the dictionary to be set).
115 if (domain.empty()) {
116 SdchErrorRecovery(DICTIONARY_MISSING_DOMAIN_SPECIFIER);
117 return false; // Domain is required.
119 if (registry_controlled_domains::GetDomainAndRegistry(
121 registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES).empty()) {
122 SdchErrorRecovery(DICTIONARY_SPECIFIES_TOP_LEVEL_DOMAIN);
123 return false; // domain was a TLD.
125 if (!Dictionary::DomainMatch(dictionary_url, domain)) {
126 SdchErrorRecovery(DICTIONARY_DOMAIN_NOT_MATCHING_SOURCE_URL);
130 std::string referrer_url_host = dictionary_url.host();
131 size_t postfix_domain_index = referrer_url_host.rfind(domain);
132 // See if it is indeed a postfix, or just an internal string.
133 if (referrer_url_host.size() == postfix_domain_index + domain.size()) {
134 // It is a postfix... so check to see if there's a dot in the prefix.
135 size_t end_of_host_index = referrer_url_host.find_first_of('.');
136 if (referrer_url_host.npos != end_of_host_index &&
137 end_of_host_index < postfix_domain_index) {
138 SdchErrorRecovery(DICTIONARY_REFERER_URL_HAS_DOT_IN_PREFIX);
144 && 0 == ports.count(dictionary_url.EffectiveIntPort())) {
145 SdchErrorRecovery(DICTIONARY_PORT_NOT_MATCHING_SOURCE_URL);
152 bool SdchManager::Dictionary::CanUse(const GURL& referring_url) {
154 1. The request URL's host name domain-matches the Domain attribute of the
156 2. If the dictionary has a Port attribute, the request port is one of the
157 ports listed in the Port attribute.
158 3. The request URL path-matches the path attribute of the dictionary.
159 4. The request is not an HTTPS request.
160 We can override (ignore) item (4) only when we have explicitly enabled
161 HTTPS support AND dictionary has been acquired over HTTPS.
163 if (!DomainMatch(referring_url, domain_)) {
164 SdchErrorRecovery(DICTIONARY_FOUND_HAS_WRONG_DOMAIN);
168 && 0 == ports_.count(referring_url.EffectiveIntPort())) {
169 SdchErrorRecovery(DICTIONARY_FOUND_HAS_WRONG_PORT_LIST);
172 if (path_.size() && !PathMatch(referring_url.path(), path_)) {
173 SdchErrorRecovery(DICTIONARY_FOUND_HAS_WRONG_PATH);
176 if (!SdchManager::secure_scheme_supported() &&
177 referring_url.SchemeIsSecure()) {
178 SdchErrorRecovery(DICTIONARY_FOUND_HAS_WRONG_SCHEME);
181 if (referring_url.SchemeIsSecure() && !url_.SchemeIsSecure()) {
182 SdchErrorRecovery(DICTIONARY_FOUND_HAS_WRONG_SCHEME);
186 // TODO(jar): Remove overly restrictive failsafe test (added per security
187 // review) when we have a need to be more general.
188 if (!referring_url.SchemeIsHTTPOrHTTPS()) {
189 SdchErrorRecovery(ATTEMPT_TO_DECODE_NON_HTTP_DATA);
196 bool SdchManager::Dictionary::PathMatch(const std::string& path,
197 const std::string& restriction) {
200 2. P2 is a prefix of P1 and either the final character in P2 is "/" or the
201 character following P2 in P1 is "/".
203 if (path == restriction)
205 size_t prefix_length = restriction.size();
206 if (prefix_length > path.size())
207 return false; // Can't be a prefix.
208 if (0 != path.compare(0, prefix_length, restriction))
210 return restriction[prefix_length - 1] == '/' || path[prefix_length] == '/';
214 bool SdchManager::Dictionary::DomainMatch(const GURL& gurl,
215 const std::string& restriction) {
216 // TODO(jar): This is not precisely a domain match definition.
217 return gurl.DomainIs(restriction.data(), restriction.size());
220 //------------------------------------------------------------------------------
221 SdchManager::SdchManager() {
222 DCHECK(CalledOnValidThread());
225 SdchManager::~SdchManager() {
226 DCHECK(CalledOnValidThread());
227 while (!dictionaries_.empty()) {
228 DictionaryMap::iterator it = dictionaries_.begin();
229 dictionaries_.erase(it->first);
233 void SdchManager::ClearData() {
234 blacklisted_domains_.clear();
235 exponential_blacklist_count_.clear();
236 allow_latency_experiment_.clear();
240 // Note that this may result in not having dictionaries we've advertised
241 // for incoming responses. The window is relatively small (as ClearData()
242 // is not expected to be called frequently), so we rely on meta-refresh
243 // to handle this case.
244 dictionaries_.clear();
248 void SdchManager::SdchErrorRecovery(ProblemCodes problem) {
249 UMA_HISTOGRAM_ENUMERATION("Sdch3.ProblemCodes_4", problem, MAX_PROBLEM_CODE);
252 void SdchManager::set_sdch_fetcher(SdchFetcher* fetcher) {
253 DCHECK(CalledOnValidThread());
254 fetcher_.reset(fetcher);
258 void SdchManager::EnableSdchSupport(bool enabled) {
259 g_sdch_enabled_ = enabled;
263 void SdchManager::EnableSecureSchemeSupport(bool enabled) {
264 g_secure_scheme_supported_ = enabled;
267 void SdchManager::BlacklistDomain(const GURL& url) {
268 SetAllowLatencyExperiment(url, false);
270 std::string domain(StringToLowerASCII(url.host()));
271 int count = blacklisted_domains_[domain];
273 return; // Domain is already blacklisted.
275 count = 1 + 2 * exponential_blacklist_count_[domain];
277 exponential_blacklist_count_[domain] = count;
281 blacklisted_domains_[domain] = count;
284 void SdchManager::BlacklistDomainForever(const GURL& url) {
285 SetAllowLatencyExperiment(url, false);
287 std::string domain(StringToLowerASCII(url.host()));
288 exponential_blacklist_count_[domain] = INT_MAX;
289 blacklisted_domains_[domain] = INT_MAX;
292 void SdchManager::ClearBlacklistings() {
293 blacklisted_domains_.clear();
294 exponential_blacklist_count_.clear();
297 void SdchManager::ClearDomainBlacklisting(const std::string& domain) {
298 blacklisted_domains_.erase(StringToLowerASCII(domain));
301 int SdchManager::BlackListDomainCount(const std::string& domain) {
302 if (blacklisted_domains_.end() == blacklisted_domains_.find(domain))
304 return blacklisted_domains_[StringToLowerASCII(domain)];
307 int SdchManager::BlacklistDomainExponential(const std::string& domain) {
308 if (exponential_blacklist_count_.end() ==
309 exponential_blacklist_count_.find(domain))
311 return exponential_blacklist_count_[StringToLowerASCII(domain)];
314 bool SdchManager::IsInSupportedDomain(const GURL& url) {
315 DCHECK(CalledOnValidThread());
316 if (!g_sdch_enabled_ )
319 if (!secure_scheme_supported() && url.SchemeIsSecure())
322 if (blacklisted_domains_.empty())
325 std::string domain(StringToLowerASCII(url.host()));
326 DomainCounter::iterator it = blacklisted_domains_.find(domain);
327 if (blacklisted_domains_.end() == it)
330 int count = it->second - 1;
332 blacklisted_domains_[domain] = count;
334 blacklisted_domains_.erase(domain);
335 SdchErrorRecovery(DOMAIN_BLACKLIST_INCLUDES_TARGET);
339 void SdchManager::FetchDictionary(const GURL& request_url,
340 const GURL& dictionary_url) {
341 DCHECK(CalledOnValidThread());
342 if (CanFetchDictionary(request_url, dictionary_url) && fetcher_.get())
343 fetcher_->Schedule(dictionary_url);
346 bool SdchManager::CanFetchDictionary(const GURL& referring_url,
347 const GURL& dictionary_url) const {
348 DCHECK(CalledOnValidThread());
349 /* The user agent may retrieve a dictionary from the dictionary URL if all of
350 the following are true:
351 1 The dictionary URL host name matches the referrer URL host name and
353 2 The dictionary URL host name domain matches the parent domain of the
354 referrer URL host name
355 3 The parent domain of the referrer URL host name is not a top level
357 4 The dictionary URL is not an HTTPS URL.
359 // Item (1) above implies item (2). Spec should be updated.
360 // I take "host name match" to be "is identical to"
361 if (referring_url.host() != dictionary_url.host() ||
362 referring_url.scheme() != dictionary_url.scheme()) {
363 SdchErrorRecovery(DICTIONARY_LOAD_ATTEMPT_FROM_DIFFERENT_HOST);
366 if (!secure_scheme_supported() && referring_url.SchemeIsSecure()) {
367 SdchErrorRecovery(DICTIONARY_SELECTED_FOR_SSL);
371 // TODO(jar): Remove this failsafe conservative hack which is more restrictive
372 // than current SDCH spec when needed, and justified by security audit.
373 if (!referring_url.SchemeIsHTTPOrHTTPS()) {
374 SdchErrorRecovery(DICTIONARY_SELECTED_FROM_NON_HTTP);
381 bool SdchManager::AddSdchDictionary(const std::string& dictionary_text,
382 const GURL& dictionary_url) {
383 DCHECK(CalledOnValidThread());
384 std::string client_hash;
385 std::string server_hash;
386 GenerateHash(dictionary_text, &client_hash, &server_hash);
387 if (dictionaries_.find(server_hash) != dictionaries_.end()) {
388 SdchErrorRecovery(DICTIONARY_ALREADY_LOADED);
389 return false; // Already loaded.
392 std::string domain, path;
394 base::Time expiration(base::Time::Now() + base::TimeDelta::FromDays(30));
396 if (dictionary_text.empty()) {
397 SdchErrorRecovery(DICTIONARY_HAS_NO_TEXT);
398 return false; // Missing header.
401 size_t header_end = dictionary_text.find("\n\n");
402 if (std::string::npos == header_end) {
403 SdchErrorRecovery(DICTIONARY_HAS_NO_HEADER);
404 return false; // Missing header.
406 size_t line_start = 0; // Start of line being parsed.
408 size_t line_end = dictionary_text.find('\n', line_start);
409 DCHECK(std::string::npos != line_end);
410 DCHECK_LE(line_end, header_end);
412 size_t colon_index = dictionary_text.find(':', line_start);
413 if (std::string::npos == colon_index) {
414 SdchErrorRecovery(DICTIONARY_HEADER_LINE_MISSING_COLON);
415 return false; // Illegal line missing a colon.
418 if (colon_index > line_end)
421 size_t value_start = dictionary_text.find_first_not_of(" \t",
423 if (std::string::npos != value_start) {
424 if (value_start >= line_end)
426 std::string name(dictionary_text, line_start, colon_index - line_start);
427 std::string value(dictionary_text, value_start, line_end - value_start);
428 name = StringToLowerASCII(name);
429 if (name == "domain") {
431 } else if (name == "path") {
433 } else if (name == "format-version") {
436 } else if (name == "max-age") {
438 base::StringToInt64(value, &seconds);
439 expiration = base::Time::Now() + base::TimeDelta::FromSeconds(seconds);
440 } else if (name == "port") {
442 base::StringToInt(value, &port);
448 if (line_end >= header_end)
450 line_start = line_end + 1;
453 if (!IsInSupportedDomain(dictionary_url))
456 if (!Dictionary::CanSet(domain, path, ports, dictionary_url))
459 // TODO(jar): Remove these hacks to preclude a DOS attack involving piles of
460 // useless dictionaries. We should probably have a cache eviction plan,
461 // instead of just blocking additions. For now, with the spec in flux, it
462 // is probably not worth doing eviction handling.
463 if (kMaxDictionarySize < dictionary_text.size()) {
464 SdchErrorRecovery(DICTIONARY_IS_TOO_LARGE);
467 if (kMaxDictionaryCount <= dictionaries_.size()) {
468 SdchErrorRecovery(DICTIONARY_COUNT_EXCEEDED);
472 UMA_HISTOGRAM_COUNTS("Sdch3.Dictionary size loaded", dictionary_text.size());
473 DVLOG(1) << "Loaded dictionary with client hash " << client_hash
474 << " and server hash " << server_hash;
475 Dictionary* dictionary =
476 new Dictionary(dictionary_text, header_end + 2, client_hash,
477 dictionary_url, domain, path, expiration, ports);
478 dictionaries_[server_hash] = dictionary;
482 void SdchManager::GetVcdiffDictionary(
483 const std::string& server_hash,
484 const GURL& referring_url,
485 scoped_refptr<Dictionary>* dictionary) {
486 DCHECK(CalledOnValidThread());
488 DictionaryMap::iterator it = dictionaries_.find(server_hash);
489 if (it == dictionaries_.end()) {
492 scoped_refptr<Dictionary> matching_dictionary = it->second;
493 if (!IsInSupportedDomain(referring_url))
495 if (!matching_dictionary->CanUse(referring_url))
497 *dictionary = matching_dictionary;
500 // TODO(jar): If we have evictions from the dictionaries_, then we need to
501 // change this interface to return a list of reference counted Dictionary
502 // instances that can be used if/when a server specifies one.
503 void SdchManager::GetAvailDictionaryList(const GURL& target_url,
505 DCHECK(CalledOnValidThread());
507 for (DictionaryMap::iterator it = dictionaries_.begin();
508 it != dictionaries_.end(); ++it) {
509 if (!IsInSupportedDomain(target_url))
511 if (!it->second->CanAdvertise(target_url))
516 list->append(it->second->client_hash());
518 // Watch to see if we have corrupt or numerous dictionaries.
520 UMA_HISTOGRAM_COUNTS("Sdch3.Advertisement_Count", count);
524 void SdchManager::GenerateHash(const std::string& dictionary_text,
525 std::string* client_hash, std::string* server_hash) {
526 char binary_hash[32];
527 crypto::SHA256HashString(dictionary_text, binary_hash, sizeof(binary_hash));
529 std::string first_48_bits(&binary_hash[0], 6);
530 std::string second_48_bits(&binary_hash[6], 6);
531 UrlSafeBase64Encode(first_48_bits, client_hash);
532 UrlSafeBase64Encode(second_48_bits, server_hash);
534 DCHECK_EQ(server_hash->length(), 8u);
535 DCHECK_EQ(client_hash->length(), 8u);
538 //------------------------------------------------------------------------------
539 // Methods for supporting latency experiments.
541 bool SdchManager::AllowLatencyExperiment(const GURL& url) const {
542 DCHECK(CalledOnValidThread());
543 return allow_latency_experiment_.end() !=
544 allow_latency_experiment_.find(url.host());
547 void SdchManager::SetAllowLatencyExperiment(const GURL& url, bool enable) {
548 DCHECK(CalledOnValidThread());
550 allow_latency_experiment_.insert(url.host());
553 ExperimentSet::iterator it = allow_latency_experiment_.find(url.host());
554 if (allow_latency_experiment_.end() == it)
555 return; // It was already erased, or never allowed.
556 SdchErrorRecovery(LATENCY_TEST_DISALLOWED);
557 allow_latency_experiment_.erase(it);
561 void SdchManager::UrlSafeBase64Encode(const std::string& input,
562 std::string* output) {
563 // Since this is only done during a dictionary load, and hashes are only 8
564 // characters, we just do the simple fixup, rather than rewriting the encoder.
565 base::Base64Encode(input, output);
566 for (size_t i = 0; i < output->size(); ++i) {
567 switch (output->data()[i]) {