Sentinel rate limiting
Per-IP, per-user, per-endpoint throttles.
A login endpoint with no rate limit is a password guessing service you host for free. Rate limiting is the cheapest security control there is, and the one most often configured in a way that quietly does nothing.
Two limits, doing different jobs
Grit mounts Sentinel with a global per-IP limit and a tighter per-route limit on the endpoints worth attacking:
// Everything, per IP.ipLimit := &sentinel.Limit{Requests: 100, Window: 1 * time.Minute}// The endpoints an attacker actually wants.return map[string]sentinel.Limit{v + "/login": {Requests: 30, Window: 5 * time.Minute},v + "/register": {Requests: 10, Window: 15 * time.Minute},v + "/forgot-password": {Requests: 10, Window: 15 * time.Minute},v + "/reset-password": {Requests: 10, Window: 15 * time.Minute},v + "/totp/verify": {Requests: 20, Window: 5 * time.Minute},}
The numbers are not arbitrary. 30 logins in five minutes is more than any human needs and far less than a guessing attack wants. Registration is tighter because the cost of a false negative is an inbox full of signups rather than a locked-out user. totp/verify is on the list because a six-digit code is a million guesses, and without a limit that is an afternoon.
The mistake that makes it do nothing
Read the comment Grit carries at the mount point. It is there because it already happened:
// Built from the prefix the router mounts, because Sentinel matches the// request path exactly. Written as /api/auth/login these never matched a// request, so the login limit and AuthShield never fired.routeLimits := authRouteLimits(false)
The limits were configured. The dashboard showed them. The paths were wrong by one version segment, so nothing ever matched, and the failure mode of a rate limit that does not match is silence. No error, no warning, and a login endpoint with no limit on it.
A rate limit is one of the few controls whose failure looks exactly like success: traffic flows, nothing is blocked, everybody is happy. The only way to know it works is to exceed it on purpose and watch for the 429.
Development limits, and why they are separate
The same function returns relaxed numbers when APP_ENV=development: 100 requests a minute instead of 30 in five, so nobody is locked out while testing a login form.
This is worth doing deliberately rather than by disabling the middleware in development, because a control that is off in development is a control nobody exercises until production. Relaxed and present beats absent.
What rate limiting cannot do
- A distributed attack. Per-IP limits assume the attacker has few addresses. A botnet has thousands, each staying under your limit. That is what Sentinel's auth shield and anomaly detection are for: they look at the account and the pattern rather than the address.
- A correct password. Rate limiting buys time against guessing; it does nothing against a credential already leaked elsewhere. That is what the breach check and 2FA are for.
- Expensive single requests. One report query that scans ten million rows is within every limit you have. Body size caps and statement timeouts cover that, not request counts.
Where state lives
Counters have to be shared or they are per-process, and a limit of 30 across four replicas is a limit of 120. Grit configures Sentinel storage explicitly for that reason, falling back to a local SQLite file only when nothing else is set:
sentinelStorage := sentinel.StorageConfig{Driver: sentinel.SQLite,DSN: config.ResolveSQLitePath("sentinel.db"),}
Try it
One. With the API running in production mode, post wrong credentials to your login endpoint in a loop. Record which attempt first returns 429.
Two. Change one route limit key to a path that does not exist, such as dropping the version segment. Restart and repeat the loop.
Three. Write down what, if anything, told you the limit had stopped working.
Quick check
Spot a typo? Have an idea?
Help us improve this lesson. One click opens a GitHub issue with the lesson URL pre-filled, suggest clearer wording, report a bug, or request more depth. The course keeps improving thanks to learners like you.
Suggest an improvement on GitHub