Is json_encode Sufficient XSS Protection?

前端 未结 5 489
挽巷
挽巷 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:24

    Seems as through the best answer to this question lies in another question.

    To sum up, PHP's JSON encoder escapes all non ASCII characters, so newlines/carriage returns can't be inserted to bollacks up the Javascript string portion of the JSON property. This may not be true of other JSON encoders.

    However, passing in a raw string to JSON encode can lead to the usual litany of XSS attacks, the following combination of constants is suggested.

    var v= ;
    

    or ensure the variable passed to json_encode is really an object.

提交回复
热议问题