fix: precompute dummy password hash at startup
- lifespan computes the dummy Argon2id hash: first unknown-user login takes as long as a wrong password - evidence for session TTL from config.toml, default and environment - REPORT.md: manual /docs check, TTL configuration row - tasks.md: why checks ran on a separate dev database Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -20,7 +20,8 @@ _hasher = PasswordHasher.from_parameters(profiles.RFC_9106_LOW_MEMORY)
|
||||
# свой лимит, а не общий пул anyio на 40 потоков: 40 × 64 MiB = 2,5 GiB.
|
||||
# При отмене запроса место освобождается только после конца потока
|
||||
_limiter = anyio.CapacityLimiter(settings.auth.kdf_concurrency)
|
||||
# хэш для неизвестного username: считается при первом входе
|
||||
# хэш для неизвестного username: считает lifespan при старте
|
||||
# (prepare_dummy_hash), иначе — первый такой вход
|
||||
_dummy_hash: str | None = None
|
||||
|
||||
|
||||
@@ -39,7 +40,7 @@ async def verify_password(*, password: str, password_hash: str | None) -> bool:
|
||||
if password_hash is None:
|
||||
await to_thread.run_sync(
|
||||
_matches,
|
||||
await _get_dummy_hash(),
|
||||
await prepare_dummy_hash(),
|
||||
password,
|
||||
limiter=_limiter,
|
||||
)
|
||||
@@ -60,7 +61,12 @@ def _matches(password_hash: str, password: str) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
async def _get_dummy_hash() -> str:
|
||||
async def prepare_dummy_hash() -> str:
|
||||
"""Фиктивный хэш для неизвестного username, один на процесс.
|
||||
|
||||
Lifespan вызывает его при старте: иначе первый вход с неизвестным
|
||||
username считал бы хэш и проверку — вдвое дольше неверного пароля.
|
||||
"""
|
||||
global _dummy_hash
|
||||
if _dummy_hash is None:
|
||||
_dummy_hash = await hash_password(secrets.token_urlsafe(16))
|
||||
|
||||
Reference in New Issue
Block a user