mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-12-01 03:21:51 +08:00
fix: remove application fingerprints from public responses
This commit is contained in:
1 parent
c0b558fa1e
commit
5203176364
9 files changed
+195
-20
No files matched your search
@@ -140,7 +140,7 @@ func (s *Server) requireBasicAuth(next http.Handler) http.Handler {
|
||||
username, password, ok := r.BasicAuth()
|
||||
if !ok || len(password) > maxPasswordBytes || username != cfg.Username ||
|
||||
bcrypt.CompareHashAndPassword([]byte(cfg.PasswordHash), []byte(password)) != nil {
|
||||
w.Header().Set("WWW-Authenticate", `Basic realm="ARTEX", charset="UTF-8"`)
|
||||
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted", charset="UTF-8"`)
|
||||
writeErr(w, http.StatusUnauthorized, "请先完成 HTTP Basic Auth 验证")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -273,7 +273,7 @@ func TestBasicAuthGate(t *testing.T) {
|
||||
if w.Code != tc.want {
|
||||
t.Fatalf("status=%d want %d: %s", w.Code, tc.want, w.Body.String())
|
||||
}
|
||||
if w.Code == 401 && w.Header().Get("WWW-Authenticate") != `Basic realm="ARTEX", charset="UTF-8"` {
|
||||
if w.Code == 401 && w.Header().Get("WWW-Authenticate") != `Basic realm="Restricted", charset="UTF-8"` {
|
||||
t.Fatal("missing browser challenge")
|
||||
}
|
||||
})
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPublicHealthHasNoApplicationFingerprint(t *testing.T) {
|
||||
for _, enabled := range []bool{false, true} {
|
||||
name := "basic auth disabled"
|
||||
if enabled {
|
||||
name = "basic auth enabled"
|
||||
}
|
||||
t.Run(name, func(t *testing.T) {
|
||||
s, _ := basicAuthTestServer(t)
|
||||
if enabled {
|
||||
w := saveBasicAuthForTest(s, `{"enabled":true,"username":"entry","password":"test-pass"}`)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("enable gate: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
h := s.Handler()
|
||||
for _, method := range []string{http.MethodGet, http.MethodHead} {
|
||||
t.Run(method, func(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
h.ServeHTTP(w, httptest.NewRequest(method, "/api/health", nil))
|
||||
if w.Code != http.StatusOK || w.Header().Get("WWW-Authenticate") != "" {
|
||||
t.Fatalf("public health unavailable: %d %v", w.Code, w.Header())
|
||||
}
|
||||
if w.Header().Get("Cache-Control") != "no-store" {
|
||||
t.Fatal("health response must not be cached")
|
||||
}
|
||||
var body map[string]any
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil || len(body) != 1 || body["ok"] != true {
|
||||
t.Fatalf("health must only expose readiness: %s (err=%v)", w.Body.String(), err)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestVersionRequiresAuthentication(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
gate bool
|
||||
basic bool
|
||||
token string
|
||||
cookie bool
|
||||
want int
|
||||
}{
|
||||
{name: "anonymous", want: http.StatusUnauthorized},
|
||||
{name: "invalid JWT", token: "invalid", want: http.StatusUnauthorized},
|
||||
{name: "valid JWT", token: "valid", want: http.StatusOK},
|
||||
{name: "valid JWT cookie", token: "valid", cookie: true, want: http.StatusOK},
|
||||
{name: "gate anonymous", gate: true, want: http.StatusUnauthorized},
|
||||
{name: "gate Basic Auth only", gate: true, basic: true, want: http.StatusUnauthorized},
|
||||
{name: "gate JWT only", gate: true, token: "valid", want: http.StatusUnauthorized},
|
||||
{name: "gate invalid JWT", gate: true, basic: true, token: "invalid", want: http.StatusUnauthorized},
|
||||
{name: "gate both credentials", gate: true, basic: true, token: "valid", cookie: true, want: http.StatusOK},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
s, _ := basicAuthTestServer(t)
|
||||
if tc.gate {
|
||||
w := saveBasicAuthForTest(s, `{"enabled":true,"username":"entry","password":"test-pass"}`)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("enable gate: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
r := httptest.NewRequest(http.MethodGet, "/api/version", nil)
|
||||
if tc.basic {
|
||||
r.SetBasicAuth("entry", "test-pass")
|
||||
}
|
||||
token := tc.token
|
||||
if token == "valid" {
|
||||
var err error
|
||||
token, err = signJWT(s.jwtKey)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if token != "" {
|
||||
if tc.cookie {
|
||||
r.AddCookie(&http.Cookie{Name: "artex_token", Value: token})
|
||||
} else {
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
}
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
s.Handler().ServeHTTP(w, r)
|
||||
if w.Code != tc.want {
|
||||
t.Fatalf("status=%d want=%d: %s", w.Code, tc.want, w.Body.String())
|
||||
}
|
||||
var body map[string]any
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if tc.want != http.StatusOK {
|
||||
if _, ok := body["version"]; ok {
|
||||
t.Fatalf("unauthorized request exposed version: %s", w.Body.String())
|
||||
}
|
||||
return
|
||||
}
|
||||
if len(body) != 1 || body["version"] != BuildVersion {
|
||||
t.Fatalf("unexpected authenticated version: %s", w.Body.String())
|
||||
}
|
||||
if w.Header().Get("Cache-Control") != "no-store" {
|
||||
t.Fatal("version response must not be cached")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
+11
-2
@@ -34,7 +34,7 @@ import (
|
||||
|
||||
// BuildVersion is the backend application version, injected from cmd/artex at
|
||||
// startup (which in turn gets it from -ldflags "-X main.version=<tag>").
|
||||
// Defaults to "dev" for local builds. Exposed to the frontend via GET /api/health.
|
||||
// Defaults to "dev" for local builds. Exposed via the authenticated GET /api/version.
|
||||
var BuildVersion = "dev"
|
||||
|
||||
// Server exposes the ARTEX backend over a JSON HTTP API for the shadcn/ui
|
||||
@@ -803,6 +803,7 @@ func (s *Server) Handler() http.Handler {
|
||||
mux.HandleFunc("PUT /api/settings/basic-auth", s.saveBasicAuthSettings)
|
||||
|
||||
mux.HandleFunc("GET /api/health", s.health)
|
||||
mux.HandleFunc("GET /api/version", s.version)
|
||||
mux.HandleFunc("GET /api/stats", s.stats)
|
||||
mux.HandleFunc("GET /api/system/metrics", s.systemMetrics)
|
||||
mux.HandleFunc("GET /api/logs", s.getLogs)
|
||||
@@ -1149,8 +1150,16 @@ func (s *Server) Handler() http.Handler {
|
||||
|
||||
// --- handlers ---
|
||||
|
||||
// health stays public for probes and must not expose application fingerprints.
|
||||
func (s *Server) health(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, 200, map[string]any{"ok": true, "service": "artex", "version": BuildVersion})
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
writeJSON(w, 200, map[string]any{"ok": true})
|
||||
}
|
||||
|
||||
// version is protected by the normal JWT middleware and optional Basic Auth gate.
|
||||
func (s *Server) version(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
writeJSON(w, 200, map[string]any{"version": BuildVersion})
|
||||
}
|
||||
|
||||
func (s *Server) stats(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
Reference in new issue
Block a user