From 04f94d7730728ea65357bd882feb7717da469a7d Mon Sep 17 00:00:00 2001 From: shoemoney Date: Tue, 23 Jun 2026 03:08:23 -0500 Subject: [PATCH] =?UTF-8?q?harden(auth):=20require=20proof-of-possession?= =?UTF-8?q?=20on=20join=20=F0=9F=AA=AA=F0=9F=94=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- cmd/devbox-hub/gc_crossshare_test.go | 4 +-- cmd/devbox-hub/gc_test.go | 4 +-- cmd/devbox/main.go | 2 +- internal/daemon/daemon_test.go | 4 +-- internal/hub/blob_traversal_test.go | 3 +- internal/hub/join_pop_test.go | 43 ++++++++++++++++++++++++++++ internal/hub/server.go | 9 ++++++ internal/hub/server_test.go | 13 +++++---- internal/syncer/syncer_test.go | 8 +++--- internal/transport/client.go | 6 ++-- internal/transport/client_test.go | 12 ++++++-- pkg/proto/proto.go | 17 +++++++++-- 12 files changed, 100 insertions(+), 25 deletions(-) create mode 100644 internal/hub/join_pop_test.go diff --git a/cmd/devbox-hub/gc_crossshare_test.go b/cmd/devbox-hub/gc_crossshare_test.go index 509cfd7..c1486a1 100644 --- a/cmd/devbox-hub/gc_crossshare_test.go +++ b/cmd/devbox-hub/gc_crossshare_test.go @@ -37,8 +37,8 @@ func TestGCKeepsChunksSharedAcrossShares(t *testing.T) { t.Fatal(err) } c := transport.New(srv.URL) - pub, _, _ := ed25519.GenerateKey(rand.Reader) - if _, err := c.Join("tok", "dev", pub); err != nil { + pub, priv, _ := ed25519.GenerateKey(rand.Reader) + if _, err := c.Join("tok", "dev", pub, priv); err != nil { t.Fatal(err) } if err := c.Publish("a"); err != nil { diff --git a/cmd/devbox-hub/gc_test.go b/cmd/devbox-hub/gc_test.go index e34abf5..0be8f64 100644 --- a/cmd/devbox-hub/gc_test.go +++ b/cmd/devbox-hub/gc_test.go @@ -49,11 +49,11 @@ func TestGCDropsOldUniqueChunks(t *testing.T) { t.Fatal(err) } c := transport.New(srv.URL) - pub, _, err := ed25519.GenerateKey(rand.Reader) + pub, priv, err := ed25519.GenerateKey(rand.Reader) if err != nil { t.Fatal(err) } - if _, err := c.Join("tok", "dev", pub); err != nil { + if _, err := c.Join("tok", "dev", pub, priv); err != nil { t.Fatal(err) } if err := c.Publish("proj"); err != nil { diff --git a/cmd/devbox/main.go b/cmd/devbox/main.go index 587caca..a3ba12f 100644 --- a/cmd/devbox/main.go +++ b/cmd/devbox/main.go @@ -90,7 +90,7 @@ func joinCmd() *cobra.Command { name, _ := os.Hostname() c := transport.New(hubURL) - resp, err := c.Join(token, name, id.Pub) + resp, err := c.Join(token, name, id.Pub, id.Priv) if err != nil { return err } diff --git a/internal/daemon/daemon_test.go b/internal/daemon/daemon_test.go index 5245264..f737e87 100644 --- a/internal/daemon/daemon_test.go +++ b/internal/daemon/daemon_test.go @@ -24,11 +24,11 @@ func join(t *testing.T, db *meta.DB, url, name string) string { t.Fatal(err) } c := transport.New(url) - pub, _, err := ed25519.GenerateKey(rand.Reader) + pub, priv, err := ed25519.GenerateKey(rand.Reader) if err != nil { t.Fatal(err) } - if _, err := c.Join(tok, name, pub); err != nil { + if _, err := c.Join(tok, name, pub, priv); err != nil { t.Fatalf("join %s: %v", name, err) } return c.Bearer() diff --git a/internal/hub/blob_traversal_test.go b/internal/hub/blob_traversal_test.go index 96dcd15..6fd4a84 100644 --- a/internal/hub/blob_traversal_test.go +++ b/internal/hub/blob_traversal_test.go @@ -18,12 +18,13 @@ func enroll(t *testing.T, base string, db *meta.DB) string { if err := db.CreateToken(HashToken("tok"), time.Now().Unix()+3600); err != nil { t.Fatal(err) } - pub, _, err := ed25519.GenerateKey(rand.Reader) + pub, priv, err := ed25519.GenerateKey(rand.Reader) if err != nil { t.Fatal(err) } status, body := do(t, "POST", base+proto.PathJoin, "", mustJSON(t, proto.JoinRequest{ Token: "tok", Name: "dev", Pubkey: pub, + Signature: ed25519.Sign(priv, proto.JoinChallenge("tok", pub)), })) if status != http.StatusOK { t.Fatalf("join status %d: %s", status, body) diff --git a/internal/hub/join_pop_test.go b/internal/hub/join_pop_test.go new file mode 100644 index 0000000..67b5ae8 --- /dev/null +++ b/internal/hub/join_pop_test.go @@ -0,0 +1,43 @@ +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) + } +} diff --git a/internal/hub/server.go b/internal/hub/server.go index 801ffdb..f4a50a4 100644 --- a/internal/hub/server.go +++ b/internal/hub/server.go @@ -104,6 +104,15 @@ func (s *Server) handleJoin(w http.ResponseWriter, r *http.Request) { return } now := time.Now().Unix() + + // Proof of possession: the device id is derived from req.Pubkey, so require a + // signature proving the joiner actually holds that key. Checked BEFORE + // redeeming the token so a bad request can't burn a legitimate one-time token. + if len(req.Pubkey) != ed25519.PublicKeySize || + !ed25519.Verify(ed25519.PublicKey(req.Pubkey), proto.JoinChallenge(req.Token, req.Pubkey), req.Signature) { + writeErr(w, http.StatusUnauthorized, "join signature invalid") + return + } deviceID := identity.FingerprintOf(ed25519.PublicKey(req.Pubkey)) ok, err := s.db.RedeemToken(HashToken(req.Token), now) diff --git a/internal/hub/server_test.go b/internal/hub/server_test.go index 087763e..338cb20 100644 --- a/internal/hub/server_test.go +++ b/internal/hub/server_test.go @@ -77,14 +77,15 @@ func TestHubHappyPath(t *testing.T) { if err := db.CreateToken(HashToken("jointok"), now+3600); err != nil { t.Fatalf("CreateToken: %v", err) } - pub, _, err := ed25519.GenerateKey(rand.Reader) + pub, priv, err := ed25519.GenerateKey(rand.Reader) if err != nil { t.Fatalf("GenerateKey: %v", err) } status, body := do(t, "POST", base+proto.PathJoin, "", mustJSON(t, proto.JoinRequest{ - Token: "jointok", - Name: "laptop", - Pubkey: pub, + Token: "jointok", + Name: "laptop", + Pubkey: pub, + Signature: ed25519.Sign(priv, proto.JoinChallenge("jointok", pub)), })) if status != http.StatusOK { t.Fatalf("join status = %d, body = %s", status, body) @@ -218,11 +219,11 @@ func TestHubAuthAndHashErrors(t *testing.T) { if err := db.CreateToken(HashToken("tok2"), now+3600); err != nil { t.Fatalf("CreateToken: %v", err) } - pub, _, err := ed25519.GenerateKey(rand.Reader) + pub, priv, err := ed25519.GenerateKey(rand.Reader) if err != nil { t.Fatalf("GenerateKey: %v", err) } - status, body := do(t, "POST", base+proto.PathJoin, "", mustJSON(t, proto.JoinRequest{Token: "tok2", Name: "n", Pubkey: pub})) + status, body := do(t, "POST", base+proto.PathJoin, "", mustJSON(t, proto.JoinRequest{Token: "tok2", Name: "n", Pubkey: pub, Signature: ed25519.Sign(priv, proto.JoinChallenge("tok2", pub))})) if status != http.StatusOK { t.Fatalf("join status = %d, body = %s", status, body) } diff --git a/internal/syncer/syncer_test.go b/internal/syncer/syncer_test.go index cfbaea4..0bcdaa2 100644 --- a/internal/syncer/syncer_test.go +++ b/internal/syncer/syncer_test.go @@ -25,11 +25,11 @@ func joinDevice(t *testing.T, db *meta.DB, baseURL, name string) *transport.Clie t.Fatal(err) } c := transport.New(baseURL) - pub, _, err := ed25519.GenerateKey(rand.Reader) + pub, priv, err := ed25519.GenerateKey(rand.Reader) if err != nil { t.Fatal(err) } - if _, err := c.Join(tok, name, pub); err != nil { + if _, err := c.Join(tok, name, pub, priv); err != nil { t.Fatalf("join %s: %v", name, err) } return c @@ -75,11 +75,11 @@ func TestPushEndToEnd(t *testing.T) { writeFile(t, root, "node_modules/x.js", "junk()\n") // ignored c := transport.New(srv.URL) - pub, _, err := ed25519.GenerateKey(rand.Reader) + pub, priv, err := ed25519.GenerateKey(rand.Reader) if err != nil { t.Fatal(err) } - if _, err := c.Join(tok, "testdev", pub); err != nil { + if _, err := c.Join(tok, "testdev", pub, priv); err != nil { t.Fatalf("join: %v", err) } if c.Bearer() == "" { diff --git a/internal/transport/client.go b/internal/transport/client.go index eef05dd..9db78b6 100644 --- a/internal/transport/client.go +++ b/internal/transport/client.go @@ -10,6 +10,7 @@ import ( "bufio" "bytes" "context" + "crypto/ed25519" "encoding/json" "fmt" "io" @@ -48,10 +49,11 @@ func (c *Client) Bearer() string { return c.bearer } // Join redeems a one-time token and enrolls this device. It is unauthenticated; // on success it stashes the returned bearer via SetBearer. -func (c *Client) Join(token, name string, pubkey []byte) (proto.JoinResponse, error) { +func (c *Client) Join(token, name string, pubkey ed25519.PublicKey, priv ed25519.PrivateKey) (proto.JoinResponse, error) { var resp proto.JoinResponse + sig := ed25519.Sign(priv, proto.JoinChallenge(token, pubkey)) // prove we hold the key err := c.do(http.MethodPost, proto.PathJoin, false, proto.JoinRequest{ - Token: token, Name: name, Pubkey: pubkey, + Token: token, Name: name, Pubkey: pubkey, Signature: sig, }, &resp) if err != nil { return proto.JoinResponse{}, err diff --git a/internal/transport/client_test.go b/internal/transport/client_test.go index 3f44a6f..7af2140 100644 --- a/internal/transport/client_test.go +++ b/internal/transport/client_test.go @@ -1,6 +1,9 @@ package transport import ( + "bytes" + "crypto/ed25519" + "crypto/rand" "encoding/json" "io" "net/http" @@ -22,6 +25,7 @@ func writeJSON(t *testing.T, w http.ResponseWriter, code int, v any) { } func TestJoinSetsBearer(t *testing.T) { + pub, priv, _ := ed25519.GenerateKey(rand.Reader) srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost || r.URL.Path != proto.PathJoin { t.Fatalf("Join: got %s %s", r.Method, r.URL.Path) @@ -33,15 +37,19 @@ func TestJoinSetsBearer(t *testing.T) { if err := json.NewDecoder(r.Body).Decode(&req); err != nil { t.Fatalf("decode JoinRequest: %v", err) } - if req.Token != "tok" || req.Name != "laptop" || string(req.Pubkey) != "PUB" { + if req.Token != "tok" || req.Name != "laptop" || !bytes.Equal(req.Pubkey, pub) { t.Fatalf("unexpected JoinRequest: %+v", req) } + // The client must prove possession: a valid signature over the challenge. + if !ed25519.Verify(pub, proto.JoinChallenge(req.Token, req.Pubkey), req.Signature) { + t.Fatal("Join did not send a valid proof-of-possession signature") + } writeJSON(t, w, http.StatusOK, proto.JoinResponse{DeviceID: "dev1", Bearer: "secret"}) })) defer srv.Close() c := New(srv.URL) - resp, err := c.Join("tok", "laptop", []byte("PUB")) + resp, err := c.Join("tok", "laptop", pub, priv) if err != nil { t.Fatalf("Join: %v", err) } diff --git a/pkg/proto/proto.go b/pkg/proto/proto.go index 9f9bb08..bb3796f 100644 --- a/pkg/proto/proto.go +++ b/pkg/proto/proto.go @@ -40,9 +40,20 @@ const AuthHeader = "Authorization" // JoinRequest enrolls a device by redeeming a one-time join token. type JoinRequest struct { - Token string `json:"token"` - Name string `json:"name"` - Pubkey []byte `json:"pubkey"` // ed25519 public key + Token string `json:"token"` + Name string `json:"name"` + Pubkey []byte `json:"pubkey"` // ed25519 public key + Signature []byte `json:"signature"` // ed25519 sig over JoinChallenge(token, pubkey) +} + +// JoinChallenge is the message a joining device signs with its private key to +// prove it actually holds the key behind Pubkey (the device id is derived from +// Pubkey, so without this anyone could claim another device's identity). Binding +// the one-time token means the signature can't be replayed for a different join. +func JoinChallenge(token string, pubkey []byte) []byte { + msg := make([]byte, 0, len(token)+len(pubkey)) + msg = append(msg, token...) + return append(msg, pubkey...) } // JoinResponse returns the device id and the bearer token for future requests.