Lesson 5 of 9 · 25m
Identity Keying, NAT, Botnets, and Abuse Evasion
Choose limiter keys that survive IP rotation and shared networks while matching the abuse model instead of punishing the wrong customer.
Every experienced API operator eventually gets the same support ticket: "Your API is broken; our whole office is rate limited." The office is behind one NAT address. That same week, an attacker rotates through a botnet and each source IP looks innocent. Source IP is simultaneously too coarse and too narrow. Good keying starts with the abuse model and then admits how unreliable network identity is.
Key by the actor and the target separately
Authentication abuse needs at least two dimensions:
source identity credential or API key, account, device, IP subnet
target identity username, email, phone number, tenant, objectIf you key only by source account, an attacker can try unlimited passwords against many victim accounts. If you key only by target account, one victim can be locked out by strangers. Composite controls are normal:
5 attempts per target account per 15 minutes
20 attempts per source /24 or ASN grouping per 15 minutes
100 attempts per API credential per hourExact numbers depend on legitimate behavior; the structure is the lesson.
Never trust client-supplied forwarding headers
This configuration is an incident waiting for a demo:
client_ip = request.headers.get("x-forwarded-for", request.remote_addr)A public API must derive forwarding identity only from trusted proxy layers. If your platform terminates at a load balancer, use the platform's verified client IP field or strip untrusted forwarding headers there. Otherwise attackers can mint limiter identities with:
curl -H 'X-Forwarded-For: 203.0.113.9' -H 'X-Real-IP: 198.51.100.7' https://api.example.internal/loginThose are documentation ranges, but the technique is real.
IPv6 makes naive subnet math useless
Rate-limiting individual IPv4 addresses was never strong. IPv6 rotation makes it weaker because attackers may control enormous prefixes. Consider progressive aggregation: exact address for a low threshold, /64 or larger for a higher threshold, ASN or reputation signals as advisory context. Do not block an entire ASN casually; mobile carriers and cloud platforms contain both victims and attackers.
API keys are identity only if lifecycle is controlled
A key that can be minted endlessly by a trial account is not a stable abuse boundary. Key-based limits need issuance quotas, lifecycle controls, workspace inheritance, and revocation. Otherwise the attacker treats keys as ammunition. When keys belong to a tenant, enforce both key and tenant ceilings so key rotation cannot multiply the tenant allowance.
Fingerprinting is useful evidence, weak as sole truth
Device fingerprints, TLS client hints, user agents, and behavioral timing can help risk engines, but they are malleable. Treat them as correlation signals, not the canonical bucket key. The strongest designs combine stable account identity for authenticated calls, verified network identity for unauthenticated calls, and target identity for abuse against third parties.
Fairness includes shared infrastructure
Large customers legitimately NAT thousands of users behind one egress address. Solve this by moving authenticated traffic to account or token keys and reserving IP limits for unauthenticated or pre-authentication abuse. If the login endpoint cannot yet know whether credentials are valid, you can still bind attempts to the claimed username plus verified network identity without trusting the claim as authorization.
Field notes
- A limiter keyed only on IP often hurts universities, mobile networks, offices, and hosted CI systems first.
- Attackers do not need to spoof TCP source addresses when they can use proxies, cloud functions, or botnets.
- Do not concatenate untrusted identity fields into storage keys without normalization and length limits.
- Measure false positives by customer segment; a single global complaints number hides who is actually being punished.
Was this lesson helpful?