Prompt
What's the safest way to stop BOLA, scraping, and replay attacks on APIs?
Latest observation
The safest approach is defense in depth: combine strong authorization, anti-enumeration controls, abuse detection, and request integrity / replay protection. No single control stops all three.
1) Stop BOLA (Broken Object Level Authorization)
This is the big one for APIs.
Do not trust object IDs from the client.
For every request that accesses a resource, check:
- Who is calling (authenticated user/service identity)
- What they’re allowed to access
- Whether that specific object belongs to them or is otherwise authorized
Best practices
- Enforce authorization server-side on every object access.
- Prefer resource-scoped authorization checks in the data layer/service layer, not just in the controller.
- Use deny by default.
- Avoid exposing raw sequential IDs if possible; use opaque identifiers (UUIDs, ULIDs) as a helper, not a security control.
- Filter list endpoints so users only see objects they can access.
- Test with direct object tampering: change IDs in path, query, and body.
Strongest pattern
Use policy-based authorization:
- e.g., “user can read invoice if invoice.account_id == user.account_id”
- or explicit ACLs / RBAC + ownership checks
2) Reduce scraping
Scraping is usually about automation and large-scale abuse, not a single auth flaw.
Best practices
- Rate limiting per user, IP, token, device, and endpoint
- Quota management and burst control
- Pagination limits and capped result sizes
- Require auth for valuable data
- Dynamic throttling based on behavior and risk signals
- Anomaly detection for high-volume sequential access, unusual patterns, or high failure rates
- Bot/abuse detection at edge/API gateway
- Device/session fingerprinting where appropriate
- Harden list/search endpoints:
- prevent bulk enumeration
- require filters
- limit export functionality
- Challenge mechanisms for suspicious traffic (step-up auth, CAPTCHA for user-facing flows)
Helpful but not sufficient
- Hiding endpoints
- Obfuscating IDs
- User-agent checks
These slow attackers down, but do not stop serious scraping.
3) Prevent replay attacks
Replay attacks happen when an attacker captures a valid request and resends it.
Best practices
- Use TLS everywhere
- Include a timestamp/expiry in signed requests
- Add a nonce / unique request ID and reject duplicates
- Use short-lived access tokens
- Bind requests to:
- a specific client key/session
- a nonce
- a narrow time window
- For high-risk APIs, sign the request body + headers with a shared secret or private key
- Keep a server-side replay cache of seen nonces/request IDs within the validity window
- For state-changing operations, use idempotency keys carefully:
- helpful for safe retries
- but not a substitute for anti-replay unless validated and stored server-side
Important note
If you’re using bearer tokens alone, anyone who steals one can replay it until it expires. Use:
- very short expirations
- refresh-token rotation
- sender-constrained tokens if possible (mTLS, DPoP, or similar)
Safest practical architecture
If you want the most robust baseline:
- Authenticate every request
- Authorize every object access server-side
- Use opaque, hard-to-enumerate IDs
- Rate limit and quota by identity + IP + behavior
- Add anti-replay:
- timestamp
- nonce/request ID
- short expiry
- server-side replay cache
- Monitor and alert on enumeration patterns, spikes, and high-error automation
- Protect high-value endpoints with step-up auth or stronger request signing
What not to rely on
- Sequential IDs “fixed” by UUIDs alone
- CORS
- User-agent checks
- Obfuscation
- Client-side permission checks
- Rate limiting alone for BOLA
If you want one concise rule
- BOLA: enforce per-object authorization on the server
- Scraping: rate limit + behavioral detection + quotas
- Replay: signed requests with nonce/timestamp + short-lived credentials
If you want, I can turn this into a concrete API security checklist or show sample request-signing and anti-replay logic in code.