devbox/internal/hub/blob_traversal_test.go
shoemoney 96fd6018a9 fix(sec): block blob-hash path traversal (arbitrary file read) 🔓🛡️
GET /v1/blob/{hash} and /v1/have passed the URL hash straight into filepath.Join
in the blob store. Go's ServeMux URL-decodes %2f/%2e in the wildcard, so
'..%2f..%2f..%2fetc%2fpasswd' (or a hash starting with '..') escaped the blob
root — any enrolled device could read arbitrary files incl. the hub token DB.

Defense in depth:
- hub: validHash() rejects non-(64 hex) keys at the HTTP boundary (404 on GET,
  400 on /have) before any store call.
- blobstore: safeKey() rejects keys that aren't a bare, separator-free filename
  in Has/Get/Put/Delete — the sink can't be escaped even if a caller forgets.
e2e + unit regression tests for both layers.
2026-06-23 02:21:03 -05:00

62 lines
1.7 KiB
Go

package hub
import (
"crypto/ed25519"
"crypto/rand"
"encoding/json"
"net/http"
"testing"
"time"
"git.shoemoney.ai/shoemoney/devbox/internal/hub/meta"
"git.shoemoney.ai/shoemoney/devbox/pkg/proto"
)
// enroll joins a device and returns its bearer token.
func enroll(t *testing.T, base string, db *meta.DB) string {
t.Helper()
if err := db.CreateToken(HashToken("tok"), time.Now().Unix()+3600); err != nil {
t.Fatal(err)
}
pub, _, 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,
}))
if status != http.StatusOK {
t.Fatalf("join status %d: %s", status, body)
}
var jr proto.JoinResponse
if err := json.Unmarshal(body, &jr); err != nil {
t.Fatal(err)
}
return jr.Bearer
}
// An authenticated device must NOT be able to read files outside the blob root
// via a path-traversal blob hash (ServeMux URL-decodes %2f in the {hash} wildcard).
func TestBlobGetRejectsTraversal(t *testing.T) {
base, db := testHub(t)
bearer := enroll(t, base, db)
for _, key := range []string{
"..%2f..%2f..%2f..%2fetc%2fpasswd",
"..%2f..%2fdevbox-hub.db",
"not-a-hash",
"deadbeef", // valid hex but wrong length
} {
status, body := do(t, "GET", base+proto.PathBlob+key, bearer, nil)
if status == http.StatusOK {
t.Errorf("GET blob %q returned 200 (leak): %s", key, body)
}
}
// /v1/have must reject a traversal hash rather than stat an arbitrary path.
status, _ := do(t, "POST", base+proto.PathHave, bearer,
mustJSON(t, proto.HaveRequest{Hashes: []string{"../../../../etc/hosts"}}))
if status != http.StatusBadRequest {
t.Errorf("have with traversal hash = %d, want 400", status)
}
}