fix(security): enforce max request body size to prevent memory exhaustion (CWE-400) - #13
Merged
Merged
Conversation
…tion (CWE-400) Add configurable `max_body_size` (default 1MB) to `Mocket` to reject oversized HTTP request bodies before they are buffered into memory. - Native backend: check Content-Length header upfront and enforce a streaming read limit via `read_body_limited()`; return 413 when exceeded. - JS backend: track cumulative chunk size in the `data` callback and return 413 when the limit is crossed. Without this fix an unauthenticated attacker can send a single POST with an arbitrarily large body, causing the server to allocate until OOM. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Contributor
Author
|
Note: CI failures are pre-existing on the
None of these are related to this PR's changes. The fix builds and tests clean on both |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Vulnerability
The HTTP request handler reads the entire request body into memory with no size limit:
Native backend (
mocket.native.mbt:385):JS backend (
mocket.js.mbt:342-345):An unauthenticated attacker can send a single POST request with an arbitrarily large body, causing the server to allocate until OOM and crash. Tested with payloads up to 100MB — all accepted without rejection.
CVSS: 7.5 (High) — Network/Low/None/None (Availability)
Fix
Add a configurable
max_body_sizefield toMocket(default: 1MB):Content-Lengthheader upfront and enforce a streaming read limit viaread_body_limited(); return413 Request Entity Too Largewhen exceeded.datacallback and return413when the limit is crossed.Users can customize via
Mocket::new(max_body_size=5242880)(5MB) or disable withmax_body_size=0.Test Plan
max_body_size=0disables the limit (backward compatible)Co-Authored-By: Claude Opus 4.6 noreply@anthropic.com