package main import ( "crypto/hmac" "crypto/sha256" "encoding/hex" "net/http/httptest" "strconv" "testing" "time" ) func hmacHex(secret, base string) string { m := hmac.New(sha256.New, []byte(secret)) _, _ = m.Write([]byte(base)) return hex.EncodeToString(m.Sum(nil)) } func TestVerifyNodeAdminRequestNonceReplayRejected(t *testing.T) { a := &Agent{panelHMAC: "test-secret", uuid: "node-uuid"} ts := strconv.FormatInt(time.Now().UnixMilli(), 10) nonce := "0123456789abcdef0123456789abcdef" base := "node-uuid|disk-add|/srv/disk1|admin|" + ts + "|" + nonce r := httptest.NewRequest("POST", "/v1/disks", 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, base)) if ok, why := a.verifyNodeAdminRequest(r, "disk-add", "/srv/disk1"); !ok { t.Fatalf("first use of fresh nonce rejected: %s", why) } if ok, why := a.verifyNodeAdminRequest(r, "disk-add", "/srv/disk1"); ok || why != "replayed" { t.Fatalf("replay accepted: ok=%v why=%q", ok, why) } } func TestVerifyReinstallAdminRequestNonceReplayRejected(t *testing.T) { a := &Agent{panelHMAC: "test-secret", uuid: "node-uuid"} ts := strconv.FormatInt(time.Now().UnixMilli(), 10) nonce := "fedcba9876543210fedcba9876543210" base := "server-1|reinstall|vanilla|admin|" + ts + "|" + nonce r := httptest.NewRequest("POST", "/v1/servers/server-1/reinstall", 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, base)) if ok, why := a.verifyReinstallAdminRequest(r, "server-1", "vanilla"); !ok { t.Fatalf("first use of fresh nonce rejected: %s", why) } if ok, why := a.verifyReinstallAdminRequest(r, "server-1", "vanilla"); ok || why != "replayed" { t.Fatalf("replay accepted: ok=%v why=%q", ok, why) } } func TestVerifyNodeAdminRequestLegacyCompatibility(t *testing.T) { ts := strconv.FormatInt(time.Now().UnixMilli(), 10) base := "node-uuid|disk-add|/srv/disk1|admin|" + ts // Panels that do not sign a nonce keep working only while the opt-out is // active (PANEL_REQUIRE_NONCE=0 / panel.require_nonce: false — the flag // defaults to required), matching the legacy base string exactly. a := &Agent{panelHMAC: "test-secret", uuid: "node-uuid"} r := httptest.NewRequest("POST", "/v1/disks", nil) r.Header.Set("x-panel-role", "admin") r.Header.Set("x-panel-ts", ts) r.Header.Set("x-panel-sign", hmacHex(a.panelHMAC, base)) if ok, why := a.verifyNodeAdminRequest(r, "disk-add", "/srv/disk1"); !ok { t.Fatalf("legacy nonce-less request rejected: %s", why) } // With panelRequireNonce set, the legacy envelope must fail closed. hard := &Agent{panelHMAC: "test-secret", uuid: "node-uuid", panelRequireNonce: true} r2 := httptest.NewRequest("POST", "/v1/disks", nil) r2.Header.Set("x-panel-role", "admin") r2.Header.Set("x-panel-ts", ts) r2.Header.Set("x-panel-sign", hmacHex(hard.panelHMAC, base)) if ok, why := hard.verifyNodeAdminRequest(r2, "disk-add", "/srv/disk1"); ok || why != "nonce-required" { t.Fatalf("legacy request accepted with require-nonce enabled: ok=%v why=%q", ok, why) } } func TestVerifyPanelSignedEnvelopeRejections(t *testing.T) { a := &Agent{panelHMAC: "test-secret", uuid: "node-uuid"} nonce := "0123456789abcdef0123456789abcdef" // Expired timestamp. oldTS := strconv.FormatInt(time.Now().Add(-6*time.Minute).UnixMilli(), 10) r := httptest.NewRequest("POST", "/x", nil) r.Header.Set("x-panel-role", "admin") r.Header.Set("x-panel-ts", oldTS) r.Header.Set("x-panel-nonce", nonce) r.Header.Set("x-panel-sign", hmacHex(a.panelHMAC, "node-uuid|disk-add|/srv/disk1|admin|"+oldTS+"|"+nonce)) if ok, why := a.verifyNodeAdminRequest(r, "disk-add", "/srv/disk1"); ok || why != "expired" { t.Fatalf("expired request accepted: ok=%v why=%q", ok, why) } // Tampered nonce: signed one value, sends another. ts := strconv.FormatInt(time.Now().UnixMilli(), 10) r = httptest.NewRequest("POST", "/x", nil) r.Header.Set("x-panel-role", "admin") r.Header.Set("x-panel-ts", ts) r.Header.Set("x-panel-nonce", "ffffffffffffffffffffffffffffffff") r.Header.Set("x-panel-sign", hmacHex(a.panelHMAC, "node-uuid|disk-add|/srv/disk1|admin|"+ts+"|"+nonce)) if ok, why := a.verifyNodeAdminRequest(r, "disk-add", "/srv/disk1"); ok || why != "bad-signature" { t.Fatalf("tampered nonce accepted: ok=%v why=%q", ok, why) } // Malformed nonce (too short, contains the field separator). r = httptest.NewRequest("POST", "/x", nil) r.Header.Set("x-panel-role", "admin") r.Header.Set("x-panel-ts", ts) r.Header.Set("x-panel-nonce", "abc|def") r.Header.Set("x-panel-sign", hmacHex(a.panelHMAC, "node-uuid|disk-add|/srv/disk1|admin|"+ts+"|"+nonce)) if ok, why := a.verifyNodeAdminRequest(r, "disk-add", "/srv/disk1"); ok || why != "bad-nonce" { t.Fatalf("malformed nonce accepted: ok=%v why=%q", ok, why) } // Wrong signature. r = httptest.NewRequest("POST", "/x", 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("other-secret", "node-uuid|disk-add|/srv/disk1|admin|"+ts+"|"+nonce)) if ok, why := a.verifyNodeAdminRequest(r, "disk-add", "/srv/disk1"); ok || why != "bad-signature" { t.Fatalf("wrong-secret signature accepted: ok=%v why=%q", ok, why) } } func TestVerifyPanelSignatureNonceRoundTrip(t *testing.T) { // The apply-version envelope (name|providerId|versionId|url|ts|nonce). a := &Agent{panelHMAC: "test-secret", uuid: "node-uuid"} ts := strconv.FormatInt(time.Now().UnixMilli(), 10) nonce := "aaaabbbbccccdddd0000111122223333" base := "server-1|provider-x|v1.0|https://example.com/server.jar|" + ts + "|" + nonce r := httptest.NewRequest("POST", "/x", nil) r.Header.Set("x-panel-ts", ts) r.Header.Set("x-panel-nonce", nonce) r.Header.Set("x-panel-sign", hmacHex(a.panelHMAC, base)) if ok, why := a.verifyPanelSignature(r, "server-1", "provider-x", "v1.0", "https://example.com/server.jar", nil); !ok { t.Fatalf("apply-version envelope rejected: %s", why) } if ok, why := a.verifyPanelSignature(r, "server-1", "provider-x", "v1.0", "https://example.com/server.jar", nil); ok || why != "replayed" { t.Fatalf("apply-version replay accepted: ok=%v why=%q", ok, why) } } func TestPanelNonceTTLExceedsDriftWindow(t *testing.T) { if panelNonceTTL <= 2*panelSignDriftMillis { t.Fatalf("panelNonceTTL (%dms) must outlive both sides of the %dms drift window", panelNonceTTL, panelSignDriftMillis) } } func TestResolvePanelRequireNonceDefaultsToRequired(t *testing.T) { // Receiver default: nonces are REQUIRED. The current panel always signs // one, so an unconfigured node must not keep the replay window open. t.Setenv("PANEL_REQUIRE_NONCE", "") if !resolvePanelRequireNonce(Config{Raw: map[string]any{}}) { t.Fatalf("default policy must require nonces") } // A panel section without the key changes nothing. cfg := Config{Raw: map[string]any{"panel": map[string]any{"hmac_secret": "s"}}} if !resolvePanelRequireNonce(cfg) { t.Fatalf("panel section without require_nonce must default to required") } } func TestResolvePanelRequireNonceOptOuts(t *testing.T) { for _, v := range []string{"0", "false", "no", "off"} { t.Setenv("PANEL_REQUIRE_NONCE", v) if resolvePanelRequireNonce(Config{Raw: map[string]any{}}) { t.Fatalf("PANEL_REQUIRE_NONCE=%s must opt out of nonce enforcement", v) } } // Explicit config key opt-out (no env). t.Setenv("PANEL_REQUIRE_NONCE", "") cfg := Config{Raw: map[string]any{"panel": map[string]any{"require_nonce": false}}} if resolvePanelRequireNonce(cfg) { t.Fatalf("panel.require_nonce: false must opt out of nonce enforcement") } } func TestResolvePanelRequireNonceExplicitValuesWin(t *testing.T) { // Env truthy values keep enforcement on, even against a config opt-out. for _, v := range []string{"1", "true", "yes", "on"} { t.Setenv("PANEL_REQUIRE_NONCE", v) cfg := Config{Raw: map[string]any{"panel": map[string]any{"require_nonce": false}}} if !resolvePanelRequireNonce(cfg) { t.Fatalf("PANEL_REQUIRE_NONCE=%s must force nonce enforcement", v) } } // Env opt-out beats a config opt-in. t.Setenv("PANEL_REQUIRE_NONCE", "0") cfg := Config{Raw: map[string]any{"panel": map[string]any{"require_nonce": true}}} if resolvePanelRequireNonce(cfg) { t.Fatalf("explicit PANEL_REQUIRE_NONCE=0 must beat panel.require_nonce: true") } // An unrecognized env value is not an explicit decision: fall through to // the config key (or the default). t.Setenv("PANEL_REQUIRE_NONCE", "maybe") if !resolvePanelRequireNonce(Config{Raw: map[string]any{}}) { t.Fatalf("unrecognized PANEL_REQUIRE_NONCE must fall through to the default (required)") } cfgOff := Config{Raw: map[string]any{"panel": map[string]any{"require_nonce": false}}} if resolvePanelRequireNonce(cfgOff) { t.Fatalf("unrecognized PANEL_REQUIRE_NONCE must fall through to the config key") } } func TestResolvePanelRequireNonceQuotedConfigStrings(t *testing.T) { // The config dialect parses a quoted require_nonce value as a STRING, so // the resolution must not fail the bare bool assertion and silently // disable enforcement for require_nonce: "true". t.Setenv("PANEL_REQUIRE_NONCE", "") for _, v := range []string{"true", "TRUE", "1"} { cfg := Config{Raw: map[string]any{"panel": map[string]any{"require_nonce": v}}} if !resolvePanelRequireNonce(cfg) { t.Fatalf("panel.require_nonce: %q must keep nonce enforcement ON", v) } } for _, v := range []string{"false", "FALSE", "0", "off", "Off"} { cfg := Config{Raw: map[string]any{"panel": map[string]any{"require_nonce": v}}} if resolvePanelRequireNonce(cfg) { t.Fatalf("panel.require_nonce: %q must opt out of nonce enforcement", v) } } // An unrecognized string is not an explicit decision: the default (ON) // applies. cfg := Config{Raw: map[string]any{"panel": map[string]any{"require_nonce": "maybe"}}} if !resolvePanelRequireNonce(cfg) { t.Fatalf("unrecognized panel.require_nonce string must fall through to the default (required)") } }