fix: delete login counters that drop to zero

A reserve returned after a KDF budget rejection or 503 left a zero key
in Valkey until the end of the window, so a flood with new usernames
created keys past the address limit and could evict real counters.
SETTLE_SCRIPT and LocalLoginLimiter now delete a counter at zero.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-18 10:42:25 +03:00
co-authored by Claude Opus 5.5
parent 3f4771c9d9
commit fa5ba51d6a
10 changed files with 163 additions and 30 deletions
+39
View File
@@ -6749,10 +6749,42 @@ def _login_at_once(
return at_once(len(actors), login)
def counter_keys(stand: Stand) -> dict[str, str]:
"""Ключ счётчика входа → значение (hook `login-keys`)."""
return {
key: value
for key, value, _ in (
line.split() for line in stand("login-keys").splitlines()
)
}
@pytest.mark.spec("login-protection", "Бюджет входа занят")
def test_kdf_budget_login(uploads_api: Api, stand: Stand) -> None:
"""И задача 13.8 change `auth-hardening`: попытка, которой отказал
бюджет (или успешный вход), возвращает резерв, и счётчик, дошедший до
0, удаляется — после всплеска в Valkey только ключи неудачного входа
(пара и адрес со значением 1), а не ключи со значением 0 на каждую
попытку."""
assert_uploads_auth_stand()
actors = [uploads_api.register() for _ in range(KDF_CLIENTS)]
before = counter_keys(stand)
# неудача до всплеска: её два ключа доказывают, что API считает в
# Valkey, а не в процессе (окно пропуска кэша после прошлых тестов)
stranger = unique_name()
deadline = time.monotonic() + CACHE_RETRY_AFTER_S + 2
failed: dict[str, str] = {}
while len(failed) < 2 and time.monotonic() < deadline:
assert_unauthorized(
fail_login(uploads_api, stranger, address="198.51.100.99"),
BASIC_CHALLENGE,
)
failed = {
key: value
for key, value in counter_keys(stand).items()
if key not in before
}
assert len(failed) == 2, f"no login counters in Valkey: {failed}"
answers = _login_at_once(str(uploads_api.client.base_url), actors)
statuses = [resp.status_code for resp in answers]
assert set(statuses) <= {201, 429}, statuses
@@ -6779,6 +6811,13 @@ def test_kdf_budget_login(uploads_api: Api, stand: Stand) -> None:
busy,
[actor.username for actor in actors],
)
after = counter_keys(stand)
new = {key: value for key, value in after.items() if key not in before}
# только ключи неудачи, с тем же счётом; нулевых нет
assert new == failed, (
f"login counters after the burst: {sorted(new.values())}, "
f"before it: {sorted(failed.values())}"
)
@pytest.mark.spec("login-protection", "Бюджет регистрации занят")