PHP Security Checklist
The things you must do when coding
最好的油墨比最好的記憶更好 -The palest ink is better is than the best memory.
PHP Security Design Best Practices Summary
Every web application needs to address the following issues in order to meet the requirements for current web application protection.
- Architect Application Character Set
- Architect Database
- Architect Request Patterns
- Architect Input Validation
- Architect Output Escaping
- Architect Session Management
- Protect Secret Files/Include Files
- Protect Against CSRF Attacks
- Protect Against XSS Attacks
- Protect Against File System Attacks
- Architect Error Handling
OWASP Recommendations for PHP
The following list is in no particular order. It simply represents the order this author tends to think things through in the software design phase. Each element is important to the total protection of the application. Neglecting or poorly implementing any one part weakens the protection as a whole. Please make it a habit to continually refer to the OWASP PHP Cheat Sheet.
https://www.owasp.org/index.php/PHP_Security_Cheat_Sheet
Stay Updated - Many experts continually contribute the latest information as security issues evolve.
The Checklist
- Upgrade to PHP 5.4+. Version 5.2 is now officially unsupported
- Enforce UTF-8 everywhere – PHP, MySQL, Text, HTML, JavaScript, Email, URL
- Employ a Content Security Policy from the start
- Use PHP’s highest levels of Session ID generation and hashing
- Login over SSL
- Use modern strength cryptography with CSPRNG quality salts (Blowfish, Rijndael256, openssl_random_pseudo_bytes(), DEV_URANDOM, etc..)
- Store hashed, then encrypted passwords, not clear text passwords
- Use cookies only via session.use_only_cookies=1
- Use HTTP-Only Cookies via session.cookie_httponly=1
- Use secure cookies over SSL for login process via session.cookie_secure=1
- Avoid shared session storage. Use custom session handler for secure storage
- Avoid Session Fixation by regenerating session id on authentication/authorization
- Set and enforce session expiration on critical actions – general timeout, inactivity periods
- Make logout button available to users at all times
- Properly delete all session data/Unset cookies immediately on logout
- RememberMe cookies should not include user/password information in any form
- $_GET, $_POST, $_REQUEST, $_FILES and $_COOKIE are untrusted
- HTTP headers and related $_SERVER data are untrusted
- $_REQUEST creates attack vector confusion by obfuscating the input source
- For MySQL, use quoted strings. MySQL type casts according to table column
- Automate injection defense by using prepared statements. PDO or mysqli
- Avoid manual quoting if possible – For dynamic column selection, use column white lists
- Remove dangerous functions from user execution (shell_exec(), exec(), etc.. )
- Do not use preg_replace() with unsanitized user input to avoid eval() calls
- Avoid HTML tags in untrusted user output
- When HTML tags must be used with untrusted user data, use HTMLPurifier
- $_FILES['filename']['type'] is untrusted
Secure Session Management Checklist
- Begin Session with SSL connection
- Check your session management configuration
- Enable a highly unpredictable session ID
- Verify that session IDs were actually generated by your server
- Enable HTTP Only and Secure Cookies via PHP
- Enable secure login over SSL
- Always regenerate a session ID on successful authentication
- Force users to re-authenticate with password over SSL on any critical actions
- Always regenerate a session ID on privilege elevation
- Store all session data in server session array only
- Make logout option available on every page
- Upon logging out, explicitly destroy all user session data on the server
- Force expiration of session cookies on the server
- Explicitly and immediately destroy session on suspicious activity
- Use only cookies for session ID transmission
Additional PHP Security Checklist
- Employ a high encryption strength cost and update this cost periodically
- Assist the user in avoiding weak passwords with a strength meter
- Encrypt sessions, encrypt user data
- Encode Header/Metatag Content-Type: as UTF-8
- Remove invalid UTF-8 characters from input through iconv()
- To filter/validate input: white list, typecast, escape or convert input
- To preserve output – escape with correct character set
- Use HTTP GET for read requests
- Use HTTP POST with authentication tokens for write modification requests
- Add high quality CSRF tokens to all forms
- Escape output according to context – HTML, URL, JavaScript
- Remove newlines from untrusted user input for email From: and Subject: headers
- Prevent information disclosure to users – Do not reflect SQL or file path errors, etc…set display_errors=0, log_errors=1, discontinue use of die(“error”);
- Disable dangerous PHP functions
Disable Dangerous PHP Functions
Certain functions are very dangerous when executed with untrusted input. Disabling these is highly recommended, especially in a shared environment. In php.ini, set disable_functions to the functions needing to be disabled. If a function is required, remove the name from the list. Example:
disable_functions =eval, exec,passthru, shell_exec, system, proc_open, popen, curl_exec, curl_multi_ exec, parse_ini_file ,show_source