API Security
Rate limiting
- Protects availability and cost; apply per IP, API key, user, and route as needed.
- Return 429 Too Many Requests with
Retry-After or structured backoff hints.
- Separate burst limits from sustained quotas for better UX.
- Validate shape, size, and semantics (types, ranges, enums) at the edge (gateway) and in services.
- Reject unknown fields explicitly if you want forward compatibility, or define ignore vs error policy.
- Cap max JSON depth, string lengths, and array sizes to reduce DoS surface.
CORS (Cross-Origin Resource Sharing)
- Browsers enforce CORS for cross-origin XHR/fetch; non-browser clients ignore it.
- Avoid
Access-Control-Allow-Origin: * with credentials; prefer explicit origins.
- Preflight (
OPTIONS) adds latency; keep allowed methods and headers minimal.
HTTPS requirement
- Encrypt data in transit; terminate TLS at the gateway or ingress with modern ciphers and HSTS where appropriate.
- Never send secrets in query strings (they leak via logs and referrers).
- Use mTLS for high-trust service meshes or partner integrations when needed.
Preventing common attacks (XSS, injection basics in APIs)
- Treat all input as hostile; parameterize queries; never concatenate SQL/HTML from user input.
- Encode or sanitize output that might render in HTML contexts; APIs returning HTML are rare but risky.
- Log and alert on auth anomalies, enumeration, and payload patterns typical of scanners.