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
24 changes: 24 additions & 0 deletions pkgs/mcpx/internal/daemon/body_limits_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package daemon

import (
"io"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -80,6 +81,29 @@ func TestBodyLimitsUnderLimitUnchanged(t *testing.T) {
}
}

type countedBody struct {
io.Reader
read int
}

func (b *countedBody) Read(p []byte) (int, error) {
n, err := b.Reader.Read(p)
b.read += n
return n, err
}

func TestAskAbandonBodyReadIsBounded(t *testing.T) {
s := &Server{set: bodyLimitSet(t)}
body := &countedBody{Reader: strings.NewReader(strings.Repeat(" ", 1024))}
req := httptest.NewRequest(http.MethodPost, "/v1/ask/missing/abandon", nil)
req.Body = io.NopCloser(body)
w := httptest.NewRecorder()
s.handleAskAbandon(w, req)
if body.read > 65 { // configured 64-byte ceiling, plus MaxBytesReader's probe byte
t.Fatalf("read %d bytes from an oversized abandon body; want at most 65", body.read)
}
}

// TestNoUnboundedBodyReaders is the #2082 guard: a non-test daemon source
// file may touch r.Body only through a ceiling (MaxBytesReader /
// LimitReader) or a bounded handoff (the artifact store enforces its own
Expand Down
6 changes: 2 additions & 4 deletions pkgs/mcpx/internal/daemon/routes_proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -906,10 +906,8 @@ func (s *Server) handleAskAbandon(w http.ResponseWriter, r *http.Request) {
var body struct {
TaskCancelled bool `json:"taskCancelled"`
}
// The decode is capped like every other body read here; a missing
// or malformed body leaves the zero value (not cancelled).
if limited := http.MaxBytesReader(w, r.Body, defaults.HTTPCallBodyLimit); r.Body != nil {
_ = json.NewDecoder(limited).Decode(&body)
if bodyReader := http.MaxBytesReader(w, r.Body, s.bodyBytes("http.controlBodyLimit", defaults.HTTPControlBodyLimit)); r.Body != nil {
_ = json.NewDecoder(bodyReader).Decode(&body)
}
if body.TaskCancelled {
s.reg.markAskTaskCancelled(id)
Expand Down