Skip to content

Fix uncaught exception in json node for malformed JSON string with schema + action=str - #5905

Merged
dceejay merged 1 commit into
node-red:mainfrom
karthikchundi-commits:fix/json-node-uncaught-exception-malformed-string-schema
Aug 27, 2026
Merged

dceejay merged 1 commit into
node-red:mainfrom
karthikchundi-commits:fix/json-node-uncaught-exception-malformed-string-schema

Conversation

@karthikchundi-commits

Copy link
Copy Markdown
Contributor

Summary

In 70-JSON.js, when node.action === "str" and the incoming msg[node.property] value is already a string, the node still needs to JSON.parse() it to run schema validation. Every other JSON.parse() call in this file is wrapped in a try/catch that calls done(e.message) on failure - this one branch (lines ~76-90 on main) is not.

Impact

A malformed JSON string reaching this exact combination (action: "str", value already a string, msg.schema set) throws straight out of the input handler instead of going through the node's own done(err) completion contract.

The runtime's outer safety net (Node.prototype._emitInput's own try/catch) does catch this and log it via node.error(), so it doesn't crash the process or take down other flows - but it means _complete() is never invoked for that message, so the onComplete hook and the "done" metric that every other error path in this node correctly triggers get silently skipped for this one path.

Verification

I bypassed the test harness/runtime wrapper and called the node's raw input handler directly with a malformed JSON string in this exact combination:

  • Before fix: handler throws (doneCalled: false, exception message: Expected double-quoted property name in JSON...)
  • After fix: handler calls done(e.message) correctly (doneCalled: true, nothing thrown)

Also ran the full existing 70-JSON_spec.js suite (27 tests, all passing with the fix) and added a new regression test for this specific combination (malformed JSON string + schema + action: "str"), following the existing test file's conventions.

Fix

Wraps the JSON.parse() call in that one branch in the same try { ... } catch(e) { done(e.message); } pattern the three sibling branches in this same function already use - no new pattern introduced, just applying the existing correct one consistently.

Checklist

  • I have raised an issue to propose this change (https://github.com/node-red/node-red - N/A, this is a small, self-contained bug fix with a test, per CONTRIBUTING.md's guidance that bug-fixes with tests are welcome to be raised directly)
  • My submission is targeted at main (bug fix)
  • I have tested this against a locally running instance of Node-RED (ran the existing + new mocha test suite directly, and verified the fix at the raw handler level, bypassing the runtime's outer exception handling, to precisely confirm the fix's effect)

Disclosure: this fix was implemented with AI assistance (Claude), under my direction and review.

…hema + action=str

When the json node's action is "str" (convert to string) and the incoming
value is already a string, the node still needs to JSON.parse() it to run
schema validation - but unlike every other parse in this file, that call
wasn't wrapped in a try/catch. A malformed JSON string in this specific
combination (action=str, value already a string, schema set) threw an
exception straight out of the "input" handler instead of going through the
node's own done(err) completion contract.

The runtime's outer safety net (Node.prototype._emitInput) does catch this
and log it, so it doesn't crash the process - but it means _complete() is
never invoked for that message, skipping the onComplete hook and the
"done" metric that every other error path in this node correctly triggers.

Verified directly (bypassing the runtime's outer catch) that the unfixed
handler throws with doneCalled=false, and the fixed handler correctly
calls done(err) with doneCalled=true, for the same malformed input.

Wraps the JSON.parse() in that branch in the same try/catch + done(e.message)
pattern already used by the three sibling branches in this file.

Signed-off-by: Karth <[email protected]>
@linux-foundation-easycla

linux-foundation-easycla Bot commented Aug 26, 2026 •

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: karthikchundi-commits / name: Karth (3465297)

@dceejay

dceejay commented Aug 27, 2026

Copy link
Copy Markdown
Member

Thanks, good "catch" :-)

@dceejay
dceejay merged commit dcceadd into node-red:main Aug 27, 2026
3 checks passed
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.

2 participants