Skip to content

Brute force and lockout

In short

The threat

An attacker guesses passwords as fast as the server answers, and a naive lockout lets anyone lock the owner out by failing on purpose.

The rule

Reserve each attempt before the hash, keep two budgets, and lock only briefly, with a capped backoff the owner’s own devices get through. No lock is permanent unless the app opts in.

In the package
throttle/throttle.ts, configured under throttle.lockout.

Without a limit, an attacker can try passwords against an account as fast as the server answers. The classic fix, locking the account after N failures, creates a second attack: anyone who knows an address can keep its owner locked out forever by failing on purpose. Limits keyed only on IP fail the other way, because attackers rotate through proxy networks while real users share office and mobile IPs.

One IP alone can never lock the account: its own budget runs out first, and only attempts from many IPs together reach the lock.

Each login attempt takes a place in its budgets before the password is hashed. In one 15-minute window, one account may take 5 attempts from one IP: the 6th is refused for that IP only, and the owner on another network still signs in. From every IP together it may take 10: the 11th is refused and locks the account.

Text version

Count every attempt, keep the lock short, and never let it stand between the owner and their account.

Reserve the attempt before checking the password

Section titled “Reserve the attempt before checking the password”

The counters are incremented before the hash runs, atomically, so fifty parallel guesses cannot all slip in before the first failure is recorded.

Budget Default Stops
Per account and IP (perAccountIp) 5 attempts per 15 minutes One source guessing at one account. When it runs out, only that source is refused; the owner on their own network signs in normally.
Per account (perAccount) 10 attempts per 15 minutes from all sources A distributed attack on one account. Close to one guess a minute.

Exhausting the account budget locks the account for 5 minutes, then 15, then 45, capped at 1 hour.

No lock lasts longer than 1 h, and a quiet day starts the ladder over. A lock that never lapses exists only if the app opts into it.

Each new lock lasts 3 times the last, capped at 1 h: 5 min, 15 min, 45 min, 1 h, 1 h. After 24 h with no new lock the next one is 5 min again.

Text version
  • One step per lock, not per request. Requests that cross the limit while a lock is already running (a parallel burst that read “no lock” a moment before, or wrong passwords let through by a device cookie) still count, but leave the lock’s end and its step alone.
  • A quiet day resets it. Every new lock renews a 24-hour quiet period; only a full day without a new lock resets the backoff to 5 minutes.
  • No permanent lock unless the app opts into one (below).
  • A correct password returns only its own source’s budget: that network’s per-(account, IP) budget, or that device’s budget, never the account-wide one, so an attacker cannot reset the shared budget by signing in once. An app whose users sign in very often can opt out with countSuccesses: false: then a completed sign-in also hands its account-wide slot back, but only once every factor is proved, matched by count and never more slots than correct passwords reserved in the current window, except that the check is not atomic: a completion racing the first attempt of a new window, or racing a password reset, can hand back one slot of the new budget (A110).
  • The ladder is yours to change: durations replaces the multiplied lock with a list of your own, the last entry repeating.
  • A completed password reset lifts the temporary lock and its history, since it proves the mailbox.

Capping the lock is not enough on its own: an attacker who spreads failures over many addresses can renew the lock every few minutes, all day. So every new session, issued only after a full login with every factor, comes with a signed device cookie (__Host-device, HttpOnly). Its HMAC binds a random device id to the user id, the user’s revocation epoch and the issue time.

A lock does not keep the owner out: their device cookie lets a correct password through, and gives an attacker who steals it only a few guesses.

Under any lock, a login that carries the owner's valid device cookie is not refused by the lock: a correct password gets in. Wrong guesses through device cookies are limited to 5 per 15 min, for all of the account's devices together; the 6th attempt is refused.

Text version

It only matters under a lock. While the account is unlocked, a login takes the normal path whatever cookie it carries, so the owner can sign in as often as they like without touching any device budget.

Under a lock, it lets only a correct password through. A login with a valid cookie is not refused by the lock. Instead, before the password check, the attempt reserves a slot in two budgets, atomically, the same way the other counters are reserved: its cookie’s own budget and one shared by all of the account’s devices (5 per 15 minutes each).

  • A correct password hands both slots back, so it costs nothing.
  • A wrong one keeps them and also counts against the account-wide budget, like any other failure.
  • Every cookie is refused once the devices together hold 5 slots, until the window ends.

Because the shared slot is taken before the check, holding ten cookies gives no more guesses than holding one, even when they all fire at once.

It expires. The server refuses a cookie older than 30 days (throttle.lockout.deviceMaxAge), checked on use, whatever the browser kept. Every new session sets a fresh one.

It is revoked with the sessions. A password change, a completed reset and “sign out everywhere” bump the revocation epoch, which voids every device cookie issued before them, so a stolen cookie dies with the stolen password.

The cookie skips no password check and grants no session. The test suite runs the hard cases:

Case Result
The owner signs in every hour while an attacker sends thirty failures an hour from fresh addresses A stranger is refused every hour; the owner gets in
Ten cookies minted before a reset Useless after it
Fifty guesses spread over ten valid cookies during a lock Five password checks in all
Eighty wrong guesses fired at once from four cookies Five password checks in all
Ten correct logins in a row with the cookie, locked or not All succeed

A stolen session cookie that is alive but not fresh is one more way to guess the password: reauthenticate and changePassword both prove it again, and a right guess returns a fresh session. So both doors reserve each attempt in login’s budgets, through the same gate and the same counters, before the password check: the per (account, IP) and account-wide budgets while the account is unlocked, so guesses through login and both doors add up.

Under a lock the doors are not simply refused, because that would let anyone who locks the account also block the owner’s step-up. The live session stands in for a device cookie instead: the attempt reserves a slot in the session’s own budget and in the budget shared by all of the account’s devices, with the same rules as above. The owner still steps up under a lock someone else set, and a stolen session gets no more checks than a stolen device cookie would at login, from a budget shared with every device (A102). A refusal answers like a wrong password, after the same password check. Each door’s own rate limit (throttle.doors) still applies first.

Two limits of this are worth knowing. First, the shared device budget can be spent by someone who both locks the account through login and holds a stolen session: the doors then draw on the budget every device of the account shares, so the owner’s own device cookie can be refused at sign-in and at step-up until the lock ends. A stolen device cookie carries the same risk. A password reset clears the lock and ends every session, which closes it. Second, a correct proof at either door while the account is unlocked still counts toward the account-wide budget: success returns only the per (account, IP) slot, as it does at login. An app that asks its users to re-authenticate very often can lock its own user and send a lock notice.

An app that would rather bound these doors only by their own per-user limits sets throttle.lockout.sessionProofs: 'door'. Then a stolen session cannot lock the owner out of sign-in by guessing passwords at these two doors, nor climb the permanent tier there, and a temporary lock does not refuse the owner’s correct password; in exchange those guesses run at the doors’ own rate, each door on its own key, and never lock anything. Wrong second-factor codes at a step-up are another budget: they still lock the factor and count toward the permanent tier (A002, Configuration).

Only the password proofs share login’s budgets, and with sessionProofs: 'door' not even those: then each door’s own limit is their only budget. A second-factor code has its own separate budget (see the two-factor plugin), and the other doors (password reset, email change, email verification) use only their own rate limits.

An app that checks a proof of its own (its own second factor, a PIN) can count each failure in login’s account-wide budget with auth.context.recordFailedProof({ userId, meta }) (or { email, meta }). Pass only the account the request is already acting on, the user of its session or challenge, never one named in the request: the call locks whatever account it is pointed at. A lock it sets is carried out like a password lock: the owner’s lockout mail, the account.locked audit row and, in the permanent tier, the end of the sessions and the onPermanentLock hook (A109). The lower-level throttle.recordFailure only counts, so a lock reached through it reaches nobody.

An app whose proof has its own budget and its own lock, as the two-factor plugin’s has, calls auth.context.escalateLock({ userId, meta }) once for each new lock instead: with the permanent tier on, the lock counts toward it, and a permanent lock is carried out the same way. Finish your own second factor with issueSession and via: 'second-factor': that is what tells the lockout the sign-in completed.

Each new lock mails the account owner once, who can reset the password if the attempts were not theirs.

See Enumeration: the counters are keyed on the address so unknown addresses lock too, a locked login still runs the full password verify, the lock’s audit row and owner mail run off the response path, and the answer is invalid_credentials either way.

Login, sign-up, reset requests, code checks and password changes each have a fixed window per IP or per user. Reset-code guesses are also counted per address, from every network, whether or not the address has an account or a code outstanding. Limits fail closed: if the counter store is down, the request is refused, not let through.

The defaults written out, and a login route that passes the device cookie and answers every refusal the same way.

lockout.ts
import type { LockoutOptions } from '@noctcore/auth';
// The defaults, written out. Pass them to createAuth as
// `throttle: { lockout }`, or leave the option out for the same values.
export const lockout = {
// One account from one IP, then from every IP together.
perAccountIp: { limit: 5, window: '15m' },
perAccount: { limit: 10, window: '15m' },
// Each new lock is three times the last, capped; a quiet day resets it.
baseLock: '5m',
maxLock: '1h',
quietPeriod: '24h',
// The owner's device cookies under a lock: each cookie's budget,
// and the budget all of them share.
perDevice: { limit: 5, window: '15m' },
deviceMaxAge: '30d',
// Off by default: a lock that never lapses, and an answer that
// says "locked".
// permanent: { afterLocks: 3 },
revealLocked: false,
} satisfies LockoutOptions;

Opt-in: a permanent tier and an explicit locked answer

Section titled “Opt-in: a permanent tier and an explicit locked answer”

Some apps need what the defaults leave out on purpose. Both options are off unless you turn them on, and each gives something up.

throttle.lockout.permanent: { afterLocks }. After afterLocks temporary locks in a row (each within the quiet period of the last), the next lock never lapses.

In a row means with no completed sign-in between. A sign-in completes when every factor is proved and the session is issued: then the owner is proved, so the count starts over (not the temporary backoff). A correct password with a second factor still owed does not count, nor does a correct password at reauthenticate or changePassword, which proves no second factor: otherwise an attacker who holds the password could start the count over before each round of guessed codes and never reach the tier (A108). A completed password reset proves the mailbox and resets it too, with the temporary lock and its backoff.

Second-factor locks count too. A lock of the two-factor plugin’s factor (its own budget, below) counts toward the tier in the same count as the password locks, so guessing codes with the password in hand reaches the permanent lock as surely as guessing the password. Getting there needs the password (or a live session, or the mailbox for a reset), a far higher bar than the address alone. An app with its own second factor does the same with context.recordFailedProof or context.escalateLock (see Your own proofs).

Only an admin lifts it, with auth.lockout.clear({ email }) or ({ userId }); auth.lockout.inspect reads the state. A password reset does not lift it, and waiting does not either. The clear is not atomic: a burst of failures running during it can lock the account again at once.

It follows the account. For an address with an account the lock is kept under the user id, so moving the account to another address does not escape it. An address with no account reaches it too, kept under the address, and a sign-up on that address clears it so the new owner does not inherit it.

It ends the account’s sessions. A lock that left sessions alive would be a way around it: a session could reauthenticate and move the account to an unlocked address. So no session may change the email while it holds (attack A086).

It tells the owner and you. When it locks a real account, the owner gets a lockout mail with permanent: true, an account.locked_permanently audit row is written, and the onPermanentLock hook runs off the response path, the place to page support. A permanent lock a second factor’s lock brings about is carried out the same way.

In this mode the lock deletes the sessions without voiding the device cookies, and a session from a device sign-in may reauthenticate and change the password. A few other ways in still mint a session, each needing a correct password or your SSO: an app’s SSO through issueSession, a two-factor challenge started before the lock, and a login racing the revocation. None of those sessions can change the email.

Set trustedDevices: false only when the lock must hold against the owner too: the lock then bumps the revocation epoch, ending every session and device cookie, and refuses every session door (issuing a session by any path, validating one, reauthentication, password change, email change, email verification) until an admin clears it (attack A084).

throttle.lockout.revealLocked: true. Login answers account_locked while the address is locked, with retryAfterSeconds for a temporary lock and none for a permanent one, instead of invalid_credentials. A wrong password and one source’s spent budget keep the generic answer.

The cost is enumeration: the answer tells anyone who tries the address that it is locked and when it lifts. Unknown addresses lock and answer the same after the same work, so it does not prove an account exists by itself, but it is a signal the uniform answer never gives (attack A085; see Enumeration).

Whichever you turn on, put a captcha on login and keep the per-IP door limits, so locking an address costs the attacker real work, and keep the admin tools on admin-only routes: they answer for any address.

The guide advises against account lockouts and exponential backoff, for exactly the victim-lockout reason above, and suggests a token bucket of about one attempt a minute per user. The package keeps a lockout because the owner chose one, but shapes it to remove the abuse:

  • the per-(account, IP) budget isolates an attacker’s source;
  • the account-wide lock is capped at an hour, with no permanent tier by default;
  • its rate is close to the guide’s one a minute;
  • the owner’s trusted devices bypass it.

It also agrees with the guide’s warning on strict IP limits: the per-IP door limits are generous (10 a minute) and exist to stop floods, not guessing.

Test Attack Stopped by
A001 Brute force by parallel guesses throttle/throttle.ts
A002 Locking the victim out throttle/throttle.ts, flows/password-reset.ts
A005 The locked-account oracle throttle/throttle.ts, flows/login.ts
A009 Forging or hoarding trusted-device cookies throttle/device-token.ts, throttle/throttle.ts
A011 Spoofing the client IP http/ip.ts
A032 Getting past a tRPC rate limit while its counter store is down auth-trpc/middlewares.ts
A038 Brute forcing the second factor from many IPs throttle/throttle.ts, plugins/two-factor/proofs.ts
A084 Locking the owner out for good through the permanent lockout tier throttle/throttle.ts, flows/lockout-admin.ts
A085 Learning account state from the explicit locked answer throttle/throttle.ts, flows/login.ts
A086 Escaping a permanent lock through a live session flows/permanent-lock.ts
A094 Escaping a per-user tRPC rate limit with a made-up session, or spending another user’s budget auth-trpc/middlewares.ts
A102 Guessing the password through a stolen session flows/password-change.ts, throttle/throttle.ts
A108 Never reaching the permanent tier while guessing the second factor with the password in hand core/auth-core.ts, throttle/throttle.ts, plugins/two-factor/runtime.ts
A109 Locking an account through a failed proof outside login, with nobody told core/auth-core.ts, flows/lock-notice.ts
A110 Winning back the lockout budget without a completed sign-in, with successes not counted throttle/throttle.ts, core/auth-core.ts