fix(auth): codes wait after ten wrong in a row, and the owner is told

Whoever knew the password could guess codes at 10 an hour forever,
unnoticed, each guess checked against every authenticator (the second
reading's M4). Every code an account's second factor refuses after a
right password (sign-in, a phrase's reset, a change) or from a signed-in
device (removing an authenticator, new recovery codes, the code from one
confirmed before) now counts, per account, from every network; after
every tenth in a row no code is taken (codesWait, saying until when), an
hour, then twice as long each time, a day at the most (longestCodeWait,
second_factor_waits.dart): at a day, ten guesses a day still have about a
1 % chance a year against one authenticator, so the first wait mails the
owner that someone knows their password. The instance keeps no address,
so sign-in and the phrase's reset now send the address the device typed
(and its language), used only for that mail after its hash is checked
against the account's. A code accepted clears it all.

Migration (module, Learn's and Todo's servers, Learn's device schema):
CREATE TABLE "second_factor_failures" ("id" bigserial PRIMARY KEY,
"authUserId" uuid NOT NULL, "failures" bigint NOT NULL, "waits"
bigint NOT NULL, "waitUntil" timestamp, "updatedAt" timestamp NOT
NULL) with a unique index on "authUserId": a new table, nothing
touched.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Tizian 「ティツィアーン」 <raisondetredev@pm.me>
This commit is contained in:
Tizian 「ティツィアーン」 2026-10-20 21:47:38 +02:00
commit 4a32f150db
Signed by: raisondetredev
GPG key ID: 07F6FCF405488B64

View file

@ -654,6 +654,8 @@ class _AccountRecoveryEndpoint {
String? totpCode,
String? recoveryCode,
required String host,
String? address,
String? language,
}) async {
return _ist.callAwaitableFunctionAndHandleExceptions(() async {
var _localUniqueSession =
@ -673,6 +675,8 @@ class _AccountRecoveryEndpoint {
'totpCode': totpCode,
'recoveryCode': recoveryCode,
'host': host,
'address': address,
'language': language,
}),
serializationManager: _serializationManager,
);
@ -1168,6 +1172,8 @@ class _PasswordSignInEndpoint {
_is.UuidValue? request,
String? code,
required String host,
String? address,
String? language,
}) async {
return _ist.callAwaitableFunctionAndHandleExceptions(() async {
var _localUniqueSession =
@ -1189,6 +1195,8 @@ class _PasswordSignInEndpoint {
'request': request,
'code': code,
'host': host,
'address': address,
'language': language,
}),
serializationManager: _serializationManager,
);