blob: 52f508aeddb1f5f13d9d3e8b273de2339a4b250e [file] [log] [blame]
Avi Drissman64595482022-09-14 20:52:291// Copyright 2012 The Chromium Authors
[email protected]6ed72be2013-01-08 22:07:332// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
avic0279142015-12-04 22:38:525#include <limits>
6
[email protected]6ed72be2013-01-08 22:07:337#include "base/base64.h"
Hans Wennborg0924470b2020-04-27 21:08:058#include "base/check_op.h"
9#include "base/notreached.h"
estark83487b62015-07-27 17:11:1410#include "base/strings/string_piece.h"
[email protected]f4ebe772013-02-02 00:21:3911#include "base/strings/string_tokenizer.h"
[email protected]125ef482013-06-11 18:32:4712#include "base/strings/string_util.h"
eromane8a43d82016-04-12 06:02:0613#include "net/base/parse_number.h"
[email protected]6ed72be2013-01-08 22:07:3314#include "net/http/http_security_headers.h"
15#include "net/http/http_util.h"
estark83487b62015-07-27 17:11:1416#include "url/gurl.h"
[email protected]6ed72be2013-01-08 22:07:3317
18namespace net {
19
20namespace {
21
estark640590d42015-07-31 23:56:2422enum MaxAgeParsing { REQUIRE_MAX_AGE, DO_NOT_REQUIRE_MAX_AGE };
23
elawrencea6c42bd2016-02-29 18:00:4224// MaxAgeToLimitedInt converts a string representation of a "whole number" of
avic0279142015-12-04 22:38:5225// seconds into a uint32_t. The string may contain an arbitrarily large number,
elawrencea6c42bd2016-02-29 18:00:4226// which will be clipped to a supplied limit and which is guaranteed to fit
[email protected]6ed72be2013-01-08 22:07:3327// within a 32-bit unsigned integer. False is returned on any parse error.
David Benjamine4b880e2019-04-26 00:07:5128bool MaxAgeToLimitedInt(base::StringPiece s, uint32_t limit, uint32_t* result) {
eromane8a43d82016-04-12 06:02:0629 ParseIntError error;
30 if (!ParseUint32(s, result, &error)) {
31 if (error == ParseIntError::FAILED_OVERFLOW) {
32 *result = limit;
33 } else {
34 return false;
35 }
36 }
[email protected]6ed72be2013-01-08 22:07:3337
eromane8a43d82016-04-12 06:02:0638 if (*result > limit)
39 *result = limit;
40
[email protected]6ed72be2013-01-08 22:07:3341 return true;
42}
43
[email protected]6ed72be2013-01-08 22:07:3344} // namespace
45
46// Parse the Strict-Transport-Security header, as currently defined in
47// http://tools.ietf.org/html/draft-ietf-websec-strict-transport-sec-14:
48//
49// Strict-Transport-Security = "Strict-Transport-Security" ":"
50// [ directive ] *( ";" [ directive ] )
51//
52// directive = directive-name [ "=" directive-value ]
53// directive-name = token
54// directive-value = token | quoted-string
55//
56// 1. The order of appearance of directives is not significant.
57//
58// 2. All directives MUST appear only once in an STS header field.
59// Directives are either optional or required, as stipulated in
60// their definitions.
61//
62// 3. Directive names are case-insensitive.
63//
64// 4. UAs MUST ignore any STS header fields containing directives, or
65// other header field value data, that does not conform to the
66// syntax defined in this specification.
67//
68// 5. If an STS header field contains directive(s) not recognized by
69// the UA, the UA MUST ignore the unrecognized directives and if the
70// STS header field otherwise satisfies the above requirements (1
71// through 4), the UA MUST process the recognized directives.
[email protected]b4e1f7e2013-05-25 13:59:0972bool ParseHSTSHeader(const std::string& value,
73 base::TimeDelta* max_age,
[email protected]9f972ec2013-04-10 20:24:3674 bool* include_subdomains) {
avic0279142015-12-04 22:38:5275 uint32_t max_age_candidate = 0;
[email protected]6ed72be2013-01-08 22:07:3376 bool include_subdomains_candidate = false;
77
78 // We must see max-age exactly once.
79 int max_age_observed = 0;
80 // We must see includeSubdomains exactly 0 or 1 times.
81 int include_subdomains_observed = 0;
82
83 enum ParserState {
84 START,
85 AFTER_MAX_AGE_LABEL,
86 AFTER_MAX_AGE_EQUALS,
87 AFTER_MAX_AGE,
88 AFTER_INCLUDE_SUBDOMAINS,
89 AFTER_UNKNOWN_LABEL,
90 DIRECTIVE_END
91 } state = START;
92
[email protected]f4ebe772013-02-02 00:21:3993 base::StringTokenizer tokenizer(value, " \t=;");
94 tokenizer.set_options(base::StringTokenizer::RETURN_DELIMS);
[email protected]6ed72be2013-01-08 22:07:3395 tokenizer.set_quote_chars("\"");
96 std::string unquoted;
97 while (tokenizer.GetNext()) {
David Benjaminbe3b04942019-04-26 21:05:2598 base::StringPiece token = tokenizer.token_piece();
99 DCHECK(!tokenizer.token_is_delim() || token.length() == 1);
[email protected]6ed72be2013-01-08 22:07:33100 switch (state) {
101 case START:
102 case DIRECTIVE_END:
David Benjaminbe3b04942019-04-26 21:05:25103 if (base::IsAsciiWhitespace(token[0]))
[email protected]6ed72be2013-01-08 22:07:33104 continue;
Dan McArdle3d5db1a2022-05-27 18:13:31105 if (base::EqualsCaseInsensitiveASCII(token, "max-age")) {
[email protected]6ed72be2013-01-08 22:07:33106 state = AFTER_MAX_AGE_LABEL;
107 max_age_observed++;
Dan McArdle3d5db1a2022-05-27 18:13:31108 } else if (base::EqualsCaseInsensitiveASCII(token,
109 "includesubdomains")) {
[email protected]6ed72be2013-01-08 22:07:33110 state = AFTER_INCLUDE_SUBDOMAINS;
111 include_subdomains_observed++;
112 include_subdomains_candidate = true;
113 } else {
114 state = AFTER_UNKNOWN_LABEL;
115 }
116 break;
117
118 case AFTER_MAX_AGE_LABEL:
David Benjaminbe3b04942019-04-26 21:05:25119 if (base::IsAsciiWhitespace(token[0]))
[email protected]6ed72be2013-01-08 22:07:33120 continue;
David Benjaminbe3b04942019-04-26 21:05:25121 if (token[0] != '=')
[email protected]6ed72be2013-01-08 22:07:33122 return false;
David Benjaminbe3b04942019-04-26 21:05:25123 DCHECK_EQ(token.length(), 1U);
[email protected]6ed72be2013-01-08 22:07:33124 state = AFTER_MAX_AGE_EQUALS;
125 break;
126
127 case AFTER_MAX_AGE_EQUALS:
David Benjaminbe3b04942019-04-26 21:05:25128 if (base::IsAsciiWhitespace(token[0]))
[email protected]6ed72be2013-01-08 22:07:33129 continue;
David Benjaminbe3b04942019-04-26 21:05:25130 unquoted = HttpUtil::Unquote(token);
David Benjamine4b880e2019-04-26 00:07:51131 if (!MaxAgeToLimitedInt(unquoted, kMaxHSTSAgeSecs, &max_age_candidate))
[email protected]6ed72be2013-01-08 22:07:33132 return false;
133 state = AFTER_MAX_AGE;
134 break;
135
136 case AFTER_MAX_AGE:
137 case AFTER_INCLUDE_SUBDOMAINS:
David Benjaminbe3b04942019-04-26 21:05:25138 if (base::IsAsciiWhitespace(token[0]))
[email protected]6ed72be2013-01-08 22:07:33139 continue;
David Benjaminbe3b04942019-04-26 21:05:25140 else if (token[0] == ';')
[email protected]6ed72be2013-01-08 22:07:33141 state = DIRECTIVE_END;
142 else
143 return false;
144 break;
145
146 case AFTER_UNKNOWN_LABEL:
147 // Consume and ignore the post-label contents (if any).
David Benjaminbe3b04942019-04-26 21:05:25148 if (token[0] != ';')
[email protected]6ed72be2013-01-08 22:07:33149 continue;
150 state = DIRECTIVE_END;
151 break;
152 }
153 }
154
elawrencea6c42bd2016-02-29 18:00:42155 // We've consumed all the input. Let's see what state we ended up in.
[email protected]6ed72be2013-01-08 22:07:33156 if (max_age_observed != 1 ||
157 (include_subdomains_observed != 0 && include_subdomains_observed != 1)) {
158 return false;
159 }
160
161 switch (state) {
[email protected]9f495462014-04-21 20:49:22162 case DIRECTIVE_END:
[email protected]6ed72be2013-01-08 22:07:33163 case AFTER_MAX_AGE:
164 case AFTER_INCLUDE_SUBDOMAINS:
165 case AFTER_UNKNOWN_LABEL:
Peter Kastinge5a38ed2021-10-02 03:06:35166 *max_age = base::Seconds(max_age_candidate);
[email protected]6ed72be2013-01-08 22:07:33167 *include_subdomains = include_subdomains_candidate;
168 return true;
169 case START:
[email protected]6ed72be2013-01-08 22:07:33170 case AFTER_MAX_AGE_LABEL:
171 case AFTER_MAX_AGE_EQUALS:
172 return false;
173 default:
174 NOTREACHED();
175 return false;
176 }
177}
178
estarka57e8162017-04-21 18:01:05179// "Expect-CT" ":"
180// "max-age" "=" delta-seconds
181// [ "," "enforce" ]
estark8ed54352017-05-23 17:35:34182// [ "," "report-uri" "=" absolute-URI ]
estarka57e8162017-04-21 18:01:05183bool ParseExpectCTHeader(const std::string& value,
184 base::TimeDelta* max_age,
185 bool* enforce,
186 GURL* report_uri) {
187 bool parsed_max_age = false;
188 bool enforce_candidate = false;
189 bool has_report_uri = false;
190 uint32_t max_age_candidate = 0;
191 GURL parsed_report_uri;
192
193 HttpUtil::NameValuePairsIterator name_value_pairs(
194 value.begin(), value.end(), ',',
195 HttpUtil::NameValuePairsIterator::Values::NOT_REQUIRED,
196 // Use STRICT_QUOTES because "UAs must not attempt to fix malformed header
197 // fields."
198 HttpUtil::NameValuePairsIterator::Quotes::STRICT_QUOTES);
199
200 while (name_value_pairs.GetNext()) {
David Benjamine4b880e2019-04-26 00:07:51201 base::StringPiece name = name_value_pairs.name_piece();
Dan McArdle3d5db1a2022-05-27 18:13:31202 if (base::EqualsCaseInsensitiveASCII(name, "max-age")) {
estarka57e8162017-04-21 18:01:05203 // "A given directive MUST NOT appear more than once in a given header
204 // field."
205 if (parsed_max_age)
206 return false;
David Benjamine4b880e2019-04-26 00:07:51207 if (!MaxAgeToLimitedInt(name_value_pairs.value_piece(),
208 kMaxExpectCTAgeSecs, &max_age_candidate)) {
estarka57e8162017-04-21 18:01:05209 return false;
210 }
211 parsed_max_age = true;
Dan McArdle3d5db1a2022-05-27 18:13:31212 } else if (base::EqualsCaseInsensitiveASCII(name, "enforce")) {
estarka57e8162017-04-21 18:01:05213 // "A given directive MUST NOT appear more than once in a given header
214 // field."
215 if (enforce_candidate)
216 return false;
David Benjamine4b880e2019-04-26 00:07:51217 if (!name_value_pairs.value_piece().empty())
estarka57e8162017-04-21 18:01:05218 return false;
219 enforce_candidate = true;
Dan McArdle3d5db1a2022-05-27 18:13:31220 } else if (base::EqualsCaseInsensitiveASCII(name, "report-uri")) {
estarka57e8162017-04-21 18:01:05221 // "A given directive MUST NOT appear more than once in a given header
222 // field."
223 if (has_report_uri)
224 return false;
estarka57e8162017-04-21 18:01:05225
226 has_report_uri = true;
David Benjamine4b880e2019-04-26 00:07:51227 parsed_report_uri = GURL(name_value_pairs.value_piece());
estarka57e8162017-04-21 18:01:05228 if (parsed_report_uri.is_empty() || !parsed_report_uri.is_valid())
229 return false;
230 } else {
231 // Silently ignore unknown directives for forward compatibility.
232 }
233 }
234
235 if (!name_value_pairs.valid())
236 return false;
237
238 if (!parsed_max_age)
239 return false;
240
Peter Kastinge5a38ed2021-10-02 03:06:35241 *max_age = base::Seconds(max_age_candidate);
estarka57e8162017-04-21 18:01:05242 *enforce = enforce_candidate;
243 *report_uri = parsed_report_uri;
244 return true;
245}
246
[email protected]6ed72be2013-01-08 22:07:33247} // namespace net