Input Validation vs Output Encoding
Also Known As
data validation
input sanitisation
server-side validation
TL;DR
Validation checks that input is acceptable; output encoding makes data safe for the context it's rendered in. Both are required.
Explanation
Input validation and output encoding are complementary — not alternatives. Validation (reject unexpected characters, enforce length and format) reduces the attack surface and catches mistakes early. Output encoding (htmlspecialchars for HTML, parameterised queries for SQL, json_encode for JS) makes data structurally safe for its destination context. Relying on validation alone fails because valid-looking data can still be malicious in a different context. Encode for the output context regardless of how the input was validated.
Common Misconception
✗ Client-side validation is sufficient if it covers all cases. Client-side validation is UX — it can be bypassed by disabling JavaScript or crafting raw HTTP requests. Server-side validation is the security control; client-side is purely for user experience.
Why It Matters
Unvalidated input is the root cause of injection attacks, unexpected behaviour, and data corruption. Validating on the server side — not just the client — ensures your application only processes data it was designed to handle.
Common Mistakes
- Relying on client-side (JavaScript) validation alone — it is trivially bypassed with browser dev tools.
- Validating format but not range — accepting a birth year of 9999 is a validation failure.
- Blocklisting known bad input instead of allowlisting known good — attackers always find new bypass strings.
- Validating on the way in but re-using stored data later without re-validation (second-order attacks).
Avoid When
- Validating only on the client side — client-side validation is for UX; server-side validation is for security.
- Using validation as a substitute for parameterised queries — validate AND use prepared statements.
- Allowlist validation that is too permissive — a regex that allows < and > defeats XSS protection.
- Validating after business logic has already acted on the input — validate at the entry point, before any processing.
When To Use
- Every piece of data entering the system from an external source — user input, API payloads, file uploads, query strings.
- Allowlist validation (accept only known-good patterns) rather than blocklist (reject known-bad).
- Before passing data to any downstream system — database, shell command, template engine, serialiser.
- Structured data like emails, URLs, dates — use format-specific validators, not generic string checks.
Code Examples
✗ Vulnerable
// Sanitising instead of validating — trying to fix bad input rather than reject it
\$email = strip_tags(\$_POST['email']);
\$age = (int) \$_POST['age']; // -1 passes silently
✓ Fixed
// Validate — reject anything that doesn't match expectations
\$email = filter_var(\$_POST['email'] ?? '', FILTER_VALIDATE_EMAIL)
?: throw new \InvalidArgumentException('Invalid email');
\$age = filter_var(\$_POST['age'] ?? '', FILTER_VALIDATE_INT,
['options' => ['min_range' => 0, 'max_range' => 150]])
?: throw new \InvalidArgumentException('Invalid age');
// In Laravel — Form Request
class StoreUserRequest extends FormRequest {
public function rules(): array {
return [
'email' => ['required', 'email:rfc,dns', 'max:255', 'unique:users'],
'age' => ['required', 'integer', 'between:0,150'],
'role' => ['required', Rule::in(['user', 'editor'])],
];
}
}
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
13 Mar 2026
Edited
25 Mar 2026
Views
36
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Perplexity 10
Amazonbot 8
Ahrefs 3
Unknown AI 3
SEMrush 3
Majestic 1
Google 1
ChatGPT 1
Also referenced
How they use it
crawler 29
pre-tracking 1
Related categories
⚡
DEV INTEL
Tools & Severity
🟠 High
⚙ Fix effort: Low
⚡ Quick Fix
Validate type, format, length, and range at the entry point; use allowlists not blocklists; reject invalid input early with a 400 response
📦 Applies To
PHP 5.0+
web
api
cli
🔗 Prerequisites
🔍 Detection Hints
User input used in business logic without type cast, filter_var, or validation before use
Auto-detectable:
✗ No
phpstan
psalm
semgrep
⚠ Related Problems
🤖 AI Agent
Confidence: High
False Positives: Medium
✗ Manual fix
Fix: Medium
Context: Function
Tests: Update
CWE-20
CWE-807