Input Validation
Last updated
Last updated
This page contains recommendations for the implementation of input validation.
Perform input validation for data from all potentially untrusted sources, including suppliers, partners, vendors, regulators, internal components or applications.
Perform input validation as early as possible in a data flow, preferably as soon as data is received from an external party before it is processed by an application.
Implement input validation on the server side. Do not rely solely on validation on the client side.
Implement centralized input validation functionality.
Implement the following validation scheme:
Implement protection against mass parameter assignment attacks.
Validate all user-controlled data that are passed in:
Query parameters or URL/GET parameters.
HTTP headers.
Cookies.
HTTP body.
Ensure all the processed data is encoded in an expected encoding (for instance, UTF-8
) and no invalid characters are present.
Use NFKC
canonical encoding form to treat canonically equivalent symbols.
Define a list of allowed Unicode characters for data input and reject input with characters outside the allowed character list. For example, avoid Cf
(Format) Unicode characters, commonly used to bypass validation or sanitization.
Use data type validators built into the used web framework.
Validate input against expected data type, such as integer, string, date, etc.
Validate input against expected value range for numerical parameters and dates. If the business logic does not define a value range, consider value range imposed by language or database.
Validate input against minimum and/or maximum length for strings.
Define an allow list and validate all data against this list. Avoid using block list validation.
Define an array of allowed values as a small set of string parameters (e.g. days of a week).
Define a list of allowed characters such as decimal digits
or letters
.
Perform input validation for all user-controlled data, see .
Normalize processed data, see .
Perform a syntactic validation of the processed data, see .
Perform a semantic validation of the processed data, see .
Log errors in the input validation, see the page.
Comply with requirements from the page.
Uploaded files, see the page.
If a free-form text is checked against a block list, canonical encoding is mandatory. Otherwise, it is optional. Remember that block list validation must only be used in exceptional cases, see .
The last two categories (not printable characters) are the most used for attacks trying to bypass input validation, and therefore they should be avoided if not needed. For more information on categories, please see .
Validate against and for input in these formats.
You can use regular expressions to define allowed values, see the page.
Implement file validation according to the page.
Implement email validation according to the page.
You can use the package to implement input validation.
Use to implement input validation.
In Django, use to implement input validation.
You can use the package to implement normalization.
You can use the class to implement normalization.
You can use the package to implement normalization.