note 58832 added to security.filesystem
| From: | anonymous at osu1 dot php dot net | Date: | Thu, 17 Nov 2005 10:58:52 +0000 |
| Subject: | note 58832 added to security.filesystem | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-98698@lists.php.net to get a copy of this message | ||
(A) Better not to create files or folders with user-supplied names. If you do not validate enough,
you can have trouble. Instead create files and folders with randomly generated names like fg3754jk3h
and store the username and this file or folder name in a table named, say, user_objects. This will
ensure that whatever the user may type, the command going to the shell will contain values from a
specific set only and no mischief can be done.
(B) The same applies to commands executed based on an operation that the user chooses. Better not to
allow any part of the user's input to go to the command that you will execute. Instead, keep a
fixed set of commands and based on what the user has input, and run those only.
For example,
(A) Keep a table named, say, user_objects with values like:
username|chosen_name |actual_name|file_or_dir
--------|--------------|-----------|-----------
jdoe |trekphotos |m5fg767h67 |D
jdoe |notes.txt |nm4b6jh756 |F
tim1997 |_imp_ folder |45jkh64j56 |D
and always use the actual_name in the filesystem operations rather than the user supplied names.
(B)
<?php
$op = $_POST['op'];//after a lot of validations
$dir = $_POST['dirname'];//after a lot of validations or maybe you can use technique (A)
switch($op){
case "cd":
chdir($dir);
break;
case "rd":
rmdir($dir);
break;
.....
default:
mail("webmaster@example.com", "Mischief",
$_SERVER['REMOTE_ADDR']." is probably attempting an attack.");
}
----
Manual Page -- http://www.php.net/manual/en/security.filesystem.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+58832
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58832&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58832&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58832&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58832&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58832&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58832&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58832&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+58832&report=yes
Search -- http://master.php.net/manage/user-notes.php