- The earliest place to break out, is, when the incoming GET or POST data is interpreted.
Might that be directly,
eval($_GET['foo']);
, or, indirect, after an assignment $a = $_GET['foo']; eval($a);
.
- The assignment operator
"="
itself doesn't trigger an execution or interpretation of the assigned content. You might consider it being safe.
- You might think of
"="
as "equal to". Don't do that.
By using the assignment operator the left operand gets set to the value of the expression on the right (not "equal to", but "gets set to").
$a = $_GET['a']; $a();
this is actually the earliest place of an exec i can think of
Ok, i should stop being a nitpicker.
My suggestions are:
Workflow
- GET/POST = untrusted variable content
- validate, sanitize
- use in prepared statement
- store in db
- get form db
- escape
- display
Do not trust incoming data. Validate it.
Use $_dirty['foo'] = $_GET['foo']
and then $foo = validate_foo($_dirty['foo']);
Use PHP's filter_input(), filter_var() or your own validation functions.
You can also rely on PHP filter_input()
function, instead of writing your own validate_foo()
logic. Read http://www.php.net/manual/en/function.filter-input.php
$search = filter_input(INPUT_GET, 'search', FILTER_SANITIZE_SPECIAL_CHARS);
Example
The example validates an incoming $_GET['id'].
The value should only be considered valid, if it is an integer and in a certain range.
$range = array('options'=>array('default'=>1, 'min_range'=>0, 'max_range'=>10));
$id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT, $range);
Database: use PDO & preparedStatements & bindValue
http://www.php.net/manual/en/pdostatement.bindvalue.php