-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
fix: improve getRawBody
parsing & handle error(s)
#1528
Merged
Merged
Changes from all commits
Commits
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 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,7 @@ | ||
--- | ||
'@sveltejs/adapter-node': patch | ||
'@sveltejs/adapter-vercel': patch | ||
'@sveltejs/kit': patch | ||
--- | ||
|
||
ensure `content-length` limit respected; handle `getRawBody` error(s) |
This file contains 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 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 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 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,44 +1,45 @@ | ||
/** | ||
* @param {import('http').IncomingMessage} req | ||
* @returns {Promise<import('types/hooks').StrictBody>} | ||
* @returns {Promise<import('types/hooks').StrictBody | null>} | ||
*/ | ||
export function getRawBody(req) { | ||
return new Promise((fulfil, reject) => { | ||
const h = req.headers; | ||
|
||
if (!h['content-type']) { | ||
fulfil(null); | ||
return; | ||
return fulfil(null); | ||
} | ||
|
||
req.on('error', reject); | ||
|
||
const length = Number(h['content-length']); | ||
|
||
/** @type {Uint8Array} */ | ||
let data; | ||
|
||
if (!isNaN(length)) { | ||
data = new Uint8Array(length); | ||
// https://github.com/jshttp/type-is/blob/c1f4388c71c8a01f79934e68f630ca4a15fffcd6/index.js#L81-L95 | ||
if (isNaN(length) && h['transfer-encoding'] == null) { | ||
return fulfil(null); | ||
} | ||
|
||
let i = 0; | ||
let data = new Uint8Array(length || 0); | ||
|
||
if (length > 0) { | ||
let offset = 0; | ||
req.on('data', (chunk) => { | ||
data.set(chunk, i); | ||
i += chunk.length; | ||
}); | ||
} else { | ||
// https://github.com/jshttp/type-is/blob/c1f4388c71c8a01f79934e68f630ca4a15fffcd6/index.js#L81-L95 | ||
if (h['transfer-encoding'] === undefined) { | ||
fulfil(null); | ||
return; | ||
} | ||
const new_len = offset + Buffer.byteLength(chunk); | ||
|
||
data = new Uint8Array(0); | ||
if (new_len > length) { | ||
return reject({ | ||
status: 413, | ||
reason: 'Exceeded "Content-Length" limit' | ||
}); | ||
} | ||
|
||
data.set(chunk, offset); | ||
offset = new_len; | ||
}); | ||
} else { | ||
req.on('data', (chunk) => { | ||
const new_data = new Uint8Array(data.length + chunk.length); | ||
new_data.set(data); | ||
new_data.set(data, 0); | ||
new_data.set(chunk, data.length); | ||
data = new_data; | ||
}); | ||
|
@@ -48,11 +49,11 @@ export function getRawBody(req) { | |
const [type] = h['content-type'].split(/;\s*/); | ||
|
||
if (type === 'application/octet-stream') { | ||
fulfil(data); | ||
return fulfil(data); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without this |
||
} | ||
|
||
const decoder = new TextDecoder(h['content-encoding'] || 'utf-8'); | ||
fulfil(decoder.decode(data)); | ||
const encoding = h['content-encoding'] || 'utf-8'; | ||
fulfil(new TextDecoder(encoding).decode(data)); | ||
}); | ||
}); | ||
} |
This file contains 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 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
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.
Using the
chunk.length
was a bug. Incomingchunks
can be strings, but even if they're still Buffer instances,length
is not always the same asbyteLength
, which is whatcontent-length
tracks