What is Input Validation?
Input validation is the process of testing input received by an application for compliance with a set of criteria before allowing it to be processed. It ensures that only properly formatted data enters the application workflow, preventing malformed data from causing harm.
Proper input validation is the first line of defense against many web application vulnerabilities, including injection attacks, cross-site scripting (XSS), and buffer overflows.
Input Validation Techniques
Input Validation Best Practices
Validate on Server-Side
Always perform validation on the server-side, even if client-side validation is implemented. Client-side validation can be bypassed and should only be used for user experience improvements.
Use Whitelist Approach
Prefer whitelist validation over blacklist validation. Define what is allowed rather than trying to block what is known to be dangerous, as new attack vectors constantly emerge.
Validate for Length and Size
Implement reasonable length restrictions and size limits for all input fields to prevent buffer overflow attacks and resource exhaustion.
Use Context-Appropriate Encoding
Apply proper output encoding based on the context where the data will be used (HTML, JavaScript, SQL, etc.) to prevent injection attacks.
Implement Input Sanitization
Remove or encode potentially dangerous characters from input while preserving data integrity. Use established libraries rather than custom implementations.
Common Input Validation Vulnerabilities
SQL Injection
Attackers inject malicious SQL code through input fields to manipulate database queries.
Cross-Site Scripting (XSS)
Malicious scripts are injected into web pages viewed by other users.
Command Injection
Attackers execute arbitrary commands on the host operating system.
Buffer Overflow
Writing more data to a buffer than it can hold, potentially executing arbitrary code.
Implementation Examples
Input Validation (Node.js)
const Joi = require('joi');
// Define validation schema
const userSchema = Joi.object({
username: Joi.string()
.alphanum()
.min(3)
.max(30)
.required(),
email: Joi.string()
.email()
.required(),
password: Joi.string()
.pattern(new RegExp('^[a-zA-Z0-9]{8,30}$'))
.required(),
age: Joi.number()
.integer()
.min(18)
.max(120)
});
// Validate input
const { error, value } = userSchema.validate(userInput);
if (error) {
throw new Error(`Validation error: ${error.details[0].message}`);
}
SQL Injection Prevention
// VULNERABLE: String concatenation
const query = `SELECT * FROM users WHERE username = '${username}'`;
// SECURE: Parameterized queries
const query = 'SELECT * FROM users WHERE username = ?';
db.execute(query, [username]);
// SECURE: Using ORM
const user = await User.findOne({
where: { username: username }
});
// SECURE: Stored procedures
const query = 'CALL getUserByUsername(?)';
db.execute(query, [username]);
Additional Resources
OWASP Input Validation
Comprehensive guide to input validation techniques and best practices.
Visit Resource →Input Validation Cheatsheet
Quick reference for implementing input validation across different contexts.
Visit Resource →