Is JSON.parse() really safer than eval() when web page and ajax call come from same server?

后端 未结 4 1188
无人及你
无人及你 2021-02-05 14:43

I get that JSON.parse() prevents an attacker from injecting javascript into the response since a JSON parser is just a text parser, not a script parser so please don\'t close th

4条回答
  •  故里飘歌
    2021-02-05 15:20

    That is a very good point. The only thing I can think of is that JSON.parse would have opportunity to be faster than eval.

    A much less likely advantage is if the browser already has the HTML/JavaScript cached and the server uses Cache-Control to say that it does not need to reload. If that happens then of course a person intercepting would not have a chance to modify the page. But that is a very rare set of circumstances. Chances are, you are going to require the browser to check for a newer version of the HTML/JavaScript which is the default behavior.

    As for the security difference, I think you are correct.

    As for myself, I work with HTTPS confirmed systems only. But I have a function that uses JSON.parse if available and falls back on eval just for the speed improvement.

提交回复
热议问题