From a security perspective, I can see simply doing an ‘eval’ on incoming JSON data as a critical mistake. If you got data like below you’d have some problems.
{ someData:((function() {
alert("i'm in ur code hackin' ur page");
})()) }
I wondered what do most popular Javascript libraries do? Is it a manual parse or simply an eval?
[Edit]
I’m not asking if I should eval/parse – I was asking what methods some of the popular Javascript libraries used (jQuery, Prototype, etc…)
Here’s what the official JavaScript parser does:
With the exception of the built-in JSON parsing support that is in modern browsers, this is what all (library-based) secure JSON parsers do (ie, a regex test before
eval).Secure libraries (in addition to the official json2 implementation)
Prototype’s
isJSONfunction.Mootools’
JSON.decodefunction (again, via a regex test beforeeval).Unsecure libraries:
dojo’s
fromJsondoes not provide secureevaling. Here is their entire implementation (minus comments):jQuery does not provide secure JSON
eval‘ing, but see the official plugin’ssecureEvalJSONfunction (line 143).