Is json_encode Sufficient XSS Protection?

前端 未结 5 408
挽巷
挽巷 2020-12-25 13:49

I have a stdClass object in PHP, something like

$o = new stdClass;
$o->foo = $bar

The variable $bar contains a

5条回答
  •  时光说笑
    2020-12-25 14:32

    As other answers have said; json_encode is not built for anti-xss protections. Unless you specifically encode the unsafe string (or sanitize properly) you're going to have a potential issue.

    Furthermore, once that string is extracted from the JSON object, it is still potentially hazardous if injected into the page at any point. For example:

    foo = ""; ?>
    var v = 
    

    isn't likely to execute (although you can't be certain). But if you were to do:

    $('#some-element').html(v.foo);
    

    you would absolutely encounter a vulnerability.

提交回复
热议问题