Prompt
How do I prevent token theft and replay abuse on APIs?
Latest observation
To prevent token theft and replay abuse on APIs, use a layered approach: minimize token value, bind tokens to the client and request context, and make replays useless.
1) Use short-lived access tokens
- Make access tokens expire quickly, e.g. 5–15 minutes.
- Use refresh tokens only where needed, and protect them more strongly.
- Rotate refresh tokens and revoke on reuse.
Why: stolen tokens have a very short window of usefulness.
2) Prefer proof-of-possession over bearer tokens
Bearer tokens can be used by anyone who gets them. Better options:
- mTLS-bound tokens: token only works when presented over a client certificate.
- DPoP (Demonstration of Proof-of-Possession): client signs each request with a private key, and the server verifies the token is bound to that key.
Why: token theft alone is not enough; attacker also needs the private key.
3) Add anti-replay data to every sensitive request
For high-risk operations, require:
- a nonce or unique request ID
- a timestamp
- a very short acceptance window
- server-side tracking of used nonces/request IDs
Example:
- Client sends
X-Request-Id,Date, and a signature over method, path, body, and timestamp. - Server rejects duplicates or stale timestamps.
Why: even if a request is captured, it can’t be reused.
4) Sign requests, not just tokens
Use request signing for important APIs:
- Include method, path, query, selected headers, body hash, timestamp, nonce.
- Verify signature server-side.
- Reject modified or replayed requests.
This is common in high-security APIs and payment systems.
5) Store tokens securely on the client
For browser-based apps:
- Prefer HttpOnly, Secure, SameSite cookies for session tokens.
- Avoid storing tokens in localStorage or sessionStorage if you can.
- Use CSRF protections if using cookies.
For mobile/desktop:
- Store in OS-provided secure storage:
- iOS Keychain
- Android Keystore / EncryptedSharedPreferences
- Windows DPAPI / Credential Manager
- macOS Keychain
Why: reduces theft via XSS, malware, or local inspection.
6) Harden the frontend against token exfiltration
- Prevent XSS with strong output encoding, CSP, sanitization, dependency hygiene.
- Avoid injecting untrusted HTML.
- Use a strict Content Security Policy.
- Minimize third-party scripts.
Why: XSS is a major path to token theft in web apps.
7) Use audience, issuer, and scope restrictions
- Validate
iss,aud,exp,nbf,sub. - Issue tokens for a single API audience.
- Keep scopes narrow and least-privilege.
- Don’t use one token across multiple services unless necessary.
Why: stolen tokens are less broadly usable.
8) Detect suspicious use
Monitor for:
- impossible travel / geo anomalies
- sudden IP or ASN changes
- unusually high request rates
- replayed IDs or nonce collisions
- token use from multiple devices simultaneously
Respond by:
- stepping up auth
- rotating credentials
- revoking tokens
- alerting users
9) Protect transport and intermediaries
- Enforce TLS 1.2+ / 1.3
- Use HSTS
- Don’t log tokens in URLs, headers, or error messages
- Scrub secrets from logs, traces, and analytics
- Avoid passing tokens through proxies that may record them
10) Make sensitive operations require re-authentication
For actions like:
- changing password
- adding payment methods
- changing MFA
- exporting data
Require:
- recent login
- step-up MFA
- fresh proof of possession / re-signed request
Practical recommendation stack
If you want a strong baseline:
- Short-lived access tokens
- Refresh token rotation
- HttpOnly Secure SameSite cookies for browser sessions
- CSP + XSS protections
- DPoP or mTLS for high-value APIs
- Nonce/timestamp request signing for sensitive operations
- Telemetry and replay detection
Simple rule of thumb
- If a token is a bearer token, assume it can be stolen and reused.
- To stop replay, either:
- make the token short-lived, and/or
- bind it to the client, and/or
- sign each request with unique freshness data.
If you want, I can give you:
- a browser API design that resists token theft,
- a DPoP example, or
- a request-signing scheme you can implement in your API.