Runbook: API Security Failing
Trigger
401responses for protected endpoints even with expected credentials.429spikes on normal traffic profile.413responses after payload changes.- CORS errors in browser or missing expected security headers in responses.
Fast triage
- Check current env values for security settings in
.env. - Verify request contains auth header configured by
API_AUTH_HEADER. - Reproduce with one endpoint:
POST /api/v1/user. - Inspect application logs for security middleware warnings.
Most common causes
- Wrong
API_MOCK_API_KEYor wrong header name. - Unexpected aggressive rate-limit settings for local/test load.
- Payload grew beyond
API_BODY_MAX_BYTES. - Frontend origin missing in
CORS_ALLOW_ORIGINS. - Middleware order or behavior changed without tests or an ADR.
Recovery steps by category
Auth failures (401)
- Validate
API_AUTH_STRATEGYis expected (mock_api_keyordisabled). - Confirm request sends
API_AUTH_HEADERwithAPI_MOCK_API_KEYvalue. - Re-run tests:
make test-one path=tests/api/v1/test_user_create.py.
Rate-limit overblocking (429)
- Check
API_RATE_LIMIT_REQUESTSandAPI_RATE_LIMIT_WINDOW_SECONDS. - Validate client retry/backoff behavior.
- Before raising limits, agree policy in an ADR and update docs.
Body limit errors (413)
- Measure actual payload size and compare with
API_BODY_MAX_BYTES. - Reduce request body or plan dedicated upload flow.
- If increasing limit, update
env/example, docs, and test coverage.
CORS / security headers
- Ensure frontend origin is explicitly allowlisted.
- Verify response headers include CSP and frame/content-type protections.
- Confirm browser preflight (
OPTIONS) behavior is not blocked by auth/rate-limit.
Final recovery sequence
make testmake verifymake docs-fix
Exit criteria
- Protected endpoints behave as expected for auth, rate limit, and body size.
- CORS and security headers are present and validated.
- CI passes and docs match the security settings you ship.
Follow-up
- Security policy changes belong in an ADR.
- Add new repeating patterns to this runbook.
Page history
| Date | Change | Author |
|---|---|---|
| Added Page history section (repository baseline). | Ivan Boyarkin |