# Input Validation And Sanitization

> Use this skill when implementing any endpoint, form handler, CLI tool, or function that accepts external input. Validate and sanitize all untrusted data before processing — never assume input is safe.

- Skill: `gabrielmoreira/input-validation-and-sanitization` (Agent Skill)
- Install (CLI): `npx skillmds@latest add gabrielmoreira/input-validation-and-sanitization`
- Raw SKILL.md: https://api.skillmd.com/api/skills/gabrielmoreira/input-validation-and-sanitization/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: gabrielmoreira (https://skillmd.com/u/gabrielmoreira)
- Updated: 2026-09-09
- Page: https://skillmd.com/skills/gabrielmoreira/input-validation-and-sanitization

---


# Input Validation and Sanitization

**Validation principles:**
- Validate at the system boundary (API layer, form handler) — not deep in business logic.
- Validate type, range, length, and format explicitly.
- Reject unexpected input by default (allowlist > denylist).

**SQL injection prevention:** Always use parameterized queries or an ORM.

**XSS prevention:** Escape HTML output; use Content-Security-Policy headers; avoid `innerHTML` with user data.

**Path traversal prevention:** Resolve paths to canonical form and verify they are under the expected directory.

```python
import os
base = '/allowed/dir'
canonical = os.path.realpath(os.path.join(base, user_input))
assert canonical.startswith(base + os.sep)
```

