Input validation — 在处理用户输入之前检查其是否满足预期标准 — 是一项基础性安全实践。由于攻击通常通过恶意输入进行,验证(和清理)输入有助于防止许多漏洞。核心原则:永远不要信任用户输入。
永远不要信任用户输入
ALL input from outside (users, APIs, files, requests) is UNTRUSTED — it can be malicious:
→ attackers send crafted input to exploit vulnerabilities (injection, XSS, etc.)
→ "never trust the client" — input can be anything, including attacks
→ Validate and handle ALL external input as potentially hostile.
Input validation 的作用
VALIDATION → check input meets expected criteria; reject what doesn't:
→ TYPE (is it a number?), FORMAT (valid email?), RANGE (0-120?), LENGTH (max?),
allowed values (whitelist), required fields
→ PREFER ALLOWLISTING (accept known-good) over blocklisting (reject known-bad —
incomplete; attackers find bypasses)
→ reject/handle invalid input safely (don't process it)
