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 // Portions of this code based on Mozilla: | 5 // Portions of this code based on Mozilla: |
6 // (netwerk/cookie/src/nsCookieService.cpp) | 6 // (netwerk/cookie/src/nsCookieService.cpp) |
7 /* ***** BEGIN LICENSE BLOCK ***** | 7 /* ***** BEGIN LICENSE BLOCK ***** |
8 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 | 8 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
9 * | 9 * |
10 * The contents of this file are subject to the Mozilla Public License Version | 10 * The contents of this file are subject to the Mozilla Public License Version |
(...skipping 29 matching lines...) Expand all Loading... | |
40 * the provisions above, a recipient may use your version of this file under | 40 * the provisions above, a recipient may use your version of this file under |
41 * the terms of any one of the MPL, the GPL or the LGPL. | 41 * the terms of any one of the MPL, the GPL or the LGPL. |
42 * | 42 * |
43 * ***** END LICENSE BLOCK ***** */ | 43 * ***** END LICENSE BLOCK ***** */ |
44 | 44 |
45 #include "net/cookies/canonical_cookie.h" | 45 #include "net/cookies/canonical_cookie.h" |
46 | 46 |
47 #include "base/format_macros.h" | 47 #include "base/format_macros.h" |
48 #include "base/logging.h" | 48 #include "base/logging.h" |
49 #include "base/metrics/histogram_macros.h" | 49 #include "base/metrics/histogram_macros.h" |
50 #include "base/strings/string_util.h" | |
50 #include "base/strings/stringprintf.h" | 51 #include "base/strings/stringprintf.h" |
51 #include "net/cookies/cookie_util.h" | 52 #include "net/cookies/cookie_util.h" |
52 #include "net/cookies/parsed_cookie.h" | 53 #include "net/cookies/parsed_cookie.h" |
53 #include "url/gurl.h" | 54 #include "url/gurl.h" |
54 #include "url/url_canon.h" | 55 #include "url/url_canon.h" |
55 | 56 |
56 using base::Time; | 57 using base::Time; |
57 using base::TimeDelta; | 58 using base::TimeDelta; |
58 | 59 |
59 namespace net { | 60 namespace net { |
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
342 // would also make no sense for our prefix match. The code that | 343 // would also make no sense for our prefix match. The code that |
343 // creates a CanonicalCookie should make sure the path is never zero length, | 344 // creates a CanonicalCookie should make sure the path is never zero length, |
344 // but we double check anyway. | 345 // but we double check anyway. |
345 if (path_.empty()) | 346 if (path_.empty()) |
346 return false; | 347 return false; |
347 | 348 |
348 // The Mozilla code broke this into three cases, based on if the cookie path | 349 // The Mozilla code broke this into three cases, based on if the cookie path |
349 // was longer, the same length, or shorter than the length of the url path. | 350 // was longer, the same length, or shorter than the length of the url path. |
350 // I think the approach below is simpler. | 351 // I think the approach below is simpler. |
351 | 352 |
352 // Make sure the cookie path is a prefix of the url path. If the | 353 // Make sure the cookie path is a prefix of the url path. If the url path is |
353 // url path is shorter than the cookie path, then the cookie path | 354 // shorter than the cookie path, then the cookie path can't be a prefix. |
354 // can't be a prefix. | 355 if (!base::StartsWith(url_path, path_, base::CompareCase::SENSITIVE)) |
mmenke
2016/03/02 20:02:01
Does this really mean that you can set cookies wit
Mike West
2016/03/03 18:56:28
That sounds wrong. The path matching algorithm at
mmenke
2016/03/03 19:02:58
Sorry, that should be "/bar/" and "/bar"... and w
| |
355 if (url_path.find(path_) != 0) | |
356 return false; | 356 return false; |
357 | 357 |
358 // Now we know that url_path is >= cookie_path, and that cookie_path | 358 // |url_path| is >= |path_|, and |path_| is a prefix of |url_path|. If they |
359 // is a prefix of url_path. If they are the are the same length then | 359 // are the are the same length then they are identical, otherwise need an |
360 // they are identical, otherwise we need an additional check: | 360 // additional check: |
361 | 361 |
362 // In order to avoid in correctly matching a cookie path of /blah | 362 // In order to avoid in correctly matching a cookie path of /blah |
363 // with a request path of '/blahblah/', we need to make sure that either | 363 // with a request path of '/blahblah/', we need to make sure that either |
364 // the cookie path ends in a trailing '/', or that we prefix up to a '/' | 364 // the cookie path ends in a trailing '/', or that we prefix up to a '/' |
365 // in the url path. Since we know that the url path length is greater | 365 // in the url path. Since we know that the url path length is greater |
366 // than the cookie path length, it's safe to index one byte past. | 366 // than the cookie path length, it's safe to index one byte past. |
367 if (path_.length() != url_path.length() && path_.back() != '/' && | 367 if (path_.length() != url_path.length() && path_.back() != '/' && |
368 url_path[path_.length()] != '/') | 368 url_path[path_.length()] != '/') { |
369 return false; | 369 return false; |
370 } | |
370 | 371 |
371 return true; | 372 return true; |
372 } | 373 } |
373 | 374 |
374 bool CanonicalCookie::IsDomainMatch(const std::string& host) const { | 375 bool CanonicalCookie::IsDomainMatch(const std::string& host) const { |
375 // Can domain match in two ways; as a domain cookie (where the cookie | 376 // Can domain match in two ways; as a domain cookie (where the cookie |
376 // domain begins with ".") or as a host cookie (where it doesn't). | 377 // domain begins with ".") or as a host cookie (where it doesn't). |
377 | 378 |
378 // Some consumers of the CookieMonster expect to set cookies on | 379 // Some consumers of the CookieMonster expect to set cookies on |
379 // URLs like http://.strange.url. To retrieve cookies in this instance, | 380 // URLs like http://.strange.url. To retrieve cookies in this instance, |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
506 if (prefix == CanonicalCookie::COOKIE_PREFIX_SECURE) | 507 if (prefix == CanonicalCookie::COOKIE_PREFIX_SECURE) |
507 return parsed_cookie.IsSecure() && url.SchemeIsCryptographic(); | 508 return parsed_cookie.IsSecure() && url.SchemeIsCryptographic(); |
508 if (prefix == CanonicalCookie::COOKIE_PREFIX_HOST) { | 509 if (prefix == CanonicalCookie::COOKIE_PREFIX_HOST) { |
509 return parsed_cookie.IsSecure() && url.SchemeIsCryptographic() && | 510 return parsed_cookie.IsSecure() && url.SchemeIsCryptographic() && |
510 !parsed_cookie.HasDomain() && parsed_cookie.Path() == "/"; | 511 !parsed_cookie.HasDomain() && parsed_cookie.Path() == "/"; |
511 } | 512 } |
512 return true; | 513 return true; |
513 } | 514 } |
514 | 515 |
515 } // namespace net | 516 } // namespace net |
OLD | NEW |