note 105439 added to mongo.security
| From: | LudovicVignals at osu1 dot php dot net | Date: | Thu, 18 Aug 2011 04:29:40 +0000 |
| Subject: | note 105439 added to mongo.security | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-182555@lists.php.net to get a copy of this message | ||
I am looking at ways to prevent Request Injection Attacks and Script Injection Attacks while
minimizing the overhead at the same time.
I wrote this general purpose script to sanitize all form parameters received via an http request
while still being able to take advantage of nested arrays sometime passed in form parameters. It is
a bit of a brute force approach right now but since it is sometime needed to store data from HTTP
GET/POST without knowing the structure of data ahead of time I thought this would come handy. It may
not be bullet proof as is so please post a comment here if you find a hole in this sanitize function
approach or if you have improvement suggestions. Thanks for your feedback!
// Function to sanitize
function sanitize(&$httpreq) { // Use of reference allows for in place update
foreach ($httpreq as $key => &$value) { // Use of reference again
if (is_array($value)) {
sanitize($value); //recursion ensure that nested arrays get sanitized too
}
if (is_string($value)) {
$value = strip_tags($value);
$value = str_replace("$", " $ ", $value);
$value = str_replace("->", " - > ", $value);
$value = str_replace("[", " [ ", $value);
$value = str_replace("]", " ] ", $value);
$value = str_replace(")", " ) ", $value);
$value = str_replace(")", " ( ", $value);
$value = str_replace("\\", " \\ ", $value);
}
if (!is_array($value) && !is_string($value)) die("ERROR: UNEXPECTED VALUE! HTTP
FORM REQUEST ARE EXPECTED TO CONTAIN STRINGS AND/OR ARRAYS");
}
unset($value);
// break the reference with the last element.
// Reference of a $value and the last array element remain even after the foreach loop.
// It is recommended to destroy it by unset().
}
// Sample use
var_dump($_POST); //before
sanitize($_POST);
var_dump($_POST); //after
----
Server IP: 69.147.83.197
Probable Submitter: 148.87.19.214
----
Manual Page -- http://www.php.net/manual/en/mongo.security.php
Edit -- https://master.php.net/note/edit/105439
Del: integrated -- https://master.php.net/note/delete/105439/integrated
Del: useless -- https://master.php.net/note/delete/105439/useless
Del: bad code -- https://master.php.net/note/delete/105439/bad+code
Del: spam -- https://master.php.net/note/delete/105439/spam
Del: non-english -- https://master.php.net/note/delete/105439/non-english
Del: in docs -- https://master.php.net/note/delete/105439/in+docs
Del: other reasons-- https://master.php.net/note/delete/105439
Reject -- https://master.php.net/note/reject/105439
Search -- https://master.php.net/manage/user-notes.php