Repository navigation
fix: always return a Buffer from buffer() - #505
Open
officialasishkumar wants to merge 1 commit into
Open
officialasishkumar wants to merge 1 commit into
officialasishkumar wants to merge 1 commit into
Conversation
buffer() passed the charset from the request's Content-Type header to raw-body as `encoding`, so any request with e.g. `Content-Type: application/json; charset=utf-8` made buffer() resolve with a decoded string instead of the Buffer it is typed and documented to return. The decoded string was also what got cached for the request, so calling text() or json() first made a later buffer() call return a string as well. Code that needs the raw bytes, such as webhook signature verification, broke depending on what the client sent. buffer() now always reads and caches the raw body as a Buffer. text() (and therefore json()) decodes that Buffer with `encoding`, falling back to the Content-Type charset as before. Decoding uses iconv-lite, the same version raw-body already uses internally, so supported charsets and BOM handling are unchanged. Unsupported charsets and malformed Content-Type headers are still rejected with a 400 before the body is read. Tests: - buffer() returns a Buffer when Content-Type has a charset, and after text() has decoded the body. - buffer() doesn't parse Content-Type at all. - text() decodes with the Content-Type charset (iso-8859-1), rejects an unsupported charset with 400, and skips parsing Content-Type when an encoding is given. Closes vercel#504
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #504 (originally reported in #419)
buffer()passed the request'sContent-Typecharset toraw-bodyasencoding. So a request withContent-Type: application/json; charset=utf-8madebuffer()resolve with a decoded string instead of aBuffer, and that string was then cached for the request. Handlers that need the raw bytes, such as webhook signature checks, behaved differently depending on what the client sent.Changes
buffer()always reads and caches the raw body as aBuffer, which matches its type and the README.text(), and sojson(), decodes that buffer usingencoding, or theContent-Typecharset ifencodingisn't set. Decoding usesiconv-lite@0.4.24, the versionraw-bodyalready uses internally, so the supported charsets and BOM handling stay the same. It adds no new package to the lockfile.Content-Typeheaders still return 400 fromtext()andjson()before the body is read.Note:
buffer(req, { encoding })used to return a string. It now returns theBuffer. To get a string, usetext(req, { encoding }).buffer()beforebuffer()nowBufferBuffercharset=utf-8stringBufferjson()called first,charset=utf-8stringBufferHow to test
In
test/suite/index.ts:buffer()tests: it returns aBufferwhenContent-Typehas a charset, it still returns one aftertext()has decoded the body, and it never parsesContent-Type. All three fail without this change.text()tests: it decodesiso-8859-1bodies and returns 400 for an unsupported charset.encodingtobuffer()now calltext().pnpm test,type-checkandeslint-checkpass.