devbox/internal/hub/server.go
shoemoney 81fbc5c81a feat(M8a): principals + per-share roles + write enforcement 👥🔐
V2-SPEC §5.2 (write-side, staged before read-side M9). Migration #2 (rides
the M8 runner, chains 0→1→2 — verified on real .10 data, version 2, all
counts preserved):

- principals table + devices.principal_id (every v1 device backfills to one
  synthetic 'owner' principal — byte-identical to v1 until a share opts in).
- members(share, principal_id, role, can_reshare) + shares.acl_mode. A share
  with zero grants is 'legacy' (every device an implicit owner = v1); the
  first grant flips it to 'explicit' = deny-by-default.
- EffectiveRole(device, share) resolves device→principal→role; legacy⇒owner.
- Push write gate: role>=editor AND the access.writable clamp (which can only
  REMOVE write). Legacy shares reduce to the v1 writable bit exactly.
- devbox-hub member set/rm/list + principal <device> <principal> admin CLI —
  the populate path, so the role layer has a real consumer now (no dead code).

Tests: legacy↔explicit lifecycle, deny-by-default, viewer<editor, HTTP push
gate (403 unmembered/viewer, opens at editor). 18 pkgs -race green; hub
cross-compiles linux/amd64; windows builds.
2026-06-23 11:09:32 -05:00

416 lines
14 KiB
Go

// Package hub is the devbox hub HTTP server: device enrollment, content-addressed
// blob upload, snapshot push, and head queries over JSON/HTTP with bearer auth.
package hub
import (
"crypto/ed25519"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strconv"
"sync"
"time"
"git.shoemoney.ai/shoemoney/devbox/internal/chunk"
"git.shoemoney.ai/shoemoney/devbox/internal/hub/blobstore"
"git.shoemoney.ai/shoemoney/devbox/internal/hub/meta"
"git.shoemoney.ai/shoemoney/devbox/internal/identity"
"git.shoemoney.ai/shoemoney/devbox/pkg/proto"
)
// Request body caps, enforced via http.MaxBytesReader to bound the DoS surface
// (an unbounded io.ReadAll / JSON decode would let a client OOM the hub).
const (
maxBlobBytes = 256 << 20 // one chunk/manifest blob — generous
maxJSONBytes = 8 << 20 // any JSON control request
)
// Server is the devbox hub: metadata in db, blob bytes in store, change events
// fanned out via broker.
type Server struct {
db *meta.DB
store blobstore.Store
broker *broker
publishMu sync.Mutex // serializes publish so the case-clash check + create is atomic
}
// NewServer builds a hub server over the given metadata store and blob store.
func NewServer(db *meta.DB, store blobstore.Store) *Server {
return &Server{db: db, store: store, broker: newBroker()}
}
// HashToken returns hex(sha256(token)); the token-mint CLI reuses it so the hub
// only ever stores token hashes, never the tokens themselves.
func HashToken(token string) string {
sum := sha256.Sum256([]byte(token))
return hex.EncodeToString(sum[:])
}
// hashBearer hashes a bearer the same way, so the hub stores only the hash (no
// plaintext credentials at rest) and lookup compares fixed-width hashes.
func hashBearer(bearer string) string {
sum := sha256.Sum256([]byte(bearer))
return hex.EncodeToString(sum[:])
}
// Handler returns the hub's HTTP routes.
func (s *Server) Handler() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("POST "+proto.PathJoin, s.handleJoin)
mux.HandleFunc("POST "+proto.PathPublish, s.auth(s.handlePublish))
mux.HandleFunc("POST "+proto.PathHave, s.auth(s.handleHave))
mux.HandleFunc("PUT "+proto.PathBlob+"{hash}", s.auth(s.handleBlob))
mux.HandleFunc("GET "+proto.PathBlob+"{hash}", s.auth(s.handleGetBlob))
mux.HandleFunc("POST "+proto.PathPush, s.auth(s.handlePush))
mux.HandleFunc("GET "+proto.PathHead, s.auth(s.handleHead))
mux.HandleFunc("GET "+proto.PathLog, s.auth(s.handleLog))
mux.HandleFunc("GET "+proto.PathEvents, s.auth(s.handleEvents))
mux.HandleFunc("GET "+proto.PathMetrics, s.handleMetrics)
return mux
}
// auth wraps a handler, requiring a valid "Authorization: Bearer <tok>" that
// resolves to a non-revoked device. The device id is stashed in the request
// context for the wrapped handler.
func (s *Server) auth(next func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
const prefix = "Bearer "
h := r.Header.Get(proto.AuthHeader)
if len(h) <= len(prefix) || h[:len(prefix)] != prefix {
writeErr(w, http.StatusUnauthorized, "missing bearer token")
return
}
deviceID, ok, err := s.db.DeviceByBearer(hashBearer(h[len(prefix):]))
if err != nil {
writeErr(w, http.StatusInternalServerError, err.Error())
return
}
if !ok {
writeErr(w, http.StatusUnauthorized, "invalid bearer token")
return
}
next(w, r, deviceID)
}
}
func (s *Server) handleJoin(w http.ResponseWriter, r *http.Request) {
var req proto.JoinRequest
if !decode(w, r, &req) {
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)
if err != nil {
writeErr(w, http.StatusInternalServerError, err.Error())
return
}
if !ok {
writeErr(w, http.StatusUnauthorized, "invalid or expired join token")
return
}
if err := s.db.AddDevice(deviceID, req.Name, req.Pubkey, now); err != nil {
writeErr(w, http.StatusInternalServerError, err.Error())
return
}
bearer, err := randomBearer()
if err != nil {
writeErr(w, http.StatusInternalServerError, err.Error())
return
}
if err := s.db.IssueBearer(deviceID, hashBearer(bearer)); err != nil {
writeErr(w, http.StatusInternalServerError, err.Error())
return
}
writeJSON(w, http.StatusOK, proto.JoinResponse{DeviceID: deviceID, Bearer: bearer})
}
func (s *Server) handlePublish(w http.ResponseWriter, r *http.Request, deviceID string) {
var req proto.PublishRequest
if !decode(w, r, &req) {
return
}
// Serialize: the case-clash check below reads all names then creates one, so
// two concurrent publishes of "Foo"/"foo" must not both pass the check.
s.publishMu.Lock()
defer s.publishMu.Unlock()
existing, err := s.db.ShareNames()
if err != nil {
writeErr(w, http.StatusInternalServerError, err.Error())
return
}
if err := ValidateShareName(req.Share, existing); err != nil {
writeErr(w, http.StatusBadRequest, err.Error())
return
}
if err := s.db.CreateShare(req.Share, deviceID, time.Now().Unix()); err != nil {
writeErr(w, http.StatusInternalServerError, err.Error())
return
}
w.WriteHeader(http.StatusOK)
}
// validHash reports whether h is a well-formed content hash (BLAKE3-256 hex).
// Blob keys flow into filesystem paths, so anything else (../, %2f-decoded
// slashes, absolute paths) is rejected at the trust boundary before it can
// escape the blob root. Legit keys are always 64 lowercase hex chars.
func validHash(h string) bool {
if len(h) != 64 {
return false
}
for i := 0; i < len(h); i++ {
c := h[i]
if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')) {
return false
}
}
return true
}
func (s *Server) handleHave(w http.ResponseWriter, r *http.Request, _ string) {
var req proto.HaveRequest
if !decode(w, r, &req) {
return
}
var missing []string
for _, h := range req.Hashes {
if !validHash(h) {
writeErr(w, http.StatusBadRequest, "invalid hash")
return
}
has, err := s.store.Has(h)
if err != nil {
writeErr(w, http.StatusInternalServerError, err.Error())
return
}
if !has {
missing = append(missing, h)
}
}
writeJSON(w, http.StatusOK, proto.HaveResponse{Missing: missing})
}
// handleGetBlob serves a blob's bytes for download (pull fetches manifests + chunks).
func (s *Server) handleGetBlob(w http.ResponseWriter, r *http.Request, _ string) {
hash := r.PathValue("hash")
if !validHash(hash) {
// ServeMux URL-decodes %2f/%2e in the {hash} wildcard, so an unvalidated
// key like "..%2f..%2fetc%2fpasswd" would escape the blob root. Reject as
// "not found" — an invalid hash is, by definition, not a stored blob.
writeErr(w, http.StatusNotFound, "no such blob")
return
}
b, err := s.store.Get(hash)
if errors.Is(err, blobstore.ErrNotFound) {
writeErr(w, http.StatusNotFound, "no such blob")
return
}
if err != nil {
writeErr(w, http.StatusInternalServerError, err.Error())
return
}
w.Header().Set("Content-Type", "application/octet-stream")
w.Write(b)
}
func (s *Server) handleBlob(w http.ResponseWriter, r *http.Request, _ string) {
hash := r.PathValue("hash")
r.Body = http.MaxBytesReader(w, r.Body, maxBlobBytes)
body, err := io.ReadAll(r.Body)
if err != nil {
// MaxBytesReader trips here when the body exceeds maxBlobBytes.
writeErr(w, http.StatusRequestEntityTooLarge, err.Error())
return
}
if chunk.Hash(body) != hash {
writeErr(w, http.StatusBadRequest, "body does not match hash")
return
}
if err := s.store.Put(hash, body); err != nil {
writeErr(w, http.StatusInternalServerError, err.Error())
return
}
w.WriteHeader(http.StatusOK)
}
func (s *Server) handlePush(w http.ResponseWriter, r *http.Request, deviceID string) {
var req proto.PushRequest
if !decode(w, r, &req) {
return
}
// Write gate (M8a): the device's effective role must be >= editor AND its
// access.writable clamp must allow it. In legacy shares (no member grants)
// every device is an implicit owner, so this reduces to the v1 writable bit;
// an explicit-ACL share is deny-by-default.
writable, err := s.db.Writable(deviceID, req.Share)
if err != nil {
writeErr(w, http.StatusInternalServerError, err.Error())
return
}
role, _, err := s.db.EffectiveRole(deviceID, req.Share)
if err != nil {
writeErr(w, http.StatusInternalServerError, err.Error())
return
}
if !writable || role < meta.RoleEditor {
writeErr(w, http.StatusForbidden, "device not permitted to write to this share")
return
}
// Conflict detection: a push must extend the current head (or replay it).
// Re-pushing the existing head is idempotent; a push whose parent IS the
// current head fast-forwards; anything else means the device is behind and
// must pull + reconcile (M3) before pushing again — we never clobber head.
head, _, err := s.db.ShareHead(req.Share)
if err != nil {
writeErr(w, http.StatusInternalServerError, err.Error())
return
}
if req.ManifestHash != head && req.Parent != head {
writeJSON(w, http.StatusOK, proto.PushResponse{Head: head, Conflict: true})
return
}
// The manifest blob and every referenced chunk must already be uploaded.
if missing, err := s.missingBlob(req.ManifestHash); err != nil {
writeErr(w, http.StatusInternalServerError, err.Error())
return
} else if missing {
writeErr(w, http.StatusConflict, "missing manifest blob "+req.ManifestHash)
return
}
chunks := make([]meta.ChunkRef, 0, len(req.Chunks))
for _, c := range req.Chunks {
if missing, err := s.missingBlob(c.Hash); err != nil {
writeErr(w, http.StatusInternalServerError, err.Error())
return
} else if missing {
writeErr(w, http.StatusConflict, "missing chunk blob "+c.Hash)
return
}
chunks = append(chunks, meta.ChunkRef{Hash: c.Hash, Size: c.Size})
}
snapshotID := req.ManifestHash
snap := meta.Snapshot{
ID: snapshotID,
Share: req.Share,
ParentID: req.Parent,
DeviceID: deviceID,
ManifestHash: req.ManifestHash,
CreatedAt: time.Now().Unix(),
}
if err := s.db.AddSnapshot(snap, chunks); err != nil {
writeErr(w, http.StatusInternalServerError, err.Error())
return
}
// Only announce a genuine head advance. An idempotent re-push (manifest ==
// current head) must NOT broadcast, or daemons would storm: pull -> re-push
// the same state -> broadcast -> pull -> ... forever.
if req.ManifestHash != head {
s.broker.publish(proto.Event{Share: req.Share, Snapshot: snapshotID})
}
writeJSON(w, http.StatusOK, proto.PushResponse{Snapshot: snapshotID, Head: snapshotID, Conflict: false})
}
// missingBlob reports whether the store lacks a blob with this hash.
func (s *Server) missingBlob(hash string) (bool, error) {
has, err := s.store.Has(hash)
return !has, err
}
func (s *Server) handleHead(w http.ResponseWriter, r *http.Request, _ string) {
head, _, err := s.db.ShareHead(r.URL.Query().Get("share"))
if err != nil {
writeErr(w, http.StatusInternalServerError, err.Error())
return
}
writeJSON(w, http.StatusOK, proto.HeadResponse{Head: head})
}
func (s *Server) handleLog(w http.ResponseWriter, r *http.Request, _ string) {
snaps, err := s.db.SnapshotLog(r.URL.Query().Get("share"), 100)
if err != nil {
writeErr(w, http.StatusInternalServerError, err.Error())
return
}
out := make([]proto.SnapshotInfo, len(snaps))
for i, s := range snaps {
out[i] = proto.SnapshotInfo{ID: s.ID, Parent: s.Parent, Device: s.Device, CreatedAt: s.CreatedAt}
}
writeJSON(w, http.StatusOK, proto.LogResponse{Snapshots: out})
}
func (s *Server) handleMetrics(w http.ResponseWriter, _ *http.Request) {
gauges := []struct {
name, table, help string
}{
{"devbox_devices", "devices", "Number of enrolled devices."},
{"devbox_shares", "shares", "Number of shares."},
{"devbox_snapshots", "snapshots", "Number of snapshots."},
{"devbox_chunks", "chunks", "Number of distinct chunks."},
}
w.Header().Set("Content-Type", "text/plain; version=0.0.4; charset=utf-8")
for _, g := range gauges {
n, err := s.db.Count(g.table)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "# HELP %s %s\n", g.name, g.help)
fmt.Fprintf(w, "# TYPE %s gauge\n", g.name)
fmt.Fprintf(w, "%s %s\n", g.name, strconv.FormatInt(n, 10))
}
}
// randomBearer returns 32 random bytes hex-encoded.
func randomBearer() (string, error) {
b := make([]byte, 32)
if _, err := rand.Read(b); err != nil {
return "", err
}
return hex.EncodeToString(b), nil
}
// decode reads a JSON request body into v, writing a 400 on failure (or a 413 if
// the body exceeds maxJSONBytes — bounding the decode DoS surface).
func decode(w http.ResponseWriter, r *http.Request, v any) bool {
r.Body = http.MaxBytesReader(w, r.Body, maxJSONBytes)
if err := json.NewDecoder(r.Body).Decode(v); err != nil {
var maxErr *http.MaxBytesError
if errors.As(err, &maxErr) {
writeErr(w, http.StatusRequestEntityTooLarge, err.Error())
return false
}
writeErr(w, http.StatusBadRequest, err.Error())
return false
}
return true
}
// writeJSON writes v as JSON with the given status.
func writeJSON(w http.ResponseWriter, status int, v any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
json.NewEncoder(w).Encode(v)
}
// writeErr writes a proto.Error JSON body with the given status.
func writeErr(w http.ResponseWriter, status int, msg string) {
writeJSON(w, status, proto.Error{Error: msg})
}