2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2021-10-13 15:06:33 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/vault"
|
|
|
|
)
|
|
|
|
|
2022-01-27 18:06:34 +00:00
|
|
|
var defaultCustomHeaders = map[string]string{
|
2021-10-13 15:06:33 +00:00
|
|
|
"Strict-Transport-Security": "max-age=1; domains",
|
2022-01-27 18:06:34 +00:00
|
|
|
"Content-Security-Policy": "default-src 'others'",
|
|
|
|
"X-Custom-Header": "Custom header value default",
|
|
|
|
"X-Frame-Options": "Deny",
|
|
|
|
"X-Content-Type-Options": "nosniff",
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
"X-XSS-Protection": "1; mode=block",
|
2021-10-13 15:06:33 +00:00
|
|
|
}
|
|
|
|
|
2022-01-27 18:06:34 +00:00
|
|
|
var customHeader2xx = map[string]string{
|
2021-10-13 15:06:33 +00:00
|
|
|
"X-Custom-Header": "Custom header value 2xx",
|
|
|
|
}
|
|
|
|
|
2022-01-27 18:06:34 +00:00
|
|
|
var customHeader200 = map[string]string{
|
|
|
|
"Someheader-200": "200",
|
2021-10-13 15:06:33 +00:00
|
|
|
"X-Custom-Header": "Custom header value 200",
|
|
|
|
}
|
|
|
|
|
2022-01-27 18:06:34 +00:00
|
|
|
var customHeader4xx = map[string]string{
|
2021-10-13 15:06:33 +00:00
|
|
|
"Someheader-4xx": "4xx",
|
|
|
|
}
|
|
|
|
|
2022-01-27 18:06:34 +00:00
|
|
|
var customHeader400 = map[string]string{
|
2021-10-13 15:06:33 +00:00
|
|
|
"Someheader-400": "400",
|
|
|
|
}
|
|
|
|
|
2022-01-27 18:06:34 +00:00
|
|
|
var customHeader405 = map[string]string{
|
2021-10-13 15:06:33 +00:00
|
|
|
"Someheader-405": "405",
|
|
|
|
}
|
|
|
|
|
|
|
|
var CustomResponseHeaders = map[string]map[string]string{
|
|
|
|
"default": defaultCustomHeaders,
|
2022-01-27 18:06:34 +00:00
|
|
|
"307": {"X-Custom-Header": "Custom header value 307"},
|
2021-10-13 15:06:33 +00:00
|
|
|
"3xx": {
|
2022-01-27 18:06:34 +00:00
|
|
|
"X-Custom-Header": "Custom header value 3xx",
|
2021-10-13 15:06:33 +00:00
|
|
|
"X-Vault-Ignored-3xx": "Ignored 3xx",
|
|
|
|
},
|
|
|
|
"200": customHeader200,
|
|
|
|
"2xx": customHeader2xx,
|
|
|
|
"400": customHeader400,
|
|
|
|
"405": customHeader405,
|
|
|
|
"4xx": customHeader4xx,
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCustomResponseHeaders(t *testing.T) {
|
|
|
|
core, _, token := vault.TestCoreWithCustomResponseHeaderAndUI(t, CustomResponseHeaders, true)
|
|
|
|
ln, addr := TestServer(t, core)
|
|
|
|
defer ln.Close()
|
|
|
|
TestServerAuth(t, addr, token)
|
|
|
|
|
|
|
|
resp := testHttpGet(t, token, addr+"/v1/sys/raw/")
|
|
|
|
testResponseStatus(t, resp, 404)
|
|
|
|
testResponseHeader(t, resp, defaultCustomHeaders)
|
|
|
|
testResponseHeader(t, resp, customHeader4xx)
|
|
|
|
|
|
|
|
resp = testHttpGet(t, token, addr+"/v1/sys/seal")
|
|
|
|
testResponseStatus(t, resp, 405)
|
|
|
|
testResponseHeader(t, resp, defaultCustomHeaders)
|
|
|
|
testResponseHeader(t, resp, customHeader4xx)
|
|
|
|
testResponseHeader(t, resp, customHeader405)
|
|
|
|
|
|
|
|
resp = testHttpGet(t, token, addr+"/v1/sys/leader")
|
|
|
|
testResponseStatus(t, resp, 200)
|
|
|
|
testResponseHeader(t, resp, customHeader200)
|
|
|
|
|
|
|
|
resp = testHttpGet(t, token, addr+"/v1/sys/health")
|
|
|
|
testResponseStatus(t, resp, 200)
|
|
|
|
testResponseHeader(t, resp, customHeader200)
|
|
|
|
|
|
|
|
resp = testHttpGet(t, token, addr+"/v1/sys/generate-root/attempt")
|
|
|
|
testResponseStatus(t, resp, 200)
|
|
|
|
testResponseHeader(t, resp, customHeader200)
|
|
|
|
|
|
|
|
resp = testHttpGet(t, token, addr+"/v1/sys/generate-root/update")
|
|
|
|
testResponseStatus(t, resp, 400)
|
|
|
|
testResponseHeader(t, resp, defaultCustomHeaders)
|
|
|
|
testResponseHeader(t, resp, customHeader4xx)
|
|
|
|
testResponseHeader(t, resp, customHeader400)
|
|
|
|
|
|
|
|
resp = testHttpGet(t, token, addr+"/v1/sys/")
|
|
|
|
testResponseStatus(t, resp, 404)
|
|
|
|
testResponseHeader(t, resp, defaultCustomHeaders)
|
|
|
|
testResponseHeader(t, resp, customHeader4xx)
|
|
|
|
|
|
|
|
resp = testHttpGet(t, token, addr+"/v1/sys")
|
|
|
|
testResponseStatus(t, resp, 404)
|
|
|
|
testResponseHeader(t, resp, defaultCustomHeaders)
|
|
|
|
testResponseHeader(t, resp, customHeader4xx)
|
|
|
|
|
|
|
|
resp = testHttpGet(t, token, addr+"/v1/")
|
|
|
|
testResponseStatus(t, resp, 404)
|
|
|
|
testResponseHeader(t, resp, defaultCustomHeaders)
|
|
|
|
testResponseHeader(t, resp, customHeader4xx)
|
|
|
|
|
|
|
|
resp = testHttpGet(t, token, addr+"/v1")
|
|
|
|
testResponseStatus(t, resp, 404)
|
|
|
|
testResponseHeader(t, resp, defaultCustomHeaders)
|
|
|
|
testResponseHeader(t, resp, customHeader4xx)
|
|
|
|
|
|
|
|
resp = testHttpGet(t, token, addr+"/")
|
|
|
|
testResponseStatus(t, resp, 200)
|
|
|
|
testResponseHeader(t, resp, customHeader200)
|
|
|
|
|
|
|
|
resp = testHttpGet(t, token, addr+"/ui")
|
|
|
|
testResponseStatus(t, resp, 200)
|
|
|
|
testResponseHeader(t, resp, customHeader200)
|
|
|
|
|
|
|
|
resp = testHttpGet(t, token, addr+"/ui/")
|
|
|
|
testResponseStatus(t, resp, 200)
|
|
|
|
testResponseHeader(t, resp, customHeader200)
|
|
|
|
|
|
|
|
resp = testHttpPost(t, token, addr+"/v1/sys/auth/foo", map[string]interface{}{
|
|
|
|
"type": "noop",
|
|
|
|
"description": "foo",
|
|
|
|
})
|
|
|
|
testResponseStatus(t, resp, 204)
|
|
|
|
testResponseHeader(t, resp, customHeader2xx)
|
2022-01-27 18:06:34 +00:00
|
|
|
}
|