Filter Input
One of the most fundamental things to understand when developing a secure site is this: all information not generated within the application itself is potentially tainted. This includes data from forms, files, and databases.
When data is described as being tainted, this doesn’t mean it’s necessarily malicious. It means it might be malicious. You can’t trust the source, so you should inspect it to make sure it’s valid. This inspection process is called filtering, and you only want to allow valid data to enter your application.
There are a few best practices regarding the filtering process:
Use a whitelist approach. This means you err on the side of caution and assume data to be invalid unless you can prove it to be valid.
Never correct invalid data. History has proven that attempts to correct invalid data often result in security vulnerabilities due to errors.
Use a naming convention to help distinguish between filtered and tainted data. Filtering is useless if you can’t reliably determine whether something has been filtered.
In order to solidify these concepts, consider a simple HTML form allowing a user to select among three colors:
<formaction="process.php"method="POST"><p>Please select a color:<selectname="color"><optionvalue="red">red</option><optionvalue="green">green</option><optionvalue="blue">blue</option></select><inputtype="submit"/></p></form>
It’s easy to appreciate the desire to trust $_POST['color'] in process.php. After all, the form seemingly ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access