summaryrefslogtreecommitdiffstats
path: root/net/http/http_security_headers_unittest.cc
blob: 4f1e5804745c2864243c36b36f7e112ff9980ac4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/base64.h"
#include "base/sha1.h"
#include "base/string_piece.h"
#include "crypto/sha2.h"
#include "net/base/net_log.h"
#include "net/base/test_completion_callback.h"
#include "net/http/http_security_headers.h"
#include "net/http/http_util.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace net {

namespace {

HashValue GetTestHashValue(uint8 label, HashValueTag tag) {
  HashValue hash_value(tag);
  memset(hash_value.data(), label, hash_value.size());
  return hash_value;
}

std::string GetTestPin(uint8 label, HashValueTag tag) {
  HashValue hash_value = GetTestHashValue(label, tag);
  std::string base64;
  base::Base64Encode(base::StringPiece(
      reinterpret_cast<char*>(hash_value.data()), hash_value.size()), &base64);

  switch (hash_value.tag) {
    case HASH_VALUE_SHA1:
      return std::string("pin-sha1=\"") + base64 + "\"";
    case HASH_VALUE_SHA256:
      return std::string("pin-sha256=\"") + base64 + "\"";
    default:
      NOTREACHED() << "Unknown HashValueTag " << hash_value.tag;
      return std::string("ERROR");
  }
}

};


class HttpSecurityHeadersTest : public testing::Test {
};


TEST_F(HttpSecurityHeadersTest, BogusHeaders) {
  base::Time now = base::Time::Now();
  base::Time expiry = now;
  bool include_subdomains = false;

  EXPECT_FALSE(
      ParseHSTSHeader(now, std::string(), &expiry, &include_subdomains));
  EXPECT_FALSE(ParseHSTSHeader(now, "    ", &expiry, &include_subdomains));
  EXPECT_FALSE(ParseHSTSHeader(now, "abc", &expiry, &include_subdomains));
  EXPECT_FALSE(ParseHSTSHeader(now, "  abc", &expiry, &include_subdomains));
  EXPECT_FALSE(ParseHSTSHeader(now, "  abc   ", &expiry, &include_subdomains));
  EXPECT_FALSE(ParseHSTSHeader(now, "max-age", &expiry, &include_subdomains));
  EXPECT_FALSE(ParseHSTSHeader(now, "  max-age", &expiry,
                               &include_subdomains));
  EXPECT_FALSE(ParseHSTSHeader(now, "  max-age  ", &expiry,
                               &include_subdomains));
  EXPECT_FALSE(ParseHSTSHeader(now, "max-age=", &expiry, &include_subdomains));
  EXPECT_FALSE(ParseHSTSHeader(now, "   max-age=", &expiry,
                               &include_subdomains));
  EXPECT_FALSE(ParseHSTSHeader(now, "   max-age  =", &expiry,
                               &include_subdomains));
  EXPECT_FALSE(ParseHSTSHeader(now, "   max-age=   ", &expiry,
                               &include_subdomains));
  EXPECT_FALSE(ParseHSTSHeader(now, "   max-age  =     ", &expiry,
                               &include_subdomains));
  EXPECT_FALSE(ParseHSTSHeader(now, "   max-age  =     xy", &expiry,
                               &include_subdomains));
  EXPECT_FALSE(ParseHSTSHeader(now, "   max-age  =     3488a923", &expiry,
                               &include_subdomains));
  EXPECT_FALSE(ParseHSTSHeader(now, "max-age=3488a923  ", &expiry,
                               &include_subdomains));
  EXPECT_FALSE(ParseHSTSHeader(now, "max-ag=3488923", &expiry,
                               &include_subdomains));
  EXPECT_FALSE(ParseHSTSHeader(now, "max-aged=3488923", &expiry,
                               &include_subdomains));
  EXPECT_FALSE(ParseHSTSHeader(now, "max-age==3488923", &expiry,
                               &include_subdomains));
  EXPECT_FALSE(ParseHSTSHeader(now, "amax-age=3488923", &expiry,
                               &include_subdomains));
  EXPECT_FALSE(ParseHSTSHeader(now, "max-age=-3488923", &expiry,
                               &include_subdomains));
  EXPECT_FALSE(ParseHSTSHeader(now, "max-age=3488923;", &expiry,
                               &include_subdomains));
  EXPECT_FALSE(ParseHSTSHeader(now, "max-age=3488923     e", &expiry,
                               &include_subdomains));
  EXPECT_FALSE(ParseHSTSHeader(now,
                               "max-age=3488923     includesubdomain",
                               &expiry, &include_subdomains));
  EXPECT_FALSE(ParseHSTSHeader(now, "max-age=3488923includesubdomains",
                               &expiry, &include_subdomains));
  EXPECT_FALSE(ParseHSTSHeader(now, "max-age=3488923=includesubdomains",
                               &expiry, &include_subdomains));
  EXPECT_FALSE(ParseHSTSHeader(now, "max-age=3488923 includesubdomainx",
                               &expiry, &include_subdomains));
  EXPECT_FALSE(ParseHSTSHeader(now, "max-age=3488923 includesubdomain=",
                               &expiry, &include_subdomains));
  EXPECT_FALSE(ParseHSTSHeader(now,
                               "max-age=3488923 includesubdomain=true",
                               &expiry, &include_subdomains));
  EXPECT_FALSE(ParseHSTSHeader(now, "max-age=3488923 includesubdomainsx",
                               &expiry, &include_subdomains));
  EXPECT_FALSE(ParseHSTSHeader(now,
                               "max-age=3488923 includesubdomains x",
                               &expiry, &include_subdomains));
  EXPECT_FALSE(ParseHSTSHeader(now, "max-age=34889.23 includesubdomains",
                               &expiry, &include_subdomains));
  EXPECT_FALSE(ParseHSTSHeader(now, "max-age=34889 includesubdomains",
                               &expiry, &include_subdomains));

  // Check the out args were not updated by checking the default
  // values for its predictable fields.
  EXPECT_EQ(now, expiry);
  EXPECT_FALSE(include_subdomains);
}

static void TestBogusPinsHeaders(HashValueTag tag) {
  base::Time now = base::Time::Now();
  base::Time expiry = now;
  HashValueVector hashes;
  HashValueVector chain_hashes;

  // Set some fake "chain" hashes
  chain_hashes.push_back(GetTestHashValue(1, tag));
  chain_hashes.push_back(GetTestHashValue(2, tag));
  chain_hashes.push_back(GetTestHashValue(3, tag));

  // The good pin must be in the chain, the backup pin must not be
  std::string good_pin = GetTestPin(2, tag);
  std::string backup_pin = GetTestPin(4, tag);

  EXPECT_FALSE(
      ParseHPKPHeader(now, std::string(), chain_hashes, &expiry, &hashes));
  EXPECT_FALSE(ParseHPKPHeader(now, "    ", chain_hashes, &expiry, &hashes));
  EXPECT_FALSE(ParseHPKPHeader(now, "abc", chain_hashes, &expiry, &hashes));
  EXPECT_FALSE(ParseHPKPHeader(now, "  abc", chain_hashes, &expiry, &hashes));
  EXPECT_FALSE(ParseHPKPHeader(now, "  abc   ", chain_hashes, &expiry,
                               &hashes));
  EXPECT_FALSE(ParseHPKPHeader(now, "max-age", chain_hashes, &expiry,
                               &hashes));
  EXPECT_FALSE(ParseHPKPHeader(now, "  max-age", chain_hashes, &expiry,
                               &hashes));
  EXPECT_FALSE(ParseHPKPHeader(now, "  max-age  ", chain_hashes, &expiry,
                               &hashes));
  EXPECT_FALSE(ParseHPKPHeader(now, "max-age=", chain_hashes, &expiry,
                               &hashes));
  EXPECT_FALSE(ParseHPKPHeader(now, "   max-age=", chain_hashes, &expiry,
                               &hashes));
  EXPECT_FALSE(ParseHPKPHeader(now, "   max-age  =", chain_hashes, &expiry,
                               &hashes));
  EXPECT_FALSE(ParseHPKPHeader(now, "   max-age=   ", chain_hashes, &expiry,
                               &hashes));
  EXPECT_FALSE(ParseHPKPHeader(now, "   max-age  =     ", chain_hashes,
                               &expiry, &hashes));
  EXPECT_FALSE(ParseHPKPHeader(now, "   max-age  =     xy", chain_hashes,
                               &expiry, &hashes));
  EXPECT_FALSE(ParseHPKPHeader(now,
                               "   max-age  =     3488a923",
                               chain_hashes, &expiry, &hashes));
  EXPECT_FALSE(ParseHPKPHeader(now, "max-age=3488a923  ", chain_hashes,
                               &expiry, &hashes));
  EXPECT_FALSE(ParseHPKPHeader(now,
                               "max-ag=3488923pins=" + good_pin + "," +
                               backup_pin,
                               chain_hashes, &expiry, &hashes));
  EXPECT_FALSE(ParseHPKPHeader(now, "max-aged=3488923" + backup_pin,
                               chain_hashes, &expiry, &hashes));
  EXPECT_FALSE(ParseHPKPHeader(now, "max-aged=3488923; " + backup_pin,
                               chain_hashes, &expiry, &hashes));
  EXPECT_FALSE(ParseHPKPHeader(now,
                               "max-aged=3488923; " + backup_pin + ";" +
                               backup_pin,
                               chain_hashes, &expiry, &hashes));
  EXPECT_FALSE(ParseHPKPHeader(now,
                               "max-aged=3488923; " + good_pin + ";" +
                               good_pin,
                               chain_hashes, &expiry, &hashes));
  EXPECT_FALSE(ParseHPKPHeader(now, "max-aged=3488923; " + good_pin,
                               chain_hashes, &expiry, &hashes));
  EXPECT_FALSE(ParseHPKPHeader(now, "max-age==3488923", chain_hashes, &expiry,
                               &hashes));
  EXPECT_FALSE(ParseHPKPHeader(now, "amax-age=3488923", chain_hashes, &expiry,
                               &hashes));
  EXPECT_FALSE(ParseHPKPHeader(now, "max-age=-3488923", chain_hashes, &expiry,
                               &hashes));
  EXPECT_FALSE(ParseHPKPHeader(now, "max-age=3488923;", chain_hashes, &expiry,
                               &hashes));
  EXPECT_FALSE(ParseHPKPHeader(now, "max-age=3488923     e", chain_hashes,
                               &expiry, &hashes));
  EXPECT_FALSE(ParseHPKPHeader(now,
                               "max-age=3488923     includesubdomain",
                               chain_hashes, &expiry, &hashes));
  EXPECT_FALSE(ParseHPKPHeader(now, "max-age=34889.23", chain_hashes, &expiry,
                               &hashes));

  // Check the out args were not updated by checking the default
  // values for its predictable fields.
  EXPECT_EQ(now, expiry);
  EXPECT_EQ(hashes.size(), (size_t)0);
}

TEST_F(HttpSecurityHeadersTest, ValidSTSHeaders) {
  base::Time now = base::Time::Now();
  base::Time expiry = now;
  base::Time expect_expiry = now;
  bool include_subdomains = false;

  EXPECT_TRUE(ParseHSTSHeader(now, "max-age=243", &expiry,
                              &include_subdomains));
  expect_expiry = now + base::TimeDelta::FromSeconds(243);
  EXPECT_EQ(expect_expiry, expiry);
  EXPECT_FALSE(include_subdomains);

  EXPECT_TRUE(ParseHSTSHeader(now, "  Max-agE    = 567", &expiry,
                              &include_subdomains));
  expect_expiry = now + base::TimeDelta::FromSeconds(567);
  EXPECT_EQ(expect_expiry, expiry);
  EXPECT_FALSE(include_subdomains);

  EXPECT_TRUE(ParseHSTSHeader(now, "  mAx-aGe    = 890      ", &expiry,
                              &include_subdomains));
  expect_expiry = now + base::TimeDelta::FromSeconds(890);
  EXPECT_EQ(expect_expiry, expiry);
  EXPECT_FALSE(include_subdomains);

  EXPECT_TRUE(ParseHSTSHeader(now, "max-age=123;incLudesUbdOmains", &expiry,
                              &include_subdomains));
  expect_expiry = now + base::TimeDelta::FromSeconds(123);
  EXPECT_EQ(expect_expiry, expiry);
  EXPECT_TRUE(include_subdomains);

  EXPECT_TRUE(ParseHSTSHeader(now, "incLudesUbdOmains; max-age=123", &expiry,
                              &include_subdomains));
  expect_expiry = now + base::TimeDelta::FromSeconds(123);
  EXPECT_EQ(expect_expiry, expiry);
  EXPECT_TRUE(include_subdomains);

  EXPECT_TRUE(ParseHSTSHeader(now, "   incLudesUbdOmains; max-age=123",
                              &expiry, &include_subdomains));
  expect_expiry = now + base::TimeDelta::FromSeconds(123);
  EXPECT_EQ(expect_expiry, expiry);
  EXPECT_TRUE(include_subdomains);

  EXPECT_TRUE(ParseHSTSHeader(now,
      "   incLudesUbdOmains; max-age=123; pumpkin=kitten", &expiry,
                                   &include_subdomains));
  expect_expiry = now + base::TimeDelta::FromSeconds(123);
  EXPECT_EQ(expect_expiry, expiry);
  EXPECT_TRUE(include_subdomains);

  EXPECT_TRUE(ParseHSTSHeader(now,
      "   pumpkin=894; incLudesUbdOmains; max-age=123  ", &expiry,
                                   &include_subdomains));
  expect_expiry = now + base::TimeDelta::FromSeconds(123);
  EXPECT_EQ(expect_expiry, expiry);
  EXPECT_TRUE(include_subdomains);

  EXPECT_TRUE(ParseHSTSHeader(now,
      "   pumpkin; incLudesUbdOmains; max-age=123  ", &expiry,
                                   &include_subdomains));
  expect_expiry = now + base::TimeDelta::FromSeconds(123);
  EXPECT_EQ(expect_expiry, expiry);
  EXPECT_TRUE(include_subdomains);

  EXPECT_TRUE(ParseHSTSHeader(now,
      "   pumpkin; incLudesUbdOmains; max-age=\"123\"  ", &expiry,
                                   &include_subdomains));
  expect_expiry = now + base::TimeDelta::FromSeconds(123);
  EXPECT_EQ(expect_expiry, expiry);
  EXPECT_TRUE(include_subdomains);

  EXPECT_TRUE(ParseHSTSHeader(now,
      "animal=\"squirrel; distinguished\"; incLudesUbdOmains; max-age=123",
                                   &expiry, &include_subdomains));
  expect_expiry = now + base::TimeDelta::FromSeconds(123);
  EXPECT_EQ(expect_expiry, expiry);
  EXPECT_TRUE(include_subdomains);

  EXPECT_TRUE(ParseHSTSHeader(now, "max-age=394082;  incLudesUbdOmains",
                              &expiry, &include_subdomains));
  expect_expiry = now + base::TimeDelta::FromSeconds(394082);
  EXPECT_EQ(expect_expiry, expiry);
  EXPECT_TRUE(include_subdomains);

  EXPECT_TRUE(ParseHSTSHeader(
      now, "max-age=39408299  ;incLudesUbdOmains", &expiry,
      &include_subdomains));
  expect_expiry = now + base::TimeDelta::FromSeconds(
      std::min(kMaxHSTSAgeSecs, static_cast<int64>(GG_INT64_C(39408299))));
  EXPECT_EQ(expect_expiry, expiry);
  EXPECT_TRUE(include_subdomains);

  EXPECT_TRUE(ParseHSTSHeader(
      now, "max-age=394082038  ; incLudesUbdOmains", &expiry,
      &include_subdomains));
  expect_expiry = now + base::TimeDelta::FromSeconds(
      std::min(kMaxHSTSAgeSecs, static_cast<int64>(GG_INT64_C(394082038))));
  EXPECT_EQ(expect_expiry, expiry);
  EXPECT_TRUE(include_subdomains);

  EXPECT_TRUE(ParseHSTSHeader(
      now, "  max-age=0  ;  incLudesUbdOmains   ", &expiry,
      &include_subdomains));
  expect_expiry = now + base::TimeDelta::FromSeconds(0);
  EXPECT_EQ(expect_expiry, expiry);
  EXPECT_TRUE(include_subdomains);

  EXPECT_TRUE(ParseHSTSHeader(
      now,
      "  max-age=999999999999999999999999999999999999999999999  ;"
      "  incLudesUbdOmains   ", &expiry, &include_subdomains));
  expect_expiry = now + base::TimeDelta::FromSeconds(
      kMaxHSTSAgeSecs);
  EXPECT_EQ(expect_expiry, expiry);
  EXPECT_TRUE(include_subdomains);
}

static void TestValidPinsHeaders(HashValueTag tag) {
  base::Time now = base::Time::Now();
  base::Time expiry = now;
  base::Time expect_expiry = now;
  HashValueVector hashes;
  HashValueVector chain_hashes;

  // Set some fake "chain" hashes into chain_hashes
  chain_hashes.push_back(GetTestHashValue(1, tag));
  chain_hashes.push_back(GetTestHashValue(2, tag));
  chain_hashes.push_back(GetTestHashValue(3, tag));

  // The good pin must be in the chain, the backup pin must not be
  std::string good_pin = GetTestPin(2, tag);
  std::string backup_pin = GetTestPin(4, tag);

  EXPECT_TRUE(ParseHPKPHeader(
      now,
      "max-age=243; " + good_pin + ";" + backup_pin,
      chain_hashes, &expiry, &hashes));
  expect_expiry = now + base::TimeDelta::FromSeconds(243);
  EXPECT_EQ(expect_expiry, expiry);

  EXPECT_TRUE(ParseHPKPHeader(
      now,
      "   " + good_pin + "; " + backup_pin + "  ; Max-agE    = 567",
      chain_hashes, &expiry, &hashes));
  expect_expiry = now + base::TimeDelta::FromSeconds(567);
  EXPECT_EQ(expect_expiry, expiry);

  EXPECT_TRUE(ParseHPKPHeader(
      now,
      good_pin + ";" + backup_pin + "  ; mAx-aGe    = 890      ",
      chain_hashes, &expiry, &hashes));
  expect_expiry = now + base::TimeDelta::FromSeconds(890);
  EXPECT_EQ(expect_expiry, expiry);

  EXPECT_TRUE(ParseHPKPHeader(
      now,
      good_pin + ";" + backup_pin + "; max-age=123;IGNORED;",
      chain_hashes, &expiry, &hashes));
  expect_expiry = now + base::TimeDelta::FromSeconds(123);
  EXPECT_EQ(expect_expiry, expiry);

  EXPECT_TRUE(ParseHPKPHeader(
      now,
      "max-age=394082;" + backup_pin + ";" + good_pin + ";  ",
      chain_hashes, &expiry, &hashes));
  expect_expiry = now + base::TimeDelta::FromSeconds(394082);
  EXPECT_EQ(expect_expiry, expiry);

  EXPECT_TRUE(ParseHPKPHeader(
      now,
      "max-age=39408299  ;" + backup_pin + ";" + good_pin + ";  ",
      chain_hashes, &expiry, &hashes));
  expect_expiry = now + base::TimeDelta::FromSeconds(
      std::min(kMaxHSTSAgeSecs, static_cast<int64>(GG_INT64_C(39408299))));
  EXPECT_EQ(expect_expiry, expiry);

  EXPECT_TRUE(ParseHPKPHeader(
      now,
      "max-age=39408038  ;    cybers=39408038  ;  " +
          good_pin + ";" + backup_pin + ";   ",
      chain_hashes, &expiry, &hashes));
  expect_expiry = now + base::TimeDelta::FromSeconds(
      std::min(kMaxHSTSAgeSecs, static_cast<int64>(GG_INT64_C(394082038))));
  EXPECT_EQ(expect_expiry, expiry);

  EXPECT_TRUE(ParseHPKPHeader(
      now,
      "  max-age=0  ;  " + good_pin + ";" + backup_pin,
      chain_hashes, &expiry, &hashes));
  expect_expiry = now + base::TimeDelta::FromSeconds(0);
  EXPECT_EQ(expect_expiry, expiry);

  EXPECT_TRUE(ParseHPKPHeader(
      now,
      "  max-age=999999999999999999999999999999999999999999999  ;  " +
          backup_pin + ";" + good_pin + ";   ",
      chain_hashes, &expiry, &hashes));
  expect_expiry = now +
      base::TimeDelta::FromSeconds(kMaxHSTSAgeSecs);
  EXPECT_EQ(expect_expiry, expiry);
}

TEST_F(HttpSecurityHeadersTest, BogusPinsHeadersSHA1) {
  TestBogusPinsHeaders(HASH_VALUE_SHA1);
}

TEST_F(HttpSecurityHeadersTest, BogusPinsHeadersSHA256) {
  TestBogusPinsHeaders(HASH_VALUE_SHA256);
}

TEST_F(HttpSecurityHeadersTest, ValidPinsHeadersSHA1) {
  TestValidPinsHeaders(HASH_VALUE_SHA1);
}

TEST_F(HttpSecurityHeadersTest, ValidPinsHeadersSHA256) {
  TestValidPinsHeaders(HASH_VALUE_SHA256);
}
};