
Why input validation matters
- Inputs are the attack surface for many high-impact issues: SQL/NoSQL injection, command injection, XSS, XXE, path traversal, and resource exhaustion via oversized requests.
- Automated attacks (bots) are common — apply rate limiting and sensible request limits to reduce automated abuse.
- Proper validation is multi-layered:
- Enforce size/type checks and required-field validation.
- Parameterize database queries; never interpolate raw user input into SQL.
- Encode/escape output by context (HTML, JS, URL, CSS).
- Disable or restrict dangerous parser features (e.g., XML external entities).
Robert'); DROP TABLE students; --), an attacker can execute arbitrary SQL. The correct approach is to use parameterized queries or ORM query binding.
Useful reference: OWASP SQL Injection
Running an automated audit (example)
In this lesson we used an automated assistant from the project directory to review input validation for an Express.js demo app:Example checklist (condensed)
Typical findings and prioritized fixes
Automated reports often surface similar gaps across small demos:- Missing or weak rate limiting on authentication endpoints — enables brute-force attacks.
- No request body size limits — opens the app to memory/CPU exhaustion.
- Weak or default JWT secrets — risk of token forgery.
- Incomplete sanitation of user-supplied content or missing security headers.
- Verify any raw SQL strings even when the app generally uses parameterized queries.
Example exploit (illustrative only) — brute-force
Do not run these commands against third-party systems. This is an illustrative brute-force loop for testing in a controlled environment you own:Limit request body size to prevent resource exhaustion
Tune body-parsing middleware to restrict payload sizes:Large-body test (illustrative only)
This curl command sends a very large payload. Only use in a local, controlled environment you own:Do not run DoS or resource-exhaustion tests against systems you do not own or explicitly have permission to test. Always perform tests in a controlled environment.
Other concrete checks and fixes
- SQL / NoSQL injection
- Use parameterized queries or ORM binding APIs.
- For MongoDB, never accept raw query operators from clients; whitelist fields and operators instead.
- Command injection
- Avoid building shell commands using user input. Use child process APIs with explicit argument arrays and strict validation.
- XSS
- Apply contextual output encoding, sanitize HTML inputs or disallow rich HTML, and implement a Content Security Policy (CSP).
- XXE
- Disable external entity resolution in XML parsers or prefer JSON parsing where possible.
- Path traversal
- Resolve and sanitize file paths, disallow
..sequences, and restrict file operations to an allowed base directory.
- Resolve and sanitize file paths, disallow
- Parameter pollution and type validation
- Use validation middleware like express-validator, Joi, or Zod to coerce and validate types.
- Security headers
- Use Helmet and configure headers (HSTS, X-Content-Type-Options, Referrer-Policy) appropriately.