← Back to Glossary
Security Glossary

Input Validation & Sanitization

Code Security

Input validation and sanitization is the practice of checking and cleaning every piece of data that enters your application before it is processed, stored, or displayed. Validation verifies that the data matches expected rules, an email field contains a valid email format, an age field contains a reasonable number, a file upload is actually an image and not a script. Sanitization goes a step further by stripping or encoding potentially dangerous characters from the input, removing HTML tags from a text field, escaping special characters in database queries, or encoding output to prevent browser execution. This happens on both the client side (for immediate user feedback) and the server side (where it actually matters for security, since client-side validation can be bypassed trivially). Every input vector must be covered: form fields, URL parameters, HTTP headers, cookies, file uploads, and API request bodies.

Why It Matters

User input is the primary attack surface of any web application. SQL injection, cross-site scripting, command injection, path traversal, and most other web vulnerabilities exist because an application trusted user input when it should not have. If your application takes user-supplied data and passes it directly to a database query, an HTML template, a shell command, or a file system operation without validation and sanitization, you have given attackers a direct channel to control your system. Input validation is not just about security, it also protects data integrity. A phone number stored as "DROP TABLE users" is useless for business purposes. Proper validation ensures that your database contains clean, consistent data that your application and your business can actually rely on.

What Happens Without It

In 2014, a vulnerability in the Bash shell known as Shellshock allowed attackers to inject commands through user-controlled input, specifically HTTP headers like User-Agent, that web servers passed to CGI scripts without sanitization. Within hours of disclosure, attackers were actively exploiting the vulnerability to install malware, create botnets, and steal data from millions of servers worldwide. The Department of Homeland Security rated it a 10 out of 10 severity. More recently, the Log4Shell vulnerability in 2021 exploited the Log4j logging library's failure to sanitize input before processing it. A simple malicious string in a chat message, username, or HTTP header could trigger remote code execution on the server. Both of these catastrophic vulnerabilities shared the same root cause: user-controlled data was processed without proper validation or sanitization.

Every app I build includes input validation and sanitization by default.

or hi@mikelatimer.ai