I was looking for security issues when using PHP json_encode inside a javascript context, but I am not completely satisfied with the results
I got a lot of warnings about this, but they always assume that I am going to enter data from the json_encode object directly into HTML without any disinfection at all
So, I want to know if this and only this small fragment represents any security problems (for example, xss attacks, etc.)
<?php
$obj = isset($_POST['js']) ? json_encode($_POST['js']) : false;
if ($obj === false) $obj = '{}';
?>
<script>var x = <?php echo $obj ?>;</script>
EDIT:
Changed fragment to handle json_encode returning false
source
share