| name | security |
| description | Guides secure development using defense-in-depth and attacker's mindset. ALWAYS trigger on "security review", "vulnerability", "authentication", "authorization", "input validation", "XSS", "SQL injection", "CSRF", "secrets management", "OWASP", "threat model", "security scan", "path traversal", "mass assignment", "privilege escalation", "security headers", "bandit", "dependency audit", "hardening". Use when implementing auth, handling user input, storing secrets, reviewing code for vulnerabilities, or preparing for production deployment. Different from devops skill which covers infrastructure; this covers application-level security patterns. |
Security: Think Like an Attacker
Core Principle
Defense in Depth + Least Privilege. Layer multiple controls. Grant minimum permissions. Assume every layer can fail.
Security Mindset
Six Questions (Every Feature)
- Who can access this? (Authentication)
- Are they allowed to? (Authorization)
- Can they see more than they should? (Data exposure)
- Can they do more than they should? (Privilege escalation)
- Can they break it for others? (Denial of service)
- Will we know if they do? (Audit logging)
OWASP Top 10 (Quick Reference)
| # | Vulnerability | Key Defense |
|---|
| 1 | Broken Access Control | Auth check per resource, deny by default |
| 2 | Cryptographic Failures | Argon2/bcrypt, never MD5/SHA1 for passwords |
| 3 | Injection (SQL, XSS, Command) | Parameterized queries, escaping, allowlists |
| 4 | Insecure Design | Rate limiting, STRIDE threat modeling |
| 5 | Security Misconfiguration | Debug off in prod, generic error messages |
| 6 | Vulnerable Components | safety check / npm audit |
| 7 | Authentication Failures | Secure cookies, crypto-random session IDs |
| 8 | Data Integrity Failures | JSON with validation, never pickle |
| 9 | Logging Failures | Log security events, failed auth, admin actions |
| 10 | SSRF | URL allowlist, block internal IPs |
See references/owasp-top-10.md for detailed bad/good code examples per vulnerability.
Input Validation
Allowlist over Denylist:
if username in ['admin', 'root']:
raise ValueError()
if not re.match(r'^[a-zA-Z0-9_]{3,20}$', username):
raise ValueError("Invalid format")
Layered Validation:
from pydantic import BaseModel, validator, constr
class UserInput(BaseModel):
username: constr(min_length=3, max_length=20, regex=r'^[a-zA-Z0-9_]+$')
email: str
age: int
@validator('email')
def validate_email(cls, v):
if not re.match(r'^[\w\.-]+@[\w\.-]+\.\w+$', v):
raise ValueError('Invalid email')
return v.lower()
@validator('age')
def validate_age(cls, v):
if not (0 <= v <= 150):
raise ValueError('Age 0-150')
return v
Output Encoding
Context-Aware:
from markupsafe import escape
from urllib.parse import quote
html = f"<div>{escape(username)}</div>"
url = f"https://example.com/search?q={quote(term)}"
js = f"var name = {json.dumps(username)};"
db.execute("SELECT * FROM users WHERE name = ?", [username])
Secrets Management
API_KEY = "sk_live_abc123"
import os
for secret in ['API_KEY', 'DATABASE_URL', 'SECRET_KEY']:
if secret not in os.environ:
raise RuntimeError(f"Missing: {secret}")
API_KEY=sk_live_abc123
API_KEY=your_api_key_here
Production: Use AWS Secrets Manager, HashiCorp Vault, or platform-native secret stores.
Common Vulnerability Fixes
Path Traversal
from pathlib import Path
BASE_DIR = Path('/var/data')
file_path = (BASE_DIR / filename).resolve()
if not file_path.is_relative_to(BASE_DIR):
abort(403)
Mass Assignment
ALLOWED = ['name', 'email', 'bio']
for field in ALLOWED:
if field in request.json:
setattr(user, field, request.json[field])
CSRF Protection
from flask_wtf.csrf import CSRFProtect
csrf = CSRFProtect(app)
app.config['SESSION_COOKIE_SAMESITE'] = 'Strict'
Security Tooling
bandit -r src/ -f json -o report.json
safety check
npm audit
trivy image myapp:latest
See references/security-tooling.md for pre-commit hooks, security headers config, and CI/CD integration.
Quick Checklist
Auth:
Input/Output:
Secrets & Crypto:
Monitoring:
Config: