Eval is evil… So what should I use instead?

前端 未结 9 1137
忘掉有多难
忘掉有多难 2020-11-27 17:27

An ajax request returns me a standard JSON array filled with my user\'s inputs. The input has been sanitized, and using the eval() function, I can easily create my javascrip

9条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-27 18:05

    Is there a standard, proven-secure way of doing this?

    There is a proposed standard way of doing this, in the forthcoming ECMAScript 3.1 version of JavaScript: JSON.parse.

    It will be supported in IE8, Firefox 3.1/3.5 and most likely the other popular browsers in the future. In the meantime, you can fall back to, or use exclusively, eval(). Evil it may or may not be; certainly it will be slower than JSON.parse. But that's the usual way to parse JSON today.

    If an attacker is able to inject malcious JavaScript into content you are spitting out via JSON, you have bigger problems to worry about than eval-is-evil.

提交回复
热议问题