OWASP - Input Validation Cheat Sheet
Introduction
- The OWASP Input Validation Cheat Sheet provides comprehensive guidelines on how to validate and sanitize inputs effectively to prevent security vulnerabilities.
- Input validation is a critical part of securing applications, as improper handling of user inputs can lead to various types of attacks, including SQL injection, cross-site scripting (XSS), and buffer overflows.
Key Principles of Input Validation
Accept Known Good (Whitelist)
:- Define and accept only known good input.
- This is often done using whitelists of acceptable input values or patterns.
Reject Known Bad (Blacklist)
:- While less preferred, blacklisting involves defining patterns or values that are known to be harmful and rejecting inputs that match these patterns.
Sanitize Input
:- Sanitize input by removing or encoding characters that can be potentially harmful.
- This helps mitigate injection attacks and other vulnerabilities.
Canonicalization
:- Normalize inputs to a standard format before validation. This ensures that equivalent forms of input are consistently validated.
Best Practices for Input Validation
Use Built-in Validation Functions
:- Leverage built-in validation functions provided by programming languages or frameworks whenever possible.
Validate Inputs on Both Client and Server Side
:- Perform input validation on both client-side (for usability) and server-side (for security). Never rely solely on client-side validation.
Define a Clear Set of Input Requirements
:- Specify and document the expected format, length, type, and range of all inputs.
Use Strong Data Typing
:- Use the correct data types for inputs (e.g., integers, dates) to enforce proper validation at the language or framework level.
Limit Input Length
:- Define and enforce maximum length constraints for all inputs to prevent buffer overflows and denial-of-service attacks.
Encode Outputs
:- Encode outputs to ensure that any potentially harmful characters are rendered harmless.
- This is particularly important for web applications to prevent XSS attacks.
Examples of Input Validation
- Example in Python ```python import re
def is_valid_username(username): # Define a regex pattern for a valid username (alphanumeric, 3-20 characters) pattern = re.compile(r’^[a-zA-Z0-9]{3,20}$’) return bool(pattern.match(username))
Example usage
username = “validUser123” if is_valid_username(username): print(“Username is valid”) else: print(“Username is invalid”)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- Example in JavaScript (Client-side)
```javascript
function isValidEmail(email) {
// Define a regex pattern for a valid email address
var pattern = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
return pattern.test(email);
// Example usage
var email = "user@example.com";
if (isValidEmail(email)) {
console.log("Email is valid");
} else {
console.log("Email is invalid");
}
}
OWASP Input Validation Cheat Sheet Summary
Input Validation Strategy
:- Prefer whitelisting over blacklisting.
- Perform canonicalization before validation.
Data Validation
:- Validate input length, format, type, and range.
- Use regular expressions for complex patterns.
- Implement context-specific validation (e.g., email, URL).
Error Handling
:- Provide clear error messages for validation failures without revealing sensitive information.
Output Encoding
:- Encode outputs to prevent injection attacks, such as XSS.
Regular Reviews and Updates
:- Regularly review and update validation rules and patterns to address new security threats.
This post is licensed under CC BY 4.0 by the author.