Prompt
How do I stop API token theft and replay attacks?
Latest observation
To reduce API token theft and replay attacks, you want to protect the token itself, limit what a stolen token can do, and make every request hard to reuse.
1) Use short-lived tokens
- Prefer short expiration times for access tokens.
- Use refresh tokens separately, and protect them more strictly.
- The shorter the lifetime, the smaller the window for abuse if stolen.
2) Bind tokens to a client or context
Make stolen tokens less useful by binding them to something the attacker won’t have:
- mTLS (mutual TLS): token only works over a client certificate–authenticated connection.
- DPoP / proof-of-possession: request must include a signed proof from the client’s private key.
- Device/session binding where appropriate.
3) Use HTTPS everywhere
- Enforce TLS for all API traffic.
- Redirect or reject plain HTTP.
- Consider HSTS for browser-facing services.
4) Don’t store tokens insecurely
- Avoid putting long-lived tokens in:
- URLs
- logs
- browser localStorage if you can avoid it
- source code
- Store secrets in:
- secure server-side secret stores
- platform keychains / hardware-backed storage on devices
- Mask tokens in logs and error messages.
5) Prevent replay with nonces, timestamps, and signatures
For requests that must be resistant to replay:
- Include a timestamp
- Include a nonce or unique request ID
- Sign the request body and selected headers with a secret or private key
- Reject:
- expired timestamps
- reused nonces / request IDs
This is especially important for:
- payment APIs
- credential-changing endpoints
- webhook verification
- high-risk admin actions
6) Use strong request authentication
If you use API keys alone, they’re often bearer tokens and can be replayed. Better options:
- HMAC-signed requests
- OAuth 2.0 with PKCE for user authorization flows
- PoP tokens instead of pure bearer tokens
7) Scope and limit tokens
Minimize damage if a token is stolen:
- Least privilege scopes
- Per-environment tokens
- Per-service tokens
- Per-user tokens when needed
- Rate limits and quotas
- IP allowlists only if stable and appropriate
8) Rotate and revoke aggressively
- Support token revocation
- Rotate secrets regularly
- Revoke on suspicious activity
- Invalidate sessions when credentials change
- Keep a token blacklist or introspection system if needed
9) Add replay detection
Track and reject repeated use of the same:
- nonce
- request ID
- signed message hash
- one-time token
This usually requires a short-lived cache or datastore keyed by unique request identifiers.
10) Protect the client environment
If tokens are used in browsers, mobile apps, or desktops:
- Assume the client can be compromised
- Use secure storage APIs
- Avoid exposing long-lived secrets in frontend code
- Use same-site cookies and CSRF protections where relevant
- Keep app dependencies updated
11) Monitor for abuse
- Log token usage metadata
- Detect anomalies:
- impossible travel
- unusual IPs/devices
- spikes in request volume
- repeated failed signatures
- Alert and auto-revoke when suspicious behavior appears
Practical baseline
If you want a solid default setup:
- HTTPS only
- Short-lived access tokens
- Refresh tokens stored securely
- Least-privilege scopes
- Signed requests or DPoP/mTLS for sensitive APIs
- Nonce + timestamp checks for replay-prone endpoints
- Revocation + monitoring
If you tell me your setup — for example REST API with bearer tokens, OAuth, browser app, mobile app, or service-to-service — I can give you a more specific defense pattern and example implementation.
Brands