-
Notifications
You must be signed in to change notification settings - Fork 38
/
domain_test.go
304 lines (282 loc) · 7.77 KB
/
domain_test.go
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
package hstspreload
import (
"fmt"
"sync"
"testing"
)
func ExamplePreloadableDomain() {
header, issues := PreloadableDomain("wikipedia.org")
if header != nil {
fmt.Printf("Header: %s", *header)
}
fmt.Printf("Issues %v", issues)
}
/******** Utility functions tests. ********/
var testCheckDomainFormatTests = []struct {
domain string
expected Issues
}{
{".example.com",
Issues{Errors: []Issue{{Code: "domain.format.begins_with_dot"}}},
},
{"example.com.",
Issues{Errors: []Issue{{Code: "domain.format.ends_with_dot"}}},
},
{"example..com",
Issues{Errors: []Issue{{Code: "domain.format.contains_double_dot"}}},
},
{"example",
Issues{Errors: []Issue{{Code: "domain.format.public_suffix"}}},
},
{"co.uk",
Issues{Errors: []Issue{{Code: "domain.format.public_suffix"}}},
},
{"example&co.com",
Issues{Errors: []Issue{{Code: "domain.format.invalid_characters"}}},
},
{"1.1.1.1",
Issues{Errors: []Issue{{Code: "domain.format.is_ip_address"}}},
},
}
func TestCheckDomainFormat(t *testing.T) {
for _, tt := range testCheckDomainFormatTests {
issues := checkDomainFormat(tt.domain)
if !issues.Match(tt.expected) {
t.Errorf(issuesShouldMatch, issues, tt.expected)
}
}
}
var testPreloadableDomainLevel = []struct {
domain string
expected Issues
}{
{"github.io",
Issues{Errors: []Issue{{
Code: "internal.domain.name.cannot_compute_etld1",
}}},
},
{"example.com",
Issues{},
},
{"example.co.uk",
Issues{},
},
{"subdomain.example.com",
Issues{Errors: []Issue{{
Code: "domain.is_subdomain",
Message: "`subdomain.example.com` is a subdomain. Please preload `example.com` instead. (Due to the size of the preload list and the behaviour of cookies across subdomains, we only accept automated preload list submissions of whole registered domains.)",
}}},
},
}
func TestPreloadableDomainLevel(t *testing.T) {
for _, tt := range testPreloadableDomainLevel {
issues := preloadableDomainLevel(tt.domain)
if !issues.Match(tt.expected) {
t.Errorf(issuesShouldMatch, issues, tt.expected)
}
}
}
/******** Real domain tests. ********/
// Avoid hitting the network for short tests.
// This gives us performant, deterministic, and offline testing.
func skipIfShort(t *testing.T) {
if testing.Short() {
t.Skip("Skipping domain test.")
}
}
type preloadableDomainTest struct {
function func(domain string) (*string, Issues)
description string
domain string
expectHeader bool
expectedHeader string
expectedIssues Issues
}
var preloadableDomainTests = []preloadableDomainTest{
/********* PreloadableDomain() ********/
{
PreloadableDomain,
"valid HSTS",
"wikipedia.org",
true, "max-age=106384710; includeSubDomains; preload",
Issues{},
},
{
PreloadableDomain,
"no TLS",
"neverssl.com",
false, "",
Issues{
Errors: []Issue{{Code: "domain.tls.cannot_connect"}},
},
},
{
PreloadableDomain,
"incomplete chain",
"incomplete-chain.badssl.com",
false, "",
Issues{
Errors: []Issue{
{Code: "domain.is_subdomain"},
{
Code: "domain.tls.invalid_cert_chain",
Message: "https://incomplete-chain.badssl.com uses an incomplete or invalid certificate chain. Check out your site at https://www.ssllabs.com/ssltest/",
},
},
},
},
{
PreloadableDomain,
"www.no_tls (not allowed)",
"lgarron.github.io",
false, "",
Issues{
Errors: []Issue{
Issue{Code: "response.no_header", Summary: "No HSTS header", Message: "Response error: No HSTS header is present on the response."},
Issue{Code: "domain.www.no_tls", Summary: "www subdomain does not support HTTPS", Message: "Domain error: The www subdomain exists, but we couldn't connect to it using HTTPS (\"x509: certificate is valid for www.github.com, *.github.com, github.com, *.github.io, github.io, *.githubusercontent.com, githubusercontent.com, not www.lgarron.github.io\"). Since many people type this by habit, HSTS preloading would likely cause issues for your site."},
},
},
},
{
PreloadableDomain,
"www.no_tls (allowed)",
"hstspreload.appspot.com",
true, "max-age=31536000; includeSubDomains; preload",
Issues{},
},
{
PreloadableDomain,
"self-signed",
"self-signed.badssl.com",
false, "",
Issues{
Errors: []Issue{
{Code: "domain.is_subdomain"},
{
Code: "domain.tls.invalid_cert_chain",
Message: "https://self-signed.badssl.com uses an incomplete or invalid certificate chain. Check out your site at https://www.ssllabs.com/ssltest/",
},
},
},
},
{
PreloadableDomain,
"SHA-1",
"sha1-intermediate.badssl.com",
false, "",
Issues{
Errors: []Issue{
{Code: "domain.is_subdomain"},
{
Code: "domain.tls.sha1",
Message: "One or more of the certificates in your certificate chain is signed using SHA-1. This needs to be replaced. See https://security.googleblog.com/2015/12/an-update-on-sha-1-certificates-in.html. (The first SHA-1 certificate found has a common-name of \"COMODO SSL CA\".)",
},
{Code: "response.no_header"},
},
},
},
{
PreloadableDomain,
"obsolete cipher suite",
"cbc.badssl.com",
false, "",
Issues{
Errors: []Issue{
{Code: "domain.is_subdomain"},
{Code: "response.no_header"},
},
Warnings: []Issue{
{
Code: "tls.obsolete_cipher_suite",
Message: "The site is using obsolete TLS settings. Check out the site at https://www.ssllabs.com/ssltest/",
},
},
},
},
{
PreloadableDomain,
"subdomain",
"en.wikipedia.org",
true, "max-age=106384710; includeSubDomains; preload",
Issues{Errors: []Issue{{
Code: "domain.is_subdomain",
Message: "`en.wikipedia.org` is a subdomain. Please preload `wikipedia.org` instead. (Due to the size of the preload list and the behaviour of cookies across subdomains, we only accept automated preload list submissions of whole registered domains.)",
}}},
},
{
PreloadableDomain,
"no HSTS",
"example.com",
false, "",
Issues{
Errors: []Issue{
{Code: "response.no_header"},
{
Code: "redirects.http.no_redirect",
Message: "`http://example.com` does not redirect to `https://example.com`.",
},
},
},
},
{
PreloadableDomain,
"bogus domain",
"example.notadomain",
false, "",
Issues{Errors: []Issue{{Code: "domain.tls.cannot_connect"}}},
},
/******** RemovableDomain() ********/
{
RemovableDomain,
"no header",
"example.com",
false, "",
Issues{Errors: []Issue{{Code: "response.no_header"}}},
},
{
RemovableDomain,
"no preload directive",
"hsts.badssl.com",
true, "max-age=15768000; includeSubDomains",
Issues{},
},
{
RemovableDomain,
"preloaded",
"preloaded-hsts.badssl.com",
true, "max-age=15768000; includeSubDomains; preload",
Issues{Errors: []Issue{{Code: "header.removable.contains.preload"}}},
},
}
func TestPreloadableDomainAndRemovableDomain(t *testing.T) {
// Skip this test because it is failing due to relying on behavior of an
// external domain: https://github.com/chromium/hstspreload/issues/112.
t.SkipNow()
skipIfShort(t)
t.Parallel()
wg := sync.WaitGroup{}
wg.Add(len(preloadableDomainTests))
for _, tt := range preloadableDomainTests {
go func(tt preloadableDomainTest) {
header, issues := tt.function(tt.domain)
if tt.expectHeader {
if header == nil {
t.Errorf("[%s] %s: Did not receive exactly one HSTS header", tt.description, tt.domain)
} else if *header != tt.expectedHeader {
t.Errorf(`[%s] %s: Did not receive expected header.
Actual: "%v"
Expected: "%v"`, tt.description, tt.domain, *header, tt.expectedHeader)
}
} else {
if header != nil {
t.Errorf("[%s] %s: Did not expect a header, but received `%s`", tt.description, tt.domain, *header)
}
}
if !issues.Match(tt.expectedIssues) {
t.Errorf("[%s] %s: "+issuesShouldMatch, tt.description, tt.domain, issues, tt.expectedIssues)
}
wg.Done()
}(tt)
}
wg.Wait()
}