Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON.parse vs. eval()

My Spider Sense warns me that using eval() to parse incoming JSON is a bad idea. I'm just wondering if JSON.parse() - which I assume is a part of JavaScript and not a browser-specific function - is more secure.

like image 707
Kevin Major Avatar asked Dec 03 '09 22:12

Kevin Major


People also ask

Why is JSON parse () more secure than eval ()?

parse() is safer to use because the eval() function will execute js where json. parse() will only process valid JSON string representations into a JavaScript value or JSON object. json. parse() will throw an error if invalid JSON strings are passed to it.

Why JSON eval is not recommended for use?

Malicious code : invoking eval can crash a computer. For example: if you use eval server-side and a mischievous user decides to use an infinite loop as their username. Terribly slow : the JavaScript language is designed to use the full gamut of JavaScript types (numbers, functions, objects, etc)… Not just strings!

What does eval () method do in JSON?

The eval() function in JavaScript is used to take an expression and return the string. As a result, it can be used to convert the string into JSON.

Can JSON be parsed using eval () procedure in JavaScript?

Using eval()The eval() function can interpret and execute any JavaScript. This represents a potential security problem. Try to avoid it. It is safer to use a JSON parser to convert a JSON text to a JavaScript object.


1 Answers

You are more vulnerable to attacks if using eval: JSON is a subset of Javascript and json.parse just parses JSON whereas eval would leave the door open to all JS expressions.

like image 118
jldupont Avatar answered Sep 28 '22 02:09

jldupont