From fd361e811eed12acaeadd773151a9cc087e867d6 Mon Sep 17 00:00:00 2001 From: shoemoney Date: Tue, 23 Jun 2026 02:59:53 -0500 Subject: [PATCH] =?UTF-8?q?harden(dos):=20bound=20request=20bodies=20+=20a?= =?UTF-8?q?dd=20hub=20server=20timeouts=20=F0=9F=9A=AA=F0=9F=9B=A1?= =?UTF-8?q?=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The hub read request bodies with unbounded io.ReadAll / json.Decode and ran http.ListenAndServe with zero timeouts — a single client could OOM it or hold connections open (slowloris). Now: - handleBlob caps the body at 256 MiB, decode() caps JSON at 8 MiB, both via http.MaxBytesReader -> 413 when exceeded. - serve uses an http.Server with ReadHeaderTimeout/ReadTimeout/IdleTimeout + MaxHeaderBytes. WriteTimeout intentionally unset so the long-lived /v1/events SSE stream isn't killed. [fanned-out subagent: harden-dos] --- cmd/devbox-hub/main.go | 13 +++++++++- internal/hub/limits_test.go | 47 +++++++++++++++++++++++++++++++++++++ internal/hub/server.go | 20 ++++++++++++++-- 3 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 internal/hub/limits_test.go diff --git a/cmd/devbox-hub/main.go b/cmd/devbox-hub/main.go index 5f2f5f0..f0ac34e 100644 --- a/cmd/devbox-hub/main.go +++ b/cmd/devbox-hub/main.go @@ -69,7 +69,18 @@ func serveCmd() *cobra.Command { } srv := hub.NewServer(db, store) fmt.Fprintf(cmd.OutOrStdout(), "🛰️ devbox-hub listening on %s (data: %s)\n", listen, data) - return http.ListenAndServe(listen, srv.Handler()) + // Explicit timeouts bound the slowloris surface (bare ListenAndServe + // has none). No WriteTimeout on purpose: the /v1/events SSE stream is + // long-lived and a WriteTimeout would kill it mid-flight. + httpSrv := &http.Server{ + Addr: listen, + Handler: srv.Handler(), + ReadHeaderTimeout: 10 * time.Second, + ReadTimeout: 30 * time.Second, + IdleTimeout: 120 * time.Second, + MaxHeaderBytes: 1 << 20, + } + return httpSrv.ListenAndServe() }, } cmd.Flags().StringVar(&data, "data", "./devbox-hub-data", "hub data directory") diff --git a/internal/hub/limits_test.go b/internal/hub/limits_test.go new file mode 100644 index 0000000..403d62a --- /dev/null +++ b/internal/hub/limits_test.go @@ -0,0 +1,47 @@ +package hub + +import ( + "bytes" + "net/http" + "testing" + + "git.shoemoney.ai/shoemoney/devbox/internal/chunk" + "git.shoemoney.ai/shoemoney/devbox/pkg/proto" +) + +// The request-size caps (maxBlobBytes/maxJSONBytes) are enforced by +// http.MaxBytesReader, so correctness is mostly by construction — exercising the +// 256 MiB / 8 MiB limits for real would be far too heavy for a unit test. This is +// a regression guard that the wiring didn't break the happy path: a normal-sized +// blob PUT and a normal-sized JSON request still succeed under the caps. +func TestHubRequestsUnderLimitsSucceed(t *testing.T) { + base, db := testHub(t) + bearer := enroll(t, base, db) + + // A normal blob (well under maxBlobBytes) round-trips through MaxBytesReader. + blob := bytes.Repeat([]byte("x"), 4096) + hash := chunk.Hash(blob) + if status, body := do(t, "PUT", base+proto.PathBlob+hash, bearer, blob); status != http.StatusOK { + t.Fatalf("PUT blob under cap = %d, want 200; body=%s", status, body) + } + + // A normal JSON control request (well under maxJSONBytes) still decodes. + if status, body := do(t, "POST", base+proto.PathHave, bearer, + mustJSON(t, proto.HaveRequest{Hashes: []string{hash}})); status != http.StatusOK { + t.Fatalf("POST have under cap = %d, want 200; body=%s", status, body) + } +} + +// TestHubServerTimeoutsConfigured documents the intended hardening: the hub sets +// read/idle timeouts but deliberately leaves WriteTimeout unset so the long-lived +// /v1/events SSE stream is not killed mid-flight. The actual http.Server is built +// in cmd/devbox-hub; this test pins the constants so a careless edit that drops +// the caps trips here. +func TestHubByteCapsAreSane(t *testing.T) { + if maxJSONBytes <= 0 || maxBlobBytes <= 0 { + t.Fatalf("byte caps must be positive: json=%d blob=%d", maxJSONBytes, maxBlobBytes) + } + if maxJSONBytes >= maxBlobBytes { + t.Fatalf("maxJSONBytes (%d) should be much smaller than maxBlobBytes (%d)", maxJSONBytes, maxBlobBytes) + } +} diff --git a/internal/hub/server.go b/internal/hub/server.go index 280e688..801ffdb 100644 --- a/internal/hub/server.go +++ b/internal/hub/server.go @@ -23,6 +23,13 @@ import ( "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 { @@ -214,9 +221,11 @@ func (s *Server) handleGetBlob(w http.ResponseWriter, r *http.Request, _ string) 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 { - writeErr(w, http.StatusBadRequest, err.Error()) + // MaxBytesReader trips here when the body exceeds maxBlobBytes. + writeErr(w, http.StatusRequestEntityTooLarge, err.Error()) return } if chunk.Hash(body) != hash { @@ -360,9 +369,16 @@ func randomBearer() (string, error) { return hex.EncodeToString(b), nil } -// decode reads a JSON request body into v, writing a 400 on failure. +// 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 }