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

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

Issue 2424443002: When parsing cookie expiration times, saturate out of range dates (Closed)
Patch Set: Merge 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
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:
32 //
33 // * Time(1) as the minimum value
34 // * Time::Max() as the maximum value
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 clamping values when imploding the time is doomed
51 // 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)
eroman 2016/10/18 21:52:54 What is the range on mac?
mmenke 2016/10/18 22:18:14 It's unclear. Note that it's passing the tests wi
58 // Allow dates prior to unix epoch (which fail on non-Mac/iOS POSIX).
59 if (exploded.year < 1970) {
60 *out = base::Time::UnixEpoch();
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 the max
66 // value that can be represented by a 32-bit time_t.
67 if (sizeof(time_t) == 4u && exploded.year >= 2038) {
68 *out = base::Time::FromTimeT(std::numeric_limits<time_t>::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 < 1961) {
eroman 2016/10/18 21:52:54 Not sure I follow, isn't the windows epoch 1601?
mmenke 2016/10/18 22:18:14 You're right, fixed. Not sure where the 1961 came
76 *out = MinNonNullTime();
77 return true;
78 }
79
80 // Allow dates after the Windows epoch.
81 if (exploded.year >= 30827) {
82 // This is the maximum value a FILETIME can represent, though FromExploded()
83 // does fail on marginally smaller FILETIME values. The division by 10 is
84 // needed because FILETIMEs are in terms of hundreds of nanoseconds.
85 // This relies on base::Time() returning the start of the Windows epoch.
eroman 2016/10/18 21:52:54 You can use base::Time::FromInternalValue() -- tha
mmenke 2016/10/18 22:18:14 Done (I didn't use it because the method doesn't e
86 *out = base::Time() + base::TimeDelta::FromMicroseconds(
87 std::numeric_limits<int64_t>::max() / 10);
88 return true;
89 }
90 #endif // defined(OS_WIN)
91
92 return false;
93 }
94
95 } // namespace
96
21 bool DomainIsHostOnly(const std::string& domain_string) { 97 bool DomainIsHostOnly(const std::string& domain_string) {
22 return (domain_string.empty() || domain_string[0] != '.'); 98 return (domain_string.empty() || domain_string[0] != '.');
23 } 99 }
24 100
25 std::string GetEffectiveDomain(const std::string& scheme, 101 std::string GetEffectiveDomain(const std::string& scheme,
26 const std::string& host) { 102 const std::string& host) {
27 if (scheme == "http" || scheme == "https" || scheme == "ws" || 103 if (scheme == "http" || scheme == "https" || scheme == "ws" ||
28 scheme == "wss") { 104 scheme == "wss") {
29 return registry_controlled_domains::GetDomainAndRegistry( 105 return registry_controlled_domains::GetDomainAndRegistry(
30 host, 106 host,
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 } 172 }
97 173
98 // Parse a cookie expiration time. We try to be lenient, but we need to 174 // 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: 175 // 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 176 // - The month name must be present and prefix the first 3 letters of the
101 // full month name (jan for January, jun for June). 177 // full month name (jan for January, jun for June).
102 // - If the year is <= 2 digits, it must occur after the day of month. 178 // - 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. 179 // - The time must be of the format hh:mm:ss.
104 // An average cookie expiration will look something like this: 180 // An average cookie expiration will look something like this:
105 // Sat, 15-Apr-17 21:01:22 GMT 181 // Sat, 15-Apr-17 21:01:22 GMT
106 base::Time ParseCookieTime(const std::string& time_string) { 182 base::Time ParseCookieExpirationTime(const std::string& time_string) {
107 static const char* const kMonths[] = { 183 static const char* const kMonths[] = {
108 "jan", "feb", "mar", "apr", "may", "jun", 184 "jan", "feb", "mar", "apr", "may", "jun",
109 "jul", "aug", "sep", "oct", "nov", "dec" }; 185 "jul", "aug", "sep", "oct", "nov", "dec" };
110 static const int kMonthsLen = arraysize(kMonths); 186 static const int kMonthsLen = arraysize(kMonths);
111 // We want to be pretty liberal, and support most non-ascii and non-digit 187 // 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 188 // 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. 189 // 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. 190 // 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 191 // 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 192 // 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; 269 // NOTREACHED() << "Cookie parse expiration failed: " << time_string;
194 return base::Time(); 270 return base::Time();
195 } 271 }
196 272
197 // Normalize the year to expand abbreviated years to the full year. 273 // Normalize the year to expand abbreviated years to the full year.
198 if (exploded.year >= 69 && exploded.year <= 99) 274 if (exploded.year >= 69 && exploded.year <= 99)
199 exploded.year += 1900; 275 exploded.year += 1900;
200 if (exploded.year >= 0 && exploded.year <= 68) 276 if (exploded.year >= 0 && exploded.year <= 68)
201 exploded.year += 2000; 277 exploded.year += 2000;
202 278
203 // If our values are within their correct ranges, we got our time. 279 // 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 && 280 // is permitted by: https://tools.ietf.org/html/rfc6265#section-5.2.1
205 exploded.month >= 1 && exploded.month <= 12 && 281 base::Time result;
206 exploded.year >= 1601 && exploded.year <= 30827 && 282 if (SaturatedTimeFromUTCExploded(exploded, &result))
207 exploded.hour <= 23 && exploded.minute <= 59 && exploded.second <= 59) { 283 return result;
208 return base::Time::FromUTCExploded(exploded);
209 }
210 284
211 // One of our values was out of expected range. For well-formed input, 285 // One of our values was out of expected range. For well-formed input,
212 // the following check would be reasonable: 286 // the following check would be reasonable:
213 // NOTREACHED() << "Cookie exploded expiration failed: " << time_string; 287 // NOTREACHED() << "Cookie exploded expiration failed: " << time_string;
214 288
215 return base::Time(); 289 return base::Time();
216 } 290 }
217 291
218 GURL CookieOriginToURL(const std::string& domain, bool is_https) { 292 GURL CookieOriginToURL(const std::string& domain, bool is_https) {
219 if (domain.empty()) 293 if (domain.empty())
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 buffer.append("; "); 347 buffer.append("; ");
274 buffer.append(i->first.begin(), i->first.end()); 348 buffer.append(i->first.begin(), i->first.end());
275 buffer.push_back('='); 349 buffer.push_back('=');
276 buffer.append(i->second.begin(), i->second.end()); 350 buffer.append(i->second.begin(), i->second.end());
277 } 351 }
278 return buffer; 352 return buffer;
279 } 353 }
280 354
281 } // namespace cookie_util 355 } // namespace cookie_util
282 } // namespace net 356 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698