skills/data-validation-pattern/SKILL.md
Security pattern for input validation and sanitization. Use when implementing input handling, preventing injection attacks (SQL, XSS, command), ensuring data integrity, or processing data from untrusted sources. Addresses "Entity provides unexpected data" problem.
npx skillsauth add igbuend/grimbard data-validation-patternInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Ensures all incoming data is validated against specifications before processing, preventing injection attacks, data corruption, and unexpected behavior.
Use this pattern when:
Entity provides unexpected data: Malicious or malformed input causes:
| Role | Type | Responsibility | |------|------|----------------| | Entity | Entity | Sends data to system | | Enforcer | Enforcement Point | Intercepts all incoming data | | Validator | Decision Point | Validates data against specification | | Specification Provider | Information Point | Manages validation rules | | System | Entity | Processes validated data |
Entity → [data] → Enforcer
Enforcer → [data] → Validator
Validator → [get_specification(type)] → Specification Provider
Specification Provider → [specification] → Validator
Validator → [validate, transform to canonical] → Validator
Validator → [canonical_data or error] → Enforcer
Enforcer → [canonical_data] → System (if valid)
→ [error] → Entity (if invalid)
Transform data to standardized form:
Benefit: System only processes data in known format.
Blocklists fail against unknown attack patterns. Use allowlists.
Both are required. Valid data doesn't mean authorized access.
Validation alone doesn't prevent all injection:
Special validation needed:
| Input Type | Validations | |------------|-------------| | Username | Length, allowed characters, no control chars | | Email | Format, length, allowlist domains (if applicable) | | Integer | Type, range, positive/negative | | URL | Protocol allowlist, format, no javascript: | | File | Extension, content-type, size, malware scan | | JSON | Schema validation, depth limits, size limits |
BAD (Vulnerable):
# ❌ VULNERABILITY: Manual, incomplete validation
@app.route("/user", methods=["POST"])
def create_user():
data = request.get_json()
if 'email' not in data: # What about type? Length? format?
return "Missing email", 400
# ... proceeding to use data['age'] which might be a string or negative
GOOD (Secure):
from pydantic import BaseModel, EmailStr, conint, constr
# ✅ Define strict schema
class UserSchema(BaseModel):
username: constr(min_length=3, max_length=50, pattern=r'^[a-zA-Z0-9_]+$')
email: EmailStr
age: conint(ge=18, le=120)
@app.route("/user", methods=["POST"])
def create_user():
try:
# ✅ Validate payload against schema
user = UserSchema(**request.get_json())
save_to_db(user.model_dump())
except ValueError as e:
return jsonify({"error": str(e)}), 400
BAD (Vulnerable):
// ❌ VULNERABILITY: Implicit trust
app.post('/api/profile', (req, res) => {
// trusting req.body.website is a valid URL
// trusting req.body.role is not "admin"
updateProfile(req.user.id, req.body);
});
GOOD (Secure):
const { z } = require('zod');
// ✅ Define strict schema
const ProfileSchema = z.object({
website: z.string().url().max(100),
bio: z.string().max(500).optional(),
role: z.enum(['user', 'editor']), // Block 'admin'
});
app.post('/api/profile', (req, res) => {
const result = ProfileSchema.safeParse(req.body);
if (!result.success) {
return res.status(400).json(result.error);
}
// ✅ Apply canonical/validated data
updateProfile(req.user.id, result.data);
});
development
Security anti-pattern for Cross-Site Scripting vulnerabilities (CWE-79). Use when generating or reviewing code that renders HTML, handles user input in web pages, uses innerHTML/document.write, or builds dynamic web content. Covers Reflected, Stored, and DOM-based XSS. AI code has 86% XSS failure rate.
development
Security anti-pattern for XPath injection vulnerabilities (CWE-643). Use when generating or reviewing code that queries XML documents, constructs XPath expressions, or handles user input in XML operations. Detects unescaped quotes and special characters in XPath queries.
development
Security anti-pattern for weak password hashing (CWE-327, CWE-759). Use when generating or reviewing code that stores or verifies user passwords. Detects use of MD5, SHA1, SHA256 without salt, or missing password hashing entirely. Recommends bcrypt, Argon2, or scrypt.
development
Security anti-pattern for weak encryption (CWE-326, CWE-327). Use when generating or reviewing code that encrypts data, handles encryption keys, or uses cryptographic modes. Detects DES, ECB mode, static IVs, and custom crypto implementations.