Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(265)

Side by Side Diff: net/cookies/cookie_util.cc

Issue 2433193002: Revert of When parsing cookie expiration times, saturate out of range dates (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/cookies/cookie_util.h ('k') | net/cookies/cookie_util_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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(), then the result is clamped to the range that
32 // FromUTCExploded() supports on the current platform.
33 bool SaturatedTimeFromUTCExploded(const base::Time::Exploded& exploded,
34 base::Time* out) {
35 // Try to calculate the base::Time in the normal fashion.
36 if (base::Time::FromUTCExploded(exploded, out)) {
37 // Don't return Time(0) on success.
38 if (out->is_null())
39 *out = MinNonNullTime();
40 return true;
41 }
42
43 // base::Time::FromUTCExploded() has platform-specific limits:
44 //
45 // * Windows: Years 1601 - 30827
46 // * 32-bit POSIX: Years 1970 - 2038
47 //
48 // Work around this by clamping values when imploding the time is doomed
49 // to fail.
50 //
51 // Note that the following implementation is NOT perfect. It will accept
52 // some invalid calendar dates in the out-of-range case.
53 if (!exploded.HasValidValues())
54 return false;
55 #if defined(OS_POSIX) && !defined(OS_MACOSX)
56 // Allow dates prior to unix epoch (which fail on non-Mac/iOS POSIX).
57 if (exploded.year < 1970) {
58 *out = base::Time::UnixEpoch();
59 return true;
60 }
61
62 // On 32-bit non-Mac/iOS POSIX systems, the time_t value that FromExploded()
63 // returns overflows in the middle of year 2038. In that case, return the max
64 // value that can be represented by a 32-bit time_t.
65 if (sizeof(time_t) == 4u && exploded.year >= 2038) {
66 *out = base::Time::FromTimeT(std::numeric_limits<time_t>::max());
67 return true;
68 }
69 #endif // defined(OS_POSIX) && !defined(OS_MACOSX)
70
71 #if defined(OS_WIN)
72 // Allow dates prior to Windows epoch.
73 if (exploded.year < 1601) {
74 *out = MinNonNullTime();
75 return true;
76 }
77
78 // Allow dates after the Windows epoch.
79 if (exploded.year >= 30827) {
80 // This is the maximum value a FILETIME can represent, though FromExploded()
81 // does fail on marginally smaller FILETIME values. The division by 10 is
82 // needed because FILETIMEs are in terms of hundreds of nanoseconds.
83 // This relies on base::Time() returning the start of the Windows epoch.
84 *out =
85 base::Time::FromInternalValue(std::numeric_limits<int64_t>::max() / 10);
86 return true;
87 }
88 #endif // defined(OS_WIN)
89
90 return false;
91 }
92
93 } // namespace
94
95 bool DomainIsHostOnly(const std::string& domain_string) { 21 bool DomainIsHostOnly(const std::string& domain_string) {
96 return (domain_string.empty() || domain_string[0] != '.'); 22 return (domain_string.empty() || domain_string[0] != '.');
97 } 23 }
98 24
99 std::string GetEffectiveDomain(const std::string& scheme, 25 std::string GetEffectiveDomain(const std::string& scheme,
100 const std::string& host) { 26 const std::string& host) {
101 if (scheme == "http" || scheme == "https" || scheme == "ws" || 27 if (scheme == "http" || scheme == "https" || scheme == "ws" ||
102 scheme == "wss") { 28 scheme == "wss") {
103 return registry_controlled_domains::GetDomainAndRegistry( 29 return registry_controlled_domains::GetDomainAndRegistry(
104 host, 30 host,
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 } 96 }
171 97
172 // Parse a cookie expiration time. We try to be lenient, but we need to 98 // Parse a cookie expiration time. We try to be lenient, but we need to
173 // assume some order to distinguish the fields. The basic rules: 99 // assume some order to distinguish the fields. The basic rules:
174 // - The month name must be present and prefix the first 3 letters of the 100 // - The month name must be present and prefix the first 3 letters of the
175 // full month name (jan for January, jun for June). 101 // full month name (jan for January, jun for June).
176 // - If the year is <= 2 digits, it must occur after the day of month. 102 // - If the year is <= 2 digits, it must occur after the day of month.
177 // - The time must be of the format hh:mm:ss. 103 // - The time must be of the format hh:mm:ss.
178 // An average cookie expiration will look something like this: 104 // An average cookie expiration will look something like this:
179 // Sat, 15-Apr-17 21:01:22 GMT 105 // Sat, 15-Apr-17 21:01:22 GMT
180 base::Time ParseCookieExpirationTime(const std::string& time_string) { 106 base::Time ParseCookieTime(const std::string& time_string) {
181 static const char* const kMonths[] = { 107 static const char* const kMonths[] = {
182 "jan", "feb", "mar", "apr", "may", "jun", 108 "jan", "feb", "mar", "apr", "may", "jun",
183 "jul", "aug", "sep", "oct", "nov", "dec" }; 109 "jul", "aug", "sep", "oct", "nov", "dec" };
184 static const int kMonthsLen = arraysize(kMonths); 110 static const int kMonthsLen = arraysize(kMonths);
185 // We want to be pretty liberal, and support most non-ascii and non-digit 111 // We want to be pretty liberal, and support most non-ascii and non-digit
186 // characters as a delimiter. We can't treat : as a delimiter, because it 112 // characters as a delimiter. We can't treat : as a delimiter, because it
187 // is the delimiter for hh:mm:ss, and we want to keep this field together. 113 // is the delimiter for hh:mm:ss, and we want to keep this field together.
188 // We make sure to include - and +, since they could prefix numbers. 114 // We make sure to include - and +, since they could prefix numbers.
189 // If the cookie attribute came in in quotes (ex expires="XXX"), the quotes 115 // If the cookie attribute came in in quotes (ex expires="XXX"), the quotes
190 // will be preserved, and we will get them here. So we make sure to include 116 // 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
267 // NOTREACHED() << "Cookie parse expiration failed: " << time_string; 193 // NOTREACHED() << "Cookie parse expiration failed: " << time_string;
268 return base::Time(); 194 return base::Time();
269 } 195 }
270 196
271 // Normalize the year to expand abbreviated years to the full year. 197 // Normalize the year to expand abbreviated years to the full year.
272 if (exploded.year >= 69 && exploded.year <= 99) 198 if (exploded.year >= 69 && exploded.year <= 99)
273 exploded.year += 1900; 199 exploded.year += 1900;
274 if (exploded.year >= 0 && exploded.year <= 68) 200 if (exploded.year >= 0 && exploded.year <= 68)
275 exploded.year += 2000; 201 exploded.year += 2000;
276 202
277 // Note that clipping the date if it is outside of a platform-specific range 203 // If our values are within their correct ranges, we got our time.
278 // is permitted by: https://tools.ietf.org/html/rfc6265#section-5.2.1 204 if (exploded.day_of_month >= 1 && exploded.day_of_month <= 31 &&
279 base::Time result; 205 exploded.month >= 1 && exploded.month <= 12 &&
280 if (SaturatedTimeFromUTCExploded(exploded, &result)) 206 exploded.year >= 1601 && exploded.year <= 30827 &&
281 return result; 207 exploded.hour <= 23 && exploded.minute <= 59 && exploded.second <= 59) {
208 return base::Time::FromUTCExploded(exploded);
209 }
282 210
283 // One of our values was out of expected range. For well-formed input, 211 // One of our values was out of expected range. For well-formed input,
284 // the following check would be reasonable: 212 // the following check would be reasonable:
285 // NOTREACHED() << "Cookie exploded expiration failed: " << time_string; 213 // NOTREACHED() << "Cookie exploded expiration failed: " << time_string;
286 214
287 return base::Time(); 215 return base::Time();
288 } 216 }
289 217
290 GURL CookieOriginToURL(const std::string& domain, bool is_https) { 218 GURL CookieOriginToURL(const std::string& domain, bool is_https) {
291 if (domain.empty()) 219 if (domain.empty())
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 buffer.append("; "); 273 buffer.append("; ");
346 buffer.append(i->first.begin(), i->first.end()); 274 buffer.append(i->first.begin(), i->first.end());
347 buffer.push_back('='); 275 buffer.push_back('=');
348 buffer.append(i->second.begin(), i->second.end()); 276 buffer.append(i->second.begin(), i->second.end());
349 } 277 }
350 return buffer; 278 return buffer;
351 } 279 }
352 280
353 } // namespace cookie_util 281 } // namespace cookie_util
354 } // namespace net 282 } // namespace net
OLDNEW
« no previous file with comments | « net/cookies/cookie_util.h ('k') | net/cookies/cookie_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698