Avi Drissman | 6459548 | 2022-09-14 20:52:29 | [diff] [blame^] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
avi | c027914 | 2015-12-04 22:38:52 | [diff] [blame] | 5 | #include <limits> |
| 6 | |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 7 | #include "base/base64.h" |
Hans Wennborg | 0924470b | 2020-04-27 21:08:05 | [diff] [blame] | 8 | #include "base/check_op.h" |
| 9 | #include "base/notreached.h" |
estark | 83487b6 | 2015-07-27 17:11:14 | [diff] [blame] | 10 | #include "base/strings/string_piece.h" |
[email protected] | f4ebe77 | 2013-02-02 00:21:39 | [diff] [blame] | 11 | #include "base/strings/string_tokenizer.h" |
[email protected] | 125ef48 | 2013-06-11 18:32:47 | [diff] [blame] | 12 | #include "base/strings/string_util.h" |
eroman | e8a43d8 | 2016-04-12 06:02:06 | [diff] [blame] | 13 | #include "net/base/parse_number.h" |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 14 | #include "net/http/http_security_headers.h" |
| 15 | #include "net/http/http_util.h" |
estark | 83487b6 | 2015-07-27 17:11:14 | [diff] [blame] | 16 | #include "url/gurl.h" |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 17 | |
| 18 | namespace net { |
| 19 | |
| 20 | namespace { |
| 21 | |
estark | 640590d4 | 2015-07-31 23:56:24 | [diff] [blame] | 22 | enum MaxAgeParsing { REQUIRE_MAX_AGE, DO_NOT_REQUIRE_MAX_AGE }; |
| 23 | |
elawrence | a6c42bd | 2016-02-29 18:00:42 | [diff] [blame] | 24 | // MaxAgeToLimitedInt converts a string representation of a "whole number" of |
avi | c027914 | 2015-12-04 22:38:52 | [diff] [blame] | 25 | // seconds into a uint32_t. The string may contain an arbitrarily large number, |
elawrence | a6c42bd | 2016-02-29 18:00:42 | [diff] [blame] | 26 | // which will be clipped to a supplied limit and which is guaranteed to fit |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 27 | // within a 32-bit unsigned integer. False is returned on any parse error. |
David Benjamin | e4b880e | 2019-04-26 00:07:51 | [diff] [blame] | 28 | bool MaxAgeToLimitedInt(base::StringPiece s, uint32_t limit, uint32_t* result) { |
eroman | e8a43d8 | 2016-04-12 06:02:06 | [diff] [blame] | 29 | 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] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 37 | |
eroman | e8a43d8 | 2016-04-12 06:02:06 | [diff] [blame] | 38 | if (*result > limit) |
| 39 | *result = limit; |
| 40 | |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 41 | return true; |
| 42 | } |
| 43 | |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 44 | } // 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] | b4e1f7e | 2013-05-25 13:59:09 | [diff] [blame] | 72 | bool ParseHSTSHeader(const std::string& value, |
| 73 | base::TimeDelta* max_age, |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 74 | bool* include_subdomains) { |
avi | c027914 | 2015-12-04 22:38:52 | [diff] [blame] | 75 | uint32_t max_age_candidate = 0; |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 76 | 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] | f4ebe77 | 2013-02-02 00:21:39 | [diff] [blame] | 93 | base::StringTokenizer tokenizer(value, " \t=;"); |
| 94 | tokenizer.set_options(base::StringTokenizer::RETURN_DELIMS); |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 95 | tokenizer.set_quote_chars("\""); |
| 96 | std::string unquoted; |
| 97 | while (tokenizer.GetNext()) { |
David Benjamin | be3b0494 | 2019-04-26 21:05:25 | [diff] [blame] | 98 | base::StringPiece token = tokenizer.token_piece(); |
| 99 | DCHECK(!tokenizer.token_is_delim() || token.length() == 1); |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 100 | switch (state) { |
| 101 | case START: |
| 102 | case DIRECTIVE_END: |
David Benjamin | be3b0494 | 2019-04-26 21:05:25 | [diff] [blame] | 103 | if (base::IsAsciiWhitespace(token[0])) |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 104 | continue; |
Dan McArdle | 3d5db1a | 2022-05-27 18:13:31 | [diff] [blame] | 105 | if (base::EqualsCaseInsensitiveASCII(token, "max-age")) { |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 106 | state = AFTER_MAX_AGE_LABEL; |
| 107 | max_age_observed++; |
Dan McArdle | 3d5db1a | 2022-05-27 18:13:31 | [diff] [blame] | 108 | } else if (base::EqualsCaseInsensitiveASCII(token, |
| 109 | "includesubdomains")) { |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 110 | 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 Benjamin | be3b0494 | 2019-04-26 21:05:25 | [diff] [blame] | 119 | if (base::IsAsciiWhitespace(token[0])) |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 120 | continue; |
David Benjamin | be3b0494 | 2019-04-26 21:05:25 | [diff] [blame] | 121 | if (token[0] != '=') |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 122 | return false; |
David Benjamin | be3b0494 | 2019-04-26 21:05:25 | [diff] [blame] | 123 | DCHECK_EQ(token.length(), 1U); |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 124 | state = AFTER_MAX_AGE_EQUALS; |
| 125 | break; |
| 126 | |
| 127 | case AFTER_MAX_AGE_EQUALS: |
David Benjamin | be3b0494 | 2019-04-26 21:05:25 | [diff] [blame] | 128 | if (base::IsAsciiWhitespace(token[0])) |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 129 | continue; |
David Benjamin | be3b0494 | 2019-04-26 21:05:25 | [diff] [blame] | 130 | unquoted = HttpUtil::Unquote(token); |
David Benjamin | e4b880e | 2019-04-26 00:07:51 | [diff] [blame] | 131 | if (!MaxAgeToLimitedInt(unquoted, kMaxHSTSAgeSecs, &max_age_candidate)) |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 132 | return false; |
| 133 | state = AFTER_MAX_AGE; |
| 134 | break; |
| 135 | |
| 136 | case AFTER_MAX_AGE: |
| 137 | case AFTER_INCLUDE_SUBDOMAINS: |
David Benjamin | be3b0494 | 2019-04-26 21:05:25 | [diff] [blame] | 138 | if (base::IsAsciiWhitespace(token[0])) |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 139 | continue; |
David Benjamin | be3b0494 | 2019-04-26 21:05:25 | [diff] [blame] | 140 | else if (token[0] == ';') |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 141 | 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 Benjamin | be3b0494 | 2019-04-26 21:05:25 | [diff] [blame] | 148 | if (token[0] != ';') |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 149 | continue; |
| 150 | state = DIRECTIVE_END; |
| 151 | break; |
| 152 | } |
| 153 | } |
| 154 | |
elawrence | a6c42bd | 2016-02-29 18:00:42 | [diff] [blame] | 155 | // We've consumed all the input. Let's see what state we ended up in. |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 156 | if (max_age_observed != 1 || |
| 157 | (include_subdomains_observed != 0 && include_subdomains_observed != 1)) { |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | switch (state) { |
[email protected] | 9f49546 | 2014-04-21 20:49:22 | [diff] [blame] | 162 | case DIRECTIVE_END: |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 163 | case AFTER_MAX_AGE: |
| 164 | case AFTER_INCLUDE_SUBDOMAINS: |
| 165 | case AFTER_UNKNOWN_LABEL: |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 166 | *max_age = base::Seconds(max_age_candidate); |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 167 | *include_subdomains = include_subdomains_candidate; |
| 168 | return true; |
| 169 | case START: |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 170 | case AFTER_MAX_AGE_LABEL: |
| 171 | case AFTER_MAX_AGE_EQUALS: |
| 172 | return false; |
| 173 | default: |
| 174 | NOTREACHED(); |
| 175 | return false; |
| 176 | } |
| 177 | } |
| 178 | |
estark | a57e816 | 2017-04-21 18:01:05 | [diff] [blame] | 179 | // "Expect-CT" ":" |
| 180 | // "max-age" "=" delta-seconds |
| 181 | // [ "," "enforce" ] |
estark | 8ed5435 | 2017-05-23 17:35:34 | [diff] [blame] | 182 | // [ "," "report-uri" "=" absolute-URI ] |
estark | a57e816 | 2017-04-21 18:01:05 | [diff] [blame] | 183 | bool 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 Benjamin | e4b880e | 2019-04-26 00:07:51 | [diff] [blame] | 201 | base::StringPiece name = name_value_pairs.name_piece(); |
Dan McArdle | 3d5db1a | 2022-05-27 18:13:31 | [diff] [blame] | 202 | if (base::EqualsCaseInsensitiveASCII(name, "max-age")) { |
estark | a57e816 | 2017-04-21 18:01:05 | [diff] [blame] | 203 | // "A given directive MUST NOT appear more than once in a given header |
| 204 | // field." |
| 205 | if (parsed_max_age) |
| 206 | return false; |
David Benjamin | e4b880e | 2019-04-26 00:07:51 | [diff] [blame] | 207 | if (!MaxAgeToLimitedInt(name_value_pairs.value_piece(), |
| 208 | kMaxExpectCTAgeSecs, &max_age_candidate)) { |
estark | a57e816 | 2017-04-21 18:01:05 | [diff] [blame] | 209 | return false; |
| 210 | } |
| 211 | parsed_max_age = true; |
Dan McArdle | 3d5db1a | 2022-05-27 18:13:31 | [diff] [blame] | 212 | } else if (base::EqualsCaseInsensitiveASCII(name, "enforce")) { |
estark | a57e816 | 2017-04-21 18:01:05 | [diff] [blame] | 213 | // "A given directive MUST NOT appear more than once in a given header |
| 214 | // field." |
| 215 | if (enforce_candidate) |
| 216 | return false; |
David Benjamin | e4b880e | 2019-04-26 00:07:51 | [diff] [blame] | 217 | if (!name_value_pairs.value_piece().empty()) |
estark | a57e816 | 2017-04-21 18:01:05 | [diff] [blame] | 218 | return false; |
| 219 | enforce_candidate = true; |
Dan McArdle | 3d5db1a | 2022-05-27 18:13:31 | [diff] [blame] | 220 | } else if (base::EqualsCaseInsensitiveASCII(name, "report-uri")) { |
estark | a57e816 | 2017-04-21 18:01:05 | [diff] [blame] | 221 | // "A given directive MUST NOT appear more than once in a given header |
| 222 | // field." |
| 223 | if (has_report_uri) |
| 224 | return false; |
estark | a57e816 | 2017-04-21 18:01:05 | [diff] [blame] | 225 | |
| 226 | has_report_uri = true; |
David Benjamin | e4b880e | 2019-04-26 00:07:51 | [diff] [blame] | 227 | parsed_report_uri = GURL(name_value_pairs.value_piece()); |
estark | a57e816 | 2017-04-21 18:01:05 | [diff] [blame] | 228 | 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 Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 241 | *max_age = base::Seconds(max_age_candidate); |
estark | a57e816 | 2017-04-21 18:01:05 | [diff] [blame] | 242 | *enforce = enforce_candidate; |
| 243 | *report_uri = parsed_report_uri; |
| 244 | return true; |
| 245 | } |
| 246 | |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 247 | } // namespace net |