-
Notifications
You must be signed in to change notification settings - Fork 57
/
index_test.go
294 lines (276 loc) · 9.84 KB
/
index_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
package main
import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"expvar"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
tls110 "github.com/jmhodges/howsmyssl/tls110"
)
func TestDumbNilishIndex(t *testing.T) {
tmpl := loadIndex()
buf := new(bytes.Buffer)
err := tmpl.Execute(buf, &clientInfo{})
if err != nil {
t.Errorf("index execution blew up with nilish clientInfo: %#v", err)
}
if len(buf.Bytes()) == 0 {
t.Errorf("index execution did not write anything")
}
}
type acmeTest struct {
challPath string
acmeRedirectURL string
expected string
code int
}
func TestACMERedirect(t *testing.T) {
staticVars := new(expvar.Map).Init()
staticHandler := makeStaticHandler("/static", staticVars)
webHandleFunc := http.NotFound
tests := []acmeTest{
// same domain redirect, acmeRedirectURL leads with "/"
{
challPath: "https://www.howsmyssl.com/.well-known/acme-challenge/foobar",
acmeRedirectURL: "/example.com",
expected: "/example.com/.well-known/acme-challenge/foobar",
},
{
challPath: "https://www.howsmyssl.com/.well-known/acme-challenge/foobar",
acmeRedirectURL: "https://example.com",
expected: "https://example.com/.well-known/acme-challenge/foobar",
},
{
challPath: "https://www.howsmyssl.com/.well-known/acme-challenge/foobar",
acmeRedirectURL: "https://example.com/",
expected: "https://example.com/.well-known/acme-challenge/foobar",
},
// Busted acmeRedirectURL. Meant to be a domain, but was not.
{
challPath: "https://www.howsmyssl.com/.well-known/acme-challenge/foobar",
acmeRedirectURL: "example.com",
expected: "/.well-known/acme-challenge/example.com/.well-known/acme-challenge/foobar",
},
// same domain redirect, acmeRedirectURL leads and trails with "/"
{
challPath: "https://www.howsmyssl.com/.well-known/acme-challenge/foobar",
acmeRedirectURL: "/okay/",
expected: "/okay/.well-known/acme-challenge/foobar",
},
// same domain redirect, acmeRedirectURL leads and trails with "/"
{
challPath: "https://www.howsmyssl.com/.well-known/acme-challenge/foobar",
acmeRedirectURL: "/okay/",
expected: "/okay/.well-known/acme-challenge/foobar",
},
// same domain redirect, acmeRedirectURL leads with "/"
{
challPath: "https://www.howsmyssl.com/.well-known/acme-challenge/foobar",
acmeRedirectURL: "/okay",
expected: "/okay/.well-known/acme-challenge/foobar",
},
{
challPath: "https://www.howsmyssl.com/.well-known/acme-challenge",
acmeRedirectURL: "https://example.com",
expected: "/.well-known/acme-challenge/",
code: http.StatusMovedPermanently,
},
{
challPath: "https://www.howsmyssl.com/.well-known/acme-challenge/",
acmeRedirectURL: "https://example.com",
expected: "",
code: http.StatusOK,
},
{
challPath: "https://www.howsmyssl.com/.well-known/acme-challenge/foobar",
acmeRedirectURL: "https://example.com",
expected: "https://example.com/.well-known/acme-challenge/foobar",
},
}
for i, tt := range tests {
tm := tlsMux("www.howsmyssl.com", "www.howsmyssl.com", tt.acmeRedirectURL, staticHandler, webHandleFunc, nil)
r, err := http.NewRequest("GET", tt.challPath, nil)
if err != nil {
t.Fatalf("borked request for %#v: %s", tt.challPath, err)
}
w := httptest.NewRecorder()
tm.ServeHTTP(w, r)
location := w.Header().Get("Location")
if tt.code == 0 {
tt.code = http.StatusFound
}
if w.Code != tt.code {
t.Errorf("#%d, want %d, got %d", i, tt.code, w.Code)
}
if location != tt.expected {
t.Errorf("#%d, %q, want %#v, got %#v", i, tt.acmeRedirectURL, tt.expected, location)
}
}
}
type vhostTest struct {
rawVHost string
httpsAddr string
expectedRouteHost string
expectedRedirectHost string
}
func TestVHostCalculation(t *testing.T) {
tests := []vhostTest{
vhostTest{
rawVHost: "www.howsmyssl.com",
httpsAddr: "0:10443",
expectedRouteHost: "www.howsmyssl.com",
expectedRedirectHost: "www.howsmyssl.com",
},
vhostTest{
rawVHost: "localhost:10443",
httpsAddr: "localhost:10443",
expectedRouteHost: "localhost",
expectedRedirectHost: "localhost:10443",
},
vhostTest{
rawVHost: "example.com:10443",
httpsAddr: "localhost:10443",
expectedRouteHost: "example.com",
expectedRedirectHost: "example.com:10443",
},
vhostTest{
rawVHost: "example.com:443",
httpsAddr: "0:443",
expectedRouteHost: "example.com",
expectedRedirectHost: "example.com",
},
}
staticVars := new(expvar.Map).Init()
staticHandler := makeStaticHandler("/static", staticVars)
webHandleFunc := http.NotFound
for i, vt := range tests {
routeHost, redirectHost := calculateDomains(vt.rawVHost, vt.httpsAddr)
if routeHost != vt.expectedRouteHost {
t.Errorf("#%d vhost %#v, httpsAddr %#v: want routeHost %#v, got %s", i, vt.rawVHost, vt.httpsAddr, vt.expectedRouteHost, routeHost)
}
if redirectHost != vt.expectedRedirectHost {
t.Errorf("#%d vhost %#v, httpsAddr %#v: want redirectHost %#v, got %#v", i, vt.rawVHost, vt.httpsAddr, vt.expectedRedirectHost, redirectHost)
}
tm := tlsMux(vt.expectedRouteHost, vt.expectedRedirectHost, "https://otherexample.com", staticHandler, webHandleFunc, nil)
r, err := http.NewRequest("GET", "https://howsmyssl.com/", nil)
if err != nil {
t.Fatalf("borked request")
}
w := httptest.NewRecorder()
tm.ServeHTTP(w, r)
expectedLocation := "https://" + vt.expectedRedirectHost + "/"
location := w.Header()["Location"][0]
if w.Code != http.StatusMovedPermanently {
t.Errorf("#%d vhost %#v, httpsAddr %#v: want Code %d, got %d", i, vt.rawVHost, vt.httpsAddr, http.StatusMovedPermanently, w.Code)
}
if location != expectedLocation {
t.Errorf("#%d vhost %#v, httpsAddr %#v: want Location %s, got %s", i, vt.rawVHost, vt.httpsAddr, expectedLocation, location)
}
}
}
func TestDisallowedBodyParses(t *testing.T) {
e := &struct {
Error string `json:"error"`
TLSVersion string `json:"tls_version"`
}{}
err := json.Unmarshal(disallowedOriginBody, e)
if err != nil {
t.Errorf("disallowedOriginBody did not parse: %s", err)
}
}
func TestJSONPCallback(t *testing.T) {
staticVars := new(expvar.Map).Init()
staticHandler := makeStaticHandler("/static", staticVars)
webHandleFunc := http.NotFound
am := &allowMaps{
AllowTheseDomains: make(map[string]bool),
AllowSubdomainsOn: make(map[string]bool),
BlockedDomains: make(map[string]bool),
}
ama := &allowMapsAtomic{}
ama.Store(am)
oa := newOriginAllower(ama, "testhostname", nullLogClient{}, new(expvar.Map).Init())
tm := tlsMux("", "www.howsmyssl.com", "www.howsmyssl.com", staticHandler, webHandleFunc, oa)
tl, err := tls110.Listen("tcp", "127.0.0.1:0", serverConf)
if err != nil {
t.Fatalf("NewListener: %s", err)
}
li := newListener(tl, new(expvar.Map).Init())
srv := httptest.NewUnstartedServer(tm)
srv.Listener = li
// Intentionally not using StartTLS to avoid stomping on our special listener.
srv.Start()
defer srv.Close()
c := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return errors.New("no redirects should be seen")
},
}
u := strings.Replace(srv.URL, "https://", "https://", -1)
r, err := http.NewRequest("GET", u+"/a/check", nil)
if err != nil {
t.Fatalf("NewRequest: %s", err)
}
resp, err := c.Do(r)
if err != nil {
t.Fatalf("Get: %s", err)
}
b, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
t.Fatalf("ReadAll: %s", err)
}
if string(b) != expectedJSONBody {
t.Errorf("json body, want:\n%#v\ngot:%#v\n", expectedJSONBody, string(b))
}
ct := resp.Header.Get("Content-Type")
if ct != "application/json" {
t.Errorf("json Content-Type, want application/json, got %s", ct)
}
rcall, err := http.NewRequest("GET", u+"/a/check?callback=parseTLS", nil)
if err != nil {
t.Fatalf("NewRequest rcall: %s", err)
}
respcall, err := c.Do(rcall)
if err != nil {
t.Fatalf("Get: %s", err)
}
bcall, err := ioutil.ReadAll(respcall.Body)
defer respcall.Body.Close()
if err != nil {
t.Fatalf("ReadAll rcall: %s", err)
}
jsonp := "parseTLS(" + expectedJSONBody + ");"
if string(bcall) != jsonp {
t.Errorf("jsonp callback body, want:\n%#v\ngot:%#v\n", jsonp, string(bcall))
}
ctcall := respcall.Header.Get("Content-Type")
if ctcall != "application/javascript" {
t.Errorf("jsonp Content-Type, want application/javascript, got %s", ctcall)
}
}
const (
expectedJSONBody = `{"given_cipher_suites":["TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256","TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384","TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256","TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384","TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256","TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256","TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA","TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA","TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA","TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA","TLS_RSA_WITH_AES_128_GCM_SHA256","TLS_RSA_WITH_AES_256_GCM_SHA384","TLS_RSA_WITH_AES_128_CBC_SHA","TLS_RSA_WITH_AES_256_CBC_SHA","TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA","TLS_RSA_WITH_3DES_EDE_CBC_SHA"],"ephemeral_keys_supported":true,"session_ticket_supported":false,"tls_compression_supported":false,"unknown_cipher_suite_supported":false,"beast_vuln":false,"able_to_detect_n_minus_one_splitting":false,"insecure_cipher_suites":{},"tls_version":"TLS 1.2","rating":"Probably Okay"}`
)