Here is a quick example of something I was wondering about, before we start I am aware that eval should only be used when absolutely needed.
Let's say I have an endpoint that runs some code like this:
endpoint.php
<?php
$input = sanitised($_POST['someData']);
$array = someDatabaseQueryMethod($input);
echo 'runtime.getItem("'.A_SAFE_DEFINED_CONSTANT.'").stateChange({"newValues":'.json_encode($array).'});';
?>
then I have an index.php that looks like this:
... ommitted...
<body>
$.ajax({
url : "./endpoint.php",
type: "POST",
data : {someData: 1},
success: function(data, textStatus, jqXHR)
{
eval(data);
},
error: function (jqXHR, textStatus, errorThrown)
{
//error logic here
}
});
...
Is there a situation that can occur where some content in $array (which, lets say, could contain anything at all, multi dimensional, loads of different strings / other data types, but will always be a valid array that won't cause json_encode to fail) could mean that the eval statement could be vulnerable to some kind of injection?
Effectively I always want .stateChange to recieve an object that it can take a look at and decide what to do in this example
I know this might seem like quite a convoluted example, it is taken out of context - this is the smallest verifiable example i could come up with.
EDIT: while the above is closes to what I am doing, i guess the smallest example would actually be this:
endpoint.php
<?php
$input = sanitised($_POST['someData']);
$array = someDatabaseQueryMethod($input);
echo 'var a = '.json_encode($array).';';
?>
OK guys i get it - no need for more comments that do not answer the question which is not about different methods of doing the same thing but thanks for your feedback
It would be great to get an example of where this would break, not hearsay about how bad eval is.
evalthe data? Just tell jQuery you're expecting a JSON response and it'll parse it as JSON.