Skip to content

Commit

Permalink
fix: Update body_size_limit to use Number instead of parseInt (#11589)
Browse files Browse the repository at this point in the history
* fix: Update body_size_limit to use parseFloat instead of parseInt

* fix: throw error if body_size_limit is NaN

* add changeset

* prettier fix

* Apply suggestions from code review

* Update packages/adapter-node/src/handler.js

* helpful error message

---------

Co-authored-by: Rich Harris <[email protected]>
Co-authored-by: Rich Harris <[email protected]>
  • Loading branch information
3 people committed Jan 10, 2024
1 parent 481593d commit 0f16e83
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/chatty-swans-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/adapter-node": major
---

breaking: allow any numeric value for `BODY_SIZE_LIMIT`, and interpret literally. Use `Infinity` rather than `0` for unrestricted body sizes
8 changes: 7 additions & 1 deletion packages/adapter-node/src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ const address_header = env('ADDRESS_HEADER', '').toLowerCase();
const protocol_header = env('PROTOCOL_HEADER', '').toLowerCase();
const host_header = env('HOST_HEADER', 'host').toLowerCase();
const port_header = env('PORT_HEADER', '').toLowerCase();
const body_size_limit = parseInt(env('BODY_SIZE_LIMIT', '524288')) || undefined;
const body_size_limit = Number(env('BODY_SIZE_LIMIT', '524288'));

if (isNaN(body_size_limit)) {
throw new Error(
`Invalid BODY_SIZE_LIMIT: '${env('BODY_SIZE_LIMIT')}'. Please provide a numeric value.`
);
}

const dir = path.dirname(fileURLToPath(import.meta.url));

Expand Down
14 changes: 9 additions & 5 deletions packages/kit/src/exports/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ function get_raw_body(req, body_size_limit) {
return new ReadableStream({
start(controller) {
if (body_size_limit !== undefined && content_length > body_size_limit) {
const error = new SvelteKitError(
413,
'Payload Too Large',
`Content-length of ${content_length} exceeds limit of ${body_size_limit} bytes.`
);
let message = `Content-length of ${content_length} exceeds limit of ${body_size_limit} bytes.`;

if (body_size_limit === 0) {
// https://github.com/sveltejs/kit/pull/11589
// TODO this exists to aid migration — remove in a future version
message += ' To disable body size limits, specify Infinity rather than 0.';
}

const error = new SvelteKitError(413, 'Payload Too Large', message);

controller.error(error);
return;
Expand Down

0 comments on commit 0f16e83

Please sign in to comment.