devbox/internal/hub/join_pop_test.go
shoemoney 03469958d3
Some checks are pending
CI / vet-build-test (push) Waiting to run
chore: prep for public release — MIT license + github.com module path 🌍⚖️
- LICENSE: MIT (enables the adoption push; public repo was all-rights-reserved).
- go.mod + all imports: git.shoemoney.ai/shoemoney/devbox -> github.com/shoemoney/devbox,
  so outsiders can 'go install github.com/shoemoney/devbox/cmd/devbox@latest'. Forgejo
  stays the self-hosted home; the registry (devbox-hub) + public dist (devbox-dist) are
  unchanged. vet + build + go test -race all green after the rename.
- README: document the go install path.

Security: full git-history scan (manual blob sweep + gitleaks, 84 commits) found
ZERO secrets — Forgejo token never committed, no keys/passwords/private keys.
2026-06-23 19:19:09 -05:00

43 lines
1.5 KiB
Go

package hub
import (
"crypto/ed25519"
"crypto/rand"
"net/http"
"testing"
"time"
"github.com/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)
}
}