Skip to content

fix: cap HTTP-loaded script body size to prevent unbounded read DoS#107

Merged
robbyt merged 4 commits into
mainfrom
fix/issue-98-max-body-size
May 7, 2026
Merged

fix: cap HTTP-loaded script body size to prevent unbounded read DoS#107
robbyt merged 4 commits into
mainfrom
fix/issue-98-max-body-size

Conversation

@robbyt
Copy link
Copy Markdown
Owner

@robbyt robbyt commented May 7, 2026

Summary

The HTTP loader returned resp.Body straight to compilers that io.ReadAll without limit, so a misbehaving server could exhaust process memory on any caller that loads scripts from arbitrary URLs.

  • HTTPOptions.MaxBodySize int64 with sentinel encoding: 0 falls back to DefaultMaxBodySize (10 MiB), negative disables the cap. DefaultHTTPOptions() populates the default so existing callers (only inference.go in production) inherit protection without code changes.
  • New WithMaxBodySize(int64) option helper mirroring WithBasicAuth / WithTimeout.
  • New ErrScriptTooLarge sentinel.
  • GetReaderWithContext switches from streaming resp.Body to an eager read of io.LimitReader(resp.Body, limit+1); the +1 trick distinguishes exactly-at-limit (pass) from over-limit (fail). On overflow, returns ErrScriptTooLarge wrapping the URL and limit.
  • Returns io.NopCloser(bytes.NewReader(buf)) so downstream Close() is a no-op now that the loader owns the response body.

Eager buffering matches what every compiler already does (io.ReadAll under the hood) and the Loader interface doesn't promise streaming, so no caller is affected.

Test plan

  • New TestFromHTTP_MaxBodySize covers six cases: under limit, exactly at limit, over limit (asserts errors.Is(err, ErrScriptTooLarge)), WithMaxBodySize(-1) disables the cap, zero-value field falls back to default, default options enforce the 10 MiB cap.
  • go test -race -count=1 ./... — green
  • go vet ./... — clean
  • CI on the PR

Closes #98

https://claude.ai/code/session_01C61VEAmjxSnX5Xhbab8NvL


Generated by Claude Code

The HTTP loader returned resp.Body straight to compilers that io.ReadAll
without limit, so a misbehaving server could exhaust process memory on
any caller that loads scripts from arbitrary URLs.

Changes:

- Add HTTPOptions.MaxBodySize int64 with sentinel encoding: 0 falls back
  to DefaultMaxBodySize (10 MiB), negative disables the cap entirely.
  DefaultHTTPOptions() sets MaxBodySize to the default so existing
  callers (only inference.go in production) inherit the protection.
- Add WithMaxBodySize(int64) option helper, mirroring the existing
  WithBasicAuth / WithTimeout shape.
- Add ErrScriptTooLarge sentinel.
- In GetReaderWithContext, switch from streaming resp.Body to an eager
  read of LimitReader(resp.Body, limit+1); the +1 trick distinguishes
  exactly-at-limit (pass) from over-limit (fail). On overflow, return
  ErrScriptTooLarge wrapping the URL and limit.
- Returns io.NopCloser(bytes.NewReader(buf)) so downstream Close() is a
  no-op now that the loader owns the response body.

Eager buffering matches what every existing compiler does (io.ReadAll
under the hood), so no caller is affected. The Loader interface doesn't
promise streaming.

Closes #98
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 7, 2026

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

errcheck (check-blank: true) flagged the _, _ = w.Write(body) form. Use
an if-err pattern with t.Logf so the broken-pipe noise surfaces in the
test log without failing it.
Copilot AI review requested due to automatic review settings May 7, 2026 05:57
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds a default maximum response size for HTTP-loaded scripts to prevent unbounded reads that could lead to memory exhaustion when loading from arbitrary URLs.

Changes:

  • Introduces HTTPOptions.MaxBodySize (default 10 MiB) plus WithMaxBodySize helper to configure/disable the cap.
  • Updates FromHTTP.GetReaderWithContext to eagerly read with a size limit and return ErrScriptTooLarge on overflow.
  • Adds ErrScriptTooLarge sentinel and new test coverage for max-body-size behavior.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
platform/script/loader/fromHTTP.go Adds default size cap/config and enforces it during HTTP body reads.
platform/script/loader/errors.go Adds a new sentinel error for oversized scripts.
platform/script/loader/fromHTTP_test.go Adds tests validating max-body-size behavior (default, override, disable).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread platform/script/loader/fromHTTP.go Outdated
Comment thread platform/script/loader/fromHTTP_test.go Outdated
claude added 2 commits May 7, 2026 06:01
- fromHTTP.go: limit+1 wraps when MaxBodySize == math.MaxInt64, which
  made io.LimitReader return EOF immediately and broke the cap. Compute
  readLimit separately and only add 1 when limit < math.MaxInt64; the
  overflow branch is unreachable for MaxInt64 (no realistic body
  reaches it), which is fine.
- fromHTTP_test.go: serveBody now takes the subtest's t so logs
  associate to the right test, and streams via io.CopyN over a small
  repeatingByte reader so multi-MiB cases don't allocate a full buffer
  up front.
Sonar (rule go:S3776) flagged GetReaderWithContext at cognitive
complexity 17/15. Hoist the limit-resolve + LimitReader + overflow-check
block into a new (l *FromHTTP).cappedBody(resp) helper. The HTTP wiring
(request build, auth, headers, status check) stays in the main method;
the size-cap policy lives next to its own godoc.
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented May 7, 2026

@robbyt robbyt merged commit 467e3a1 into main May 7, 2026
3 checks passed
@robbyt robbyt deleted the fix/issue-98-max-body-size branch May 7, 2026 06:10
@robbyt robbyt mentioned this pull request May 10, 2026
3 tasks
robbyt added a commit that referenced this pull request May 10, 2026
* docs: add CHANGELOG.md (Keep a Changelog 1.1.0)

Closes #102

Repo had no CHANGELOG.md; users had to read GitHub release notes to
find what changed between versions. With v1 ahead and a queue of
breaking changes (#86, #87, #88, #89, #90, #91, #104), having a
CHANGELOG before landing those gives downstream code reviewers and
IDEs visibility into what's coming.

Format follows Keep a Changelog 1.1.0 with the standard six headings
(Added / Changed / Deprecated / Removed / Fixed / Security).

[Unreleased] section captures the run of merged-but-not-released PRs
since v0.7.0:

  - #103  polyscript.New[E] generic constructor (deprecates 12 FromXxx)
  - #105  slog.Handler optional in engine subpackages
  - #106  RequestToMap mutation fix
  - #107  HTTP loader MaxBodySize cap
  - #110  unify nil-handler on slog.Default(); drop stdout fallbacks
  - #113  WithLogHandler(nil)/WithLogger(nil) → no-op
  - #114  drop redundant nil-guards; tighten WithLogHandler doc
  - #115  docs/LOGGING.md
  - #117  fix bare top-level json.Number leak
  - #118  WithGlobals additive; drop dead URL check
  - #119  extism Eval test coverage

Backfilled the three releases the issue called out (v0.5.0, v0.6.0,
v0.7.0) from existing GitHub release notes, sorted into Keep a
Changelog categories.

Earlier releases (v0.0.x through v0.4.0) intentionally not backfilled
per the issue scope; can be added in a follow-up if desired.

Out of scope: the optional CI gate that fails PRs not touching
CHANGELOG.md. Adds noise to small bug PRs and wants opt-out-label
infrastructure to support it; better as a separate issue.

* docs(CHANGELOG): clarify deprecated FromXxx still take positional handler

Copilot review noted that "no constructor demands a handler" is
misleading because the deprecated FromXxx constructors still take a
positional `logHandler slog.Handler` argument (even though nil is
accepted). Reword to distinguish the new generic constructor (no
handler arg) from the deprecated ones (still take it, but nil is OK).

---------

Co-authored-by: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[security] Add MaxBodySize limit to HTTP loader (DoS via unbounded body)

3 participants