tags:

views:

71

answers:

3

Is escaping eval variables safe enough from security point of view. For e.g.

$path = "a";    //sample value; is generated dynamically
$var = "phpinfo()";     //sample attack value; is generated dynamically
eval("\$struct$path = \$var;");

this seems to be working safely to me. Although there seems to be no reason of using the code in the first place, now that it is in, it cannot be removed without a reason.

Is there any way (any value for $var or $path) that can break this eval or is it that i am simply worrying too much :-) and this is a safe case???

+2  A: 

It depends on where $path is coming from. This value breaks it:

=0;unlink('/important/file');//
Lekensteyn
+1. excellent.. is there any case in which $var can break it?
pinaki
Not in this eval, the $ is prefixed with a \. But if $structa should be a number (e.g. an ID for MySQL), and you're entering a string like `'`, then it will be broken on that.
Lekensteyn
cool.. so i take it that this is a safe case of eval at work since it turned out $path is calculated and not from user inputs.. accepting urs as the correct answer... thanks
pinaki
A: 

You could need eval to implement a plugin system with hooks... But you should never execute dynamic content with it (like phpinfo).

Josua Schmid
A: 

I have no clue what your eval code is actually supposed to do. But if it's just about a dynamic varname how about something like that:

extract(array("\$struct$path", "\$var"));

Faster.

mario