Skip to content

Commit

Permalink
Respond with status code 413 if request body is too large (#6936)
Browse files Browse the repository at this point in the history
* Respond with status code 413 if request body is too large

* Use the error method

* Prevent arbitrary error messages from reaching users

* Create strong-baboons-travel.md

* Create five-tools-arrive.md

Co-authored-by: Rich Harris <[email protected]>
  • Loading branch information
repsac-by and Rich-Harris authored Sep 21, 2022
1 parent 63bc2e2 commit 737160d
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 7 deletions.
7 changes: 7 additions & 0 deletions .changeset/five-tools-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@sveltejs/adapter-node": patch
"@sveltejs/adapter-vercel": patch
"@sveltejs/kit": patch
---

Redact error message if `getRequest` fails
5 changes: 5 additions & 0 deletions .changeset/strong-baboons-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/kit": patch
---

Respond with 413 if request body is too large
2 changes: 1 addition & 1 deletion packages/adapter-node/src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const ssr = async (req, res) => {
});
} catch (err) {
res.statusCode = err.status || 400;
res.end(err.reason || 'Invalid request body');
res.end('Invalid request body');
return;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-vercel/files/serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default async (req, res) => {
request = await getRequest({ base: `https://${req.headers.host}`, request: req });
} catch (err) {
res.statusCode = err.status || 400;
return res.end(err.reason || 'Invalid request body');
return res.end('Invalid request body');
}

setResponse(
Expand Down
11 changes: 8 additions & 3 deletions packages/kit/src/exports/node/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as set_cookie_parser from 'set-cookie-parser';
import { error } from '../index.js';

/**
* @param {import('http').IncomingMessage} req
Expand Down Expand Up @@ -27,7 +28,8 @@ function get_raw_body(req, body_size_limit) {
if (!length) {
length = body_size_limit;
} else if (length > body_size_limit) {
throw new Error(
throw error(
413,
`Received content-length of ${length}, but only accept up to ${body_size_limit} bytes.`
);
}
Expand All @@ -45,6 +47,7 @@ function get_raw_body(req, body_size_limit) {
return new ReadableStream({
start(controller) {
req.on('error', (error) => {
cancelled = true;
controller.error(error);
});

Expand All @@ -58,8 +61,10 @@ function get_raw_body(req, body_size_limit) {

size += chunk.length;
if (size > length) {
req.destroy(
new Error(
cancelled = true;
controller.error(
error(
413,
`request body size exceeded ${
content_length ? "'content-length'" : 'BODY_SIZE_LIMIT'
} of ${length}`
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/exports/vite/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ export async function dev(vite, vite_config, svelte_config) {
});
} catch (/** @type {any} */ err) {
res.statusCode = err.status || 400;
return res.end(err.message || 'Invalid request body');
return res.end('Invalid request body');
}

const template = load_template(cwd, svelte_config);
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/exports/vite/preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export async function preview(vite, vite_config, svelte_config) {
});
} catch (/** @type {any} */ err) {
res.statusCode = err.status || 400;
return res.end(err.message || 'Invalid request body');
return res.end('Invalid request body');
}

setResponse(
Expand Down

0 comments on commit 737160d

Please sign in to comment.