422.1 Input validation and sanitisation
Protect software from corrupted data and malicious attacks by checking that all user input is correct, safe, and expected.
Overview
Input validation is one of the most essential strategies in secure coding. It ensures that all data received by a program is safe to use, whether it comes from a user form, a URL, an uploaded file, or another system. Without proper validation, even simple inputs can be used to exploit vulnerabilities and compromise an application.
Sanitisation is a related technique used to clean up unsafe input when it cannot be rejected entirely. Together, validation and sanitisation help prevent common threats such as code injection, cross-site scripting (XSS), and data corruption.
Targets
In this topic, students learn to:
Explain the importance of input validation and sanitisation in secure coding
Identify techniques that prevent unauthorised or dangerous input
Apply secure coding patterns to reduce the risk of injection and other common threats
Syllabus references
What is input validation?
Input validation is the process of checking that all data received by the program:
Is of the correct type (e.g. number, string, date)
Is in the correct format (e.g. email, URL, phone number)
Falls within an acceptable range (e.g. age between 13–120)
Meets expectations defined in the specification
Sources of input include:
User forms and UI fields
URL query strings and parameters
Cookies and session variables
API requests and uploaded files
Data retrieved from other systems or databases
Key principle: Never trust user input. Even if the interface looks safe (e.g. dropdown menus or hidden fields), attackers can tamper with it. Assume all input might be:
Incorrect (e.g. a letter instead of a number)
Malicious (e.g. SQL, JavaScript, or shell code)
Unexpected (e.g. blank, too long, or invalid format)
Validation vs sanitisation
Validation checks if the input is acceptable. If it fails, the input should be rejected.
Sanitisation modifies the input to remove or escape dangerous content. This is used when rejection isn’t practical.
Validate first. Only sanitise if you must accept the input.
Safe input validation strategies
1. Whitelist allowed values
Accept only input that matches an explicit, known-good set of rules.
if age in range(13, 100):
register_user()
Questions
Define input validation in the context of registering as a new user on a web app and creating a username and password.
Explain how input validation can make html form tags more secure.
Outline THREE vulnerabilities that can arise from poor input validation.
Give THREE reasons why user input should never be trusted.
After a user securely signs into a website, they post comments on an article written by a blogger. Explain how sanitisation is used in this context to reduce vulnerabilities and improve security.
Outline some security-by-design principles for validating a username and password during the signup process for a website.
Last updated
Was this helpful?