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/cookies/cookie_util.h" | 5 #include "net/cookies/cookie_util.h" |
6 | 6 |
7 #include <cstdio> | 7 #include <cstdio> |
8 #include <cstdlib> | 8 #include <cstdlib> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/strings/string_tokenizer.h" | 11 #include "base/strings/string_tokenizer.h" |
12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
14 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 14 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
15 #include "net/base/url_util.h" | 15 #include "net/base/url_util.h" |
16 #include "url/gurl.h" | 16 #include "url/gurl.h" |
17 | 17 |
18 namespace net { | 18 namespace net { |
19 namespace cookie_util { | 19 namespace cookie_util { |
20 | 20 |
| 21 namespace { |
| 22 |
| 23 base::Time MinNonNullTime() { |
| 24 return base::Time::FromInternalValue(1); |
| 25 } |
| 26 |
| 27 // Tries to assemble a base::Time given a base::Time::Exploded representing a |
| 28 // UTC calendar date. |
| 29 // |
| 30 // If the date falls outside of the range supported internally by |
| 31 // FromUTCExploded() on the current platform, then the result is: |
| 32 // |
| 33 // * Time(1) if it's below the range FromUTCExploded() supports. |
| 34 // * Time::Max() if it's above the range FromUTCExploded() supports. |
| 35 bool SaturatedTimeFromUTCExploded(const base::Time::Exploded& exploded, |
| 36 base::Time* out) { |
| 37 // Try to calculate the base::Time in the normal fashion. |
| 38 if (base::Time::FromUTCExploded(exploded, out)) { |
| 39 // Don't return Time(0) on success. |
| 40 if (out->is_null()) |
| 41 *out = MinNonNullTime(); |
| 42 return true; |
| 43 } |
| 44 |
| 45 // base::Time::FromUTCExploded() has platform-specific limits: |
| 46 // |
| 47 // * Windows: Years 1601 - 30827 |
| 48 // * 32-bit POSIX: Years 1970 - 2038 |
| 49 // |
| 50 // Work around this by returning min/max valid times for times outside those |
| 51 // ranges when imploding the time is doomed to fail. |
| 52 // |
| 53 // Note that the following implementation is NOT perfect. It will accept |
| 54 // some invalid calendar dates in the out-of-range case. |
| 55 if (!exploded.HasValidValues()) |
| 56 return false; |
| 57 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
| 58 // Allow dates prior to unix epoch (which fail on non-Mac/iOS POSIX). |
| 59 if (exploded.year < 1970) { |
| 60 *out = MinNonNullTime(); |
| 61 return true; |
| 62 } |
| 63 |
| 64 // On 32-bit non-Mac/iOS POSIX systems, the time_t value that FromExploded() |
| 65 // returns overflows in the middle of year 2038. In that case, return |
| 66 // Time::Max(). |
| 67 if (sizeof(time_t) == 4u && exploded.year >= 2038) { |
| 68 *out = base::Time::Max(); |
| 69 return true; |
| 70 } |
| 71 #endif // defined(OS_POSIX) && !defined(OS_MACOSX) |
| 72 |
| 73 #if defined(OS_WIN) |
| 74 // Allow dates prior to Windows epoch. |
| 75 if (exploded.year < 1601) { |
| 76 *out = MinNonNullTime(); |
| 77 return true; |
| 78 } |
| 79 |
| 80 // Allow dates after the Windows epoch. |
| 81 if (exploded.year >= 30827) { |
| 82 *out = base::Time::Max(); |
| 83 return true; |
| 84 } |
| 85 #endif // defined(OS_WIN) |
| 86 |
| 87 return false; |
| 88 } |
| 89 |
| 90 } // namespace |
| 91 |
21 bool DomainIsHostOnly(const std::string& domain_string) { | 92 bool DomainIsHostOnly(const std::string& domain_string) { |
22 return (domain_string.empty() || domain_string[0] != '.'); | 93 return (domain_string.empty() || domain_string[0] != '.'); |
23 } | 94 } |
24 | 95 |
25 std::string GetEffectiveDomain(const std::string& scheme, | 96 std::string GetEffectiveDomain(const std::string& scheme, |
26 const std::string& host) { | 97 const std::string& host) { |
27 if (scheme == "http" || scheme == "https" || scheme == "ws" || | 98 if (scheme == "http" || scheme == "https" || scheme == "ws" || |
28 scheme == "wss") { | 99 scheme == "wss") { |
29 return registry_controlled_domains::GetDomainAndRegistry( | 100 return registry_controlled_domains::GetDomainAndRegistry( |
30 host, | 101 host, |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 } | 167 } |
97 | 168 |
98 // Parse a cookie expiration time. We try to be lenient, but we need to | 169 // Parse a cookie expiration time. We try to be lenient, but we need to |
99 // assume some order to distinguish the fields. The basic rules: | 170 // assume some order to distinguish the fields. The basic rules: |
100 // - The month name must be present and prefix the first 3 letters of the | 171 // - The month name must be present and prefix the first 3 letters of the |
101 // full month name (jan for January, jun for June). | 172 // full month name (jan for January, jun for June). |
102 // - If the year is <= 2 digits, it must occur after the day of month. | 173 // - If the year is <= 2 digits, it must occur after the day of month. |
103 // - The time must be of the format hh:mm:ss. | 174 // - The time must be of the format hh:mm:ss. |
104 // An average cookie expiration will look something like this: | 175 // An average cookie expiration will look something like this: |
105 // Sat, 15-Apr-17 21:01:22 GMT | 176 // Sat, 15-Apr-17 21:01:22 GMT |
106 base::Time ParseCookieTime(const std::string& time_string) { | 177 base::Time ParseCookieExpirationTime(const std::string& time_string) { |
107 static const char* const kMonths[] = { | 178 static const char* const kMonths[] = { |
108 "jan", "feb", "mar", "apr", "may", "jun", | 179 "jan", "feb", "mar", "apr", "may", "jun", |
109 "jul", "aug", "sep", "oct", "nov", "dec" }; | 180 "jul", "aug", "sep", "oct", "nov", "dec" }; |
110 static const int kMonthsLen = arraysize(kMonths); | 181 static const int kMonthsLen = arraysize(kMonths); |
111 // We want to be pretty liberal, and support most non-ascii and non-digit | 182 // We want to be pretty liberal, and support most non-ascii and non-digit |
112 // characters as a delimiter. We can't treat : as a delimiter, because it | 183 // characters as a delimiter. We can't treat : as a delimiter, because it |
113 // is the delimiter for hh:mm:ss, and we want to keep this field together. | 184 // is the delimiter for hh:mm:ss, and we want to keep this field together. |
114 // We make sure to include - and +, since they could prefix numbers. | 185 // We make sure to include - and +, since they could prefix numbers. |
115 // If the cookie attribute came in in quotes (ex expires="XXX"), the quotes | 186 // If the cookie attribute came in in quotes (ex expires="XXX"), the quotes |
116 // will be preserved, and we will get them here. So we make sure to include | 187 // will be preserved, and we will get them here. So we make sure to include |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 // NOTREACHED() << "Cookie parse expiration failed: " << time_string; | 264 // NOTREACHED() << "Cookie parse expiration failed: " << time_string; |
194 return base::Time(); | 265 return base::Time(); |
195 } | 266 } |
196 | 267 |
197 // Normalize the year to expand abbreviated years to the full year. | 268 // Normalize the year to expand abbreviated years to the full year. |
198 if (exploded.year >= 69 && exploded.year <= 99) | 269 if (exploded.year >= 69 && exploded.year <= 99) |
199 exploded.year += 1900; | 270 exploded.year += 1900; |
200 if (exploded.year >= 0 && exploded.year <= 68) | 271 if (exploded.year >= 0 && exploded.year <= 68) |
201 exploded.year += 2000; | 272 exploded.year += 2000; |
202 | 273 |
203 // If our values are within their correct ranges, we got our time. | 274 // Note that clipping the date if it is outside of a platform-specific range |
204 if (exploded.day_of_month >= 1 && exploded.day_of_month <= 31 && | 275 // is permitted by: https://tools.ietf.org/html/rfc6265#section-5.2.1 |
205 exploded.month >= 1 && exploded.month <= 12 && | 276 base::Time result; |
206 exploded.year >= 1601 && exploded.year <= 30827 && | 277 if (SaturatedTimeFromUTCExploded(exploded, &result)) |
207 exploded.hour <= 23 && exploded.minute <= 59 && exploded.second <= 59) { | 278 return result; |
208 return base::Time::FromUTCExploded(exploded); | |
209 } | |
210 | 279 |
211 // One of our values was out of expected range. For well-formed input, | 280 // One of our values was out of expected range. For well-formed input, |
212 // the following check would be reasonable: | 281 // the following check would be reasonable: |
213 // NOTREACHED() << "Cookie exploded expiration failed: " << time_string; | 282 // NOTREACHED() << "Cookie exploded expiration failed: " << time_string; |
214 | 283 |
215 return base::Time(); | 284 return base::Time(); |
216 } | 285 } |
217 | 286 |
218 GURL CookieOriginToURL(const std::string& domain, bool is_https) { | 287 GURL CookieOriginToURL(const std::string& domain, bool is_https) { |
219 if (domain.empty()) | 288 if (domain.empty()) |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 buffer.append("; "); | 342 buffer.append("; "); |
274 buffer.append(i->first.begin(), i->first.end()); | 343 buffer.append(i->first.begin(), i->first.end()); |
275 buffer.push_back('='); | 344 buffer.push_back('='); |
276 buffer.append(i->second.begin(), i->second.end()); | 345 buffer.append(i->second.begin(), i->second.end()); |
277 } | 346 } |
278 return buffer; | 347 return buffer; |
279 } | 348 } |
280 | 349 |
281 } // namespace cookie_util | 350 } // namespace cookie_util |
282 } // namespace net | 351 } // namespace net |
OLD | NEW |