Securing this process is a two-way procedure:
- ensuring the input meets some criteria (especially on maximum types)
- ensuring the input cannot leak and change the process itself
Let's say I'm passing a number to a program...
$num = $_GET['num']; // get the input
$num = (int)$_GET['num']; // ensure it is an integer
$num = max($num, 0); // ensure it is at least 0
$num = min($num, 800); // ensure it is at most 800
$num = escapeshellarg($num); // this is overkill at this point, but you never know
exec('command '.$num);
As advised above, you can also have your own little language to do this but...
- it may still be vulnerable
- it may be overkill for a simple task
- it is just an advanced version of the filter system
Finally, there's another alternative. There are functions that accept the command and parameters as separate arguments, such as popen() (you can push command arguments through pipes). But this depends on implementation.