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

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

Issue 2424443002: When parsing cookie expiration times, saturate out of range dates (Closed)
Patch Set: Response to comments 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
21 bool DomainIsHostOnly(const std::string& domain_string) { 95 bool DomainIsHostOnly(const std::string& domain_string) {
22 return (domain_string.empty() || domain_string[0] != '.'); 96 return (domain_string.empty() || domain_string[0] != '.');
23 } 97 }
24 98
25 std::string GetEffectiveDomain(const std::string& scheme, 99 std::string GetEffectiveDomain(const std::string& scheme,
26 const std::string& host) { 100 const std::string& host) {
27 if (scheme == "http" || scheme == "https" || scheme == "ws" || 101 if (scheme == "http" || scheme == "https" || scheme == "ws" ||
28 scheme == "wss") { 102 scheme == "wss") {
29 return registry_controlled_domains::GetDomainAndRegistry( 103 return registry_controlled_domains::GetDomainAndRegistry(
30 host, 104 host,
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 } 170 }
97 171
98 // Parse a cookie expiration time. We try to be lenient, but we need to 172 // 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: 173 // 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 174 // - The month name must be present and prefix the first 3 letters of the
101 // full month name (jan for January, jun for June). 175 // full month name (jan for January, jun for June).
102 // - If the year is <= 2 digits, it must occur after the day of month. 176 // - 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. 177 // - The time must be of the format hh:mm:ss.
104 // An average cookie expiration will look something like this: 178 // An average cookie expiration will look something like this:
105 // Sat, 15-Apr-17 21:01:22 GMT 179 // Sat, 15-Apr-17 21:01:22 GMT
106 base::Time ParseCookieTime(const std::string& time_string) { 180 base::Time ParseCookieExpirationTime(const std::string& time_string) {
107 static const char* const kMonths[] = { 181 static const char* const kMonths[] = {
108 "jan", "feb", "mar", "apr", "may", "jun", 182 "jan", "feb", "mar", "apr", "may", "jun",
109 "jul", "aug", "sep", "oct", "nov", "dec" }; 183 "jul", "aug", "sep", "oct", "nov", "dec" };
110 static const int kMonthsLen = arraysize(kMonths); 184 static const int kMonthsLen = arraysize(kMonths);
111 // We want to be pretty liberal, and support most non-ascii and non-digit 185 // 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 186 // 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. 187 // 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. 188 // 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 189 // 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 190 // 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
193 // NOTREACHED() << "Cookie parse expiration failed: " << time_string; 267 // NOTREACHED() << "Cookie parse expiration failed: " << time_string;
194 return base::Time(); 268 return base::Time();
195 } 269 }
196 270
197 // Normalize the year to expand abbreviated years to the full year. 271 // Normalize the year to expand abbreviated years to the full year.
198 if (exploded.year >= 69 && exploded.year <= 99) 272 if (exploded.year >= 69 && exploded.year <= 99)
199 exploded.year += 1900; 273 exploded.year += 1900;
200 if (exploded.year >= 0 && exploded.year <= 68) 274 if (exploded.year >= 0 && exploded.year <= 68)
201 exploded.year += 2000; 275 exploded.year += 2000;
202 276
203 // If our values are within their correct ranges, we got our time. 277 // 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 && 278 // is permitted by: https://tools.ietf.org/html/rfc6265#section-5.2.1
205 exploded.month >= 1 && exploded.month <= 12 && 279 base::Time result;
206 exploded.year >= 1601 && exploded.year <= 30827 && 280 if (SaturatedTimeFromUTCExploded(exploded, &result))
207 exploded.hour <= 23 && exploded.minute <= 59 && exploded.second <= 59) { 281 return result;
208 return base::Time::FromUTCExploded(exploded);
209 }
210 282
211 // One of our values was out of expected range. For well-formed input, 283 // One of our values was out of expected range. For well-formed input,
212 // the following check would be reasonable: 284 // the following check would be reasonable:
213 // NOTREACHED() << "Cookie exploded expiration failed: " << time_string; 285 // NOTREACHED() << "Cookie exploded expiration failed: " << time_string;
214 286
215 return base::Time(); 287 return base::Time();
216 } 288 }
217 289
218 GURL CookieOriginToURL(const std::string& domain, bool is_https) { 290 GURL CookieOriginToURL(const std::string& domain, bool is_https) {
219 if (domain.empty()) 291 if (domain.empty())
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 buffer.append("; "); 345 buffer.append("; ");
274 buffer.append(i->first.begin(), i->first.end()); 346 buffer.append(i->first.begin(), i->first.end());
275 buffer.push_back('='); 347 buffer.push_back('=');
276 buffer.append(i->second.begin(), i->second.end()); 348 buffer.append(i->second.begin(), i->second.end());
277 } 349 }
278 return buffer; 350 return buffer;
279 } 351 }
280 352
281 } // namespace cookie_util 353 } // namespace cookie_util
282 } // namespace net 354 } // 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