Initial upload to Gitea
This commit is contained in:
1 parent
78193254ad
commit
8478998cae
1485 files changed
+461025
-42348
No files matched your search
@@ -0,0 +1,184 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// probeStatsRequest builds a signed GET /api/system/stats probe exactly the
|
||||
// way the panel does (utils/node-hmac.js signPanelProbeRequest): the standard
|
||||
// node admin envelope with action "probe" and subject "stats".
|
||||
func probeStatsRequest(a *Agent, purpose, nonce string) *httptest.ResponseRecorder {
|
||||
ts := strconv.FormatInt(time.Now().UnixMilli(), 10)
|
||||
base := a.uuid + "|probe|" + purpose + "|admin|" + ts + "|" + nonce
|
||||
|
||||
r := httptest.NewRequest("GET", "/api/system/stats", nil)
|
||||
r.Header.Set("Authorization", "Bearer "+a.token)
|
||||
r.Header.Set("x-panel-role", "admin")
|
||||
r.Header.Set("x-panel-ts", ts)
|
||||
r.Header.Set("x-panel-nonce", nonce)
|
||||
r.Header.Set("x-panel-sign", hmacHex(a.panelHMAC, base))
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
a.ServeHTTP(w, r)
|
||||
return w
|
||||
}
|
||||
|
||||
func probeJSONBody(t *testing.T, w *httptest.ResponseRecorder) map[string]any {
|
||||
t.Helper()
|
||||
var body map[string]any
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
|
||||
t.Fatalf("response is not JSON: %v", err)
|
||||
}
|
||||
return body
|
||||
}
|
||||
|
||||
func TestSystemStatsProbeEchoesFreshNonce(t *testing.T) {
|
||||
a := &Agent{uuid: "node-uuid", token: "tok", panelHMAC: "test-secret"}
|
||||
|
||||
w := probeStatsRequest(a, "stats", "0123456789abcdef0123456789abcdef")
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("signed probe rejected: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
body := probeJSONBody(t, w)
|
||||
if body["panel_nonce"] != "0123456789abcdef0123456789abcdef" {
|
||||
t.Fatalf("nonce not echoed: %v", body["panel_nonce"])
|
||||
}
|
||||
if body["panel_nonce_proof"] != hmacHex("test-secret", "node-uuid|probe-response|stats|0123456789abcdef0123456789abcdef") {
|
||||
t.Fatalf("missing or invalid stats response proof")
|
||||
}
|
||||
if body["ok"] != true {
|
||||
t.Fatalf("stats body missing ok:true: %v", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemStatsProbeReplayRejected(t *testing.T) {
|
||||
a := &Agent{uuid: "node-uuid", token: "tok", panelHMAC: "test-secret"}
|
||||
|
||||
nonce := "11111111111111111111111111111111"
|
||||
if w := probeStatsRequest(a, "stats", nonce); w.Code != 200 {
|
||||
t.Fatalf("first probe rejected: %d", w.Code)
|
||||
}
|
||||
w := probeStatsRequest(a, "stats", nonce)
|
||||
if w.Code != 401 {
|
||||
t.Fatalf("replayed probe accepted: %d", w.Code)
|
||||
}
|
||||
if body := probeJSONBody(t, w); body["error"] != "replayed" {
|
||||
t.Fatalf("replay rejection reason: %v", body["error"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemStatsProbeBadSignatureRejected(t *testing.T) {
|
||||
a := &Agent{uuid: "node-uuid", token: "tok", panelHMAC: "test-secret"}
|
||||
|
||||
ts := strconv.FormatInt(time.Now().UnixMilli(), 10)
|
||||
r := httptest.NewRequest("GET", "/api/system/stats", nil)
|
||||
r.Header.Set("Authorization", "Bearer "+a.token)
|
||||
r.Header.Set("x-panel-role", "admin")
|
||||
r.Header.Set("x-panel-ts", ts)
|
||||
r.Header.Set("x-panel-nonce", "22222222222222222222222222222222")
|
||||
r.Header.Set("x-panel-sign", hmacHex("wrong-secret", a.uuid+"|probe|stats|admin|"+ts+"|22222222222222222222222222222222"))
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
a.ServeHTTP(w, r)
|
||||
if w.Code != 401 {
|
||||
t.Fatalf("bad-signature probe accepted: %d", w.Code)
|
||||
}
|
||||
if body := probeJSONBody(t, w); body["error"] != "bad-signature" {
|
||||
t.Fatalf("bad-signature rejection reason: %v", body["error"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemStatsLegacyUnsignedUnchanged(t *testing.T) {
|
||||
a := &Agent{uuid: "node-uuid", token: "tok", panelHMAC: "test-secret"}
|
||||
|
||||
r := httptest.NewRequest("GET", "/api/system/stats", nil)
|
||||
r.Header.Set("Authorization", "Bearer "+a.token)
|
||||
w := httptest.NewRecorder()
|
||||
a.ServeHTTP(w, r)
|
||||
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("legacy unsigned probe rejected: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
body := probeJSONBody(t, w)
|
||||
if _, present := body["panel_nonce"]; present {
|
||||
t.Fatalf("legacy probe must not carry a nonce echo")
|
||||
}
|
||||
if _, present := body["panel_nonce_proof"]; present {
|
||||
t.Fatalf("legacy probe must not carry a response proof")
|
||||
}
|
||||
if body["ok"] != true {
|
||||
t.Fatalf("legacy stats body missing ok:true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemStatsProbeStillRequiresBearer(t *testing.T) {
|
||||
// A signed envelope alone must not bypass bearer auth: authOK runs before
|
||||
// the handler, so an attacker holding only the signing secret (and not
|
||||
// the node token) still cannot read stats.
|
||||
a := &Agent{uuid: "node-uuid", token: "tok", panelHMAC: "test-secret"}
|
||||
|
||||
ts := strconv.FormatInt(time.Now().UnixMilli(), 10)
|
||||
nonce := "33333333333333333333333333333333"
|
||||
r := httptest.NewRequest("GET", "/api/system/stats", nil)
|
||||
r.Header.Set("x-panel-role", "admin")
|
||||
r.Header.Set("x-panel-ts", ts)
|
||||
r.Header.Set("x-panel-nonce", nonce)
|
||||
r.Header.Set("x-panel-sign", hmacHex(a.panelHMAC, a.uuid+"|probe|stats|admin|"+ts+"|"+nonce))
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
a.ServeHTTP(w, r)
|
||||
if w.Code != 401 {
|
||||
t.Fatalf("signed probe without bearer accepted: %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestV1InfoProbeEchoesFreshNonce(t *testing.T) {
|
||||
a := &Agent{uuid: "node-uuid", token: "tok", panelHMAC: "test-secret"}
|
||||
|
||||
ts := strconv.FormatInt(time.Now().UnixMilli(), 10)
|
||||
nonce := "44444444444444444444444444444445"
|
||||
r := httptest.NewRequest("GET", "/v1/info", nil)
|
||||
r.Header.Set("Authorization", "Bearer "+a.token)
|
||||
r.Header.Set("x-panel-role", "admin")
|
||||
r.Header.Set("x-panel-ts", ts)
|
||||
r.Header.Set("x-panel-nonce", nonce)
|
||||
r.Header.Set("x-panel-sign", hmacHex(a.panelHMAC, a.uuid+"|probe|info|admin|"+ts+"|"+nonce))
|
||||
a.setPanelHMAC("rotated-secret", time.Minute)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
a.ServeHTTP(w, r)
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("signed /v1/info probe rejected: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
body := probeJSONBody(t, w)
|
||||
if body["panel_nonce"] != nonce {
|
||||
t.Fatalf("nonce not echoed on /v1/info: %v", body["panel_nonce"])
|
||||
}
|
||||
if body["panel_nonce_proof"] != hmacHex("test-secret", "node-uuid|probe-response|info|"+nonce) {
|
||||
t.Fatalf("info response proof did not use the accepted signing key")
|
||||
}
|
||||
if node, ok := body["node"].(map[string]any); !ok || node["uuid"] != a.uuid {
|
||||
t.Fatalf("info body missing node.uuid: %v", body["node"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyPanelProbeHelper(t *testing.T) {
|
||||
a := &Agent{uuid: "node-uuid", token: "tok", panelHMAC: "test-secret"}
|
||||
|
||||
// Unsigned request: legacy path, ok with no echo.
|
||||
ok, proof, why := a.verifyPanelProbe(httptest.NewRequest("GET", "/v1/info", nil), "info")
|
||||
if !ok || proof != (panelProbeProof{}) || why != "" {
|
||||
t.Fatalf("unsigned probe should be legacy-ok: ok=%v proof=%v why=%q", ok, proof, why)
|
||||
}
|
||||
|
||||
// Partial envelope (ts only) must fail closed, not silently downgrade.
|
||||
r := httptest.NewRequest("GET", "/v1/info", nil)
|
||||
r.Header.Set("x-panel-ts", strconv.FormatInt(time.Now().UnixMilli(), 10))
|
||||
if ok, _, why := a.verifyPanelProbe(r, "info"); ok || why == "" {
|
||||
t.Fatalf("partial envelope accepted: ok=%v why=%q", ok, why)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user