Skip to content

Forget only the failed logins of the name that just logged in - #276

Merged
danielplohmann merged 2 commits into
masterfrom
fix/login-throttle-own-failures
Oct 16, 2026
Merged

danielplohmann merged 2 commits into
masterfrom
fix/login-throttle-own-failures

Conversation

@r0ny123

@r0ny123 r0ny123 commented Oct 9, 2026 •

Copy link
Copy Markdown
Collaborator

The login throttle from #101 is keyed on the source address: ten failures in fifteen minutes and the address is refused on /login and /register. A successful login then forgot every failure the address had made (mcritweb/db.py:801, clear_login_failures), so that someone who mistyped their own password a few times does not carry the count around. That reset was available to anyone holding an account - a self-registered pending one is enough, since /login checks no role. Nine wrong guesses at another account's name, one login as themselves, and the budget was full again. The same reset applied to the registration token: /register recorded a wrong token under whatever username was typed, and the attacker types their own.

Change. clear_login_failures(remote_addr, username) deletes only the rows recorded under the name that just authenticated (mcritweb/db.py:801-818); login() passes the typed name (mcritweb/views/authentication.py:297). /register records a wrong token under register:<name> (mcritweb/views/authentication.py:42-48, used at :201 and :215). No account can carry that string: the username pattern on /register and on /admin/change_username allows letters, digits, dots and underscores only (its $ also accepts one trailing newline, but never a colon), so no login ever reaches those rows. test_no_account_can_be_named_like_a_registration_token_counter pins that, since the patterns live elsewhere. The lookup is an exact WHERE username = ?, with SQLite's default binary collation, so a case variant of a name is a different name.

before after
successful login as mallory deletes every row of the address deletes the address's rows for mallory
wrong registration token typed as mallory one row for mallory one row for register:mallory
throttle decision all rows of the address, unchanged all rows of the address, unchanged

The cost. Failures under a mistyped username stay counted until the window ends, even once the right name has logged in: two attempts as alcie followed by a login as alice leave the address with two of its ten attempts spent for the rest of the fifteen minutes. That is the right side to err on. The throttle meters guesses made from an address, and only a name the person actually logged in with is proven to be theirs; the alternative of also clearing rows for names that do not exist would hand any account holder an unmetered way to confirm which names exist (#101).

Verification. Measured offline, through HTTP against a loopback app. A reproducer that alternates nine wrong guesses at a victim's name with a login as the attacker's own account, on a scratch copy of master and of this branch, both at 1cdc195: master lets all 54 guesses through unthrottled and the victim's password still logs in afterwards; the branch refuses the guesses once ten are counted. The registration-token variant, 54 wrong tokens in six rounds of nine plus a login: master refuses 0 of 54, the branch 44 of 54. A second probe that types the attacker's name in three variants (as is, capitalised, with a trailing newline on an account of exactly that name) is throttled in every variant, and register:... names are refused by both /register and /admin/change_username.

Tests. tests/testLoginThrottle.py:102 (another account's login does not forget the failures) and tests/testLoginThrottle.py:191 (a login does not forget wrong registration tokens). Both fail on master and pass here, and test_no_account_can_be_named_like_a_registration_token_counter fails if the registration pattern ever accepts a colon; the existing test_a_successful_login_forgets_the_failures still passes, so a person's own mistyped passwords are still forgiven.

Relation to open PRs. fix/login-keeps-account-changes is stacked on this branch: it changes the line directly above clear_login_failures in login(), so the two could not be kept independent. Merge this one first. A trial merge (git merge-tree) with #254 and #262 is clean. #160 already conflicts with master in twelve files, authentication.py among them; this branch adds no file to that list.

Not changed here: parallel guesses that all pass the throttle check before any of them is recorded. That is on master and independent of the reset.

A successful login deleted every login_attempt row of the address. The
throttle is keyed on the address, so anyone holding an account - a
self-registered pending one is enough, login checks no role - could guess
at another account's password without end: nine wrong guesses at the
victim's name, one login as themselves, repeat. The same reset applied to
the registration-token budget, because /register recorded a wrong token
under whatever username was typed, and the attacker types their own.

Clear only the rows recorded under the username that just authenticated.
A wrong registration token is recorded under "register:<name>", which no
account can carry (the username format forbids ':'), so no login reaches
those rows at all.

The cost: failures under a mistyped username stay counted until the
window ends, even once the right name has logged in. Those rows are
guesses the address made, and only a username the person actually logged
in with is proven to be theirs.
Wrong registration tokens are counted under register:<name> so that a
login can never clear them, which only works while no username can hold
a colon. The username patterns that make sure of that live in two other
places, so a test now tries to register such a name.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants