Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/cold-seals-write.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@sveltejs/kit": patch
---

fix: enforce request body size limits when Content-Type is absent

4 changes: 0 additions & 4 deletions packages/kit/src/exports/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ const body_data_listeners = new WeakMap();
function get_raw_body(req, body_size_limit) {
const h = req.headers;

if (!h['content-type']) {
return null;
}

const content_length = Number(h['content-length']);
const has_content_length = Number.isFinite(content_length);

Expand Down
26 changes: 26 additions & 0 deletions packages/kit/src/exports/node/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,32 @@ test('rejects request bodies that exceed content-length', async () => {
});
});

test.each([{ 'content-length': '11' }, { 'transfer-encoding': 'chunked' }])(
'enforces body size limit without content-type (%j)',
async (headers) => {
const req = new PassThrough();
const incoming = /** @type {import('http').IncomingMessage} */ (/** @type {unknown} */ (req));
incoming.headers = headers;
incoming.method = 'POST';
incoming.url = '/';
incoming.httpVersionMajor = 1;

const request = getRequest({
request: incoming,
base: 'http://localhost',
bodySizeLimit: 10
});

const text = request.text();
req.end(Buffer.from('0123456789a'));

await expect(text).rejects.toMatchObject({
status: 413,
text: 'Payload Too Large'
});
}
);

/**
* Minimal `ServerResponse` stand-in that records headers and body writes and
* emits `finish` when ended.
Expand Down
Loading