A web security story from 2008: silently securing JSON.parse
The JSON parsing library json2.js used JavaScript's eval to parse JSON text, and its regular expression checks did not account for Unicode format-control characters that JavaScript strips before tokenisation. An attacker could craft a JSON string containing such a character to make eval execute arbitrary JavaScript code.
Published on dev.to
My 8 year old is doing a report for school on cyber security so I thought I'd dig up an old report for a break-the-web level security vulnerability that we silently fixed and which, as far as I know, has never been disclosed.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the JSON validation regex did not reject Unicode format-control characters that JavaScript removes before parsing, causing a mismatch between what the regex saw and what eval executed.
Impact
An attacker could execute arbitrary JavaScript code in the context of any application using json2.js to parse untrusted JSON.
What the attacker needed
Application uses json2.js to parse untrusted JSON; attacker can supply JSON input
The fix
The library was fixed by changing the regular expression to only match a backslash followed by a printable ASCII character, which rejects the malicious input.
How it was found
- Create a JSON string containing a Unicode zero-width joiner between a backslash and a quote.
- Pass the string to JSON.parse from json2.js.
- The regex checks accept the string as valid JSON.
- eval executes the string, treating the control character as removed and running the injected code.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
JSON is a way to format data as text. The json2.js library turned that text into JavaScript objects using eval, which runs any JavaScript code it is given. The library tried to check the text was safe JSON first, but a hidden Unicode character could trick the check into allowing code that eval would then run.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.