mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-19 22:50:55 +08:00
feat(server): add browser protections for the operator console
This commit is contained in:
+8
-1
@@ -1046,7 +1046,14 @@ func (s *Server) Handler() http.Handler {
|
||||
api := cors(s.requireAuth(mux))
|
||||
root := http.NewServeMux()
|
||||
root.Handle("/api/", api)
|
||||
root.Handle("/", s.webuiHandler())
|
||||
webui := s.webuiHandler()
|
||||
root.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
// Keep the operator console out of cross-origin frames and referrers.
|
||||
w.Header().Set("Content-Security-Policy", "frame-ancestors 'self'")
|
||||
w.Header().Set("X-Frame-Options", "SAMEORIGIN")
|
||||
w.Header().Set("Referrer-Policy", "same-origin")
|
||||
webui.ServeHTTP(w, r)
|
||||
})
|
||||
return limitRequestBody(root)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestConsoleBrowserProtections(t *testing.T) {
|
||||
handler := (&Server{}).Handler()
|
||||
headers := map[string]string{
|
||||
"Content-Security-Policy": "frame-ancestors 'self'",
|
||||
"X-Frame-Options": "SAMEORIGIN",
|
||||
"Referrer-Policy": "same-origin",
|
||||
}
|
||||
|
||||
for _, path := range []string{"/", "/settings/"} {
|
||||
res := httptest.NewRecorder()
|
||||
handler.ServeHTTP(res, httptest.NewRequest(http.MethodGet, path, nil))
|
||||
sent := res.Result().Header
|
||||
for name, want := range headers {
|
||||
if got := sent.Get(name); got != want {
|
||||
t.Errorf("%s: %s = %q, want %q", path, name, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
handler.ServeHTTP(res, httptest.NewRequest(http.MethodGet, "/api/health", nil))
|
||||
if res.Code != http.StatusOK {
|
||||
t.Fatalf("health: status = %d, want %d", res.Code, http.StatusOK)
|
||||
}
|
||||
sent := res.Result().Header
|
||||
for name := range headers {
|
||||
if got := sent.Get(name); got != "" {
|
||||
t.Errorf("health: unexpected %s = %q", name, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user