Repository navigation
fix: parse JSON data with leading whitespace - #679
Open
rida-ghreizy wants to merge 1 commit into
Open
rida-ghreizy wants to merge 1 commit into
rida-ghreizy wants to merge 1 commit into
Conversation
JSONParser wrapped any string that did not start with [, { or " in
quotes before calling JSON.parse. A JSON body starting with a newline
or spaces was therefore treated as a string and threw on the newline,
and bare strings containing quotes or line breaks threw too. Check the
first non-whitespace character and quote bare strings with
JSON.stringify.
Signed-off-by: rida <ghreizyrida1@gmail.com>
This branch has not been deployed
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.
Proposed Changes
JSONParser.parse(src/parsers.ts) wraps a string payload in quotes when it doesn't start with[,{or", so that a bare string such assome dataparses as a JSON string:Two cases go wrong:
JSON with leading whitespace. JSON allows whitespace before the value, and request bodies often start with a newline. The check sees
\ninstead of{, wraps the body in quotes, andJSON.parsethrows on the raw newline. Through the public API:Leading spaces before an object or array (
" [1, 2]") didn't throw, but the value came back as the string" [1, 2]"instead of the array.Bare strings with quotes or line breaks.
`"${payload}"`doesn't escape anything, soShe said "hi"orline 1\nline 2became invalid JSON and threw.This PR:
payload.trimStart(), so leading whitespace no longer hides an object, array or quoted string;JSON.stringify(payload), soJSON.parsereturns exactly the original string, quotes and line breaks included.Other inputs behave as before. Bare strings without special characters still come back unchanged, and well-formed objects and arrays are parsed. The base64 decorator path still gets a quoted base64 string, as before.
Description
Tests added to
test/integration/parser_test.ts: strings containing double quotes, strings containing line breaks, an object with a leading newline and spaces, an array with a leading space. Without the change these 4 fail. With it, the full suite passes (npx mocha --require ts-node/register ./test/integration/**/*.ts: 238 passing), and ESLint is clean on the changed files.Relation to #676
#676 (open) rewrites
JSONParser.parsefor #675. Because it triesJSON.parsefirst, it also fixes the leading-whitespace case. Its fallback still quotes with`"${text}"`, so bare strings containing"or line breaks would still throw there. This PR is the smaller change for these two cases. If #676 is merged first, theJSON.stringifyfallback and the new tests can be carried over to it, and this PR can be closed.