Security Best Practices
Use this skill for implementation or review when code touches trust boundaries, credentials, PII, authorization, uploads, payments, or external integrations.
Priorities
- Least privilege
- Validation at boundaries
- Safe defaults
- Secret hygiene
- Auditability
Core Rules
1. Treat every external input as untrusted
Validate and normalize:
- request params
- headers
- bodies
- cookies
- files
- third-party payloads
- environment variables
Do not rely on frontend validation for security.
2. Keep authentication and authorization separate
- Authentication proves identity.
- Authorization proves permission for the конкретный action/resource.
- Always check authorization close to the business action, not only at the router edge.
3. Minimize secret exposure
- Never hardcode secrets.
- Keep secrets out of logs, client responses, screenshots, and test fixtures.
- Rotate and scope tokens/keys where the platform supports it.
- Prefer short-lived credentials when feasible.
4. Protect sensitive data deliberately
- Collect the minimum data needed.
- Encrypt or hash sensitive data where appropriate.
- Do not store passwords reversibly.
- Be explicit about retention, masking, and audit needs.
5. Prevent injection and unsafe execution
- Use parameterized queries.
- Avoid shell execution with interpolated user input.
- Validate file paths, URLs, and dynamic selectors before use.
- Normalize or reject untrusted structured objects before handing them to query builders or ORMs.
6. Fail safely
- Deny by default.
- Return safe, minimal error responses.
- Do not leak stack traces, internal IDs, provider payloads, or permission model details to untrusted clients.
7. Make side effects resistant to abuse
- Rate-limit abuse-prone endpoints.
- Add CSRF protection where relevant.
- Use idempotency or replay protection for sensitive write operations.
- Put sensible limits on uploads, payload size, and expensive operations.
Review Heuristics
Look for:
- missing authz checks
- trust in client-supplied roles/flags
- secrets in source or logs
- unsafe file or command handling
- missing validation at service boundaries
- over-broad access to data or admin features
- internal error leakage
- retries or webhooks that can duplicate sensitive actions
Common High-Risk Areas
- auth flows
- password reset / email verification
- payments and billing
- uploads and file parsing
- webhooks
- admin tools
- search/query builders
- any endpoint returning user-specific data
Anti-Patterns
Avoid:
- "admin if flag says so" authorization
- passing raw provider payloads through the system unchecked
- logging tokens, passwords, card-like data, or full personal records
- broad catch blocks that convert security failures into generic success/fallback behavior
- storing long-lived secrets in client-accessible places
Quick Checklist
1---2name: security-best-practices3description: Practical secure-coding rules for auth, data handling, input validation, and defense-in-depth.4license: See repository LICENSE5---67# Security Best Practices89Use this skill for implementation or review when code touches trust boundaries, credentials, PII, authorization, uploads, payments, or external integrations.1011## Priorities12131. **Least privilege**142. **Validation at boundaries**153. **Safe defaults**164. **Secret hygiene**175. **Auditability**1819## Core Rules2021### 1. Treat every external input as untrusted2223Validate and normalize:2425- request params26- headers27- bodies28- cookies29- files30- third-party payloads31- environment variables3233Do not rely on frontend validation for security.3435### 2. Keep authentication and authorization separate3637- Authentication proves identity.38- Authorization proves permission for the конкретный action/resource.39- Always check authorization close to the business action, not only at the router edge.4041### 3. Minimize secret exposure4243- Never hardcode secrets.44- Keep secrets out of logs, client responses, screenshots, and test fixtures.45- Rotate and scope tokens/keys where the platform supports it.46- Prefer short-lived credentials when feasible.4748### 4. Protect sensitive data deliberately4950- Collect the minimum data needed.51- Encrypt or hash sensitive data where appropriate.52- Do not store passwords reversibly.53- Be explicit about retention, masking, and audit needs.5455### 5. Prevent injection and unsafe execution5657- Use parameterized queries.58- Avoid shell execution with interpolated user input.59- Validate file paths, URLs, and dynamic selectors before use.60- Normalize or reject untrusted structured objects before handing them to query builders or ORMs.6162### 6. Fail safely6364- Deny by default.65- Return safe, minimal error responses.66- Do not leak stack traces, internal IDs, provider payloads, or permission model details to untrusted clients.6768### 7. Make side effects resistant to abuse6970- Rate-limit abuse-prone endpoints.71- Add CSRF protection where relevant.72- Use idempotency or replay protection for sensitive write operations.73- Put sensible limits on uploads, payload size, and expensive operations.7475## Review Heuristics7677Look for:7879- missing authz checks80- trust in client-supplied roles/flags81- secrets in source or logs82- unsafe file or command handling83- missing validation at service boundaries84- over-broad access to data or admin features85- internal error leakage86- retries or webhooks that can duplicate sensitive actions8788## Common High-Risk Areas8990- auth flows91- password reset / email verification92- payments and billing93- uploads and file parsing94- webhooks95- admin tools96- search/query builders97- any endpoint returning user-specific data9899## Anti-Patterns100101Avoid:102103- "admin if flag says so" authorization104- passing raw provider payloads through the system unchecked105- logging tokens, passwords, card-like data, or full personal records106- broad catch blocks that convert security failures into generic success/fallback behavior107- storing long-lived secrets in client-accessible places108109## Quick Checklist110111- [ ] Inputs are validated at the boundary112- [ ] Auth and authz are both enforced113- [ ] Secrets are not exposed in code, logs, or responses114- [ ] Sensitive actions resist replay/abuse115- [ ] Error responses are safe116- [ ] Data exposure is limited to the minimum required