-
Notifications
You must be signed in to change notification settings - Fork 6
/
test_hstspreload.py
79 lines (63 loc) · 2.27 KB
/
test_hstspreload.py
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
import base64
import hashlib
import json
import pytest
import urllib3
import hstspreload
HSTS_PRELOAD_URL = (
"https://chromium.googlesource.com/chromium/src/+/main/"
"net/http/transport_security_state_static.json?format=TEXT"
)
def load_test_cases():
http = urllib3.PoolManager()
r = http.request(
"GET",
HSTS_PRELOAD_URL,
headers={"Accept": "application/json"},
preload_content=True,
)
content = base64.b64decode(r.data)
content_checksum = hashlib.sha256(content).hexdigest()
assert content_checksum == hstspreload.__checksum__
content = content.decode("ascii")
entries = json.loads(
"\n".join(
[line for line in content.split("\n") if not line.strip().startswith("//")]
)
)["entries"]
allow_subdomains = []
for entry in sorted(entries, key=lambda x: len(x["name"])):
host = entry["name"].encode("ascii")
include_subdomains = entry.get("include_subdomains", False)
force_https = entry.get("mode", "") == "force-https"
if force_https:
yield host, True
if include_subdomains:
allow_subdomains.append(b"." + host)
# Handle cases where a subdomain like 'example.org' is
# given 'includeSubdomains' but then the 'www.example.org'
# entry isn't assigned 'includeSubdomains'. Thankfully
# this isn't allowed anymore and they only accept
# 'example.org' domains now.
if not include_subdomains:
for subdom in allow_subdomains:
if host.endswith(subdom):
include_subdomains = True
break
yield b"zzz-subdomain." + host, include_subdomains
@pytest.mark.parametrize(
["host", "expected"],
[
(b"www.google.com", False),
("www.google.com", False),
(b"google.com", False),
("google.com", False),
("paypal.com", True),
(b"paypal.com", True),
],
)
def test_data_types(host, expected):
assert hstspreload.in_hsts_preload(host) is expected
@pytest.mark.parametrize(["host", "expected"], list(load_test_cases()))
def test_in_hsts_preload(host, expected):
assert hstspreload.in_hsts_preload(host) is expected