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.

Join the PC World newsletter!

Error: Please check your email address.

Our Back to Business guide highlights the best products for you to boost your productivity at home, on the road, at the office, or in the classroom.

Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World
Show Comments

Essentials

Lexar® JumpDrive® S57 USB 3.0 flash drive

Learn more >

Microsoft L5V-00027 Sculpt Ergonomic Keyboard Desktop

Learn more >

Mobile

Lexar® JumpDrive® S45 USB 3.0 flash drive 

Learn more >

Exec

Audio-Technica ATH-ANC70 Noise Cancelling Headphones

Learn more >

Lexar® Professional 1800x microSDHC™/microSDXC™ UHS-II cards 

Learn more >

Lexar® JumpDrive® C20c USB Type-C flash drive 

Learn more >

HD Pan/Tilt Wi-Fi Camera with Night Vision NC450

Learn more >

Budget

Back To Business Guide

Click for more ›

Most Popular Reviews

Latest News Articles

Resources

PCW Evaluation Team

Azadeh Williams

HP OfficeJet Pro 8730

A smarter way to print for busy small business owners, combining speedy printing with scanning and copying, making it easier to produce high quality documents and images at a touch of a button.

Andrew Grant

HP OfficeJet Pro 8730

I've had a multifunction printer in the office going on 10 years now. It was a neat bit of kit back in the day -- print, copy, scan, fax -- when printing over WiFi felt a bit like magic. It’s seen better days though and an upgrade’s well overdue. This HP OfficeJet Pro 8730 looks like it ticks all the same boxes: print, copy, scan, and fax. (Really? Does anyone fax anything any more? I guess it's good to know the facility’s there, just in case.) Printing over WiFi is more-or- less standard these days.

Ed Dawson

HP OfficeJet Pro 8730

As a freelance writer who is always on the go, I like my technology to be both efficient and effective so I can do my job well. The HP OfficeJet Pro 8730 Inkjet Printer ticks all the boxes in terms of form factor, performance and user interface.

Michael Hargreaves

Windows 10 for Business / Dell XPS 13

I’d happily recommend this touchscreen laptop and Windows 10 as a great way to get serious work done at a desk or on the road.

Aysha Strobbe

Windows 10 / HP Spectre x360

Ultimately, I think the Windows 10 environment is excellent for me as it caters for so many different uses. The inclusion of the Xbox app is also great for when you need some downtime too!

Mark Escubio

Windows 10 / Lenovo Yoga 910

For me, the Xbox Play Anywhere is a great new feature as it allows you to play your current Xbox games with higher resolutions and better graphics without forking out extra cash for another copy. Although available titles are still scarce, but I’m sure it will grow in time.

Featured Content

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?