Input Validation
Last updated
Last updated
This page contains recommendations for the implementation of input validation.
Do not use input validation as the primary method of preventing Cross-Site Scripting, SQL injection and other attacks which are covered by the Vulnerability Mitigation section. Nevertheless, input validation can make a significant contribution to reducing the impact of these attacks if implemented properly.
Perform input validation for data from all potentially untrusted sources, including suppliers, partners, vendors, regulators, internal components or applications.
Perform input validation for all user-controlled data, see Data source for validation.
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:
Normalize processed data, see Normalization.
Perform a syntactic validation of the processed data, see Syntactic validation.
Perform a semantic validation of the processed data, see Semantic validation.
Implement protection against mass parameter assignment attacks.
Log errors in the input validation, see the Logging and Monitoring page.
Comply with requirements from the Error and Exception Handling page.
Validate all user-controlled data that are passed in:
Query parameters or URL/GET parameters.
HTTP headers.
Cookies.
HTTP body.
Uploaded files, see the File Upload 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 Semantic validation.
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 against JSON Schema and XML Schema (XSD) for input in these formats.
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
.
You can use regular expressions to define allowed values, see the Regular Expressions page.
Implement file validation according to the File Upload page.
Implement email validation according to the Authentication: Email Address Confirmation page.
You can use the go-playground/validator package to implement input validation.
You can use the golang.org/x/text/unicode/norm package to implement normalization.