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

Side by Side Diff: net/http/http_security_headers.cc

Issue 1831963002: Fix number parsing for max-age for HSTS/HPKP. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@parse_refactor
Patch Set: rebase Created 4 years, 8 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/http/http_security_headers.h ('k') | net/http/http_security_headers_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 <limits> 5 #include <limits>
6 6
7 #include "base/base64.h" 7 #include "base/base64.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/string_piece.h" 8 #include "base/strings/string_piece.h"
10 #include "base/strings/string_tokenizer.h" 9 #include "base/strings/string_tokenizer.h"
11 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
11 #include "net/base/parse_number.h"
12 #include "net/http/http_security_headers.h" 12 #include "net/http/http_security_headers.h"
13 #include "net/http/http_util.h" 13 #include "net/http/http_util.h"
14 #include "url/gurl.h" 14 #include "url/gurl.h"
15 15
16 namespace net { 16 namespace net {
17 17
18 namespace { 18 namespace {
19 19
20 enum MaxAgeParsing { REQUIRE_MAX_AGE, DO_NOT_REQUIRE_MAX_AGE }; 20 enum MaxAgeParsing { REQUIRE_MAX_AGE, DO_NOT_REQUIRE_MAX_AGE };
21 21
22 static_assert(kMaxHSTSAgeSecs <= UINT32_MAX, "kMaxHSTSAgeSecs too large");
23 static_assert(kMaxHPKPAgeSecs <= UINT32_MAX, "kMaxHPKPAgeSecs too large");
24
25 // MaxAgeToLimitedInt converts a string representation of a "whole number" of 22 // MaxAgeToLimitedInt converts a string representation of a "whole number" of
26 // seconds into a uint32_t. The string may contain an arbitrarily large number, 23 // seconds into a uint32_t. The string may contain an arbitrarily large number,
27 // which will be clipped to a supplied limit and which is guaranteed to fit 24 // which will be clipped to a supplied limit and which is guaranteed to fit
28 // within a 32-bit unsigned integer. False is returned on any parse error. 25 // within a 32-bit unsigned integer. False is returned on any parse error.
29 bool MaxAgeToLimitedInt(std::string::const_iterator begin, 26 bool MaxAgeToLimitedInt(std::string::const_iterator begin,
30 std::string::const_iterator end, 27 std::string::const_iterator end,
31 uint32_t limit, 28 uint32_t limit,
32 uint32_t* result) { 29 uint32_t* result) {
33 const base::StringPiece s(begin, end); 30 const base::StringPiece s(begin, end);
34 if (s.empty())
35 return false;
36 31
37 int64_t i = 0; 32 ParseIntError error;
33 if (!ParseUint32(s, result, &error)) {
34 if (error == ParseIntError::FAILED_OVERFLOW) {
35 *result = limit;
36 } else {
37 return false;
38 }
39 }
38 40
39 // Return false on any StringToInt64 parse errors *except* for int64_t 41 if (*result > limit)
40 // overflow. StringToInt64 is used, rather than StringToUint64, in order to 42 *result = limit;
41 // properly handle and reject negative numbers (StringToUint64 does not return 43
42 // false on negative numbers). For values too large to be stored in an
43 // int64_t, StringToInt64 will return false with i set to
44 // std::numeric_limits<int64_t>::max(), so this case is allowed to fall
45 // through so that i gets clipped to limit.
46 if (!base::StringToInt64(s, &i) && i != std::numeric_limits<int64_t>::max())
47 return false;
48 if (i < 0)
49 return false;
50 if (i > limit)
51 i = limit;
52 *result = (uint32_t)i;
53 return true; 44 return true;
54 } 45 }
55 46
56 // Returns true iff there is an item in |pins| which is not present in 47 // Returns true iff there is an item in |pins| which is not present in
57 // |from_cert_chain|. Such an SPKI hash is called a "backup pin". 48 // |from_cert_chain|. Such an SPKI hash is called a "backup pin".
58 bool IsBackupPinPresent(const HashValueVector& pins, 49 bool IsBackupPinPresent(const HashValueVector& pins,
59 const HashValueVector& from_cert_chain) { 50 const HashValueVector& from_cert_chain) {
60 for (HashValueVector::const_iterator i = pins.begin(); i != pins.end(); 51 for (HashValueVector::const_iterator i = pins.begin(); i != pins.end();
61 ++i) { 52 ++i) {
62 HashValueVector::const_iterator j = 53 HashValueVector::const_iterator j =
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after
372 bool* include_subdomains, 363 bool* include_subdomains,
373 HashValueVector* hashes, 364 HashValueVector* hashes,
374 GURL* report_uri) { 365 GURL* report_uri) {
375 // max-age is irrelevant for Report-Only headers. 366 // max-age is irrelevant for Report-Only headers.
376 base::TimeDelta unused_max_age; 367 base::TimeDelta unused_max_age;
377 return ParseHPKPHeaderImpl(value, DO_NOT_REQUIRE_MAX_AGE, &unused_max_age, 368 return ParseHPKPHeaderImpl(value, DO_NOT_REQUIRE_MAX_AGE, &unused_max_age,
378 include_subdomains, hashes, report_uri); 369 include_subdomains, hashes, report_uri);
379 } 370 }
380 371
381 } // namespace net 372 } // namespace net
OLDNEW
« no previous file with comments | « net/http/http_security_headers.h ('k') | net/http/http_security_headers_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698