User Submitted Data
The greatest weakness in many PHP programs is not inherent in the
language itself, but merely an issue of code not being written with
security in mind. For this reason, you should always take the time
to consider the implications of a given piece of code, to ascertain
the possible damage if an unexpected variable is submitted to it.
Example #1 Dangerous Variable Usage
<?php
// remove a file from the user's home directory... or maybe
// somebody else's?
unlink ($evil_var);
// Write logging of their access... or maybe an /etc/passwd entry?
fwrite ($fp, $evil_var);
// Execute something trivial.. or rm -rf *?
system ($evil_var);
exec ($evil_var);
?>
You should always carefully examine your code to make sure that any
variables being submitted from a web browser are being properly
checked, and ask yourself the following questions:
-
Will this script only affect the intended files?
-
Can unusual or undesirable data be acted upon?
-
Can this script be used in unintended ways?
-
Can this be used in conjunction with other scripts in a negative
manner?
-
Will any transactions be adequately logged?
By adequately asking these questions while writing the script,
rather than later, you prevent an unfortunate re-write when you
need to increase your security. By starting out with this mindset,
you won't guarantee the security of your system, but you can help
improve it.
Improve security by disabling convenience settings that obscure input
data's origin, validity, or integrity. Implicit variable creation and
unchecked input can lead to vulnerabilities like injection attacks and
data manipulation.
Features like register_globals and
magic_quotes (both removed in PHP 5.4.0) once contributed
to these risks by automatically creating variables from user input and
escaping data inconsistently. While no longer in PHP, similar risks persist
if input handling is mismanaged.
Enable error_reporting(E_ALL) to
help detect uninitialized variables and validate input. Use strict types
(declare(strict_types=1),
introduced in PHP 7) to enforce type safety, prevent unintended type conversions,
and improving overall security.
Uli Kusterer ¶20 years ago
One thing I would repeat in the docs here is what information actually comes from the user. Many people think a Cookie, since it's written by PHP, was safe. But the fact is that it's stored on the user's computer, transferred by the user's browser, and thus very easy to manipulate.
So, it'd be handy to mention here again that:
CGI parameters in the URL, HTTP POST data and cookie variables are considered "user data" and thus need to be validated. Session data and SQL database contents only need to be validated if they came from untrustworthy sources (like the ones just mentioned).
Not new, but I would have expected this info under this headline, at least as a short recap plus linlk to the actual docs.
Livingstone@stonyhills[dot]com ¶18 years ago
making sure your form is submitted from your page! Could also be adapted to url, by additing &token to the query string and checking this against session data(or what ever array you like) with $_GET, not that this string is randomly generated and stored. If you like you could build your own array to store the generated string if you dont want to use $_SESSION, say you could make yours like $tokens = array(), and in your easysecure class you store all the stuff in that array!
<?php
class easysecure {
var $curr_user;
var $curr_permission;
var $curr_task;
var $validpermission;
var $error;
function &setVar( $name, $value=null ) {
if (!is_null( $value )) {
$this->$name = $value;
}
return $this->$name;
}
function maketoken($formname, $id){
$token = md5(uniqid(rand(), true));
$_SESSION[$formname.$id] = $token;
return $token;
}
function checktoken($token, $formname, $id){
if(!$token){
$this->setVar('validpermission', 0);
$this->setVar('error', 'no token found, security bridgedetected');
return false;
}
$key = $_SESSION[$formname.$id];
if($key !== $token ){
$this->setVar('validpermission', 0);
$this->setVar('error', 'invalid token');
return false;
}
if($this->validpermission !==1){
echo 'invalid Permissions to run this script';
return false;
}else{
return true;
}
}
}
?>
<?php $userid = *** ?>
<form name="newform" action="index.php" method="post">
<input type="text" name="potentialeveilfield" value="" size 30 />
<input type="hidden" name="token" value="<?php echo maketoken(newform, $userid); ?>" />
<input type="submit" />
</form>
Now when processing the form... check the value of your token
<?php
if(!checktoken($_POST['token'], 'newform', $userid))
{ exit(); }
?>