-
Couldn't load subscription status.
- Fork 295
feat: add assertBodySize util and bodyLimit middleware
#1222
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
+207
−18
Merged
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d65aa3e
feat: add body size limit util and change readValidatedBody example
aquapi fcfd99e
feat: add isBodySizeWithin to package unit test
aquapi f3b90c1
feat: add bodyLimit middleware
aquapi 2bdde4f
chore: apply automated updates
autofix-ci[bot] 90f4635
Merge branch 'main' into main
pi0 277eb3d
Merge branch 'main' into main
pi0 19b5bac
feat: improve isBodySizeWithin code and add unit tests for it
aquapi 0fda23d
chore: apply automated updates
autofix-ci[bot] 1170174
Merge branch 'h3js:main' into main
aquapi 22e11ae
fix: typo in body limit unit test
aquapi 33074fb
refactors
pi0 0e0720c
update
pi0 cf42d4e
chore: apply automated updates
autofix-ci[bot] 2fd08d6
lint example
pi0 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
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 |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| packages: | ||
| - "playground" | ||
| - "examples" | ||
| - playground | ||
| - examples | ||
|
|
||
| ignoredBuiltDependencies: | ||
| - esbuild |
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
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,80 @@ | ||
| import { expect, it, describe } from "vitest"; | ||
| import { mockEvent, assertBodySize, HTTPError } from "../../src/index.ts"; | ||
|
|
||
| describe("body limit (unit)", () => { | ||
| const streamBytesFrom = (it: Iterable<any, any>) => | ||
| new ReadableStream({ | ||
| start(c) { | ||
| for (const part of it) c.enqueue(part); | ||
| c.close(); | ||
| }, | ||
| }).pipeThrough(new TextEncoderStream()); | ||
|
|
||
| describe("assertBodySize", () => { | ||
| it("buffered body", async () => { | ||
| const BODY = "a small request body"; | ||
|
|
||
| const eventMock = mockEvent("/", { | ||
| method: "POST", | ||
| body: BODY, | ||
| }); | ||
|
|
||
| await expect( | ||
| assertBodySize(eventMock, BODY.length), | ||
| ).resolves.toBeUndefined(); | ||
| await expect( | ||
| assertBodySize(eventMock, BODY.length + 10), | ||
| ).resolves.toBeUndefined(); | ||
| await expect(assertBodySize(eventMock, BODY.length - 2)).rejects.toThrow( | ||
| HTTPError, | ||
| ); | ||
| }); | ||
|
|
||
| it("streaming body", async () => { | ||
| const BODY_PARTS = [ | ||
| "parts", | ||
| "of", | ||
| "the", | ||
| "body", | ||
| "that", | ||
| "are", | ||
| "streamed", | ||
| "in", | ||
| ]; | ||
|
|
||
| const eventMock = mockEvent("/", { | ||
| method: "POST", | ||
| body: streamBytesFrom(BODY_PARTS), | ||
| }); | ||
|
|
||
| await expect(assertBodySize(eventMock, 100)).resolves.toBeUndefined(); | ||
| await expect(assertBodySize(eventMock, 10)).rejects.toThrow(HTTPError); | ||
| }); | ||
|
|
||
| it("streaming body with content-length header", async () => { | ||
| const BODY_PARTS = [ | ||
| "parts", | ||
| "of", | ||
| "the", | ||
| "body", | ||
| "that", | ||
| "are", | ||
| "streamed", | ||
| "in", | ||
| ]; | ||
|
|
||
| const eventMock = mockEvent("/", { | ||
| method: "POST", | ||
| body: streamBytesFrom(BODY_PARTS), | ||
| headers: { | ||
| // Should ignore content-length | ||
| "content-length": "7", | ||
| "transfer-encoding": "chunked", | ||
| }, | ||
| }); | ||
|
|
||
| await expect(assertBodySize(eventMock, 100)).resolves.toBeUndefined(); | ||
| await expect(assertBodySize(eventMock, 10)).rejects.toThrow(HTTPError); | ||
| }); | ||
| }); | ||
| }); |
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.
@pi0 see #1150 (comment)
by your previous statement this code is insecure
Uh oh!
There was an error while loading. Please reload this page.
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.
We should add a flag to force check when no length.
But also test which runtimes allow body with length (invalid) + transfer-encoding (if they do yes indeed we should enable flag by default)
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.
According to the HTTP/1.1 specification (RFC 7230, section 3.3.2):
A message must not include both a Content-Length header field and a Transfer-Encoding header field.
(we should actually fail in this case)
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.
9ccd301
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.
but it stills allows a faked content-length
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.
When running in real HTTP server (Node.js server for example) it should stop reading body when content-length excceds as it violated protocol.
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.
(Bun and Deno also "cut off" reading body by validating
content-length)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.
Thank you very much