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.
This commit is contained in:
parent
f9c33d49d1
commit
04f94d7730
12 changed files with 100 additions and 25 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
43
internal/hub/join_pop_test.go
Normal file
43
internal/hub/join_pop_test.go
Normal file
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -77,7 +77,7 @@ 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)
|
||||
}
|
||||
|
|
@ -85,6 +85,7 @@ func TestHubHappyPath(t *testing.T) {
|
|||
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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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() == "" {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,6 +43,17 @@ type JoinRequest struct {
|
|||
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.
|
||||
|
|
|
|||
Loading…
Reference in a new issue