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
10 changes: 10 additions & 0 deletions pkgs/mcpx/internal/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,16 @@ var (
// JSON has no business being larger, and an unbounded read lets a
// bad endpoint make the daemon allocate arbitrarily much (#4354).
ProviderResponseBodyLimit = mustBytes(builtin.Upstream.ResponseBodyLimit, "upstream.responseBodyLimit")
// OpenAPISpecBodyLimit bounds the buffered peer reads in
// internal/openapi: a fetched OpenAPI document (Load) and a tool
// call's response body (API.Call). Per contract decision 2
// (#4375), both are refused past this cap — read cap+1 and fail
// when the extra byte exists — never silently truncated: a
// truncated spec parses as a shorter document or fails
// downstream with a misleading error (#8271). A plain constant,
// not a builtin config key: the bound is a property of the
// contract, not an operator tuning knob.
OpenAPISpecBodyLimit = int64(32 << 20)
// UpstreamSSEReconnectDelay is the resume wait when a server sent no
// retry field.
UpstreamSSEReconnectDelay = mustDur(builtin.Upstream.SSEReconnectDelay, "upstream.sseReconnectDelay")
Expand Down
21 changes: 19 additions & 2 deletions pkgs/mcpx/internal/openapi/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"time"

"gopkg.in/yaml.v3"

"github.com/developing-today/code/pkgs/mcpx/internal/defaults"
)

// Spec is the subset of OpenAPI that matters for making calls.
Expand Down Expand Up @@ -154,7 +156,15 @@ func Load(ctx context.Context, source string, timeout time.Duration) (*Spec, err
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s returned %s", source, resp.Status)
}
raw, err = io.ReadAll(io.LimitReader(resp.Body, 32<<20))
// Contract decision 2 (#4375, #8271): a fetched spec is a
// buffered success body — refuse it past the named cap
// (read cap+1, fail when the extra byte exists) instead of
// silently truncating it into a shorter document.
raw, err = io.ReadAll(io.LimitReader(resp.Body, defaults.OpenAPISpecBodyLimit+1))
if err == nil && int64(len(raw)) > defaults.OpenAPISpecBodyLimit {
err = fmt.Errorf("fetching %s: spec exceeds the %d-byte limit",
source, defaults.OpenAPISpecBodyLimit)
}
} else {
raw, err = os.ReadFile(source)
}
Expand Down Expand Up @@ -687,10 +697,17 @@ func (a *API) Call(ctx context.Context, tool string, args map[string]any) (*Resu
return nil, fmt.Errorf("%s %s: %w", spec.Method, target, err)
}
defer func() { _ = resp.Body.Close() }()
raw, err := io.ReadAll(io.LimitReader(resp.Body, 32<<20))
// Contract decision 2 (#4375, #8271): the call's buffered
// response body is refused past the named cap, never silently
// truncated into a shorter result.
raw, err := io.ReadAll(io.LimitReader(resp.Body, defaults.OpenAPISpecBodyLimit+1))
if err != nil {
return nil, err
}
if int64(len(raw)) > defaults.OpenAPISpecBodyLimit {
return nil, fmt.Errorf("%s %s: response body exceeds the %d-byte limit",
spec.Method, target, defaults.OpenAPISpecBodyLimit)
}

res := &Result{Status: resp.StatusCode, Body: string(raw), URL: target}
if ct := resp.Header.Get("Content-Type"); strings.Contains(ct, "json") {
Expand Down
56 changes: 56 additions & 0 deletions pkgs/mcpx/internal/openapi/openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"testing"

"github.com/developing-today/code/pkgs/mcpx/internal/defaults"
"github.com/developing-today/code/pkgs/mcpx/internal/openapi"
)

Expand Down Expand Up @@ -571,3 +572,58 @@ func TestSameNameTwoLocationsIsABuildError(t *testing.T) {
}
}
}

func TestLoadRefusesASpecPastTheCap(t *testing.T) {
// #8271 / contract decision 2 (#4375): a fetched spec is refused
// past defaults.OpenAPISpecBodyLimit, never silently truncated
// into a shorter document that parses wrong downstream.
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(strings.Repeat("x", int(defaults.OpenAPISpecBodyLimit)+1)))
}))
defer srv.Close()

_, err := openapi.Load(context.Background(), srv.URL+"/openapi.json", 0)
if err == nil || !strings.Contains(err.Error(), "exceeds") {
t.Fatalf("an over-cap spec must fail the fetch with an exceeds error, got: %v", err)
}
}

func TestLoadReadsASpecAtExactlyTheCap(t *testing.T) {
// The cap+1 pattern's boundary: exactly-at-cap is read whole
// (and then fails as the invalid document it is), not refused
// as over-cap.
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(strings.Repeat("x", int(defaults.OpenAPISpecBodyLimit))))
}))
defer srv.Close()

_, err := openapi.Load(context.Background(), srv.URL+"/openapi.json", 0)
if err == nil {
t.Fatal("a cap-sized body of non-spec bytes must fail to parse")
}
if strings.Contains(err.Error(), "exceeds") {
t.Fatalf("an exactly-at-cap body must not be refused as over-cap: %v", err)
}
}

func TestCallRefusesAResponsePastTheCap(t *testing.T) {
// #8271 / contract decision 2 (#4375): a tool call's buffered
// response body is refused past the same named cap, never
// silently truncated into a shorter result.
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(strings.Repeat("x", int(defaults.OpenAPISpecBodyLimit)+1)))
}))
defer srv.Close()

api := build(t, openapi.Options{BaseURL: srv.URL})
var tool string
for _, tl := range api.Tools {
if strings.HasSuffix(tl.Name, "getpet") {
tool = tl.Name
}
}
_, err := api.Call(context.Background(), tool, map[string]any{"petId": "123"})
if err == nil || !strings.Contains(err.Error(), "exceeds") {
t.Fatalf("an over-cap response must fail the call with an exceeds error, got: %v", err)
}
}