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

后端 未结 4 1183
无人及你
无人及你 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条回答
  •  闹比i
    闹比i (楼主)
    2021-02-05 15:24

    Yes, it is really safer. Every precaution you do not take is a set of potential exploits you don't prevent.

    An attacker might be able to have some control over your server's output without being able to change it entirely. Nobody's suggesting it's a magic bullet, but it's potentially faster and you're not creating a potential vulnerability that could come back and hurt you.

    Maybe someone running your server is having a bad day, and does something silly like constructing JSON by concatenating unsanitized user input:

    
    

    If you're using JSON.parse, the worst they can do is shove a large object into your memory. If you're using eval they can hijack everything.

提交回复
热议问题