05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 12 CHAPTER 12<br />

Security<br />

<strong>PHP</strong> is a flexible language that has hooks into just about every API offered on the<br />

machines on which it runs.Because it was designed to be a forms-processing language<br />

for HTML pages, <strong>PHP</strong> makes it easy to use form data sent to a script.Convenience<br />

is a double-edged sword, however.The very features that let you quickly write<br />

programs in <strong>PHP</strong> can open doors for those who would break into your systems.<br />

It’s important to understand that <strong>PHP</strong> itself is neither secure nor insecure.The security<br />

of your web applications is entirely determined by the code you write.For example,<br />

take a script that opens a file whose name was passed as a form parameter.If<br />

you don’t check the filename, the user can give a URL, an absolute pathname, or<br />

even a relative path to back out of the application data directory and into a personal<br />

or system directory.<br />

This chapter looks at several common issues that can lead to insecure scripts, such as<br />

filenames, file uploads, and the eval( ) function.Some problems are solved through<br />

code (e.g., checking filenames before opening them), while others are solved through<br />

changing <strong>PHP</strong>’s configuration (e.g., to permit access only to files in a particular<br />

directory).<br />

Global Variables and Form Data<br />

One of the most fundamental things to consider when creating a secure system is<br />

that any information you didn’t generate within the system should be regarded as<br />

tainted.You should either untaint this data before using it—that is, ensure that<br />

there’s nothing malicious in it—or limit what you do with it.<br />

In <strong>PHP</strong>, however, it’s not always easy to tell whether a variable is tainted.When<br />

register_globals is enabled in the php.ini file, <strong>PHP</strong> automatically creates variables<br />

from form parameters and cookies.Poorly written programs assume that their variables<br />

have values only when the variables are explicitly assigned values in the program<br />

code. With register_globals, this assumption is false.<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.<br />

285

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!