13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

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

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

374 Chapter 16 <strong>Web</strong> Application Security<br />

For those situations where we would like to permit users to enter some HTML, such<br />

as a message board where people might like to use characters to control font, color, <strong>and</strong><br />

style (bold or italicized), we will have to actually pick our way through the strings to<br />

find those <strong>and</strong> not strip them out.<br />

Code Organization<br />

There are those who would argue that any file that is not directly accessible to the user<br />

from the Internet should not find a place in the document root of the website. For<br />

example, if the document root for our message board website is /home/httpd/messageboard/www,<br />

we should place all our include files <strong>and</strong> any other files we write for the<br />

site in some place such as /home/httpd/messageboard/code.Then, in our code, when<br />

we want to include those files, we can write:<br />

require_once('../code/user_object.php);<br />

The reasons for this degree of caution come down to what happens when a malicious<br />

user makes a request for a file that is not a .php or .html file. Many web servers will<br />

default to dumping the contents of that file to the output stream.Thus, if we were to<br />

keep user_object.php somewhere in the public document root, <strong>and</strong> the user were to<br />

request it, the user might see a full dump of our code in the web browser.This would let<br />

the user see the implementation, get at any intellectual property we might have in this<br />

file, <strong>and</strong> potentially find exploits that we might have missed.<br />

To fix this, we should be sure that the web server is configured to only allow the<br />

request of .php <strong>and</strong> .html files <strong>and</strong> that requests for other types of files should return an<br />

error from the server.<br />

Similarly, any other files, such as password files, text files, configuration files, or special<br />

directories, are likely best kept away from the public document root. Even if we think<br />

we have our web server configured properly, we might have missed something, or if, in<br />

the future, our web application is moved to a new server that is not properly configured,<br />

we might be exposed to exploitation.<br />

If we have allow_url_fopen enabled in our php.ini, then we could theoretically<br />

include or require files from remote servers.This would be another possible point of<br />

security failure in our application, <strong>and</strong> we would do well to avoid including files from<br />

separate machines, especially those over which we do not have full control.We should<br />

likewise not use user input when choosing which files to include or require, as bad input<br />

here could also cause problems.<br />

What Goes in Your Code<br />

Many of the code snippets we have shown thus far for accessing databases have included<br />

in the code the database name, username, <strong>and</strong> user password in plain text, as follows:<br />

$conn = @new mysqli("localhost", "bob", "secret", "somedb");

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

Saved successfully!

Ooh no, something went wrong!