-
Notifications
You must be signed in to change notification settings - Fork 2.1k
[v1.x] fix(server): read HTTP request bodies with a size limit and bound JSON-RPC batch length #2717
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[v1.x] fix(server): read HTTP request bodies with a size limit and bound JSON-RPC batch length #2717
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /** Default upper bound, in bytes, on a request body read by the HTTP entry points (4 MiB). */ | ||
| export const DEFAULT_MAX_REQUEST_BODY_SIZE = 4 * 1024 * 1024; | ||
|
|
||
| /** Upper bound on the number of messages accepted in one JSON-RPC batch array. */ | ||
| export const MAX_BATCH_SIZE = 100; | ||
|
|
||
| /** The message answered with 413 for a request body over `maxBytes`. */ | ||
| export function requestBodyTooLargeMessage(maxBytes: number): string { | ||
| return `Payload Too Large: Request body must not exceed ${maxBytes} bytes`; | ||
| } | ||
|
|
||
| /** | ||
| * Resolves a `maxRequestBodySize` option to the bound to apply: the default when | ||
| * omitted, otherwise the value itself, which must be a positive finite number of | ||
| * bytes (a `RangeError` is thrown at configuration time for anything else). | ||
| */ | ||
| export function resolveMaxRequestBodySize(value: number | undefined): number { | ||
| if (value === undefined) { | ||
| return DEFAULT_MAX_REQUEST_BODY_SIZE; | ||
| } | ||
| if (typeof value !== 'number' || !Number.isFinite(value) || value <= 0) { | ||
| throw new RangeError(`maxRequestBodySize must be a positive number of bytes, got ${String(value)}`); | ||
| } | ||
| return value; | ||
| } | ||
|
|
||
| /** | ||
| * Reads a request body as text, up to `maxBytes` (default | ||
| * {@linkcode DEFAULT_MAX_REQUEST_BODY_SIZE}). A declared `Content-Length` over the | ||
| * limit is refused without reading anything; otherwise the read stops as soon as | ||
| * more than the limit has arrived. Stream failures propagate. | ||
| */ | ||
| export async function readRequestBody( | ||
| request: Request, | ||
| maxBytes: number = DEFAULT_MAX_REQUEST_BODY_SIZE | ||
| ): Promise<{ tooLarge: true } | { tooLarge: false; text: string }> { | ||
| if (Number(request.headers.get('content-length')) > maxBytes) { | ||
| return { tooLarge: true }; | ||
| } | ||
| if (request.body === null) { | ||
| return { tooLarge: false, text: '' }; | ||
| } | ||
| const reader = request.body.getReader(); | ||
| const decoder = new TextDecoder(); | ||
| let received = 0; | ||
| let text = ''; | ||
| try { | ||
| for (;;) { | ||
| const { done, value } = await reader.read(); | ||
| if (done) { | ||
| break; | ||
| } | ||
| received += value.byteLength; | ||
| if (received > maxBytes) { | ||
| return { tooLarge: true }; | ||
| } | ||
| text += decoder.decode(value, { stream: true }); | ||
| } | ||
| } finally { | ||
| reader.releaseLock(); | ||
| } | ||
| return { tooLarge: false, text: text + decoder.decode() }; | ||
| } |
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟣 Pre-existing (kept by this diff): the bare catch around the body read hard-codes 400/-32700 'Parse error: Invalid JSON' for all failures, including stream/read errors from readRequestBody, without discriminating the thrown cause — the repo review instructions (Recurring Catches: Error Handling) explicitly require flagging catch-alls that emit client-fault JSON-RPC codes for server/transport-internal failures.
Extended reasoning...
A client's connection drops (or the upstream stream errors) mid-upload of a POST body. readRequestBody propagates the stream failure, the catch at lines 757-759 swallows it, and the server answers 400 with JSON-RPC code -32700 'Parse error: Invalid JSON'. The client is told its request was malformed and may reformat/retry the same payload pointlessly, and onerror receives a fabricated parse error instead of the real network/stream error, hiding the actual cause from server operators.
Verification: pre-existing. The bare catch is real at HEAD src/server/webStandardStreamableHttp.ts:750-760:
const body = await readRequestBody(req, this._maxRequestBodySize); ... rawMessage = JSON.parse(body.text); } catch { this.onerror?.(new Error('Parse error: Invalid JSON')); return this.createJsonErrorResponse(400, -32700, 'Parse error: Invalid JSON'); }. src/server/requestBody.ts's readRequestBody expli