From f558e5b71aaf522bf74a0ef7e35ebd387f384e95 Mon Sep 17 00:00:00 2001 From: repo-worker-tmp-code-b Date: Mon, 16 Nov 2026 21:57:10 -0600 Subject: [PATCH] fix(mcpx/openapi): refuse fetched specs and call responses past the 32MiB cap instead of truncating (#8271) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Contract decision 2 (#4375) requires a buffered success body to be refused past its named cap, never silently truncated; both openapi peer reads used io.LimitReader at 32MiB and truncated. The cap is now named as defaults.OpenAPISpecBodyLimit (a defaults constant, per the issue's recorded form) and both sites read cap+1 and fail with a clear exceeds error: Load (fetched spec) and API.Call (tool-call response). The os.ReadFile local-path branch is untouched (not peer bytes). Tests: over-cap refusal at both sites plus the exactly-at-cap boundary (read whole, fails as parse, not as over-cap). Note: internal/defaults TestNoPackageRedeclaresADefault fails identically on clean main (pre-existing elicit/presence.go violation) — untouched by this change; the new constant lives in internal/defaults itself and is not flagged. --- pkgs/mcpx/internal/defaults/defaults.go | 10 ++++ pkgs/mcpx/internal/openapi/openapi.go | 21 +++++++- pkgs/mcpx/internal/openapi/openapi_test.go | 56 ++++++++++++++++++++++ 3 files changed, 85 insertions(+), 2 deletions(-) diff --git a/pkgs/mcpx/internal/defaults/defaults.go b/pkgs/mcpx/internal/defaults/defaults.go index 3a4e4bb22d..c6bbd326fa 100644 --- a/pkgs/mcpx/internal/defaults/defaults.go +++ b/pkgs/mcpx/internal/defaults/defaults.go @@ -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") diff --git a/pkgs/mcpx/internal/openapi/openapi.go b/pkgs/mcpx/internal/openapi/openapi.go index ddc2990fa2..2d3df66b0e 100644 --- a/pkgs/mcpx/internal/openapi/openapi.go +++ b/pkgs/mcpx/internal/openapi/openapi.go @@ -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. @@ -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) } @@ -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") { diff --git a/pkgs/mcpx/internal/openapi/openapi_test.go b/pkgs/mcpx/internal/openapi/openapi_test.go index d0ff65041a..60172cc0ea 100644 --- a/pkgs/mcpx/internal/openapi/openapi_test.go +++ b/pkgs/mcpx/internal/openapi/openapi_test.go @@ -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" ) @@ -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) + } +}