Take auth/ whole from boilerplate 09e144d: the lockout claimed before the scrypt, APP_PASSWORD recovery closing every session, Starlette's cookie parser, the rightmost forwarded hop in the log, revoke_all on a password change, a 421 for a non-loopback Host on an open app, API tokens in auth_tokens behind Authorization: Bearer, and sockets closed with their credential
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
1 parent
83e965dfeb
commit
244e57ac29
19 files changed
+2288
-214
No files matched your search
@@ -53,7 +53,7 @@ def test_the_tables_are_namespaced(store: AuthStore):
|
||||
named = {name for (name,) in rows(
|
||||
store, "SELECT name FROM sqlite_master WHERE type='table'")}
|
||||
assert {"auth_credential", "auth_sessions", "auth_passkeys",
|
||||
"auth_challenges"} <= named
|
||||
"auth_challenges", "auth_tokens"} <= named
|
||||
assert not {n for n in named if not n.startswith(("auth_", "sqlite_"))}
|
||||
|
||||
|
||||
@@ -119,6 +119,71 @@ def test_the_delay_doubles_and_then_stops(store: AuthStore):
|
||||
assert store.check_password(PASSWORD) is True
|
||||
|
||||
|
||||
def test_wrong_guesses_in_parallel_are_each_counted(store: AuthStore):
|
||||
"""`failures` was read, scrypt ran, and the read plus one was written, so
|
||||
forty wrong guesses at once recorded one failure and no lockout. The raise
|
||||
is one statement under the write lock now."""
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
store.set_password(PASSWORD)
|
||||
with ThreadPoolExecutor(max_workers=16) as pool:
|
||||
said = list(pool.map(lambda _: store.check_password("wrong"), range(40)))
|
||||
assert said == [False] * 40
|
||||
assert rows(store, "SELECT failures FROM auth_credential") == [(40,)]
|
||||
assert store.locked_for() > 0
|
||||
|
||||
|
||||
def test_parallel_claims_admit_the_grace_and_no_more(store: AuthStore):
|
||||
"""What both doors do: claim, then derive. A claim reads the lockout and
|
||||
counts the attempt in one write transaction, so thirty claims at once
|
||||
admit `_GRACE` guesses and tell the rest to wait, where a read before the
|
||||
derivation and a write after it admitted all thirty."""
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
store.set_password(PASSWORD)
|
||||
|
||||
def guess(_: int) -> str:
|
||||
if store.claim_attempt():
|
||||
return "wait"
|
||||
return "right" if store.check_password("wrong", claimed=True) else "wrong"
|
||||
|
||||
with ThreadPoolExecutor(max_workers=30) as pool:
|
||||
said = list(pool.map(guess, range(30)))
|
||||
assert said.count("wrong") == _GRACE and said.count("wait") == 30 - _GRACE
|
||||
assert rows(store, "SELECT failures FROM auth_credential") == [(_GRACE,)]
|
||||
|
||||
|
||||
def test_a_claimed_right_password_clears_the_count(store: AuthStore):
|
||||
"""Counted up front, credited back: the fifth claim writes the lockout
|
||||
before the derivation, and a right answer then clears it."""
|
||||
store.set_password(PASSWORD)
|
||||
for _ in range(_GRACE - 1):
|
||||
assert store.claim_attempt() == 0
|
||||
assert store.check_password("wrong", claimed=True) is False
|
||||
assert store.claim_attempt() == 0
|
||||
assert store.locked_for() > 0, "the fifth claim did not write the lockout"
|
||||
assert store.check_password(PASSWORD, claimed=True) is True
|
||||
assert store.locked_for() == 0
|
||||
assert rows(store, "SELECT failures, locked_until FROM auth_credential") == [(0, "")]
|
||||
|
||||
|
||||
def test_a_claim_while_locked_counts_nothing(store: AuthStore):
|
||||
"""Hammering while locked out does not extend the lockout."""
|
||||
store.set_password(PASSWORD)
|
||||
for _ in range(_GRACE):
|
||||
assert store.claim_attempt() == 0
|
||||
store.check_password("wrong", claimed=True)
|
||||
before = rows(store, "SELECT failures, locked_until FROM auth_credential")
|
||||
for _ in range(10):
|
||||
assert store.claim_attempt() > 0
|
||||
assert rows(store, "SELECT failures, locked_until FROM auth_credential") == before
|
||||
|
||||
|
||||
def test_a_claim_on_a_database_with_no_password_is_granted(store: AuthStore):
|
||||
assert store.claim_attempt() == 0
|
||||
assert store.check_password(PASSWORD, claimed=True) is False
|
||||
|
||||
|
||||
# -------------------------------------------------------------- the sessions
|
||||
|
||||
def test_a_session_is_live_until_it_is_not(store: AuthStore):
|
||||
|
||||
Reference in new issue
Block a user