Home

Security of PHP scripts

Security of PHP scripts: Part III

This column will round off our three-page series on security PHP scripting by discussing three important issues: the safety of file uploads, issues surrounding error reporting, and PHP safe mode.

File uploads

PHP allows you to handle file uploads from users easily. The following script implements a basic upload form.

<html>
<body>
<?
if(!isset($file)) {
?>
<form method=post enctype="multipart/form-data" action="<? echo  $PHP_SELF; ?>"?>
Upload a file: <input type=file name=file /> <br />
<input type=submit value="Upload" />
</form>
<?
} else {
  if(is_uploaded_file($file)) {
    if($file_size > 1000000) {
      echo "The uploaded file is too large";
      } else {
        move_uploaded_file($userfile, "/usr/local/apache/uploads/");
        echo "Your file was successfully uploaded";
    }
  } else {
    echo "Invalid upload. Try again with a valid file upload";
  }
}
?>
</body>
</html>

The first part of the script tests to see if the file, $file, was uploaded. If not, it presents the user with the upload form. Notice that the <form> element has an attribute 'enctype' set to 'multipart/form-data'. This is necessary when allowing file uploads.

The example script tests for an important security threat: whether or not the file named in $file is an uploaded file. When PHP accepts a file upload, it saves the uploaded file in a temporary directory and provides the script with its filename in the variable set in the script (in our example, $file). A malicious user could manipulate this situation. Consider an upload script that contained the code:

if(isset($file)) {
move($file,"/usr/local/apache/uploads/");
/* ... */ 
}

What if the user did not actually upload a file but rather passed a filename as follows: http://www.myhost.com/upload.php?file=../bin/httpd? The script would effectively run: move("../bin/httpd","/usr/local/apache/uploads").Given that most people install their scripts in /usr/local/apache/htdocs, this would mean that PHP would move the location of the Apache binary (httpd) to another directory. If the server was to be stopped and restarted, the restart would fail since the Apache binary would not exist where expected. This would cause a complete denial of service.

To safeguard against this, you can also access upload file variables, such as $file, through the global array $HTTP_POST_FILES["file"].

The script tests for another security problem: the size of the file, $file_size. If this test did not take place, a user could upload files of any size and intentionally exhaust the server's disk space.

Security of error reporting

When developing a script, PHP's error warnings can be very useful in diagnosing bugs. However, once you publicly deploy your script it serves no purpose, and could expose details of the internals of your script to users. As such, it is always best to redirect errors to a log file. This can easily be done by setting the `error_log' directive in the PHP configuration file, php.ini.

Safe mode

Safe mode supplements the operating system's permission system by further isolating PHP from the rest of the file system. To turn on safe mode, set the 'safe_mode' directive in php.ini to 'On'.

In safe mode, PHP can only access files owned by the Web server user - usually called 'nobody'. This means a malicious user would not be able to manipulate a script to read system configuration and other important files.

Further still, scripts can be prohibited from using risky functions - such as those involved in file system manipulation, execution of external programs or any other kind of function - by explicitly disabling such functions using the 'disable_functions' directive. A comma-separated list of function names will ensure that they can never be used. An error such as the following will be generated: "Warning: system() has been disabled for security reasons. in safe.php on line 401".

Finally, whenever you deploy PHP on a public system always be sure to keep up with new releases of PHP to ensure that you are protected against any security problems with older releases.

Keep up with the latest tech news, reviews and previews by subscribing to the PC World newsletter.

Gavin Sherry

PC World
Comments are now closed.

Best Deals on PCWorld

Printers & ScannersView all »
NotebooksView all »
TabletsView all »
Mobile PhonesView all »
Networking, Wireless & VoIPView all »