mirror of
https://github.com/tailscale/tailscale.git
synced 2025-10-09 08:01:31 +00:00
cmd/derper,tsweb: consistently add HTTP security headers (#8579)
Add a few helper functions in tsweb to add common security headers to handlers. Use those functions for all non-tailscaled-facing endpoints in derper. Signed-off-by: Andrew Lytvynov <awly@tailscale.com>
This commit is contained in:
@@ -51,7 +51,7 @@ func Debugger(mux *http.ServeMux) *DebugHandler {
|
||||
// Register this one directly on mux, rather than using
|
||||
// ret.URL/etc, as we don't need another line of output on the
|
||||
// index page. The /pprof/ index already covers it.
|
||||
mux.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
|
||||
mux.Handle("/debug/pprof/profile", BrowserHeaderHandler(http.HandlerFunc(pprof.Profile)))
|
||||
|
||||
ret.KVFunc("Uptime", func() any { return varz.Uptime() })
|
||||
ret.KV("Version", version.Long())
|
||||
@@ -80,6 +80,7 @@ func (d *DebugHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
AddBrowserHeaders(w)
|
||||
f := func(format string, args ...any) { fmt.Fprintf(w, format, args...) }
|
||||
f("<html><body><h1>%s debug</h1><ul>", version.CmdName())
|
||||
for _, kv := range d.kvs {
|
||||
@@ -97,7 +98,7 @@ func (d *DebugHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
// entry in /debug/ for it.
|
||||
func (d *DebugHandler) Handle(slug, desc string, handler http.Handler) {
|
||||
href := "/debug/" + slug
|
||||
d.mux.Handle(href, Protected(handler))
|
||||
d.mux.Handle(href, Protected(BrowserHeaderHandler(handler)))
|
||||
d.URL(href, desc)
|
||||
}
|
||||
|
||||
|
@@ -423,3 +423,37 @@ func Error(code int, msg string, err error) HTTPError {
|
||||
func VarzHandler(w http.ResponseWriter, r *http.Request) {
|
||||
varz.Handler(w, r)
|
||||
}
|
||||
|
||||
// AddBrowserHeaders sets various HTTP security headers for browser-facing endpoints.
|
||||
//
|
||||
// The specific headers:
|
||||
// - require HTTPS access (HSTS)
|
||||
// - disallow iframe embedding
|
||||
// - mitigate MIME confusion attacks
|
||||
//
|
||||
// These headers are based on
|
||||
// https://infosec.mozilla.org/guidelines/web_security
|
||||
func AddBrowserHeaders(w http.ResponseWriter) {
|
||||
w.Header().Set("Strict-Transport-Security", "max-age=63072000; includeSubDomains")
|
||||
w.Header().Set("Content-Security-Policy", "default-src 'self'; frame-ancestors 'none'; form-action 'none'; base-uri 'self'; block-all-mixed-content; plugin-types 'none'")
|
||||
w.Header().Set("X-Frame-Options", "DENY")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
}
|
||||
|
||||
// BrowserHeaderHandler wraps the provided http.Handler with a call to
|
||||
// AddBrowserHeaders.
|
||||
func BrowserHeaderHandler(h http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
AddBrowserHeaders(w)
|
||||
h.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
// BrowserHeaderHandlerFunc wraps the provided http.HandlerFunc with a call to
|
||||
// AddBrowserHeaders.
|
||||
func BrowserHeaderHandlerFunc(h http.HandlerFunc) http.HandlerFunc {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
AddBrowserHeaders(w)
|
||||
h.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user