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
@@ -32,6 +32,8 @@ from auth.tests.softkey import SoftKey, b64 # noqa: E402 after the skip
|
||||
#: the real `blocked()` path rather than a configured override.
|
||||
RP_ID = "localhost"
|
||||
ORIGIN = "http://localhost"
|
||||
#: What starting a registration takes besides a session: the password.
|
||||
CONFIRM = {"current": PASSWORD}
|
||||
|
||||
|
||||
def unb64(text: str) -> bytes:
|
||||
@@ -48,7 +50,7 @@ def client(make_client: Callable[..., TestClient]) -> TestClient:
|
||||
|
||||
def register(client: TestClient, key: SoftKey, label: str = "Test key"):
|
||||
"""The whole registration ceremony, both round trips."""
|
||||
options = client.post("/api/account/passkeys/options").json()
|
||||
options = client.post("/api/account/passkeys/options", json=CONFIRM).json()
|
||||
made = key.register(unb64(options["challenge"]), RP_ID, ORIGIN)
|
||||
return client.post("/api/account/passkeys",
|
||||
json={"credential": made, "label": label})
|
||||
@@ -66,7 +68,45 @@ def login(client: TestClient, key: SoftKey, origin: str = ORIGIN):
|
||||
def test_registering_needs_a_session(client: TestClient):
|
||||
"""A passkey is a new way in, so adding one is gated on already being in."""
|
||||
client.post("/logout")
|
||||
assert client.post("/api/account/passkeys/options").status_code == 401
|
||||
assert client.post("/api/account/passkeys/options", json=CONFIRM).status_code == 401
|
||||
|
||||
|
||||
def test_registering_needs_the_password_too(client: TestClient, caplog):
|
||||
"""A passkey outlives every session and a password change, so a borrowed
|
||||
signed-in browser must not be able to add one: a wrong or missing password
|
||||
gets no challenge, is logged like the other doors, and is counted."""
|
||||
with caplog.at_level("WARNING"):
|
||||
wrong = client.post("/api/account/passkeys/options", json={"current": "nope"})
|
||||
assert wrong.status_code == 403
|
||||
assert "wrong current password" in caplog.text
|
||||
assert client.post("/api/account/passkeys/options", json={}).status_code == 422
|
||||
assert client.post("/api/account/passkeys/options").status_code == 422
|
||||
assert client.get("/api/account").json()["passkeys"] == []
|
||||
|
||||
|
||||
def test_registering_is_behind_the_same_lockout(client: TestClient):
|
||||
"""Guessing at the passkey door locks the front door too, and once locked
|
||||
the right password is not looked at."""
|
||||
for _ in range(5):
|
||||
assert client.post("/api/account/passkeys/options",
|
||||
json={"current": "nope"}).status_code == 403
|
||||
refused = client.post("/api/account/passkeys/options", json=CONFIRM)
|
||||
assert refused.status_code == 429
|
||||
client.post("/logout")
|
||||
assert sign_in(client).status_code == 429
|
||||
|
||||
|
||||
def test_an_app_with_no_password_registers_without_one(
|
||||
make_client: Callable[..., TestClient]):
|
||||
"""Loopback under `auto` and never locked: there is no password to confirm
|
||||
and no session to borrow, so registering before deploying still works."""
|
||||
open_app = make_client(host="127.0.0.1", password=None, base_url=ORIGIN)
|
||||
assert not open_app.app.state.auth.has_password()
|
||||
options = open_app.post("/api/account/passkeys/options", json={"current": ""})
|
||||
assert options.status_code == 200
|
||||
made = SoftKey().register(unb64(options.json()["challenge"]), RP_ID, ORIGIN)
|
||||
assert open_app.post("/api/account/passkeys",
|
||||
json={"credential": made}).status_code == 200
|
||||
|
||||
|
||||
def test_a_passkey_registers_and_is_listed(client: TestClient):
|
||||
@@ -95,14 +135,14 @@ def test_a_second_registration_is_excluded_not_duplicated(client: TestClient):
|
||||
say "you have one of these" rather than making a second."""
|
||||
key = SoftKey()
|
||||
register(client, key)
|
||||
options = client.post("/api/account/passkeys/options").json()
|
||||
options = client.post("/api/account/passkeys/options", json=CONFIRM).json()
|
||||
assert [c["id"] for c in options["excludeCredentials"]] == [b64(key.credential_id)]
|
||||
|
||||
|
||||
def test_a_stale_challenge_is_refused(client: TestClient):
|
||||
"""A challenge is single-use, so the same response cannot be sent twice."""
|
||||
key = SoftKey()
|
||||
options = client.post("/api/account/passkeys/options").json()
|
||||
options = client.post("/api/account/passkeys/options", json=CONFIRM).json()
|
||||
made = key.register(unb64(options["challenge"]), RP_ID, ORIGIN)
|
||||
|
||||
assert client.post("/api/account/passkeys", json={"credential": made}).status_code == 200
|
||||
@@ -224,6 +264,23 @@ def test_a_malformed_body_is_a_400_and_not_a_traceback(client: TestClient):
|
||||
assert client.post("/login/passkey", json={"credential": {}}).status_code == 400
|
||||
|
||||
|
||||
def test_a_raw_id_that_is_not_base64url_is_a_400_and_spends_nothing(client: TestClient):
|
||||
"""`rawId` was decoded unguarded after the challenge was taken, so `"a"`
|
||||
was a 500 from an endpoint anyone can reach and the challenge was gone.
|
||||
Now it is refused before the challenge is spent: the same challenge,
|
||||
answered properly, still signs in."""
|
||||
key = SoftKey()
|
||||
register(client, key)
|
||||
client.post("/logout")
|
||||
|
||||
options = client.post("/login/passkey/options").json()
|
||||
signed = key.sign(unb64(options["challenge"]), RP_ID, ORIGIN)
|
||||
refused = client.post("/login/passkey", json={"credential": {**signed, "rawId": "a"}})
|
||||
assert refused.status_code == 400
|
||||
assert "rawId is not base64url" in refused.json()["detail"]
|
||||
assert client.post("/login/passkey", json={"credential": signed}).status_code == 200
|
||||
|
||||
|
||||
def test_no_passkeys_registered_says_so(client: TestClient):
|
||||
client.post("/logout")
|
||||
assert client.post("/login/passkey/options").status_code == 404
|
||||
@@ -243,7 +300,7 @@ def test_a_registration_challenge_cannot_be_answered_at_the_login_door(
|
||||
"""
|
||||
key = SoftKey()
|
||||
register(client, key)
|
||||
theirs = client.post("/api/account/passkeys/options").json()
|
||||
theirs = client.post("/api/account/passkeys/options", json=CONFIRM).json()
|
||||
client.post("/logout")
|
||||
|
||||
signed = key.sign(unb64(theirs["challenge"]), RP_ID, ORIGIN)
|
||||
@@ -352,7 +409,7 @@ def test_a_configured_rp_id_and_origin_win(make_client: Callable[..., TestClient
|
||||
client = make_client(enabled="always", rp_id="app.example.com",
|
||||
origin="https://app.example.com")
|
||||
sign_in(client)
|
||||
options = client.post("/api/account/passkeys/options").json()
|
||||
options = client.post("/api/account/passkeys/options", json=CONFIRM).json()
|
||||
assert options["rp"]["id"] == "app.example.com"
|
||||
assert client.get("/api/account").json()["passkeys_blocked"] == ""
|
||||
|
||||
|
||||
Reference in new issue
Block a user