Skip to content

fix: parse JSON data with leading whitespace - #679

Open
rida-ghreizy wants to merge 1 commit into
cloudevents:mainfrom
rida-ghreizy:fix/json-parser-leading-whitespace
Open

rida-ghreizy wants to merge 1 commit into
cloudevents:mainfrom
rida-ghreizy:fix/json-parser-leading-whitespace

Conversation

@rida-ghreizy

@rida-ghreizy rida-ghreizy commented Nov 27, 2026 •

Copy link
Copy Markdown

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 as some data parses as a JSON string:

if (!/^[[|{|"]/.test(payload)) {
  payload = `"${payload}"`;
}

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 \n instead of {, wraps the body in quotes, and JSON.parse throws on the raw newline. Through the public API:

    HTTP.toEvent({ headers: { ...ceHeaders, "content-type": "application/json" }, body: "\n{\"order\": 1}\n" })
    // before: SyntaxError: Bad control character in string literal in JSON at position 1
    // after:  event.data = { order: 1 }

    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, so She said "hi" or line 1\nline 2 became invalid JSON and threw.

This PR:

  • runs the check on payload.trimStart(), so leading whitespace no longer hides an object, array or quoted string;
  • quotes bare strings with JSON.stringify(payload), so JSON.parse returns 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.parse for #675. Because it tries JSON.parse first, 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, the JSON.stringify fallback and the new tests can be carried over to it, and this PR can be closed.

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant