This site is the archived OWASP Foundation Wiki and is no longer accepting Account Requests.
To view the new OWASP Foundation website, please visit https://owasp.org
Input Validation Cheat Sheet
Last revision (mm/dd/yy): 01/10/2016 Introduction
This article is focused on providing clear, simple, actionable guidance for providing Input Validation security functionality in your applications. Goal of Input ValidationInput validation is performed to minimize malformed data from entering the system. Input Validation is NOT the primary method of preventing XSS, SQL Injection. These are covered in output encoding and related cheat sheets. Goal of Output EncodingOutput encoding is to ensure that attack data is neutralized before being displayed to a user. For more information on XSS defense and output encoding visit the XSS (Cross Site Scripting) Prevention Cheat Sheet. Goal of Securing File UploadsFiles uploaded to servers must be secured to ensure that malware / auto-executables / OS configuration changing files etc are not uploaded to servers that can impact the confidentiality, integrity and availability of the data stored on the server or other servers. Goal of Error HandlingErrors should be handled in a manner that internal details of the systems are not disclosed to the end user via a stack trace Goal of Secure Application DeploymentApplication deployments should ensure that confidentiality, integrity and availability of the application are not vulnerable to malicious attacks White List Input ValidationIt is always recommended to prevent attacks as early as possible in the processing of the user’s (attacker's) request. Input validation can be used to detect unauthorized input before it is processed by the application. Developers frequently perform black list validation in order to try to detect attack characters and patterns like the ' character, the string 1=1, or the <script> tag, but this is a massively flawed approach as it is typically trivial for an attacker to avoid getting caught by such filters. Plus, such filters frequently prevent authorized input, like O'Brian, when the ' character is being filtered out. White list validation is appropriate for all input fields provided by the user. White list validation involves defining exactly what IS authorized, and by definition, everything else is not authorized. If it's well structured data, like dates, social security numbers, zip codes, e-mail addresses, etc. then the developer should be able to define a very strong validation pattern, usually based on regular expressions, for validating such input. If the input field comes from a fixed set of options, like a drop down list or radio buttons, then the input needs to match exactly one of the values offered to the user in the first place. The most difficult fields to validate are so called 'free text' fields, like blog entries. However, even those types of fields can be validated to some degree, you can at least exclude all non-printable characters, and define a maximum size for the input field. Developing regular expressions can be complicated, and is well beyond the scope of this cheat sheet. There are lots of resources on the internet about how to write regular expressions, including: http://www.regular-expressions.info/ and the OWASP Validation Regex Repository. The following provides a few examples of ‘white list’ style regular expressions: White List Regular Expression ExamplesValidating a Zip Code (5 digits plus optional -4) ^\d{5}(-\d{4})?$ Validating U.S. State Selection From a Drop-Down Menu ^(AA|AE|AP|AL|AK|AS|AZ|AR|CA|CO|CT|DE|DC|FM|FL|GA|GU| HI|ID|IL|IN|IA|KS|KY|LA|ME|MH|MD|MA|MI|MN|MS|MO|MT|NE| NV|NH|NJ|NM|NY|NC|ND|MP|OH|OK|OR|PW|PA|PR|RI|SC|SD|TN| TX|UT|VT|VI|VA|WA|WV|WI|WY)$
Example validating the parameter “zip” using a regular expression. private static final Pattern zipPattern = Pattern.compile("^\d{5}(-\d{4})?$"); public void doPost( HttpServletRequest request, HttpServletResponse response) { try { String zipCode = request.getParameter( "zip" ); if ( !zipPattern.matcher( zipCode ).matches() { throw new YourValidationException( "Improper zipcode format." ); } .. do what you want here, after its been validated .. } catch(YourValidationException e ) { response.sendError( response.SC_BAD_REQUEST, e.getMessage() ); } } Some white list validators have also been predefined in various open source packages that you can leverage. For example:
Client Side vs Server Side ValidationBe aware that any JavaScript input validation performed on the client can be bypassed by an attacker that disables JavaScript or uses a Web Proxy. Ensure that any input validation performed on the client is also performed on the server. Positive ApproachThe variations of attacks are enormous. Use regular expressions to define what is good and then deny the input if anything else is received. In other words, we want to use the approach "Accept Known Good" instead of "Reject Known Bad" Example A field accepts a username. A good regex would be to verify that the data consists of the following [0-9a-zA-Z]{3,10}. The data is rejected if it doesn't match. A bad approach would be to build a list of malicious strings and then just verify that the username does not contain the bad string. This approach begs the question, did you think of all possible bad strings? Robust Use of Input ValidationAll data received from the user should be treated as malicious and verified before using within the application. This includes the following
Input ValidationData recieved from the user should be validated for the following factors as well: 1. Boundary conditions (Out of range values) 2. Length of the data inputed (for example, if the input control can accept only 8 character, the same should be validated while accepting the data. The input chars should not exceed 8 characters). Validating Rich User ContentIt is very difficult to validate rich content submitted by a user. Consider more formal approaches such as HTML Purifier (PHP), AntiSamy or bleach (Python) Preventing XSS and Content Security Policy
Detailed information on XSS prevention here: OWASP XSS Prevention Cheat Sheet Output EncodingPreventing SQL Injection
Further Reading: SQL Injection Prevention Cheat Sheet Preventing OS Injection
Further Reading: Reviewing Code for OS Injection Preventing XML Injection
File UploadsUpload Verification
Upload Storage
Public Serving of Uploaded Content
Beware of "special" files
Upload Verification
Error HandlingTypical types of errors: • The result of business logic conditions not being met. • The result of the environment wherein the business logic resides fails. • The result of upstream or downstream systems upon which the application depends fail. • Technical hardware / physical failure. To address these errors:
This is very important. An example would be if a method gained a database connection from a pool of connections, and an exception occurred without finally, the connection object shall not be returned to the pool for some time (until the timeout). This can lead to pool exhaustion. The method finally() is called even if no exception is thrown. Secure Deployment
Authors and Primary EditorsDave Wichers - dave.wichers [at] aspectsecurity.com Other Cheatsheets |