Skip to content

feat: add assertBodySize util and bodyLimit middleware#1222

Merged
pi0 merged 14 commits intoh3js:mainfrom
aquapi:main
Oct 25, 2025
Merged

feat: add assertBodySize util and bodyLimit middleware#1222
pi0 merged 14 commits intoh3js:mainfrom
aquapi:main

Conversation

@aquapi
Copy link
Copy Markdown
Contributor

@aquapi aquapi commented Oct 15, 2025

resolves #1221

@aquapi aquapi requested a review from pi0 as a code owner October 15, 2025 15:00
@aquapi
Copy link
Copy Markdown
Contributor Author

aquapi commented Oct 15, 2025

I need to add tests for this feature as well
We can get some tests from Hono: https://github.com/honojs/hono/blob/main/src/middleware/body-limit/index.test.ts

@aquapi
Copy link
Copy Markdown
Contributor Author

aquapi commented Oct 25, 2025

@pi0 do u think I should add more tests

@pi0 pi0 changed the title Add request body size limit utility and middleware feat: add assertBodySize util and bodyLimit middleware Oct 25, 2025
Copy link
Copy Markdown
Member

@pi0 pi0 left a comment

Choose a reason for hiding this comment

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

Thanks ❤️

@pi0 pi0 merged commit 412bba4 into h3js:main Oct 25, 2025
3 checks passed
@pi0
Copy link
Copy Markdown
Member

pi0 commented Oct 25, 2025

Now we can add it a a Nitro v3 route rule even!

Comment thread src/utils/body.ts
Comment on lines +143 to +146
const bodyLen = req.headers.get("content-length");
if (bodyLen !== null && !req.headers.has("transfer-encoding")) {
return +bodyLen <= limit;
}
Copy link
Copy Markdown

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

Copy link
Copy Markdown
Member

@pi0 pi0 Oct 27, 2025

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)

Copy link
Copy Markdown
Member

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown

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

Copy link
Copy Markdown
Member

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.

image

Copy link
Copy Markdown
Member

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)

image

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thank you very much

@pi0 pi0 mentioned this pull request Oct 27, 2025
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.

Add request body size limit utility and middleware

3 participants