Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion packages/sandbox/daemon-go/internal/dispatch/dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dispatch
import (
"context"
"encoding/json"
"errors"
"io"
"log/slog"
"net/http"
Expand All @@ -18,6 +19,12 @@ import (

const tombstoneTTL = 60 * time.Second

// maxDispatchBodyBytes bounds the inline `/dispatch` request body. Matches
// maxOffloadBytes: a run whose messages are actually this large is expected to
// come in via messagesRef, not inline, so this cap just stops an unbounded
// read from parking the pod's memory on one request.
const maxDispatchBodyBytes = maxOffloadBytes

// Terminal code for a run this pod could not finish (shutdown / dropped
// connection) as opposed to one that was cancelled on purpose. Studio maps it
// to `SandboxUnreachableError` and continues the turn elsewhere — the literal
Expand Down Expand Up @@ -282,8 +289,13 @@ func (reg *Registry) HandleDispatch(w http.ResponseWriter, r *http.Request, deps
jsonError(w, 401, map[string]string{"error": "unauthorized"})
return
}
body, err := io.ReadAll(r.Body)
body, err := io.ReadAll(http.MaxBytesReader(w, r.Body, maxDispatchBodyBytes))
if err != nil {
var tooLarge *http.MaxBytesError
if errors.As(err, &tooLarge) {
jsonError(w, 413, map[string]string{"error": "body_too_large"})
return
}
jsonError(w, 400, map[string]string{"error": "bad_json"})
return
}
Expand Down
15 changes: 15 additions & 0 deletions packages/sandbox/daemon-go/internal/dispatch/dispatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,18 @@ func TestUnauthorizedCancelLeavesNoTombstone(t *testing.T) {
t.Fatal("a rejected cancel must not tombstone the run")
}
}

// An oversized inline dispatch body must be rejected before it is buffered in
// full — the offload path already caps at this size, and the inline path had
// no cap at all.
func TestDispatchRejectsOversizedBody(t *testing.T) {
const token = "tkn"
body := strings.NewReader(strings.Repeat("a", maxDispatchBodyBytes+1))
req := httptest.NewRequest("POST", "/dispatch", body)
req.Header.Set("Authorization", "Bearer "+token)
rec := httptest.NewRecorder()
NewRegistry().HandleDispatch(rec, req, Deps{DaemonToken: func() string { return token }})
if rec.Code != 413 {
t.Fatalf("dispatch with oversized body returned %d, want 413", rec.Code)
}
}
Loading