| 1 |
package secure |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/tls" |
| 5 |
"net/http" |
| 6 |
"net/http/httptest" |
| 7 |
"reflect" |
| 8 |
"testing" |
| 9 |
) |
| 10 |
|
| 11 |
var myHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 12 |
w.Write([]byte("bar")) |
| 13 |
}) |
| 14 |
|
| 15 |
func TestNoConfig(t *testing.T) { |
| 16 |
s := New() |
| 17 |
|
| 18 |
res := httptest.NewRecorder() |
| 19 |
req, _ := http.NewRequest("GET", "http://example.com/foo", nil) |
| 20 |
|
| 21 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 22 |
|
| 23 |
expect(t, res.Code, http.StatusOK) |
| 24 |
expect(t, res.Body.String(), "bar") |
| 25 |
} |
| 26 |
|
| 27 |
func TestNoAllowHosts(t *testing.T) { |
| 28 |
s := New(Options{ |
| 29 |
AllowedHosts: []string{}, |
| 30 |
}) |
| 31 |
|
| 32 |
res := httptest.NewRecorder() |
| 33 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 34 |
req.Host = "www.example.com" |
| 35 |
|
| 36 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 37 |
|
| 38 |
expect(t, res.Code, http.StatusOK) |
| 39 |
expect(t, res.Body.String(), `bar`) |
| 40 |
} |
| 41 |
|
| 42 |
func TestGoodSingleAllowHosts(t *testing.T) { |
| 43 |
s := New(Options{ |
| 44 |
AllowedHosts: []string{"www.example.com"}, |
| 45 |
}) |
| 46 |
|
| 47 |
res := httptest.NewRecorder() |
| 48 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 49 |
req.Host = "www.example.com" |
| 50 |
|
| 51 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 52 |
|
| 53 |
expect(t, res.Code, http.StatusOK) |
| 54 |
expect(t, res.Body.String(), `bar`) |
| 55 |
} |
| 56 |
|
| 57 |
func TestBadSingleAllowHosts(t *testing.T) { |
| 58 |
s := New(Options{ |
| 59 |
AllowedHosts: []string{"sub.example.com"}, |
| 60 |
}) |
| 61 |
|
| 62 |
res := httptest.NewRecorder() |
| 63 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 64 |
req.Host = "www.example.com" |
| 65 |
|
| 66 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 67 |
|
| 68 |
expect(t, res.Code, http.StatusInternalServerError) |
| 69 |
} |
| 70 |
|
| 71 |
func TestRegexSingleAllowHosts(t *testing.T) { |
| 72 |
s := New(Options{ |
| 73 |
AllowedHosts: []string{"*\\.example\\.com"}, |
| 74 |
AllowedHostsAreRegex: true, |
| 75 |
}) |
| 76 |
|
| 77 |
res := httptest.NewRecorder() |
| 78 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 79 |
req.Host = "sub.example.com" |
| 80 |
|
| 81 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 82 |
|
| 83 |
expect(t, res.Code, http.StatusOK) |
| 84 |
expect(t, res.Body.String(), `bar`) |
| 85 |
} |
| 86 |
|
| 87 |
func TestRegexMultipleAllowHosts(t *testing.T) { |
| 88 |
s := New(Options{ |
| 89 |
AllowedHosts: []string{".+\\.example\\.com", ".*sub\\..+-awesome-example\\.com"}, |
| 90 |
AllowedHostsAreRegex: true, |
| 91 |
}) |
| 92 |
|
| 93 |
res := httptest.NewRecorder() |
| 94 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 95 |
req.Host = "sub.first-awesome-example.com" |
| 96 |
|
| 97 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 98 |
|
| 99 |
expect(t, res.Code, http.StatusOK) |
| 100 |
expect(t, res.Body.String(), `bar`) |
| 101 |
|
| 102 |
res = httptest.NewRecorder() |
| 103 |
req, _ = http.NewRequest("GET", "/foo", nil) |
| 104 |
req.Host = "test.sub.second-awesome-example.com" |
| 105 |
|
| 106 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 107 |
|
| 108 |
expect(t, res.Code, http.StatusOK) |
| 109 |
expect(t, res.Body.String(), `bar`) |
| 110 |
|
| 111 |
res = httptest.NewRecorder() |
| 112 |
req, _ = http.NewRequest("GET", "/foo", nil) |
| 113 |
req.Host = "test.sub.second-example.com" |
| 114 |
|
| 115 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 116 |
|
| 117 |
expect(t, res.Code, http.StatusInternalServerError) |
| 118 |
} |
| 119 |
|
| 120 |
func TestInvalidRegexAllowHosts(t *testing.T) { |
| 121 |
defer func() { |
| 122 |
if r := recover(); r == nil { |
| 123 |
t.Errorf("The code did not panic") |
| 124 |
} |
| 125 |
}() |
| 126 |
New(Options{ |
| 127 |
AllowedHosts: []string{"[*.e"}, |
| 128 |
AllowedHostsAreRegex: true, |
| 129 |
}) |
| 130 |
} |
| 131 |
|
| 132 |
func TestGoodSingleAllowHostsProxyHeaders(t *testing.T) { |
| 133 |
s := New(Options{ |
| 134 |
AllowedHosts: []string{"www.example.com"}, |
| 135 |
HostsProxyHeaders: []string{"X-Proxy-Host"}, |
| 136 |
}) |
| 137 |
|
| 138 |
res := httptest.NewRecorder() |
| 139 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 140 |
req.Host = "example-internal" |
| 141 |
req.Header.Set("X-Proxy-Host", "www.example.com") |
| 142 |
|
| 143 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 144 |
|
| 145 |
expect(t, res.Code, http.StatusOK) |
| 146 |
expect(t, res.Body.String(), `bar`) |
| 147 |
} |
| 148 |
|
| 149 |
func TestBadSingleAllowHostsProxyHeaders(t *testing.T) { |
| 150 |
s := New(Options{ |
| 151 |
AllowedHosts: []string{"sub.example.com"}, |
| 152 |
HostsProxyHeaders: []string{"X-Proxy-Host"}, |
| 153 |
}) |
| 154 |
|
| 155 |
res := httptest.NewRecorder() |
| 156 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 157 |
req.Host = "example-internal" |
| 158 |
req.Header.Set("X-Proxy-Host", "www.example.com") |
| 159 |
|
| 160 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 161 |
|
| 162 |
expect(t, res.Code, http.StatusInternalServerError) |
| 163 |
} |
| 164 |
|
| 165 |
func TestGoodMultipleAllowHosts(t *testing.T) { |
| 166 |
s := New(Options{ |
| 167 |
AllowedHosts: []string{"www.example.com", "sub.example.com"}, |
| 168 |
}) |
| 169 |
|
| 170 |
res := httptest.NewRecorder() |
| 171 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 172 |
req.Host = "sub.example.com" |
| 173 |
|
| 174 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 175 |
|
| 176 |
expect(t, res.Code, http.StatusOK) |
| 177 |
expect(t, res.Body.String(), `bar`) |
| 178 |
} |
| 179 |
|
| 180 |
func TestBadMultipleAllowHosts(t *testing.T) { |
| 181 |
s := New(Options{ |
| 182 |
AllowedHosts: []string{"www.example.com", "sub.example.com"}, |
| 183 |
}) |
| 184 |
|
| 185 |
res := httptest.NewRecorder() |
| 186 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 187 |
req.Host = "www3.example.com" |
| 188 |
|
| 189 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 190 |
|
| 191 |
expect(t, res.Code, http.StatusInternalServerError) |
| 192 |
} |
| 193 |
|
| 194 |
func TestAllowHostsInDevMode(t *testing.T) { |
| 195 |
s := New(Options{ |
| 196 |
AllowedHosts: []string{"www.example.com", "sub.example.com"}, |
| 197 |
IsDevelopment: true, |
| 198 |
}) |
| 199 |
|
| 200 |
res := httptest.NewRecorder() |
| 201 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 202 |
req.Host = "www3.example.com" |
| 203 |
|
| 204 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 205 |
|
| 206 |
expect(t, res.Code, http.StatusOK) |
| 207 |
} |
| 208 |
|
| 209 |
func TestBadHostHandler(t *testing.T) { |
| 210 |
s := New(Options{ |
| 211 |
AllowedHosts: []string{"www.example.com", "sub.example.com"}, |
| 212 |
}) |
| 213 |
|
| 214 |
badHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 215 |
http.Error(w, "BadHost", http.StatusInternalServerError) |
| 216 |
}) |
| 217 |
|
| 218 |
s.SetBadHostHandler(badHandler) |
| 219 |
|
| 220 |
res := httptest.NewRecorder() |
| 221 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 222 |
req.Host = "www3.example.com" |
| 223 |
|
| 224 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 225 |
|
| 226 |
expect(t, res.Code, http.StatusInternalServerError) |
| 227 |
|
| 228 |
|
| 229 |
expect(t, res.Body.String(), "BadHost\n") |
| 230 |
} |
| 231 |
|
| 232 |
func TestSSL(t *testing.T) { |
| 233 |
s := New(Options{ |
| 234 |
SSLRedirect: true, |
| 235 |
}) |
| 236 |
|
| 237 |
res := httptest.NewRecorder() |
| 238 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 239 |
req.Host = "www.example.com" |
| 240 |
req.URL.Scheme = "https" |
| 241 |
|
| 242 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 243 |
|
| 244 |
expect(t, res.Code, http.StatusOK) |
| 245 |
} |
| 246 |
|
| 247 |
func TestSSLInDevMode(t *testing.T) { |
| 248 |
s := New(Options{ |
| 249 |
SSLRedirect: true, |
| 250 |
IsDevelopment: true, |
| 251 |
}) |
| 252 |
|
| 253 |
res := httptest.NewRecorder() |
| 254 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 255 |
req.Host = "www.example.com" |
| 256 |
req.URL.Scheme = "http" |
| 257 |
|
| 258 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 259 |
|
| 260 |
expect(t, res.Code, http.StatusOK) |
| 261 |
} |
| 262 |
|
| 263 |
func TestBasicSSL(t *testing.T) { |
| 264 |
s := New(Options{ |
| 265 |
SSLRedirect: true, |
| 266 |
}) |
| 267 |
|
| 268 |
res := httptest.NewRecorder() |
| 269 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 270 |
req.Host = "www.example.com" |
| 271 |
req.URL.Scheme = "http" |
| 272 |
|
| 273 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 274 |
|
| 275 |
expect(t, res.Code, http.StatusMovedPermanently) |
| 276 |
expect(t, res.Header().Get("Location"), "https://www.example.com/foo") |
| 277 |
} |
| 278 |
|
| 279 |
func TestBasicSSLWithHost(t *testing.T) { |
| 280 |
s := New(Options{ |
| 281 |
SSLRedirect: true, |
| 282 |
SSLHost: "secure.example.com", |
| 283 |
}) |
| 284 |
|
| 285 |
res := httptest.NewRecorder() |
| 286 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 287 |
req.Host = "www.example.com" |
| 288 |
req.URL.Scheme = "http" |
| 289 |
|
| 290 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 291 |
|
| 292 |
expect(t, res.Code, http.StatusMovedPermanently) |
| 293 |
expect(t, res.Header().Get("Location"), "https://secure.example.com/foo") |
| 294 |
} |
| 295 |
|
| 296 |
func TestBasicSSLWithHostFunc(t *testing.T) { |
| 297 |
sslHostFunc := (func() SSLHostFunc { |
| 298 |
isServerDown := false |
| 299 |
return func(host string) (newHost string) { |
| 300 |
if isServerDown { |
| 301 |
newHost = "404.example.com" |
| 302 |
return |
| 303 |
} |
| 304 |
if host == "www.example.com" { |
| 305 |
newHost = "secure.example.com:8443" |
| 306 |
} else if host == "www.example.org" { |
| 307 |
newHost = "secure.example.org" |
| 308 |
} |
| 309 |
return |
| 310 |
} |
| 311 |
})() |
| 312 |
s := New(Options{ |
| 313 |
SSLRedirect: true, |
| 314 |
SSLHostFunc: &sslHostFunc, |
| 315 |
}) |
| 316 |
|
| 317 |
|
| 318 |
res := httptest.NewRecorder() |
| 319 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 320 |
req.Host = "www.example.com" |
| 321 |
req.URL.Scheme = "http" |
| 322 |
|
| 323 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 324 |
|
| 325 |
expect(t, res.Code, http.StatusMovedPermanently) |
| 326 |
expect(t, res.Header().Get("Location"), "https://secure.example.com:8443/foo") |
| 327 |
|
| 328 |
|
| 329 |
res = httptest.NewRecorder() |
| 330 |
req, _ = http.NewRequest("GET", "/foo", nil) |
| 331 |
req.Host = "www.example.org" |
| 332 |
req.URL.Scheme = "http" |
| 333 |
|
| 334 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 335 |
|
| 336 |
expect(t, res.Code, http.StatusMovedPermanently) |
| 337 |
expect(t, res.Header().Get("Location"), "https://secure.example.org/foo") |
| 338 |
|
| 339 |
|
| 340 |
res = httptest.NewRecorder() |
| 341 |
req, _ = http.NewRequest("GET", "/foo", nil) |
| 342 |
req.Host = "www.other.com" |
| 343 |
req.URL.Scheme = "http" |
| 344 |
|
| 345 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 346 |
|
| 347 |
expect(t, res.Code, http.StatusMovedPermanently) |
| 348 |
expect(t, res.Header().Get("Location"), "https://www.other.com/foo") |
| 349 |
} |
| 350 |
|
| 351 |
func TestBadProxySSL(t *testing.T) { |
| 352 |
s := New(Options{ |
| 353 |
SSLRedirect: true, |
| 354 |
}) |
| 355 |
|
| 356 |
res := httptest.NewRecorder() |
| 357 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 358 |
req.Host = "www.example.com" |
| 359 |
req.URL.Scheme = "http" |
| 360 |
req.Header.Add("X-Forwarded-Proto", "https") |
| 361 |
|
| 362 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 363 |
|
| 364 |
expect(t, res.Code, http.StatusMovedPermanently) |
| 365 |
expect(t, res.Header().Get("Location"), "https://www.example.com/foo") |
| 366 |
} |
| 367 |
|
| 368 |
func TestCustomProxySSL(t *testing.T) { |
| 369 |
s := New(Options{ |
| 370 |
SSLRedirect: true, |
| 371 |
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"}, |
| 372 |
}) |
| 373 |
|
| 374 |
res := httptest.NewRecorder() |
| 375 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 376 |
req.Host = "www.example.com" |
| 377 |
req.URL.Scheme = "http" |
| 378 |
req.Header.Add("X-Forwarded-Proto", "https") |
| 379 |
|
| 380 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 381 |
|
| 382 |
expect(t, res.Code, http.StatusOK) |
| 383 |
} |
| 384 |
|
| 385 |
func TestCustomProxySSLInDevMode(t *testing.T) { |
| 386 |
s := New(Options{ |
| 387 |
SSLRedirect: true, |
| 388 |
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"}, |
| 389 |
IsDevelopment: true, |
| 390 |
}) |
| 391 |
|
| 392 |
res := httptest.NewRecorder() |
| 393 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 394 |
req.Host = "www.example.com" |
| 395 |
req.URL.Scheme = "http" |
| 396 |
req.Header.Add("X-Forwarded-Proto", "http") |
| 397 |
|
| 398 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 399 |
|
| 400 |
expect(t, res.Code, http.StatusOK) |
| 401 |
} |
| 402 |
|
| 403 |
func TestCustomProxyAndHostProxyHeadersWithRedirect(t *testing.T) { |
| 404 |
s := New(Options{ |
| 405 |
HostsProxyHeaders: []string{"X-Forwarded-Host"}, |
| 406 |
SSLRedirect: true, |
| 407 |
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "http"}, |
| 408 |
}) |
| 409 |
|
| 410 |
res := httptest.NewRecorder() |
| 411 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 412 |
req.Host = "example-internal" |
| 413 |
req.URL.Scheme = "http" |
| 414 |
req.Header.Add("X-Forwarded-Proto", "https") |
| 415 |
req.Header.Add("X-Forwarded-Host", "www.example.com") |
| 416 |
|
| 417 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 418 |
|
| 419 |
expect(t, res.Code, http.StatusMovedPermanently) |
| 420 |
expect(t, res.Header().Get("Location"), "https://www.example.com/foo") |
| 421 |
} |
| 422 |
|
| 423 |
func TestCustomProxyAndHostSSL(t *testing.T) { |
| 424 |
s := New(Options{ |
| 425 |
SSLRedirect: true, |
| 426 |
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"}, |
| 427 |
SSLHost: "secure.example.com", |
| 428 |
}) |
| 429 |
|
| 430 |
res := httptest.NewRecorder() |
| 431 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 432 |
req.Host = "www.example.com" |
| 433 |
req.URL.Scheme = "http" |
| 434 |
req.Header.Add("X-Forwarded-Proto", "https") |
| 435 |
|
| 436 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 437 |
|
| 438 |
expect(t, res.Code, http.StatusOK) |
| 439 |
} |
| 440 |
|
| 441 |
func TestCustomBadProxyAndHostSSL(t *testing.T) { |
| 442 |
s := New(Options{ |
| 443 |
SSLRedirect: true, |
| 444 |
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "superman"}, |
| 445 |
SSLHost: "secure.example.com", |
| 446 |
}) |
| 447 |
|
| 448 |
res := httptest.NewRecorder() |
| 449 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 450 |
req.Host = "www.example.com" |
| 451 |
req.URL.Scheme = "http" |
| 452 |
req.Header.Add("X-Forwarded-Proto", "https") |
| 453 |
|
| 454 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 455 |
|
| 456 |
expect(t, res.Code, http.StatusMovedPermanently) |
| 457 |
expect(t, res.Header().Get("Location"), "https://secure.example.com/foo") |
| 458 |
} |
| 459 |
|
| 460 |
func TestCustomBadProxyAndHostSSLWithTempRedirect(t *testing.T) { |
| 461 |
s := New(Options{ |
| 462 |
SSLRedirect: true, |
| 463 |
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "superman"}, |
| 464 |
SSLHost: "secure.example.com", |
| 465 |
SSLTemporaryRedirect: true, |
| 466 |
}) |
| 467 |
|
| 468 |
res := httptest.NewRecorder() |
| 469 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 470 |
req.Host = "www.example.com" |
| 471 |
req.URL.Scheme = "http" |
| 472 |
req.Header.Add("X-Forwarded-Proto", "https") |
| 473 |
|
| 474 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 475 |
|
| 476 |
expect(t, res.Code, http.StatusTemporaryRedirect) |
| 477 |
expect(t, res.Header().Get("Location"), "https://secure.example.com/foo") |
| 478 |
} |
| 479 |
|
| 480 |
func TestStsHeaderWithNoSSL(t *testing.T) { |
| 481 |
s := New(Options{ |
| 482 |
STSSeconds: 315360000, |
| 483 |
}) |
| 484 |
|
| 485 |
res := httptest.NewRecorder() |
| 486 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 487 |
|
| 488 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 489 |
|
| 490 |
expect(t, res.Code, http.StatusOK) |
| 491 |
expect(t, res.Header().Get("Strict-Transport-Security"), "") |
| 492 |
} |
| 493 |
|
| 494 |
func TestStsHeaderWithNoSSLButWithForce(t *testing.T) { |
| 495 |
s := New(Options{ |
| 496 |
STSSeconds: 315360000, |
| 497 |
ForceSTSHeader: true, |
| 498 |
}) |
| 499 |
|
| 500 |
res := httptest.NewRecorder() |
| 501 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 502 |
|
| 503 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 504 |
|
| 505 |
expect(t, res.Code, http.StatusOK) |
| 506 |
expect(t, res.Header().Get("Strict-Transport-Security"), "max-age=315360000") |
| 507 |
} |
| 508 |
|
| 509 |
func TestStsHeaderWithNoSSLButWithForceForRequestOnly(t *testing.T) { |
| 510 |
s := New(Options{ |
| 511 |
STSSeconds: 315360000, |
| 512 |
ForceSTSHeader: true, |
| 513 |
}) |
| 514 |
|
| 515 |
res := httptest.NewRecorder() |
| 516 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 517 |
|
| 518 |
s.HandlerForRequestOnly(myHandler).ServeHTTP(res, req) |
| 519 |
|
| 520 |
expect(t, res.Code, http.StatusOK) |
| 521 |
expect(t, res.Header().Get("Strict-Transport-Security"), "") |
| 522 |
} |
| 523 |
|
| 524 |
func TestStsHeaderWithNoSSLButWithForceAndIsDev(t *testing.T) { |
| 525 |
s := New(Options{ |
| 526 |
STSSeconds: 315360000, |
| 527 |
ForceSTSHeader: true, |
| 528 |
IsDevelopment: true, |
| 529 |
}) |
| 530 |
|
| 531 |
res := httptest.NewRecorder() |
| 532 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 533 |
|
| 534 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 535 |
|
| 536 |
expect(t, res.Code, http.StatusOK) |
| 537 |
expect(t, res.Header().Get("Strict-Transport-Security"), "") |
| 538 |
} |
| 539 |
|
| 540 |
func TestStsHeaderWithSSL(t *testing.T) { |
| 541 |
s := New(Options{ |
| 542 |
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"}, |
| 543 |
STSSeconds: 315360000, |
| 544 |
}) |
| 545 |
|
| 546 |
res := httptest.NewRecorder() |
| 547 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 548 |
req.URL.Scheme = "http" |
| 549 |
req.Header.Add("X-Forwarded-Proto", "https") |
| 550 |
|
| 551 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 552 |
|
| 553 |
expect(t, res.Code, http.StatusOK) |
| 554 |
expect(t, res.Header().Get("Strict-Transport-Security"), "max-age=315360000") |
| 555 |
} |
| 556 |
|
| 557 |
func TestStsHeaderWithSSLForRequestOnly(t *testing.T) { |
| 558 |
s := New(Options{ |
| 559 |
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"}, |
| 560 |
STSSeconds: 315360000, |
| 561 |
}) |
| 562 |
|
| 563 |
res := httptest.NewRecorder() |
| 564 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 565 |
req.URL.Scheme = "http" |
| 566 |
req.Header.Add("X-Forwarded-Proto", "https") |
| 567 |
|
| 568 |
s.HandlerForRequestOnly(myHandler).ServeHTTP(res, req) |
| 569 |
|
| 570 |
expect(t, res.Code, http.StatusOK) |
| 571 |
expect(t, res.Header().Get("Strict-Transport-Security"), "") |
| 572 |
} |
| 573 |
|
| 574 |
func TestStsHeaderInDevMode(t *testing.T) { |
| 575 |
s := New(Options{ |
| 576 |
STSSeconds: 315360000, |
| 577 |
IsDevelopment: true, |
| 578 |
}) |
| 579 |
|
| 580 |
res := httptest.NewRecorder() |
| 581 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 582 |
req.URL.Scheme = "https" |
| 583 |
|
| 584 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 585 |
|
| 586 |
expect(t, res.Code, http.StatusOK) |
| 587 |
expect(t, res.Header().Get("Strict-Transport-Security"), "") |
| 588 |
} |
| 589 |
|
| 590 |
func TestStsHeaderWithSubdomains(t *testing.T) { |
| 591 |
s := New(Options{ |
| 592 |
STSSeconds: 315360000, |
| 593 |
STSIncludeSubdomains: true, |
| 594 |
}) |
| 595 |
|
| 596 |
res := httptest.NewRecorder() |
| 597 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 598 |
req.URL.Scheme = "https" |
| 599 |
|
| 600 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 601 |
|
| 602 |
expect(t, res.Code, http.StatusOK) |
| 603 |
expect(t, res.Header().Get("Strict-Transport-Security"), "max-age=315360000; includeSubDomains") |
| 604 |
} |
| 605 |
|
| 606 |
func TestStsHeaderWithSubdomainsForRequestOnly(t *testing.T) { |
| 607 |
s := New(Options{ |
| 608 |
STSSeconds: 315360000, |
| 609 |
STSIncludeSubdomains: true, |
| 610 |
}) |
| 611 |
|
| 612 |
res := httptest.NewRecorder() |
| 613 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 614 |
req.URL.Scheme = "https" |
| 615 |
|
| 616 |
s.HandlerForRequestOnly(myHandler).ServeHTTP(res, req) |
| 617 |
|
| 618 |
expect(t, res.Code, http.StatusOK) |
| 619 |
expect(t, res.Header().Get("Strict-Transport-Security"), "") |
| 620 |
} |
| 621 |
|
| 622 |
func TestStsHeaderWithPreload(t *testing.T) { |
| 623 |
s := New(Options{ |
| 624 |
STSSeconds: 315360000, |
| 625 |
STSPreload: true, |
| 626 |
}) |
| 627 |
|
| 628 |
res := httptest.NewRecorder() |
| 629 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 630 |
req.URL.Scheme = "https" |
| 631 |
|
| 632 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 633 |
|
| 634 |
expect(t, res.Code, http.StatusOK) |
| 635 |
expect(t, res.Header().Get("Strict-Transport-Security"), "max-age=315360000; preload") |
| 636 |
} |
| 637 |
|
| 638 |
func TestStsHeaderWithPreloadForRequest(t *testing.T) { |
| 639 |
s := New(Options{ |
| 640 |
STSSeconds: 315360000, |
| 641 |
STSPreload: true, |
| 642 |
}) |
| 643 |
|
| 644 |
res := httptest.NewRecorder() |
| 645 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 646 |
req.URL.Scheme = "https" |
| 647 |
|
| 648 |
s.HandlerForRequestOnly(myHandler).ServeHTTP(res, req) |
| 649 |
|
| 650 |
expect(t, res.Code, http.StatusOK) |
| 651 |
expect(t, res.Header().Get("Strict-Transport-Security"), "") |
| 652 |
} |
| 653 |
|
| 654 |
func TestStsHeaderWithSubdomainsWithPreload(t *testing.T) { |
| 655 |
s := New(Options{ |
| 656 |
STSSeconds: 315360000, |
| 657 |
STSIncludeSubdomains: true, |
| 658 |
STSPreload: true, |
| 659 |
}) |
| 660 |
|
| 661 |
res := httptest.NewRecorder() |
| 662 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 663 |
req.URL.Scheme = "https" |
| 664 |
|
| 665 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 666 |
|
| 667 |
expect(t, res.Code, http.StatusOK) |
| 668 |
expect(t, res.Header().Get("Strict-Transport-Security"), "max-age=315360000; includeSubDomains; preload") |
| 669 |
} |
| 670 |
|
| 671 |
func TestStsHeaderWithSubdomainsWithPreloadForRequestOnly(t *testing.T) { |
| 672 |
s := New(Options{ |
| 673 |
STSSeconds: 315360000, |
| 674 |
STSIncludeSubdomains: true, |
| 675 |
STSPreload: true, |
| 676 |
}) |
| 677 |
|
| 678 |
res := httptest.NewRecorder() |
| 679 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 680 |
req.URL.Scheme = "https" |
| 681 |
|
| 682 |
s.HandlerForRequestOnly(myHandler).ServeHTTP(res, req) |
| 683 |
|
| 684 |
expect(t, res.Code, http.StatusOK) |
| 685 |
expect(t, res.Header().Get("Strict-Transport-Security"), "") |
| 686 |
} |
| 687 |
|
| 688 |
func TestFrameDeny(t *testing.T) { |
| 689 |
s := New(Options{ |
| 690 |
FrameDeny: true, |
| 691 |
}) |
| 692 |
|
| 693 |
res := httptest.NewRecorder() |
| 694 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 695 |
|
| 696 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 697 |
|
| 698 |
expect(t, res.Code, http.StatusOK) |
| 699 |
expect(t, res.Header().Get("X-Frame-Options"), "DENY") |
| 700 |
} |
| 701 |
|
| 702 |
func TestFrameDenyForRequestOnly(t *testing.T) { |
| 703 |
s := New(Options{ |
| 704 |
FrameDeny: true, |
| 705 |
}) |
| 706 |
|
| 707 |
res := httptest.NewRecorder() |
| 708 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 709 |
|
| 710 |
s.HandlerForRequestOnly(myHandler).ServeHTTP(res, req) |
| 711 |
|
| 712 |
expect(t, res.Code, http.StatusOK) |
| 713 |
expect(t, res.Header().Get("X-Frame-Options"), "") |
| 714 |
} |
| 715 |
|
| 716 |
func TestCustomFrameValue(t *testing.T) { |
| 717 |
s := New(Options{ |
| 718 |
CustomFrameOptionsValue: "SAMEORIGIN", |
| 719 |
}) |
| 720 |
|
| 721 |
res := httptest.NewRecorder() |
| 722 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 723 |
|
| 724 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 725 |
|
| 726 |
expect(t, res.Code, http.StatusOK) |
| 727 |
expect(t, res.Header().Get("X-Frame-Options"), "SAMEORIGIN") |
| 728 |
} |
| 729 |
|
| 730 |
func TestCustomFrameValueWithDeny(t *testing.T) { |
| 731 |
s := New(Options{ |
| 732 |
FrameDeny: true, |
| 733 |
CustomFrameOptionsValue: "SAMEORIGIN", |
| 734 |
}) |
| 735 |
|
| 736 |
res := httptest.NewRecorder() |
| 737 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 738 |
|
| 739 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 740 |
|
| 741 |
expect(t, res.Code, http.StatusOK) |
| 742 |
expect(t, res.Header().Get("X-Frame-Options"), "SAMEORIGIN") |
| 743 |
} |
| 744 |
|
| 745 |
func TestCustomFrameValueWithDenyForRequestOnly(t *testing.T) { |
| 746 |
s := New(Options{ |
| 747 |
FrameDeny: true, |
| 748 |
CustomFrameOptionsValue: "SAMEORIGIN", |
| 749 |
}) |
| 750 |
|
| 751 |
res := httptest.NewRecorder() |
| 752 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 753 |
|
| 754 |
s.HandlerForRequestOnly(myHandler).ServeHTTP(res, req) |
| 755 |
|
| 756 |
expect(t, res.Code, http.StatusOK) |
| 757 |
expect(t, res.Header().Get("X-Frame-Options"), "") |
| 758 |
} |
| 759 |
|
| 760 |
func TestContentNosniff(t *testing.T) { |
| 761 |
s := New(Options{ |
| 762 |
ContentTypeNosniff: true, |
| 763 |
}) |
| 764 |
|
| 765 |
res := httptest.NewRecorder() |
| 766 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 767 |
|
| 768 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 769 |
|
| 770 |
expect(t, res.Code, http.StatusOK) |
| 771 |
expect(t, res.Header().Get("X-Content-Type-Options"), "nosniff") |
| 772 |
} |
| 773 |
|
| 774 |
func TestContentNosniffForRequestOnly(t *testing.T) { |
| 775 |
s := New(Options{ |
| 776 |
ContentTypeNosniff: true, |
| 777 |
}) |
| 778 |
|
| 779 |
res := httptest.NewRecorder() |
| 780 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 781 |
|
| 782 |
s.HandlerForRequestOnly(myHandler).ServeHTTP(res, req) |
| 783 |
|
| 784 |
expect(t, res.Code, http.StatusOK) |
| 785 |
expect(t, res.Header().Get("X-Content-Type-Options"), "") |
| 786 |
} |
| 787 |
|
| 788 |
func TestXSSProtection(t *testing.T) { |
| 789 |
s := New(Options{ |
| 790 |
BrowserXssFilter: true, |
| 791 |
}) |
| 792 |
|
| 793 |
res := httptest.NewRecorder() |
| 794 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 795 |
|
| 796 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 797 |
|
| 798 |
expect(t, res.Code, http.StatusOK) |
| 799 |
expect(t, res.Header().Get("X-XSS-Protection"), "1; mode=block") |
| 800 |
} |
| 801 |
|
| 802 |
func TestXSSProtectionForRequestOnly(t *testing.T) { |
| 803 |
s := New(Options{ |
| 804 |
BrowserXssFilter: true, |
| 805 |
}) |
| 806 |
|
| 807 |
res := httptest.NewRecorder() |
| 808 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 809 |
|
| 810 |
s.HandlerForRequestOnly(myHandler).ServeHTTP(res, req) |
| 811 |
|
| 812 |
expect(t, res.Code, http.StatusOK) |
| 813 |
expect(t, res.Header().Get("X-XSS-Protection"), "") |
| 814 |
} |
| 815 |
|
| 816 |
func TestCustomXSSProtection(t *testing.T) { |
| 817 |
xssVal := "1; report=https://example.com" |
| 818 |
s := New(Options{ |
| 819 |
CustomBrowserXssValue: xssVal, |
| 820 |
}) |
| 821 |
|
| 822 |
res := httptest.NewRecorder() |
| 823 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 824 |
|
| 825 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 826 |
|
| 827 |
expect(t, res.Code, http.StatusOK) |
| 828 |
expect(t, res.Header().Get("X-XSS-Protection"), xssVal) |
| 829 |
} |
| 830 |
|
| 831 |
func TestCustomXSSProtectionForRequestOnly(t *testing.T) { |
| 832 |
xssVal := "1; report=https://example.com" |
| 833 |
s := New(Options{ |
| 834 |
CustomBrowserXssValue: xssVal, |
| 835 |
}) |
| 836 |
|
| 837 |
res := httptest.NewRecorder() |
| 838 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 839 |
|
| 840 |
s.HandlerForRequestOnly(myHandler).ServeHTTP(res, req) |
| 841 |
|
| 842 |
expect(t, res.Code, http.StatusOK) |
| 843 |
expect(t, res.Header().Get("X-XSS-Protection"), "") |
| 844 |
} |
| 845 |
|
| 846 |
func TestBothXSSProtection(t *testing.T) { |
| 847 |
xssVal := "0" |
| 848 |
s := New(Options{ |
| 849 |
BrowserXssFilter: true, |
| 850 |
CustomBrowserXssValue: xssVal, |
| 851 |
}) |
| 852 |
|
| 853 |
res := httptest.NewRecorder() |
| 854 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 855 |
|
| 856 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 857 |
|
| 858 |
expect(t, res.Code, http.StatusOK) |
| 859 |
expect(t, res.Header().Get("X-XSS-Protection"), xssVal) |
| 860 |
} |
| 861 |
|
| 862 |
func TestBothXSSProtectionForRequestOnly(t *testing.T) { |
| 863 |
xssVal := "0" |
| 864 |
s := New(Options{ |
| 865 |
BrowserXssFilter: true, |
| 866 |
CustomBrowserXssValue: xssVal, |
| 867 |
}) |
| 868 |
|
| 869 |
res := httptest.NewRecorder() |
| 870 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 871 |
|
| 872 |
s.HandlerForRequestOnly(myHandler).ServeHTTP(res, req) |
| 873 |
|
| 874 |
expect(t, res.Code, http.StatusOK) |
| 875 |
expect(t, res.Header().Get("X-XSS-Protection"), "") |
| 876 |
} |
| 877 |
|
| 878 |
func TestCsp(t *testing.T) { |
| 879 |
s := New(Options{ |
| 880 |
ContentSecurityPolicy: "default-src 'self'", |
| 881 |
}) |
| 882 |
|
| 883 |
res := httptest.NewRecorder() |
| 884 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 885 |
|
| 886 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 887 |
|
| 888 |
expect(t, res.Code, http.StatusOK) |
| 889 |
expect(t, res.Header().Get("Content-Security-Policy"), "default-src 'self'") |
| 890 |
} |
| 891 |
|
| 892 |
func TestCspForRequestOnly(t *testing.T) { |
| 893 |
s := New(Options{ |
| 894 |
ContentSecurityPolicy: "default-src 'self'", |
| 895 |
}) |
| 896 |
|
| 897 |
res := httptest.NewRecorder() |
| 898 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 899 |
|
| 900 |
s.HandlerForRequestOnly(myHandler).ServeHTTP(res, req) |
| 901 |
|
| 902 |
expect(t, res.Code, http.StatusOK) |
| 903 |
expect(t, res.Header().Get("Content-Security-Policy"), "") |
| 904 |
} |
| 905 |
|
| 906 |
func TestCspReportOnly(t *testing.T) { |
| 907 |
s := New(Options{ |
| 908 |
ContentSecurityPolicyReportOnly: "default-src 'self'", |
| 909 |
}) |
| 910 |
|
| 911 |
res := httptest.NewRecorder() |
| 912 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 913 |
|
| 914 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 915 |
|
| 916 |
expect(t, res.Code, http.StatusOK) |
| 917 |
expect(t, res.Header().Get("Content-Security-Policy-Report-Only"), "default-src 'self'") |
| 918 |
} |
| 919 |
|
| 920 |
func TestCspReportOnlyForRequestOnly(t *testing.T) { |
| 921 |
s := New(Options{ |
| 922 |
ContentSecurityPolicyReportOnly: "default-src 'self'", |
| 923 |
}) |
| 924 |
|
| 925 |
res := httptest.NewRecorder() |
| 926 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 927 |
|
| 928 |
s.HandlerForRequestOnly(myHandler).ServeHTTP(res, req) |
| 929 |
|
| 930 |
expect(t, res.Code, http.StatusOK) |
| 931 |
expect(t, res.Header().Get("Content-Security-Policy-Report-Only"), "") |
| 932 |
} |
| 933 |
|
| 934 |
func TestInlineSecure(t *testing.T) { |
| 935 |
s := New(Options{ |
| 936 |
FrameDeny: true, |
| 937 |
}) |
| 938 |
|
| 939 |
res := httptest.NewRecorder() |
| 940 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 941 |
|
| 942 |
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 943 |
s.HandlerFuncWithNext(w, r, nil) |
| 944 |
w.Write([]byte("bar")) |
| 945 |
}) |
| 946 |
|
| 947 |
handler.ServeHTTP(res, req) |
| 948 |
|
| 949 |
expect(t, res.Code, http.StatusOK) |
| 950 |
expect(t, res.Header().Get("X-Frame-Options"), "DENY") |
| 951 |
} |
| 952 |
|
| 953 |
func TestInlineSecureForRequestOnly(t *testing.T) { |
| 954 |
s := New(Options{ |
| 955 |
FrameDeny: true, |
| 956 |
}) |
| 957 |
|
| 958 |
res := httptest.NewRecorder() |
| 959 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 960 |
|
| 961 |
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 962 |
s.HandlerFuncWithNextForRequestOnly(w, r, nil) |
| 963 |
w.Write([]byte("bar")) |
| 964 |
}) |
| 965 |
|
| 966 |
handler.ServeHTTP(res, req) |
| 967 |
|
| 968 |
expect(t, res.Code, http.StatusOK) |
| 969 |
expect(t, res.Header().Get("X-Frame-Options"), "") |
| 970 |
} |
| 971 |
|
| 972 |
|
| 973 |
const hpkp = `pin-sha256="cUPcTAZWKaASuYWhhneDttWpY3oBAkE3h2+soZS7sWs="; pin-sha256="M8HztCzM3elUxkcjR2S5P4hhyBNf6lHkmjAHKhpGPWE="; max-age=5184000; includeSubdomains; report-uri="https://www.example.net/hpkp-report"` |
| 974 |
|
| 975 |
func TestHPKP(t *testing.T) { |
| 976 |
s := New(Options{ |
| 977 |
PublicKey: hpkp, |
| 978 |
}) |
| 979 |
|
| 980 |
res := httptest.NewRecorder() |
| 981 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 982 |
req.URL.Scheme = "https" |
| 983 |
|
| 984 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 985 |
|
| 986 |
expect(t, res.Code, http.StatusOK) |
| 987 |
expect(t, res.Header().Get("Public-Key-Pins"), hpkp) |
| 988 |
} |
| 989 |
|
| 990 |
func TestHPKPForRequestOnly(t *testing.T) { |
| 991 |
s := New(Options{ |
| 992 |
PublicKey: hpkp, |
| 993 |
}) |
| 994 |
|
| 995 |
res := httptest.NewRecorder() |
| 996 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 997 |
req.URL.Scheme = "https" |
| 998 |
|
| 999 |
s.HandlerForRequestOnly(myHandler).ServeHTTP(res, req) |
| 1000 |
|
| 1001 |
expect(t, res.Code, http.StatusOK) |
| 1002 |
expect(t, res.Header().Get("Public-Key-Pins"), "") |
| 1003 |
} |
| 1004 |
|
| 1005 |
func TestHPKPNotSet(t *testing.T) { |
| 1006 |
s := New() |
| 1007 |
|
| 1008 |
res := httptest.NewRecorder() |
| 1009 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 1010 |
|
| 1011 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 1012 |
|
| 1013 |
expect(t, res.Code, http.StatusOK) |
| 1014 |
expect(t, res.Header().Get("Public-Key-Pins"), "") |
| 1015 |
} |
| 1016 |
|
| 1017 |
func TestHPKPNotSetForRequestOnly(t *testing.T) { |
| 1018 |
s := New() |
| 1019 |
|
| 1020 |
res := httptest.NewRecorder() |
| 1021 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 1022 |
|
| 1023 |
s.HandlerForRequestOnly(myHandler).ServeHTTP(res, req) |
| 1024 |
|
| 1025 |
expect(t, res.Code, http.StatusOK) |
| 1026 |
expect(t, res.Header().Get("Public-Key-Pins"), "") |
| 1027 |
} |
| 1028 |
|
| 1029 |
func TestHPKPInDevMode(t *testing.T) { |
| 1030 |
s := New(Options{ |
| 1031 |
PublicKey: hpkp, |
| 1032 |
IsDevelopment: true, |
| 1033 |
}) |
| 1034 |
|
| 1035 |
res := httptest.NewRecorder() |
| 1036 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 1037 |
|
| 1038 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 1039 |
|
| 1040 |
expect(t, res.Code, http.StatusOK) |
| 1041 |
expect(t, res.Header().Get("Public-Key-Pins"), "") |
| 1042 |
} |
| 1043 |
|
| 1044 |
func TestHPKPInDevModeForRequestOnly(t *testing.T) { |
| 1045 |
s := New(Options{ |
| 1046 |
PublicKey: hpkp, |
| 1047 |
IsDevelopment: true, |
| 1048 |
}) |
| 1049 |
|
| 1050 |
res := httptest.NewRecorder() |
| 1051 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 1052 |
|
| 1053 |
s.HandlerForRequestOnly(myHandler).ServeHTTP(res, req) |
| 1054 |
|
| 1055 |
expect(t, res.Code, http.StatusOK) |
| 1056 |
expect(t, res.Header().Get("Public-Key-Pins"), "") |
| 1057 |
} |
| 1058 |
|
| 1059 |
func TestHPKPNonSSL(t *testing.T) { |
| 1060 |
s := New(Options{ |
| 1061 |
PublicKey: hpkp, |
| 1062 |
}) |
| 1063 |
|
| 1064 |
res := httptest.NewRecorder() |
| 1065 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 1066 |
|
| 1067 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 1068 |
|
| 1069 |
expect(t, res.Code, http.StatusOK) |
| 1070 |
expect(t, res.Header().Get("Public-Key-Pins"), "") |
| 1071 |
} |
| 1072 |
|
| 1073 |
func TestHPKPNonSSLForRequestOnly(t *testing.T) { |
| 1074 |
s := New(Options{ |
| 1075 |
PublicKey: hpkp, |
| 1076 |
}) |
| 1077 |
|
| 1078 |
res := httptest.NewRecorder() |
| 1079 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 1080 |
|
| 1081 |
s.HandlerForRequestOnly(myHandler).ServeHTTP(res, req) |
| 1082 |
|
| 1083 |
expect(t, res.Code, http.StatusOK) |
| 1084 |
expect(t, res.Header().Get("Public-Key-Pins"), "") |
| 1085 |
} |
| 1086 |
|
| 1087 |
func TestReferrer(t *testing.T) { |
| 1088 |
s := New(Options{ |
| 1089 |
ReferrerPolicy: "same-origin", |
| 1090 |
}) |
| 1091 |
|
| 1092 |
res := httptest.NewRecorder() |
| 1093 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 1094 |
|
| 1095 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 1096 |
|
| 1097 |
expect(t, res.Code, http.StatusOK) |
| 1098 |
expect(t, res.Header().Get("Referrer-Policy"), "same-origin") |
| 1099 |
} |
| 1100 |
|
| 1101 |
func TestReferrerForRequestOnly(t *testing.T) { |
| 1102 |
s := New(Options{ |
| 1103 |
ReferrerPolicy: "same-origin", |
| 1104 |
}) |
| 1105 |
|
| 1106 |
res := httptest.NewRecorder() |
| 1107 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 1108 |
|
| 1109 |
s.HandlerForRequestOnly(myHandler).ServeHTTP(res, req) |
| 1110 |
|
| 1111 |
expect(t, res.Code, http.StatusOK) |
| 1112 |
expect(t, res.Header().Get("Referrer-Policy"), "") |
| 1113 |
} |
| 1114 |
|
| 1115 |
func TestFeaturePolicy(t *testing.T) { |
| 1116 |
s := New(Options{ |
| 1117 |
FeaturePolicy: "vibrate 'none';", |
| 1118 |
}) |
| 1119 |
|
| 1120 |
res := httptest.NewRecorder() |
| 1121 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 1122 |
|
| 1123 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 1124 |
|
| 1125 |
expect(t, res.Code, http.StatusOK) |
| 1126 |
expect(t, res.Header().Get("Feature-Policy"), "vibrate 'none';") |
| 1127 |
} |
| 1128 |
|
| 1129 |
func TestPermissionsPolicy(t *testing.T) { |
| 1130 |
s := New(Options{ |
| 1131 |
PermissionsPolicy: "geolocation=(self)", |
| 1132 |
}) |
| 1133 |
|
| 1134 |
res := httptest.NewRecorder() |
| 1135 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 1136 |
|
| 1137 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 1138 |
|
| 1139 |
expect(t, res.Code, http.StatusOK) |
| 1140 |
expect(t, res.Header().Get("Permissions-Policy"), "geolocation=(self)") |
| 1141 |
} |
| 1142 |
|
| 1143 |
func TestExpectCT(t *testing.T) { |
| 1144 |
s := New(Options{ |
| 1145 |
ExpectCTHeader: `enforce, max-age=30, report-uri="https://www.example.com/ct-report"`, |
| 1146 |
}) |
| 1147 |
|
| 1148 |
res := httptest.NewRecorder() |
| 1149 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 1150 |
req.Host = "www.example.com" |
| 1151 |
|
| 1152 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 1153 |
|
| 1154 |
expect(t, res.Code, http.StatusOK) |
| 1155 |
expect(t, res.Header().Get("Expect-CT"), `enforce, max-age=30, report-uri="https://www.example.com/ct-report"`) |
| 1156 |
} |
| 1157 |
|
| 1158 |
func TestIsSSL(t *testing.T) { |
| 1159 |
s := New(Options{ |
| 1160 |
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"}, |
| 1161 |
}) |
| 1162 |
|
| 1163 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 1164 |
expect(t, s.isSSL(req), false) |
| 1165 |
|
| 1166 |
req, _ = http.NewRequest("GET", "/foo", nil) |
| 1167 |
req.TLS = &tls.ConnectionState{ |
| 1168 |
CipherSuite: 16, |
| 1169 |
} |
| 1170 |
expect(t, s.isSSL(req), true) |
| 1171 |
|
| 1172 |
req, _ = http.NewRequest("GET", "/foo", nil) |
| 1173 |
req.URL.Scheme = "https" |
| 1174 |
expect(t, s.isSSL(req), true) |
| 1175 |
|
| 1176 |
req, _ = http.NewRequest("GET", "/foo", nil) |
| 1177 |
req.Header.Add("X-Forwarded-Proto", "https") |
| 1178 |
expect(t, s.isSSL(req), true) |
| 1179 |
} |
| 1180 |
|
| 1181 |
func TestSSLForceHostWithHTTPS(t *testing.T) { |
| 1182 |
s := New(Options{ |
| 1183 |
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"}, |
| 1184 |
SSLHost: "secure.example.com", |
| 1185 |
SSLForceHost: true, |
| 1186 |
}) |
| 1187 |
|
| 1188 |
res := httptest.NewRecorder() |
| 1189 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 1190 |
req.Host = "www.example.com" |
| 1191 |
req.URL.Scheme = "https" |
| 1192 |
req.Header.Add("X-Forwarded-Proto", "https") |
| 1193 |
|
| 1194 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 1195 |
|
| 1196 |
expect(t, res.Code, http.StatusMovedPermanently) |
| 1197 |
} |
| 1198 |
|
| 1199 |
func TestSSLForceHostWithHTTP(t *testing.T) { |
| 1200 |
s := New(Options{ |
| 1201 |
SSLHost: "secure.example.com", |
| 1202 |
SSLForceHost: true, |
| 1203 |
}) |
| 1204 |
|
| 1205 |
res := httptest.NewRecorder() |
| 1206 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 1207 |
req.Host = "www.example.com" |
| 1208 |
req.URL.Scheme = "http" |
| 1209 |
req.Header.Add("X-Forwarded-Proto", "http") |
| 1210 |
|
| 1211 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 1212 |
|
| 1213 |
expect(t, res.Code, http.StatusMovedPermanently) |
| 1214 |
} |
| 1215 |
|
| 1216 |
func TestSSLForceHostWithSSLRedirect(t *testing.T) { |
| 1217 |
s := New(Options{ |
| 1218 |
SSLRedirect: true, |
| 1219 |
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"}, |
| 1220 |
SSLHost: "secure.example.com", |
| 1221 |
SSLForceHost: true, |
| 1222 |
}) |
| 1223 |
|
| 1224 |
res := httptest.NewRecorder() |
| 1225 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 1226 |
req.Host = "www.example.com" |
| 1227 |
req.URL.Scheme = "https" |
| 1228 |
req.Header.Add("X-Forwarded-Proto", "https") |
| 1229 |
|
| 1230 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 1231 |
|
| 1232 |
expect(t, res.Code, http.StatusMovedPermanently) |
| 1233 |
} |
| 1234 |
|
| 1235 |
func TestSSLForceHostTemporaryRedirect(t *testing.T) { |
| 1236 |
s := New(Options{ |
| 1237 |
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"}, |
| 1238 |
SSLHost: "secure.example.com", |
| 1239 |
SSLForceHost: true, |
| 1240 |
SSLTemporaryRedirect: true, |
| 1241 |
}) |
| 1242 |
|
| 1243 |
res := httptest.NewRecorder() |
| 1244 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 1245 |
req.Host = "www.example.com" |
| 1246 |
req.URL.Scheme = "https" |
| 1247 |
req.Header.Add("X-Forwarded-Proto", "https") |
| 1248 |
|
| 1249 |
s.Handler(myHandler).ServeHTTP(res, req) |
| 1250 |
|
| 1251 |
expect(t, res.Code, http.StatusTemporaryRedirect) |
| 1252 |
} |
| 1253 |
|
| 1254 |
func TestModifyResponseHeadersNoSSL(t *testing.T) { |
| 1255 |
s := New(Options{ |
| 1256 |
SSLRedirect: false, |
| 1257 |
}) |
| 1258 |
|
| 1259 |
res := &http.Response{} |
| 1260 |
res.Header = http.Header{"Location": []string{"http://example.com"}} |
| 1261 |
|
| 1262 |
err := s.ModifyResponseHeaders(res) |
| 1263 |
expect(t, err, nil) |
| 1264 |
|
| 1265 |
expect(t, res.Header.Get("Location"), "http://example.com") |
| 1266 |
} |
| 1267 |
|
| 1268 |
func TestModifyResponseHeadersWithSSLAndDifferentSSLHost(t *testing.T) { |
| 1269 |
s := New(Options{ |
| 1270 |
SSLRedirect: true, |
| 1271 |
SSLHost: "secure.example.com", |
| 1272 |
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"}, |
| 1273 |
}) |
| 1274 |
|
| 1275 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 1276 |
req.Host = "www.example.com" |
| 1277 |
req.URL.Scheme = "http" |
| 1278 |
req.Header.Add("X-Forwarded-Proto", "https") |
| 1279 |
|
| 1280 |
res := &http.Response{} |
| 1281 |
res.Header = http.Header{"Location": []string{"http://example.com"}} |
| 1282 |
res.Request = req |
| 1283 |
|
| 1284 |
expect(t, res.Header.Get("Location"), "http://example.com") |
| 1285 |
|
| 1286 |
err := s.ModifyResponseHeaders(res) |
| 1287 |
expect(t, err, nil) |
| 1288 |
|
| 1289 |
expect(t, res.Header.Get("Location"), "http://example.com") |
| 1290 |
} |
| 1291 |
|
| 1292 |
func TestModifyResponseHeadersWithSSLAndNoSSLHost(t *testing.T) { |
| 1293 |
s := New(Options{ |
| 1294 |
SSLRedirect: true, |
| 1295 |
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"}, |
| 1296 |
}) |
| 1297 |
|
| 1298 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 1299 |
req.Host = "www.example.com" |
| 1300 |
req.URL.Scheme = "http" |
| 1301 |
req.Header.Add("X-Forwarded-Proto", "https") |
| 1302 |
|
| 1303 |
res := &http.Response{} |
| 1304 |
res.Header = http.Header{"Location": []string{"http://example.com"}} |
| 1305 |
res.Request = req |
| 1306 |
|
| 1307 |
expect(t, res.Header.Get("Location"), "http://example.com") |
| 1308 |
|
| 1309 |
err := s.ModifyResponseHeaders(res) |
| 1310 |
expect(t, err, nil) |
| 1311 |
|
| 1312 |
expect(t, res.Header.Get("Location"), "http://example.com") |
| 1313 |
} |
| 1314 |
|
| 1315 |
func TestModifyResponseHeadersWithSSLAndMatchingSSLHost(t *testing.T) { |
| 1316 |
s := New(Options{ |
| 1317 |
SSLRedirect: true, |
| 1318 |
SSLHost: "secure.example.com", |
| 1319 |
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"}, |
| 1320 |
}) |
| 1321 |
|
| 1322 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 1323 |
req.Host = "www.example.com" |
| 1324 |
req.URL.Scheme = "http" |
| 1325 |
req.Header.Add("X-Forwarded-Proto", "https") |
| 1326 |
|
| 1327 |
res := &http.Response{} |
| 1328 |
res.Header = http.Header{"Location": []string{"http://secure.example.com"}} |
| 1329 |
res.Request = req |
| 1330 |
|
| 1331 |
expect(t, res.Header.Get("Location"), "http://secure.example.com") |
| 1332 |
|
| 1333 |
err := s.ModifyResponseHeaders(res) |
| 1334 |
expect(t, err, nil) |
| 1335 |
|
| 1336 |
expect(t, res.Header.Get("Location"), "https://secure.example.com") |
| 1337 |
} |
| 1338 |
|
| 1339 |
func TestModifyResponseHeadersWithSSLAndPortInLocationResponse(t *testing.T) { |
| 1340 |
s := New(Options{ |
| 1341 |
SSLRedirect: true, |
| 1342 |
SSLHost: "secure.example.com", |
| 1343 |
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"}, |
| 1344 |
}) |
| 1345 |
|
| 1346 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 1347 |
req.Host = "www.example.com" |
| 1348 |
req.URL.Scheme = "http" |
| 1349 |
req.Header.Add("X-Forwarded-Proto", "https") |
| 1350 |
|
| 1351 |
res := &http.Response{} |
| 1352 |
res.Header = http.Header{"Location": []string{"http://secure.example.com:877"}} |
| 1353 |
res.Request = req |
| 1354 |
|
| 1355 |
expect(t, res.Header.Get("Location"), "http://secure.example.com:877") |
| 1356 |
|
| 1357 |
err := s.ModifyResponseHeaders(res) |
| 1358 |
expect(t, err, nil) |
| 1359 |
|
| 1360 |
expect(t, res.Header.Get("Location"), "http://secure.example.com:877") |
| 1361 |
} |
| 1362 |
|
| 1363 |
func TestModifyResponseHeadersWithSSLAndPathInLocationResponse(t *testing.T) { |
| 1364 |
s := New(Options{ |
| 1365 |
SSLRedirect: true, |
| 1366 |
SSLHost: "secure.example.com", |
| 1367 |
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"}, |
| 1368 |
}) |
| 1369 |
|
| 1370 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 1371 |
req.Host = "www.example.com" |
| 1372 |
req.URL.Scheme = "http" |
| 1373 |
req.Header.Add("X-Forwarded-Proto", "https") |
| 1374 |
|
| 1375 |
res := &http.Response{} |
| 1376 |
res.Header = http.Header{"Location": []string{"http://secure.example.com/admin/login"}} |
| 1377 |
res.Request = req |
| 1378 |
|
| 1379 |
expect(t, res.Header.Get("Location"), "http://secure.example.com/admin/login") |
| 1380 |
|
| 1381 |
err := s.ModifyResponseHeaders(res) |
| 1382 |
expect(t, err, nil) |
| 1383 |
|
| 1384 |
expect(t, res.Header.Get("Location"), "https://secure.example.com/admin/login") |
| 1385 |
} |
| 1386 |
|
| 1387 |
func TestCustomSecureContextKey(t *testing.T) { |
| 1388 |
s1 := New(Options{ |
| 1389 |
BrowserXssFilter: true, |
| 1390 |
CustomBrowserXssValue: "0", |
| 1391 |
SecureContextKey: "totallySecureContextKey", |
| 1392 |
}) |
| 1393 |
|
| 1394 |
res := httptest.NewRecorder() |
| 1395 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 1396 |
|
| 1397 |
var actual *http.Request |
| 1398 |
hf := func(w http.ResponseWriter, r *http.Request) { |
| 1399 |
actual = r |
| 1400 |
} |
| 1401 |
|
| 1402 |
s1.HandlerFuncWithNextForRequestOnly(res, req, hf) |
| 1403 |
contextHeaders := actual.Context().Value(s1.ctxSecureHeaderKey).(http.Header) |
| 1404 |
expect(t, contextHeaders.Get(xssProtectionHeader), s1.opt.CustomBrowserXssValue) |
| 1405 |
} |
| 1406 |
|
| 1407 |
func TestMultipleCustomSecureContextKeys(t *testing.T) { |
| 1408 |
s1 := New(Options{ |
| 1409 |
BrowserXssFilter: true, |
| 1410 |
CustomBrowserXssValue: "0", |
| 1411 |
SecureContextKey: "totallySecureContextKey", |
| 1412 |
}) |
| 1413 |
|
| 1414 |
s2 := New(Options{ |
| 1415 |
FeaturePolicy: "test", |
| 1416 |
SecureContextKey: "anotherSecureContextKey", |
| 1417 |
}) |
| 1418 |
|
| 1419 |
res := httptest.NewRecorder() |
| 1420 |
req, _ := http.NewRequest("GET", "/foo", nil) |
| 1421 |
|
| 1422 |
var actual *http.Request |
| 1423 |
hf := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 1424 |
actual = r |
| 1425 |
}) |
| 1426 |
|
| 1427 |
next := s1.HandlerForRequestOnly(hf) |
| 1428 |
s2.HandlerFuncWithNextForRequestOnly(res, req, next.ServeHTTP) |
| 1429 |
|
| 1430 |
s1Headers := actual.Context().Value(s1.ctxSecureHeaderKey).(http.Header) |
| 1431 |
s2Headers := actual.Context().Value(s2.ctxSecureHeaderKey).(http.Header) |
| 1432 |
|
| 1433 |
expect(t, s1Headers.Get(xssProtectionHeader), s1.opt.CustomBrowserXssValue) |
| 1434 |
expect(t, s2Headers.Get(featurePolicyHeader), s2.opt.FeaturePolicy) |
| 1435 |
} |
| 1436 |
|
| 1437 |
|
| 1438 |
func expect(t *testing.T, a interface{}, b interface{}) { |
| 1439 |
if a != b { |
| 1440 |
t.Errorf("Expected [%v] (type %v) - Got [%v] (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a)) |
| 1441 |
} |
| 1442 |
} |