------------------------------------------------------------------------ - Validate input - Guard your file system - Guard your database - Guard your session data - Guard against Cross-Site Scripting (XSS) vulnerabilities - Verify form posts - Protect against Cross-Site Request Forgeries (CSRF) ------------------------------------------------------------------------ - Validate input Validating data is the most important habit you can possibly adopt when it comes to security. And when it comes to input, it's simple: Don't trust users. Your users are probably good people, and most are likely to use your application exactly as you intended. However, whenever there is chance for input, there is also chance for really, really bad input. As an application developer, you must guard your application against bad input. Carefully considering where your user input is going and what it should be will allow you to build a robust, secure application. Although file system and database interaction are covered later, there are general validation tips that cover every sort of validation: Use white-listed values Always revalidate limited selections Use built-in escape functions Validate for correct data types, like numbers White-listed values are values that are valid, as opposed to black-listed values that are invalid. The distinction is that often when doing validation, the list or range of possible values is smaller than the list of invalid values, many of which can be unknown or unexpected. When you're doing validation, remember that it's often easier to conceptualize and validate what the application allows instead of trying to guard against all the unknown values. For instance, to limit values in a field to all numbers, write a routine that makes sure the input is all numbers. Don't write the routine to search for non-numerical values and mark it as invalid if any are found. - Guard your file system In July 2000, a Web site leaked customer data that was found in files on a Web server. A visitor to the Web site manipulated the URL to view files containing the data. Although the files were erroneously placed, this example underscores the importance of guarding your file system against attackers. If your PHP application does anything with files and has variable data that a user can enter, be careful that you scrub the user input to make sure users can't do anything with the file system that you don't want them to do. Listing 1 shows an example of a PHP site that downloads an image given a name. Listing 1. Downloading a file
<"); echo("title>Guard your filesystem"); echo("