Skip to content

fix(git): limit size of request bodies buffered in memory - #422

Merged
stuartwdouglas merged 2 commits into
mainfrom
feat/limit-request-body-size
Nov 3, 2026
Merged

stuartwdouglas merged 2 commits into
mainfrom
feat/limit-request-body-size

Conversation

@stuartwdouglas

Copy link
Copy Markdown
Contributor

Upload-pack negotiation bodies and ensure-refs JSON bodies are read fully into memory (to hash a spool key, to replay after a "not our ref" fallback, and to work around cgi.Handler rejecting chunked bodies). A client could send an oversized or endless chunked body and exhaust cachewd's memory.

This adds a git strategy option, max-request-body-mb (default 100, matching the GIT_HTTP_MAX_REQUEST_BUFFER=100M we already pass to git http-backend; 0 disables it). It applies only where bodies are buffered:

  • git-upload-pack POSTs, for both ready mirrors and mirrors that are still cloning (spooling)
  • ensure-refs

Requests whose Content-Length exceeds the limit are rejected with 413 before any work is done. Chunked bodies are wrapped in http.MaxBytesReader and also return 413 when they cross the limit. They are not forwarded upstream, because the body has already been partly consumed.

Streamed requests are not limited: cache PUTs (cache warming), git-receive-pack pushes, LFS, and other requests proxied to upstream.

Tests: TestRequestBodyLimit covers a 750KB body under a 1MB limit (served), oversized bodies with a declared length and with chunked encoding, the cloning/spool path, and ensure-refs. Without the limit, all four oversized cases fail.

Upload-pack negotiation and ensure-refs bodies are read fully into memory,
so an oversized or endless (chunked) body could exhaust cachewd's memory.
These bodies are now capped by the new git max-request-body-mb option
(default 100MB, matching GIT_HTTP_MAX_REQUEST_BUFFER), returning 413 when
exceeded. Streamed requests such as cache PUTs (cache warming), pushes, and
other proxied requests are unaffected.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1a052-93bd-73a1-83b2-d55b78c2b1c0
Co-authored-by: Amp <amp@ampcode.com>
@stuartwdouglas
stuartwdouglas requested a review from a team as a code owner November 3, 2026 07:45
@stuartwdouglas
stuartwdouglas requested review from alecthomas and removed request for a team November 3, 2026 07:45
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-03T07:47:48.114912Z fab778b PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fab778b801

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread internal/strategy/git/git.go Outdated
Comment on lines +646 to +648
// limitRequestBody caps the body of requests whose body is buffered in memory, so a client can't exhaust memory with
// an oversized or endless body. Declared oversized bodies are rejected immediately; chunked bodies fail on read with an
// error matched by [requestTooLarge]. It returns false if it wrote a response.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Remove the comment on the short body-limit helper

The added three-line comment describes what this small, self-explanatory private helper does, but the repository explicitly permits comments only for non-obvious blocks of at least 20 lines and requires comments to explain why rather than what. Remove this comment and rely on the helper’s naming.

AGENTS.md reference: AGENTS.md:L19-L22

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed in 542f1a9, along with the inline comment in handleGitRequest.

@stuartwdouglas
stuartwdouglas force-pushed the feat/limit-request-body-size branch from 542f1a9 to 5dd67e8 Compare November 3, 2026 09:23
@stuartwdouglas
stuartwdouglas enabled auto-merge (squash) November 3, 2026 09:23
@stuartwdouglas
stuartwdouglas merged commit f436296 into main Nov 3, 2026
7 checks passed
@stuartwdouglas
stuartwdouglas deleted the feat/limit-request-body-size branch November 3, 2026 09:27
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.

2 participants