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

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

Issue 11274032: Separate http_security_headers from transport_security_state (Closed) Base URL: https://src.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 11 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.cc ('k') | net/net.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/base64.h"
6 #include "base/sha1.h"
7 #include "base/string_piece.h"
8 #include "crypto/sha2.h"
9 #include "net/base/net_log.h"
10 #include "net/base/test_completion_callback.h"
11 #include "net/http/http_security_headers.h"
12 #include "net/http/http_util.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace net {
16
17 namespace {
18
19 HashValue GetTestHashValue(uint8 label, HashValueTag tag) {
20 HashValue hash_value(tag);
21 memset(hash_value.data(), label, hash_value.size());
22 return hash_value;
23 }
24
25 std::string GetTestPin(uint8 label, HashValueTag tag) {
26 HashValue hash_value = GetTestHashValue(label, tag);
27 std::string base64;
28 base::Base64Encode(base::StringPiece(
29 reinterpret_cast<char*>(hash_value.data()), hash_value.size()), &base64);
30
31 switch (hash_value.tag) {
32 case HASH_VALUE_SHA1:
33 return std::string("pin-sha1=\"") + base64 + "\"";
34 case HASH_VALUE_SHA256:
35 return std::string("pin-sha256=\"") + base64 + "\"";
36 default:
37 NOTREACHED() << "Unknown HashValueTag " << hash_value.tag;
38 return std::string("ERROR");
39 }
40 }
41
42 };
43
44
45 class HttpSecurityHeadersTest : public testing::Test {
46 };
47
48
49 TEST_F(HttpSecurityHeadersTest, BogusHeaders) {
50 base::Time now = base::Time::Now();
51 base::Time expiry = now;
52 bool include_subdomains = false;
53
54 EXPECT_FALSE(ParseHSTSHeader(now, "", &expiry, &include_subdomains));
55 EXPECT_FALSE(ParseHSTSHeader(now, " ", &expiry, &include_subdomains));
56 EXPECT_FALSE(ParseHSTSHeader(now, "abc", &expiry, &include_subdomains));
57 EXPECT_FALSE(ParseHSTSHeader(now, " abc", &expiry, &include_subdomains));
58 EXPECT_FALSE(ParseHSTSHeader(now, " abc ", &expiry, &include_subdomains));
59 EXPECT_FALSE(ParseHSTSHeader(now, "max-age", &expiry, &include_subdomains));
60 EXPECT_FALSE(ParseHSTSHeader(now, " max-age", &expiry,
61 &include_subdomains));
62 EXPECT_FALSE(ParseHSTSHeader(now, " max-age ", &expiry,
63 &include_subdomains));
64 EXPECT_FALSE(ParseHSTSHeader(now, "max-age=", &expiry, &include_subdomains));
65 EXPECT_FALSE(ParseHSTSHeader(now, " max-age=", &expiry,
66 &include_subdomains));
67 EXPECT_FALSE(ParseHSTSHeader(now, " max-age =", &expiry,
68 &include_subdomains));
69 EXPECT_FALSE(ParseHSTSHeader(now, " max-age= ", &expiry,
70 &include_subdomains));
71 EXPECT_FALSE(ParseHSTSHeader(now, " max-age = ", &expiry,
72 &include_subdomains));
73 EXPECT_FALSE(ParseHSTSHeader(now, " max-age = xy", &expiry,
74 &include_subdomains));
75 EXPECT_FALSE(ParseHSTSHeader(now, " max-age = 3488a923", &expiry,
76 &include_subdomains));
77 EXPECT_FALSE(ParseHSTSHeader(now, "max-age=3488a923 ", &expiry,
78 &include_subdomains));
79 EXPECT_FALSE(ParseHSTSHeader(now, "max-ag=3488923", &expiry,
80 &include_subdomains));
81 EXPECT_FALSE(ParseHSTSHeader(now, "max-aged=3488923", &expiry,
82 &include_subdomains));
83 EXPECT_FALSE(ParseHSTSHeader(now, "max-age==3488923", &expiry,
84 &include_subdomains));
85 EXPECT_FALSE(ParseHSTSHeader(now, "amax-age=3488923", &expiry,
86 &include_subdomains));
87 EXPECT_FALSE(ParseHSTSHeader(now, "max-age=-3488923", &expiry,
88 &include_subdomains));
89 EXPECT_FALSE(ParseHSTSHeader(now, "max-age=3488923;", &expiry,
90 &include_subdomains));
91 EXPECT_FALSE(ParseHSTSHeader(now, "max-age=3488923 e", &expiry,
92 &include_subdomains));
93 EXPECT_FALSE(ParseHSTSHeader(now,
94 "max-age=3488923 includesubdomain",
95 &expiry, &include_subdomains));
96 EXPECT_FALSE(ParseHSTSHeader(now, "max-age=3488923includesubdomains",
97 &expiry, &include_subdomains));
98 EXPECT_FALSE(ParseHSTSHeader(now, "max-age=3488923=includesubdomains",
99 &expiry, &include_subdomains));
100 EXPECT_FALSE(ParseHSTSHeader(now, "max-age=3488923 includesubdomainx",
101 &expiry, &include_subdomains));
102 EXPECT_FALSE(ParseHSTSHeader(now, "max-age=3488923 includesubdomain=",
103 &expiry, &include_subdomains));
104 EXPECT_FALSE(ParseHSTSHeader(now,
105 "max-age=3488923 includesubdomain=true",
106 &expiry, &include_subdomains));
107 EXPECT_FALSE(ParseHSTSHeader(now, "max-age=3488923 includesubdomainsx",
108 &expiry, &include_subdomains));
109 EXPECT_FALSE(ParseHSTSHeader(now,
110 "max-age=3488923 includesubdomains x",
111 &expiry, &include_subdomains));
112 EXPECT_FALSE(ParseHSTSHeader(now, "max-age=34889.23 includesubdomains",
113 &expiry, &include_subdomains));
114 EXPECT_FALSE(ParseHSTSHeader(now, "max-age=34889 includesubdomains",
115 &expiry, &include_subdomains));
116
117 // Check the out args were not updated by checking the default
118 // values for its predictable fields.
119 EXPECT_EQ(now, expiry);
120 EXPECT_FALSE(include_subdomains);
121 }
122
123 static void TestBogusPinsHeaders(HashValueTag tag) {
124 base::Time now = base::Time::Now();
125 base::Time expiry = now;
126 HashValueVector hashes;
127 HashValueVector chain_hashes;
128
129 // Set some fake "chain" hashes
130 chain_hashes.push_back(GetTestHashValue(1, tag));
131 chain_hashes.push_back(GetTestHashValue(2, tag));
132 chain_hashes.push_back(GetTestHashValue(3, tag));
133
134 // The good pin must be in the chain, the backup pin must not be
135 std::string good_pin = GetTestPin(2, tag);
136 std::string backup_pin = GetTestPin(4, tag);
137
138 EXPECT_FALSE(ParseHPKPHeader(now, "", chain_hashes, &expiry, &hashes));
139 EXPECT_FALSE(ParseHPKPHeader(now, " ", chain_hashes, &expiry, &hashes));
140 EXPECT_FALSE(ParseHPKPHeader(now, "abc", chain_hashes, &expiry, &hashes));
141 EXPECT_FALSE(ParseHPKPHeader(now, " abc", chain_hashes, &expiry, &hashes));
142 EXPECT_FALSE(ParseHPKPHeader(now, " abc ", chain_hashes, &expiry,
143 &hashes));
144 EXPECT_FALSE(ParseHPKPHeader(now, "max-age", chain_hashes, &expiry,
145 &hashes));
146 EXPECT_FALSE(ParseHPKPHeader(now, " max-age", chain_hashes, &expiry,
147 &hashes));
148 EXPECT_FALSE(ParseHPKPHeader(now, " max-age ", chain_hashes, &expiry,
149 &hashes));
150 EXPECT_FALSE(ParseHPKPHeader(now, "max-age=", chain_hashes, &expiry,
151 &hashes));
152 EXPECT_FALSE(ParseHPKPHeader(now, " max-age=", chain_hashes, &expiry,
153 &hashes));
154 EXPECT_FALSE(ParseHPKPHeader(now, " max-age =", chain_hashes, &expiry,
155 &hashes));
156 EXPECT_FALSE(ParseHPKPHeader(now, " max-age= ", chain_hashes, &expiry,
157 &hashes));
158 EXPECT_FALSE(ParseHPKPHeader(now, " max-age = ", chain_hashes,
159 &expiry, &hashes));
160 EXPECT_FALSE(ParseHPKPHeader(now, " max-age = xy", chain_hashes,
161 &expiry, &hashes));
162 EXPECT_FALSE(ParseHPKPHeader(now,
163 " max-age = 3488a923",
164 chain_hashes, &expiry, &hashes));
165 EXPECT_FALSE(ParseHPKPHeader(now, "max-age=3488a923 ", chain_hashes,
166 &expiry, &hashes));
167 EXPECT_FALSE(ParseHPKPHeader(now,
168 "max-ag=3488923pins=" + good_pin + "," +
169 backup_pin,
170 chain_hashes, &expiry, &hashes));
171 EXPECT_FALSE(ParseHPKPHeader(now, "max-aged=3488923" + backup_pin,
172 chain_hashes, &expiry, &hashes));
173 EXPECT_FALSE(ParseHPKPHeader(now, "max-aged=3488923; " + backup_pin,
174 chain_hashes, &expiry, &hashes));
175 EXPECT_FALSE(ParseHPKPHeader(now,
176 "max-aged=3488923; " + backup_pin + ";" +
177 backup_pin,
178 chain_hashes, &expiry, &hashes));
179 EXPECT_FALSE(ParseHPKPHeader(now,
180 "max-aged=3488923; " + good_pin + ";" +
181 good_pin,
182 chain_hashes, &expiry, &hashes));
183 EXPECT_FALSE(ParseHPKPHeader(now, "max-aged=3488923; " + good_pin,
184 chain_hashes, &expiry, &hashes));
185 EXPECT_FALSE(ParseHPKPHeader(now, "max-age==3488923", chain_hashes, &expiry,
186 &hashes));
187 EXPECT_FALSE(ParseHPKPHeader(now, "amax-age=3488923", chain_hashes, &expiry,
188 &hashes));
189 EXPECT_FALSE(ParseHPKPHeader(now, "max-age=-3488923", chain_hashes, &expiry,
190 &hashes));
191 EXPECT_FALSE(ParseHPKPHeader(now, "max-age=3488923;", chain_hashes, &expiry,
192 &hashes));
193 EXPECT_FALSE(ParseHPKPHeader(now, "max-age=3488923 e", chain_hashes,
194 &expiry, &hashes));
195 EXPECT_FALSE(ParseHPKPHeader(now,
196 "max-age=3488923 includesubdomain",
197 chain_hashes, &expiry, &hashes));
198 EXPECT_FALSE(ParseHPKPHeader(now, "max-age=34889.23", chain_hashes, &expiry,
199 &hashes));
200
201 // Check the out args were not updated by checking the default
202 // values for its predictable fields.
203 EXPECT_EQ(now, expiry);
204 EXPECT_EQ(hashes.size(), (size_t)0);
205 }
206
207 TEST_F(HttpSecurityHeadersTest, ValidSTSHeaders) {
208 base::Time now = base::Time::Now();
209 base::Time expiry = now;
210 base::Time expect_expiry = now;
211 bool include_subdomains = false;
212
213 EXPECT_TRUE(ParseHSTSHeader(now, "max-age=243", &expiry,
214 &include_subdomains));
215 expect_expiry = now + base::TimeDelta::FromSeconds(243);
216 EXPECT_EQ(expect_expiry, expiry);
217 EXPECT_FALSE(include_subdomains);
218
219 EXPECT_TRUE(ParseHSTSHeader(now, " Max-agE = 567", &expiry,
220 &include_subdomains));
221 expect_expiry = now + base::TimeDelta::FromSeconds(567);
222 EXPECT_EQ(expect_expiry, expiry);
223 EXPECT_FALSE(include_subdomains);
224
225 EXPECT_TRUE(ParseHSTSHeader(now, " mAx-aGe = 890 ", &expiry,
226 &include_subdomains));
227 expect_expiry = now + base::TimeDelta::FromSeconds(890);
228 EXPECT_EQ(expect_expiry, expiry);
229 EXPECT_FALSE(include_subdomains);
230
231 EXPECT_TRUE(ParseHSTSHeader(now, "max-age=123;incLudesUbdOmains", &expiry,
232 &include_subdomains));
233 expect_expiry = now + base::TimeDelta::FromSeconds(123);
234 EXPECT_EQ(expect_expiry, expiry);
235 EXPECT_TRUE(include_subdomains);
236
237 EXPECT_TRUE(ParseHSTSHeader(now, "incLudesUbdOmains; max-age=123", &expiry,
238 &include_subdomains));
239 expect_expiry = now + base::TimeDelta::FromSeconds(123);
240 EXPECT_EQ(expect_expiry, expiry);
241 EXPECT_TRUE(include_subdomains);
242
243 EXPECT_TRUE(ParseHSTSHeader(now, " incLudesUbdOmains; max-age=123",
244 &expiry, &include_subdomains));
245 expect_expiry = now + base::TimeDelta::FromSeconds(123);
246 EXPECT_EQ(expect_expiry, expiry);
247 EXPECT_TRUE(include_subdomains);
248
249 EXPECT_TRUE(ParseHSTSHeader(now,
250 " incLudesUbdOmains; max-age=123; pumpkin=kitten", &expiry,
251 &include_subdomains));
252 expect_expiry = now + base::TimeDelta::FromSeconds(123);
253 EXPECT_EQ(expect_expiry, expiry);
254 EXPECT_TRUE(include_subdomains);
255
256 EXPECT_TRUE(ParseHSTSHeader(now,
257 " pumpkin=894; incLudesUbdOmains; max-age=123 ", &expiry,
258 &include_subdomains));
259 expect_expiry = now + base::TimeDelta::FromSeconds(123);
260 EXPECT_EQ(expect_expiry, expiry);
261 EXPECT_TRUE(include_subdomains);
262
263 EXPECT_TRUE(ParseHSTSHeader(now,
264 " pumpkin; incLudesUbdOmains; max-age=123 ", &expiry,
265 &include_subdomains));
266 expect_expiry = now + base::TimeDelta::FromSeconds(123);
267 EXPECT_EQ(expect_expiry, expiry);
268 EXPECT_TRUE(include_subdomains);
269
270 EXPECT_TRUE(ParseHSTSHeader(now,
271 " pumpkin; incLudesUbdOmains; max-age=\"123\" ", &expiry,
272 &include_subdomains));
273 expect_expiry = now + base::TimeDelta::FromSeconds(123);
274 EXPECT_EQ(expect_expiry, expiry);
275 EXPECT_TRUE(include_subdomains);
276
277 EXPECT_TRUE(ParseHSTSHeader(now,
278 "animal=\"squirrel; distinguished\"; incLudesUbdOmains; max-age=123",
279 &expiry, &include_subdomains));
280 expect_expiry = now + base::TimeDelta::FromSeconds(123);
281 EXPECT_EQ(expect_expiry, expiry);
282 EXPECT_TRUE(include_subdomains);
283
284 EXPECT_TRUE(ParseHSTSHeader(now, "max-age=394082; incLudesUbdOmains",
285 &expiry, &include_subdomains));
286 expect_expiry = now + base::TimeDelta::FromSeconds(394082);
287 EXPECT_EQ(expect_expiry, expiry);
288 EXPECT_TRUE(include_subdomains);
289
290 EXPECT_TRUE(ParseHSTSHeader(
291 now, "max-age=39408299 ;incLudesUbdOmains", &expiry,
292 &include_subdomains));
293 expect_expiry = now + base::TimeDelta::FromSeconds(
294 std::min(kMaxHSTSAgeSecs, static_cast<int64>(GG_INT64_C(39408299))));
295 EXPECT_EQ(expect_expiry, expiry);
296 EXPECT_TRUE(include_subdomains);
297
298 EXPECT_TRUE(ParseHSTSHeader(
299 now, "max-age=394082038 ; incLudesUbdOmains", &expiry,
300 &include_subdomains));
301 expect_expiry = now + base::TimeDelta::FromSeconds(
302 std::min(kMaxHSTSAgeSecs, static_cast<int64>(GG_INT64_C(394082038))));
303 EXPECT_EQ(expect_expiry, expiry);
304 EXPECT_TRUE(include_subdomains);
305
306 EXPECT_TRUE(ParseHSTSHeader(
307 now, " max-age=0 ; incLudesUbdOmains ", &expiry,
308 &include_subdomains));
309 expect_expiry = now + base::TimeDelta::FromSeconds(0);
310 EXPECT_EQ(expect_expiry, expiry);
311 EXPECT_TRUE(include_subdomains);
312
313 EXPECT_TRUE(ParseHSTSHeader(
314 now,
315 " max-age=999999999999999999999999999999999999999999999 ;"
316 " incLudesUbdOmains ", &expiry, &include_subdomains));
317 expect_expiry = now + base::TimeDelta::FromSeconds(
318 kMaxHSTSAgeSecs);
319 EXPECT_EQ(expect_expiry, expiry);
320 EXPECT_TRUE(include_subdomains);
321 }
322
323 static void TestValidPinsHeaders(HashValueTag tag) {
324 base::Time now = base::Time::Now();
325 base::Time expiry = now;
326 base::Time expect_expiry = now;
327 HashValueVector hashes;
328 HashValueVector chain_hashes;
329
330 // Set some fake "chain" hashes into chain_hashes
331 chain_hashes.push_back(GetTestHashValue(1, tag));
332 chain_hashes.push_back(GetTestHashValue(2, tag));
333 chain_hashes.push_back(GetTestHashValue(3, tag));
334
335 // The good pin must be in the chain, the backup pin must not be
336 std::string good_pin = GetTestPin(2, tag);
337 std::string backup_pin = GetTestPin(4, tag);
338
339 EXPECT_TRUE(ParseHPKPHeader(
340 now,
341 "max-age=243; " + good_pin + ";" + backup_pin,
342 chain_hashes, &expiry, &hashes));
343 expect_expiry = now + base::TimeDelta::FromSeconds(243);
344 EXPECT_EQ(expect_expiry, expiry);
345
346 EXPECT_TRUE(ParseHPKPHeader(
347 now,
348 " " + good_pin + "; " + backup_pin + " ; Max-agE = 567",
349 chain_hashes, &expiry, &hashes));
350 expect_expiry = now + base::TimeDelta::FromSeconds(567);
351 EXPECT_EQ(expect_expiry, expiry);
352
353 EXPECT_TRUE(ParseHPKPHeader(
354 now,
355 good_pin + ";" + backup_pin + " ; mAx-aGe = 890 ",
356 chain_hashes, &expiry, &hashes));
357 expect_expiry = now + base::TimeDelta::FromSeconds(890);
358 EXPECT_EQ(expect_expiry, expiry);
359
360 EXPECT_TRUE(ParseHPKPHeader(
361 now,
362 good_pin + ";" + backup_pin + "; max-age=123;IGNORED;",
363 chain_hashes, &expiry, &hashes));
364 expect_expiry = now + base::TimeDelta::FromSeconds(123);
365 EXPECT_EQ(expect_expiry, expiry);
366
367 EXPECT_TRUE(ParseHPKPHeader(
368 now,
369 "max-age=394082;" + backup_pin + ";" + good_pin + "; ",
370 chain_hashes, &expiry, &hashes));
371 expect_expiry = now + base::TimeDelta::FromSeconds(394082);
372 EXPECT_EQ(expect_expiry, expiry);
373
374 EXPECT_TRUE(ParseHPKPHeader(
375 now,
376 "max-age=39408299 ;" + backup_pin + ";" + good_pin + "; ",
377 chain_hashes, &expiry, &hashes));
378 expect_expiry = now + base::TimeDelta::FromSeconds(
379 std::min(kMaxHSTSAgeSecs, static_cast<int64>(GG_INT64_C(39408299))));
380 EXPECT_EQ(expect_expiry, expiry);
381
382 EXPECT_TRUE(ParseHPKPHeader(
383 now,
384 "max-age=39408038 ; cybers=39408038 ; " +
385 good_pin + ";" + backup_pin + "; ",
386 chain_hashes, &expiry, &hashes));
387 expect_expiry = now + base::TimeDelta::FromSeconds(
388 std::min(kMaxHSTSAgeSecs, static_cast<int64>(GG_INT64_C(394082038))));
389 EXPECT_EQ(expect_expiry, expiry);
390
391 EXPECT_TRUE(ParseHPKPHeader(
392 now,
393 " max-age=0 ; " + good_pin + ";" + backup_pin,
394 chain_hashes, &expiry, &hashes));
395 expect_expiry = now + base::TimeDelta::FromSeconds(0);
396 EXPECT_EQ(expect_expiry, expiry);
397
398 EXPECT_TRUE(ParseHPKPHeader(
399 now,
400 " max-age=999999999999999999999999999999999999999999999 ; " +
401 backup_pin + ";" + good_pin + "; ",
402 chain_hashes, &expiry, &hashes));
403 expect_expiry = now +
404 base::TimeDelta::FromSeconds(kMaxHSTSAgeSecs);
405 EXPECT_EQ(expect_expiry, expiry);
406 }
407
408 TEST_F(HttpSecurityHeadersTest, BogusPinsHeadersSHA1) {
409 TestBogusPinsHeaders(HASH_VALUE_SHA1);
410 }
411
412 TEST_F(HttpSecurityHeadersTest, BogusPinsHeadersSHA256) {
413 TestBogusPinsHeaders(HASH_VALUE_SHA256);
414 }
415
416 TEST_F(HttpSecurityHeadersTest, ValidPinsHeadersSHA1) {
417 TestValidPinsHeaders(HASH_VALUE_SHA1);
418 }
419
420 TEST_F(HttpSecurityHeadersTest, ValidPinsHeadersSHA256) {
421 TestValidPinsHeaders(HASH_VALUE_SHA256);
422 }
423 };
424
OLDNEW
« no previous file with comments | « net/http/http_security_headers.cc ('k') | net/net.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698