mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-12-15 08:23:34 +01:00
ed8fc04898
support `Strict-Transport-Security`, `Content-Security-Policy` and `X-Frame-Options`
HTTP headers in all VictoriaMetrics components.
The values for headers can be specified by users via the following flags:
`-http.header.hsts`, `-http.header.csp` and `-http.header.frameOptions`.
Co-authored-by: hagen1778 <roman@victoriametrics.com>
(cherry picked from commit ad839aa492
)
Signed-off-by: hagen1778 <roman@victoriametrics.com>
67 lines
1.6 KiB
Go
67 lines
1.6 KiB
Go
package httpserver
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestGetQuotedRemoteAddr(t *testing.T) {
|
|
f := func(remoteAddr, xForwardedFor, expectedAddr string) {
|
|
t.Helper()
|
|
|
|
req := &http.Request{
|
|
RemoteAddr: remoteAddr,
|
|
}
|
|
if xForwardedFor != "" {
|
|
req.Header = map[string][]string{
|
|
"X-Forwarded-For": {xForwardedFor},
|
|
}
|
|
}
|
|
addr := GetQuotedRemoteAddr(req)
|
|
if addr != expectedAddr {
|
|
t.Fatalf("unexpected remote addr;\ngot\n%s\nwant\n%s", addr, expectedAddr)
|
|
}
|
|
|
|
// Verify that the addr can be unmarshaled as JSON string
|
|
var s string
|
|
if err := json.Unmarshal([]byte(addr), &s); err != nil {
|
|
t.Fatalf("cannot unmarshal addr: %s", err)
|
|
}
|
|
}
|
|
|
|
f("1.2.3.4", "", `"1.2.3.4"`)
|
|
f("1.2.3.4", "foo.bar", `"1.2.3.4, X-Forwarded-For: foo.bar"`)
|
|
f("1.2\n\"3.4", "foo\nb\"ar", `"1.2\n\"3.4, X-Forwarded-For: foo\nb\"ar"`)
|
|
}
|
|
|
|
func TestHandlerWrapper(t *testing.T) {
|
|
*headerHSTS = "foo"
|
|
*headerFrameOptions = "bar"
|
|
*headerCSP = "baz"
|
|
defer func() {
|
|
*headerHSTS = ""
|
|
*headerFrameOptions = ""
|
|
*headerCSP = ""
|
|
}()
|
|
|
|
req, _ := http.NewRequest("GET", "/health", nil)
|
|
|
|
srv := &server{s: &http.Server{}}
|
|
w := &httptest.ResponseRecorder{}
|
|
handlerWrapper(srv, w, req, func(_ http.ResponseWriter, _ *http.Request) bool {
|
|
return true
|
|
})
|
|
|
|
if w.Header().Get("Strict-Transport-Security") != "foo" {
|
|
t.Errorf("HSTS header not set")
|
|
}
|
|
if w.Header().Get("X-Frame-Options") != "bar" {
|
|
t.Errorf("X-Frame-Options header not set")
|
|
}
|
|
if w.Header().Get("Content-Security-Policy") != "baz" {
|
|
t.Errorf("CSP header not set")
|
|
}
|
|
}
|