--- url: "https://cheatsheetseries.owasp.org/cheatsheets/Input_Validation_Cheat_Sheet.html" titulek: "Input Validation - OWASP Cheat Sheet Series" h1: "Input Validation Cheat Sheet¶" popis: "Website with the collection of all the cheat sheets of the project." jazyk: "en" kanonicka: "https://cheatsheetseries.owasp.org/cheatsheets/Input_Validation_Cheat_Sheet.html" drobecky: [] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 01:18:18" znaku: 18843 slov: 2758 kodovani: "utf-8" sha256: "3311d7f4ebe339b6bb9bf77087e93b1df6e9379257e27abf3e8a8a891db9d1b7" --- # Input Validation Cheat Sheet¶ ## Introduction¶ This article is focused on providing clear, simple, actionable guidance for providing Input Validation security functionality in your applications. ## Goals of Input Validation¶ Input validation is performed to ensure only properly formed data is entering the workflow in an information system, preventing malformed data from persisting in the database and triggering malfunction of various downstream components. Input validation should happen as early as possible in the data flow, preferably as soon as the data is received from the external party. Data from all potentially untrusted sources should be subject to input validation, including not only Internet-facing web clients but also backend feeds over extranets, from [suppliers, partners, vendors or regulators](https://badcyber.com/several-polish-banks-hacked-information-stolen-by-unknown-attackers/), each of which may be compromised on their own and start sending malformed data. Input Validation should not be used as the *primary* method of preventing [XSS](Cross_Site_Scripting_Prevention_Cheat_Sheet.html), [SQL Injection](SQL_Injection_Prevention_Cheat_Sheet.html) and other attacks which are covered in respective [cheat sheets](https://cheatsheetseries.owasp.org/) but can significantly contribute to reducing their impact if implemented properly. ## Input Validation Strategies¶ Input validation should be applied at both syntactic and semantic levels: - **Syntactic** validation should enforce correct syntax of structured fields (e.g. SSN, date, currency symbol). - **Semantic** validation should enforce correctness of their *values* in the specific business context (e.g. start date is before end date, price is within expected range). It 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. ## Implementing Input Validation¶ Input validation can be implemented using any programming technique that allows effective enforcement of syntactic and semantic correctness, for example: - Use data type validators available natively in web application frameworks (such as [Django Validators](https://docs.djangoproject.com/en/1.11/ref/validators/) , [Apache Commons Validators](https://commons.apache.org/proper/commons-validator/apidocs/org/apache/commons/validator/package-summary.html#doc.Usage.validator) etc). - Validation against [JSON Schema](http://json-schema.org/) and [XML Schema (XSD)](https://www.w3schools.com/xml/schema_intro.asp) for input in these formats. - Type conversion (e.g. `Integer.parseInt()` in Java, `int()` in Python) with strict exception handling - Minimum and maximum value range check for numerical parameters and dates, minimum and maximum length check for strings. - Array of allowed values for small sets of string parameters (e.g. days of week). - Regular expressions for any other structured data covering the whole input string `(^...$)` and **not** using "any character" wildcard (such as `.` or `\S` ) - Denylisting known dangerous patterns can be used as an additional layer of defense, but it should supplement - not replace - allowlisting, to help catch some commonly observed attacks or patterns without relying on it as the main validation method. ### Allowlist vs Denylist¶ It is a common mistake to use denylist validation in order to try to detect possibly dangerous characters and patterns like the apostrophe `'` character, the string `1=1`, or the `"@example.org` - `[[email protected]](/cdn-cgi/l/email-protection)` - `user@[IPv6:2001:db8::1]` - `" "@example.org` Properly parsing email addresses for validity with regular expressions is very complicated, although there are a number of [publicly available documents on regex](https://datatracker.ietf.org/doc/html/draft-seantek-mail-regexen-03#rfc.section.3). The biggest caveat on this is that although the RFC defines a very flexible format for email addresses, most real world implementations (such as mail servers) use a far more restricted address format, meaning that they will reject addresses that are *technically* valid. Although they may be technically correct, these addresses are of little use if your application will not be able to actually send emails to them. As such, the best way to validate email addresses is to perform some basic initial validation, and then pass the address to the mail server and catch the exception if it rejects it. This means that the application can be confident that its mail server can send emails to any addresses it accepts. The initial validation could be as simple as: - The email address contains two parts, separated with an `@` symbol. - The email address does not contain dangerous characters (such as backticks, single or double quotes, or null bytes). - Exactly which characters are dangerous will depend on how the address is going to be used (echoed in page, inserted into database, etc). - The domain part contains only letters, numbers, hyphens ( `-` ) and periods ( `.` ). - The email address is a reasonable length: - The local part (before the `@` ) should be no more than 63 characters. - The total length should be no more than 254 characters. ### Semantic Validation¶ Semantic validation is about determining whether the email address is correct and legitimate. The most common way to do this is to send an email to the user, and require that they click a link in the email, or enter a code that has been sent to them. This provides a basic level of assurance that: - The email address is correct. - The application can successfully send emails to it. - The user has access to the mailbox. The links that are sent to users to prove ownership should contain a token that is: - At least 32 characters long. - Generated using a [secure source of randomness](Cryptographic_Storage_Cheat_Sheet.html#secure-random-number-generation) . - Single use. - Time limited (e.g, expiring after eight hours). After validating the ownership of the email address, the user should then be required to authenticate on the application through the usual mechanism. #### Disposable Email Addresses¶ In some cases, users may not want to give their real email address when registering on the application, and will instead provide a disposable email address. These are publicly available addresses that do not require the user to authenticate, and are typically used to reduce the amount of spam received by users' primary email addresses. Blocking disposable email addresses is almost impossible, as there are a large number of websites offering these services, with new domains being created every day. There are a number of publicly available lists and commercial lists of known disposable domains, but these will always be incomplete. If these lists are used to block the use of disposable email addresses then the user should be presented with a message explaining why they are blocked (although they are likely to simply search for another disposable provider rather than giving their legitimate address). If it is essential that disposable email addresses are blocked, then registrations should only be allowed from specifically-allowed email providers. However, if this includes public providers such as Google or Yahoo, users can simply register their own disposable address with them. #### Sub-Addressing¶ Sub-addressing allows a user to specify a *tag* in the local part of the email address (before the `@` sign), which will be ignored by the mail server. For example, if that `example.org` domain supports sub-addressing, then the following email addresses are equivalent: - `[[email protected]](/cdn-cgi/l/email-protection)` - `[[email protected]](/cdn-cgi/l/email-protection)` - `[[email protected]](/cdn-cgi/l/email-protection)` Many mail providers (such as Microsoft Exchange) do not support sub-addressing. The most notable provider who does is Gmail, although there are many others that also do. Some users will use a different *tag* for each website they register on, so that if they start receiving spam to one of the sub-addresses they can identify which website leaked or sold their email address. Because it could allow users to register multiple accounts with a single email address, some sites may wish to block sub-addressing by stripping out everything between the `+` and `@` signs. This is not generally recommended, as it suggests that the website owner is either unaware of sub-addressing or wishes to prevent users from identifying them when they leak or sell email addresses. Additionally, it can be trivially bypassed by using disposable email addresses, or simply registering multiple email accounts with a trusted provider. ## References¶ - [OWASP Top 10 Proactive Controls 2024: C3: Validate all Input & Handle Exceptions](https://top10proactive.owasp.org/the-top-10/c3-validate-input-and-handle-exceptions) - [CWE-20 Improper Input Validation](https://cwe.mitre.org/data/definitions/20.html) - [OWASP Top 10 2021: A03:2021-Injection](https://owasp.org/Top10/A03_2021-Injection/) - [Snyk: Improper Input Validation](https://learn.snyk.io/lesson/improper-input-validation/)