Version
v16.11.1
Platform
Darwin Thoms-iMac.lan 17.7.0 Darwin Kernel Version 17.7.0: Fri Oct 30 13:34:27 PDT 2020; root:xnu-4570.71.82.8~1/RELEASE_X86_64 x86_64 i386 iMac18,3 Darwin
Subsystem
No response
What steps will reproduce the bug?
How often does it reproduce? Is there a required condition?
Always.
What is the expected behavior?
Throws SyntaxError.
What do you see instead?
Evaluates to 'N'.
Additional information
(Copied from https://bugs.chromium.org/p/chromium/issues/detail?id=1292075. Reporting it here because it's relevant to Node. Feel free to close if it's not appropriate to have here.)
The JSON spec (ECMA 404, section 9) is clear that a backslash in a string must be part of a valid escape sequence. \N is not a valid escape sequence, so the example input is not a valid JSON text.
The ECMAScript spec (section 25.5.1) defines the behavior of JSON.parse. Step 2 of the algorithm requires that the implementation “[t]hrow a SyntaxError exception if [the input] is not a valid JSON text as defined in” ECMA-404.
However, it appears that when JSON.parse encounters an invalid escape sequence in a string, it ignores the backslash rather than throwing a SyntaxError. This is contrary to the spec.
This bug could lead to interoperability problems. In the first place, a text accepted by node may be rejected as invalid by another JSON implementation, leading to unexpected bugs.
In the second place, there are interoperability concerns with languages that are supersets of JSON. For instance, YAML 1.2 is a superset of JSON: any valid JSON document is also a valid YAML 1.2 document with identical semantics. However, YAML 1.2 supports additional escape sequences in double-quoted strings, including \N. The example document is valid YAML 1.2, and not valid JSON, but node's JSON.parse will accept it as JSON with different semantics.
Version
v16.11.1
Platform
Darwin Thoms-iMac.lan 17.7.0 Darwin Kernel Version 17.7.0: Fri Oct 30 13:34:27 PDT 2020; root:xnu-4570.71.82.8~1/RELEASE_X86_64 x86_64 i386 iMac18,3 Darwin
Subsystem
No response
What steps will reproduce the bug?
How often does it reproduce? Is there a required condition?
Always.
What is the expected behavior?
Throws
SyntaxError.What do you see instead?
Evaluates to
'N'.Additional information
(Copied from https://bugs.chromium.org/p/chromium/issues/detail?id=1292075. Reporting it here because it's relevant to Node. Feel free to close if it's not appropriate to have here.)
The JSON spec (ECMA 404, section 9) is clear that a backslash in a string must be part of a valid escape sequence.
\Nis not a valid escape sequence, so the example input is not a valid JSON text.The ECMAScript spec (section 25.5.1) defines the behavior of JSON.parse. Step 2 of the algorithm requires that the implementation “[t]hrow a SyntaxError exception if [the input] is not a valid JSON text as defined in” ECMA-404.
However, it appears that when JSON.parse encounters an invalid escape sequence in a string, it ignores the backslash rather than throwing a SyntaxError. This is contrary to the spec.
This bug could lead to interoperability problems. In the first place, a text accepted by node may be rejected as invalid by another JSON implementation, leading to unexpected bugs.
In the second place, there are interoperability concerns with languages that are supersets of JSON. For instance, YAML 1.2 is a superset of JSON: any valid JSON document is also a valid YAML 1.2 document with identical semantics. However, YAML 1.2 supports additional escape sequences in double-quoted strings, including
\N. The example document is valid YAML 1.2, and not valid JSON, but node's JSON.parse will accept it as JSON with different semantics.