128 lines
5.6 KiB
Go
128 lines
5.6 KiB
Go
package main
|
|
|
|
// Compatibility pins for the panel's apply-version signature envelope.
|
|
//
|
|
// POST /v1/servers/<name>/apply-version is dispatched before bearer-token
|
|
// auth, so the signed panel envelope is the request's only authentication.
|
|
// The panel signs (utils/node-hmac.js applyVersionSignatureBase, exercised in
|
|
// utils/heartbeat-auth.test.js) the base:
|
|
//
|
|
// <name>|<providerId>|<versionId>|<url>|<ts>|<nonce>
|
|
//
|
|
// with empty segments for a body that omits providerId/versionId (the pruned
|
|
// backward-compat body) and no nonce segment for legacy binaries. These tests
|
|
// verify the daemon accepts exactly that composition through the real
|
|
// verifyPanelSignature path and rejects look-alike bases — if either side
|
|
// drifts, the JS-side fixed-literal suite and these tests fail together.
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
const applyVersionFixtureName = "srv-1"
|
|
const applyVersionFixtureProvider = "papermc"
|
|
const applyVersionFixtureVersion = "1.21.4"
|
|
const applyVersionFixtureURL = "https://example.com/server.jar"
|
|
|
|
func applyVersionSignedRequest(t *testing.T, a *Agent, base string, nonce string) *http.Request {
|
|
t.Helper()
|
|
r := httptest.NewRequest("POST", "/v1/servers/"+applyVersionFixtureName+"/apply-version", nil)
|
|
r.Header.Set("x-panel-ts", strconv.FormatInt(time.Now().UnixMilli(), 10))
|
|
if nonce != "" {
|
|
r.Header.Set("x-panel-nonce", nonce)
|
|
}
|
|
r.Header.Set("x-panel-sign", hmacHex(a.panelHMAC, base+r.Header.Get("x-panel-ts")+nonceSuffix(nonce)))
|
|
return r
|
|
}
|
|
|
|
func nonceSuffix(nonce string) string {
|
|
if nonce == "" {
|
|
return ""
|
|
}
|
|
return "|" + nonce
|
|
}
|
|
|
|
func TestApplyVersionPanelSignatureAcceptsProviderBoundBase(t *testing.T) {
|
|
a := &Agent{panelHMAC: "test-secret"}
|
|
nonce := "0123456789abcdef0123456789abcdef"
|
|
r := applyVersionSignedRequest(t, a,
|
|
applyVersionFixtureName+"|"+applyVersionFixtureProvider+"|"+applyVersionFixtureVersion+"|"+applyVersionFixtureURL+"|",
|
|
nonce)
|
|
|
|
if ok, why := a.verifyPanelSignature(r, applyVersionFixtureName, applyVersionFixtureProvider, applyVersionFixtureVersion, applyVersionFixtureURL, nil); !ok {
|
|
t.Fatalf("provider-bound apply-version signature rejected: %s", why)
|
|
}
|
|
|
|
// The bound fields are the verifier's inputs: signing over one
|
|
// provider/version and verifying against another must fail.
|
|
if ok, why := a.verifyPanelSignature(r, applyVersionFixtureName, "purpurmc", applyVersionFixtureVersion, applyVersionFixtureURL, nil); ok {
|
|
t.Fatalf("signature accepted under different providerId binding")
|
|
} else if why != "bad-signature" {
|
|
t.Fatalf("expected bad-signature, got %q", why)
|
|
}
|
|
}
|
|
|
|
func TestApplyVersionPanelSignatureAcceptsEmptyProviderVersion(t *testing.T) {
|
|
// The pruned backward-compat body omits providerId/versionId; the daemon
|
|
// binds them as empty segments and the panel signs exactly that.
|
|
a := &Agent{panelHMAC: "test-secret"}
|
|
nonce := "fedcba9876543210fedcba9876543210"
|
|
r := applyVersionSignedRequest(t, a,
|
|
applyVersionFixtureName+"|||"+applyVersionFixtureURL+"|",
|
|
nonce)
|
|
|
|
if ok, why := a.verifyPanelSignature(r, applyVersionFixtureName, "", "", applyVersionFixtureURL, nil); !ok {
|
|
t.Fatalf("empty provider/version apply-version signature rejected: %s", why)
|
|
}
|
|
}
|
|
|
|
func TestApplyVersionPanelSignatureLegacyNonceLess(t *testing.T) {
|
|
base := applyVersionFixtureName + "|" + applyVersionFixtureProvider + "|" + applyVersionFixtureVersion + "|" + applyVersionFixtureURL + "|"
|
|
|
|
// panelRequireNonce defaults to false in the test Agent: the legacy
|
|
// nonce-less envelope verifies, matching the panel's final ladder rung.
|
|
a := &Agent{panelHMAC: "test-secret"}
|
|
r := applyVersionSignedRequest(t, a, base, "")
|
|
if ok, why := a.verifyPanelSignature(r, applyVersionFixtureName, applyVersionFixtureProvider, applyVersionFixtureVersion, applyVersionFixtureURL, nil); !ok {
|
|
t.Fatalf("legacy nonce-less apply-version signature rejected: %s", why)
|
|
}
|
|
|
|
// With the require-nonce policy on (the production default), the same
|
|
// envelope must fail closed.
|
|
hard := &Agent{panelHMAC: "test-secret", panelRequireNonce: true}
|
|
r2 := applyVersionSignedRequest(t, hard, base, "")
|
|
if ok, why := hard.verifyPanelSignature(r2, applyVersionFixtureName, applyVersionFixtureProvider, applyVersionFixtureVersion, applyVersionFixtureURL, nil); ok || why != "nonce-required" {
|
|
t.Fatalf("nonce-less envelope accepted under require-nonce: ok=%v why=%q", ok, why)
|
|
}
|
|
}
|
|
|
|
func TestApplyVersionPanelSignatureRejectsLookalikeBases(t *testing.T) {
|
|
a := &Agent{panelHMAC: "test-secret"}
|
|
nonce := "00112233445566778899aabbccddeeff"
|
|
|
|
// Wrong composition: reinstall-style base (no provider/version, admin
|
|
// role segment) must not verify against the apply-version verifier.
|
|
r := applyVersionSignedRequest(t, a,
|
|
applyVersionFixtureName+"|reinstall|"+applyVersionFixtureVersion+"|admin|",
|
|
nonce)
|
|
if ok, why := a.verifyPanelSignature(r, applyVersionFixtureName, applyVersionFixtureProvider, applyVersionFixtureVersion, applyVersionFixtureURL, nil); ok {
|
|
t.Fatalf("reinstall-shaped base accepted by the apply-version verifier")
|
|
} else if why != "bad-signature" {
|
|
t.Fatalf("expected bad-signature, got %q", why)
|
|
}
|
|
|
|
// Missing the provider segment entirely (url shifted into its place).
|
|
r2 := applyVersionSignedRequest(t, a,
|
|
applyVersionFixtureName+"|"+applyVersionFixtureVersion+"|"+applyVersionFixtureURL+"|",
|
|
nonce)
|
|
if ok, why := a.verifyPanelSignature(r2, applyVersionFixtureName, applyVersionFixtureProvider, applyVersionFixtureVersion, applyVersionFixtureURL, nil); ok {
|
|
t.Fatalf("three-segment base accepted by the apply-version verifier")
|
|
} else if why != "bad-signature" {
|
|
t.Fatalf("expected bad-signature, got %q", why)
|
|
}
|
|
}
|