-
Notifications
You must be signed in to change notification settings - Fork 38
/
redirects_test.go
338 lines (288 loc) · 10.3 KB
/
redirects_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
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
package hstspreload
import (
"net/url"
"sync"
"testing"
)
func chainsEqual(actual []*url.URL, expected []string) bool {
if len(actual) != len(expected) {
return false
}
for i, u := range actual {
if u.String() != expected[i] {
return false
}
}
return true
}
var tooManyRedirectsTests = []struct {
description string
url string
expectedChain []string
expectedIssues Issues
}{
{
"almost too many redirects",
"https://httpbin.org/redirect/3",
[]string{"https://httpbin.org/relative-redirect/2", "https://httpbin.org/relative-redirect/1", "https://httpbin.org/get"},
Issues{},
},
{
"too many redirects",
"https://httpbin.org/redirect/4",
[]string{"https://httpbin.org/relative-redirect/3", "https://httpbin.org/relative-redirect/2", "https://httpbin.org/relative-redirect/1", "https://httpbin.org/get"},
Issues{Errors: []Issue{{
Code: "redirects.too_many",
Message: "There are more than 3 redirects starting from `https://httpbin.org/redirect/4`.",
}}},
},
}
func TestTooManyRedirects(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()
for _, tt := range tooManyRedirectsTests {
chain, issues := preloadableRedirects(tt.url)
if !chainsEqual(chain, tt.expectedChain) {
t.Errorf("[%s] Unexpected chain: %v", tt.description, chain)
}
if !issues.Match(tt.expectedIssues) {
t.Errorf("[%s] "+issuesShouldMatch, tt.description, issues, tt.expectedIssues)
}
}
}
func TestInsecureRedirect(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()
u := "https://httpbin.org/redirect-to?url=http://httpbin.org"
chain, issues := preloadableRedirects(u)
if !chainsEqual(chain, []string{"http://httpbin.org"}) {
t.Errorf("Unexpected chain: %v", chain)
}
if !issues.Match(Issues{}) {
t.Errorf(issuesShouldBeEmpty, issues)
}
httpsIssues := preloadableHTTPSRedirectsURL(u)
expected := Issues{Errors: []Issue{{
Code: "redirects.insecure.initial",
Message: "`https://httpbin.org/redirect-to?url=http://httpbin.org` redirects to an insecure page: `http://httpbin.org`",
}}}
if !httpsIssues.Match(expected) {
t.Errorf(issuesShouldMatch, httpsIssues, expected)
}
}
func TestIndirectInsecureRedirect(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()
u := "https://httpbin.org/redirect-to?url=https://httpbin.org/redirect-to?url=http://httpbin.org"
chain, issues := preloadableRedirects(u)
if !chainsEqual(chain, []string{"https://httpbin.org/redirect-to?url=http://httpbin.org", "http://httpbin.org"}) {
t.Errorf("Unexpected chain: %v", chain)
}
if !issues.Match(Issues{}) {
t.Errorf(issuesShouldBeEmpty, issues)
}
httpsIssues := preloadableHTTPSRedirectsURL(u)
expected := Issues{Errors: []Issue{{
Code: "redirects.insecure.subsequent",
Message: "`https://httpbin.org/redirect-to?url=https://httpbin.org/redirect-to?url=http://httpbin.org` redirects to an insecure page on redirect #2: `http://httpbin.org`",
}}}
if !httpsIssues.Match(expected) {
t.Errorf(issuesShouldMatch, httpsIssues, expected)
}
}
func TestExplicitPortFirstRedirect(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()
u := "https://tls-v1-1.badssl.com"
chain, issues := preloadableRedirects(u)
if !chainsEqual(chain, []string{"https://tls-v1-1.badssl.com:1011/"}) {
t.Errorf("Unexpected chain: %v", chain)
}
if !issues.Match(Issues{}) {
t.Errorf(issuesShouldBeEmpty, issues)
}
httpsIssues := preloadableHTTPSRedirectsURL(u)
expected := Issues{}
if !httpsIssues.Match(expected) {
t.Errorf(issuesShouldMatch, httpsIssues, expected)
}
}
func TestHTTPUnavailable(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()
u := "http://oskuro.net"
domain := "oskuro.net"
// Test the helper
issues, cont := checkHSTSOverHTTP(u)
expected := Issues{Warnings: []Issue{{
Code: "redirects.http.does_not_exist",
Message: "The site appears to be unavailable over plain HTTP (http://oskuro.net). This can prevent users without a freshly updated modern browser from connecting to the site when they visit a URL with the http:// scheme (or with an unspecified scheme). However, this is okay if the site does not wish to support those users.",
}}}
if !issues.Match(expected) {
t.Errorf(issuesShouldMatch, issues, expected)
}
if cont {
t.Errorf("Should continue.")
}
// Mini integration test
mainIssues, firstRedirectHSTSIssues := preloadableHTTPRedirectsURL(u, domain)
expected = Issues{
Warnings: []Issue{{Code: "redirects.http.does_not_exist"}},
}
if !mainIssues.Match(expected) {
t.Errorf(issuesShouldMatch, mainIssues, expected)
}
if !firstRedirectHSTSIssues.Match(Issues{}) {
t.Errorf(issuesShouldBeEmpty, firstRedirectHSTSIssues)
}
}
func TestHSTSOverHTTP(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()
u := "http://history.google.com"
domain := "history.google.com"
_, issues := preloadableRedirects(u)
if !issues.Match(Issues{}) {
t.Errorf(issuesShouldBeEmpty, issues)
}
// Test the helper
issues, cont := checkHSTSOverHTTP(u)
expected := Issues{Warnings: []Issue{{
Code: "redirects.http.useless_header",
Message: "The HTTP page at http://history.google.com sends an HSTS header. This has no effect over HTTP, and should be removed.",
}}}
if !issues.Match(expected) {
t.Errorf(issuesShouldMatch, issues, expected)
}
if !cont {
t.Fatalf("Should not continue.")
}
// Mini integration test
mainIssues, firstRedirectHSTSIssues := preloadableHTTPRedirectsURL(u, domain)
expected = Issues{
Errors: []Issue{{Code: "redirects.http.first_redirect.insecure"}},
Warnings: []Issue{{Code: "redirects.http.useless_header"}},
}
if !mainIssues.Match(expected) {
t.Errorf(issuesShouldMatch, mainIssues, expected)
}
if !firstRedirectHSTSIssues.Match(Issues{}) {
t.Errorf(issuesShouldBeEmpty, firstRedirectHSTSIssues)
}
}
func TestHTTPNoRedirect(t *testing.T) {
skipIfShort(t)
t.Parallel()
u := "http://httpbin.org"
domain := "httpbin.org"
chain, issues := preloadableRedirects(u)
if !chainsEqual(chain, []string{}) {
t.Errorf("Unexpected chain: %v", chain)
}
if !issues.Match(Issues{}) {
t.Errorf(issuesShouldBeEmpty, issues)
}
mainIssues, firstRedirectHSTSIssues := preloadableHTTPRedirectsURL(u, domain)
expected := Issues{Errors: []Issue{{
Code: "redirects.http.no_redirect",
Message: "`http://httpbin.org` does not redirect to `https://httpbin.org`.",
}}}
if !mainIssues.Match(expected) {
t.Errorf(issuesShouldMatch, mainIssues, expected)
}
if !firstRedirectHSTSIssues.Match(Issues{}) {
t.Errorf(issuesShouldBeEmpty, firstRedirectHSTSIssues)
}
}
type preloadableHTTPRedirectsTest struct {
description string
domain string
expectedMainIssues Issues
expectedFirstRedirectHSTSIssues Issues
}
var preloadableHTTPRedirectsTests = []preloadableHTTPRedirectsTest{
{
"different host",
"bofa.com", // http://bofa.com redirects to https://www.bankofamerica.com
Issues{Errors: []Issue{{
Code: "redirects.http.first_redirect.insecure",
Message: "`http://bofa.com` (HTTP) redirects to `https://www.bankofamerica.com/vanity/redirect.go?src=/`. The first redirect from `http://bofa.com` should be to a secure page on the same host (`https://bofa.com`).",
}}},
Issues{},
},
{
"same origin",
"www.wikia.com", // http://www.wikia.com redirects to http://www.wikia.com/fandom
Issues{Errors: []Issue{{
Code: "redirects.http.first_redirect.insecure",
Message: "`http://www.wikia.com` (HTTP) redirects to `http://www.wikia.com/fandom`. The first redirect from `http://www.wikia.com` should be to a secure page on the same host (`https://www.wikia.com`).",
}}},
Issues{},
},
{
"www first and > 3 redirects",
"blogger.com",
Issues{
Errors: []Issue{
{
Code: "redirects.too_many",
Message: "There are more than 3 redirects starting from `http://blogger.com`.",
},
{
Code: "redirects.http.www_first",
Message: "`http://blogger.com` (HTTP) should immediately redirect to `https://blogger.com` (HTTPS) before adding the www subdomain. Right now, the first redirect is to `http://www.blogger.com/`. The extra redirect is required to ensure that any browser which supports HSTS will record the HSTS entry for the top level domain, not just the subdomain.",
},
},
},
Issues{},
},
{
"correct origin but not HSTS",
"sha256.badssl.com",
Issues{},
Issues{Errors: []Issue{{
Code: "redirects.http.first_redirect.no_hsts",
Message: "`http://sha256.badssl.com` redirects to `https://sha256.badssl.com/`, which does not serve a HSTS header that satisfies preload conditions. First error: No HSTS header",
}}},
},
}
func TestPreloadableHTTPRedirects(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(preloadableHTTPRedirectsTests))
for _, tt := range preloadableHTTPRedirectsTests {
go func(tt preloadableHTTPRedirectsTest) {
mainIssues, firstRedirectHSTSIssues := preloadableHTTPRedirects(tt.domain)
if !mainIssues.Match(tt.expectedMainIssues) {
t.Errorf("[%s] main issues for %s: "+issuesShouldMatch, tt.description, tt.domain, mainIssues, tt.expectedMainIssues)
}
if !firstRedirectHSTSIssues.Match(tt.expectedFirstRedirectHSTSIssues) {
t.Errorf("[%s] first redirect HSTS issues for %s: "+issuesShouldMatch, tt.description, tt.domain, firstRedirectHSTSIssues, tt.expectedFirstRedirectHSTSIssues)
}
wg.Done()
}(tt)
}
wg.Wait()
}