devbox/internal/hub/join_pop_test.go
shoemoney 04f94d7730 harden(auth): require proof-of-possession on join 🪪🔐
The device id is derived from the pubkey in JoinRequest, but the hub never
checked the joiner actually held that key — anyone with a token could enroll
claiming another device's identity. Now the client signs JoinChallenge(token,
pubkey) with its ed25519 private key and the hub verifies it (ed25519.Verify)
BEFORE redeeming the token — so a bad request can't burn a legitimate one-time
token either. proto gains a Signature field + the shared JoinChallenge helper.
Regression test: garbage/missing/wrong-key sigs all 401, token survives, valid
join enrolls.
2026-06-23 03:08:23 -05:00

43 lines
1.5 KiB
Go

package hub
import (
"crypto/ed25519"
"crypto/rand"
"net/http"
"testing"
"time"
"git.shoemoney.ai/shoemoney/devbox/pkg/proto"
)
// A join must carry a valid proof-of-possession signature over JoinChallenge,
// and a bad attempt must NOT consume the one-time token (the signature is checked
// before the token is redeemed).
func TestJoinRequiresProofOfPossession(t *testing.T) {
base, db := testHub(t)
if err := db.CreateToken(HashToken("tok"), time.Now().Unix()+3600); err != nil {
t.Fatal(err)
}
pub, priv, _ := ed25519.GenerateKey(rand.Reader)
_, otherPriv, _ := ed25519.GenerateKey(rand.Reader)
bad := []proto.JoinRequest{
{Token: "tok", Name: "d", Pubkey: pub, Signature: []byte("garbage")},
{Token: "tok", Name: "d", Pubkey: pub}, // missing signature
{Token: "tok", Name: "d", Pubkey: pub, Signature: ed25519.Sign(otherPriv, proto.JoinChallenge("tok", pub))}, // signed by the wrong key
}
for i, req := range bad {
if status, _ := do(t, "POST", base+proto.PathJoin, "", mustJSON(t, req)); status != http.StatusUnauthorized {
t.Fatalf("bad join %d = %d, want 401", i, status)
}
}
// The token survived every bad attempt — a correct signature still enrolls.
status, _ := do(t, "POST", base+proto.PathJoin, "", mustJSON(t, proto.JoinRequest{
Token: "tok", Name: "d", Pubkey: pub,
Signature: ed25519.Sign(priv, proto.JoinChallenge("tok", pub)),
}))
if status != http.StatusOK {
t.Fatalf("valid join after bad attempts = %d, want 200 (one-time token wrongly burned?)", status)
}
}