Table of Contents

Class PasswordAttemptLimiter

Namespace
MailFathom.Infrastructure.Security.Passwords
Assembly
MailFathom.Infrastructure.dll

Bounds how often one source and one username may have a password checked.

public sealed class PasswordAttemptLimiter : IDisposable
Inheritance
PasswordAttemptLimiter
Implements
Inherited Members

Remarks

The transport's own limiter counts requests per authenticated caller, and a request guessing a password has no authenticated caller — every one of them lands in the surface's shared anonymous bucket, which is a bound on the endpoint rather than on the guessing. This is the bound on the guessing, and it is two axes because the two attacks are different: one host trying many passwords is caught per source, and many hosts trying one account's passwords are caught per username.

A username never reaches a partition key. Both keys are HMAC-SHA-256 digests under a key this process generates at construction and never publishes, so what is held in memory, what a dump of this object would show, and what any future metric could tag are all a value nothing outside this process can reverse — while two attempts against one username still land in one partition, which is the whole of what the limiter needs. The source is digested for the same reason, a remote address being personal data in its own right.

Two different things are bounded here, and conflating them refuses a user. One is how many wrong passwords a minute an axis admits, which is what AttemptsPerMinute states. The other is how many derivations may be in flight for one axis at once, which is what stops a caller opening five hundred connections from making this process perform five hundred concurrent PBKDF2 derivations. They are separate limiters because a permit held across a derivation is also a cap on simultaneous requests, and HTTP Basic re-presents the credential on every request with no session — so a single limit serving both would refuse a user's eleventh parallel call with the answer a wrong password gets, however right their password was.

So a verification permit is taken before the derivation and returned whatever the answer was, while a failure permit is taken only by Spend() and held for MailFathom.Infrastructure.Security.Passwords.PasswordAttemptLimiter.SpentAttemptWindow, which is what turns AttemptsPerMinute into an allowance per minute rather than a ceiling the process never recovers from. Reserve(PasswordAttempt) reads the failure axis before it takes a verification permit, so an axis that has spent its allowance is refused before anything expensive happens.

Reading the failure axis rather than taking from it leaves a window between the reading and the spending, and ConcurrentVerificationsPerPartition is what bounds it: at most that many derivations for one axis are ever in flight, so a burst arriving together can overshoot the stated allowance by at most one such window's worth before the axis closes, rather than by as many connections as an attacker cares to open. That is the honest bound — AttemptsPerMinute plus at most ConcurrentVerificationsPerPartition derivations in the first minute of a burst, and AttemptsPerMinute a minute after it.

A per-partition ceiling is not a bound on this process, which is why there is a third one. Every distinct username is a fresh partition with a fresh ceiling and a fresh allowance, and behind a declared proxy the username is the only axis there is — so a caller presenting five hundred well-formed usernames at once would meet no per-partition limit at all and have this process derive five hundred times concurrently, each derivation deliberately expensive and each occupying a thread. The surface's own ceiling is the bound that does not move with what a caller names: one partition per transport surface, sized by that surface's own MaxConcurrentVerifications and consulted before either of the others, so what a caller can spend of this process is decided by the surface it reached rather than by how many names it thought of.

The username axis partitions on the whole login a caller presented. Two logins that differ in any part are two allowances, so one login's guesses never spend another's, and the value is the canonical one the credential store resolves rather than the header's spelling of it.

Nothing queues: a request that is out of capacity is refused immediately rather than held, because a queue in front of a deliberately expensive verification is a way to make this process hold connections on an attacker's behalf. The partitions are reclaimed once they go idle by the same machinery the endpoint limiters run on, and a partition holding a spent attempt's permits is not idle.

An attempt whose source this deployment cannot tell apart is bounded by username alone. Behind a reverse proxy every request arrives from the proxy's own address, so a per-source partition there would be one partition for the whole world — and a single guesser emptying it would close password sign-in for every user at once. Source is therefore null in that arrangement, and the source axis is skipped rather than applied to a value that distinguishes nobody.

The per-username failure axis is shared with the user it protects, deliberately. Somebody who knows a username can spend its allowance on wrong passwords and have that user's correct password refused until the window elapses. Nothing can avoid it while the answer is unknowable before the derivation, which is what the axis exists to bound; what the design chooses is the cost of it being one minute rather than a lockout an operator has to lift. The source axis is what catches the caller doing it, wherever this deployment can tell one caller from another.

Constructors

PasswordAttemptLimiter(TimeProvider)

Initializes the two axes every attempt is counted on, each bounding derivations in flight and wrong passwords a minute separately.

Fields

ConcurrentVerificationsPerPartition

How many password verifications may be in flight for one axis at once.

Methods

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

Reserve(PasswordAttempt)

Takes one attempt's verification capacity on every axis that applies, before the password is verified.