From 10a0b89281ecbbec4e282c32483b68c883aaea22 Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Mon, 16 Nov 2026 19:07:09 -0600 Subject: [PATCH 01/23] The anchor walk refuses a drive-relative spelling on every pass, not only the given text windows_chain refused a drive-relative path such as C:anchor.pem only on the text it was given (BACKLOG #5197). A relative anchor joined to a drive-relative working directory (C: plus ca.pem reads C:ca.pem) was then walked from the drive root, and a subst drive as that working directory spliced its target onto the remainder with no separator (C:\realcerts\ca.pem), so the walk would vouch for another chain than the one Windows opens. The loop now refuses a drive-relative spelling at the top of every pass, which also makes the rest after the drive always rooted or empty, so the subst splice always has its separator. The literal C:anchor.pem case in the row was already refused on origin/main since PR 2815 (BACKLOG #5197 item 12); this closes the remaining path to the same defect. BACKLOG #5337 --- messagefoundry/auth/anchor_path.py | 9 ++++++- tests/test_anchor_path.py | 42 ++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/messagefoundry/auth/anchor_path.py b/messagefoundry/auth/anchor_path.py index 89e9824694..a780bb2712 100644 --- a/messagefoundry/auth/anchor_path.py +++ b/messagefoundry/auth/anchor_path.py @@ -549,7 +549,8 @@ def windows_chain( At least these end the walk with a cause, since Windows would open another object than the text names (vault BACKLOG #5197): a drive-relative spelling (:func:`_drive_relative`), in the path, a - link target or a ``subst`` target joined to the rest of the path; a name ending in a space or a + link target or a ``subst`` target joined to the rest of the path, or in the path joined to the + working directory (vault BACKLOG #5337); a name ending in a space or a dot, which Win32 strips; and a link target rooted with no drive (:data:`_ROOTED_LINK`); a device prefix written with forward slashes (``//?/S:/x``); and a stream name after a link. A path that reaches a share ends with :data:`SHARE_CAUSE`.""" @@ -588,6 +589,12 @@ def add(obj: str, kind: str) -> None: drive, rest = ntpath.splitdrive(norm) if not drive: return chain, WalkCause.plain("the path has no drive or volume") + if _drive_relative(norm): + # Checked on every pass, not only on the text the caller gave: a drive-relative working + # directory (``C:``) joined to ``ca.pem`` reads ``C:ca.pem``, which the loop below + # would walk from the drive root, and which a subst splice would join to its target + # with no separator (vault BACKLOG #5337). So ``rest`` below is rooted or empty. + return chain, WalkCause.plain(_DRIVE_RELATIVE) try: substituted = drive_target(drive) if len(drive) == 2 else None except OSError as exc: diff --git a/tests/test_anchor_path.py b/tests/test_anchor_path.py index c7a76ba492..0b007c5094 100644 --- a/tests/test_anchor_path.py +++ b/tests/test_anchor_path.py @@ -1200,3 +1200,45 @@ def test_posix_root_owned_755_placement_loads_at_enforce_with_no_pin( monkeypatch.setattr(ta, "anchor_path_verdict", lambda _p: v) monkeypatch.setattr(ta, "dacl_is_owner_only", lambda _p: True) # 644: no group or other write ta.enforce_anchor(AnchorSpec("t", "[x]", str(_pem(tmp_path)), None), enforcing=True) + + +# --- vault BACKLOG #5337: a drive-relative spelling reached through the working directory -------- + + +def _plain_probe(p: str) -> tuple[str, str | None]: + """Every name is a folder, or a file when it ends in ``.pem``; nothing is a link.""" + return (FILE if p.lower().endswith(".pem") else DIRECTORY), None + + +def _subst_s(drive: str) -> str | None: + return "\\??\\C:\\real" if drive == "S:" else None + + +def _no_subst(_drive: str) -> str | None: + return None + + +@pytest.mark.parametrize( + ("anchor", "cwd", "drive_target"), + [ + # ``C:`` joined to ``ca.pem`` reads ``C:ca.pem``. Red: walked as C:\ca.pem, from the root. + ("ca.pem", "C:", _no_subst), + # A subst drive as that working directory. Red: spliced as C:\realcerts\ca.pem. + ("certs\\ca.pem", "S:", _subst_s), + ], +) +def test_a_relative_anchor_under_a_drive_relative_working_directory_is_not_walked( + anchor: str, cwd: str, drive_target: Any +) -> None: + """Windows reads ``C:x`` from that drive's own working directory, which the walk does not know, + so it refuses on every pass, not only on the text it was given (vault BACKLOG #5337).""" + chain, cause = windows_chain(anchor, cwd, _plain_probe, drive_target=drive_target) + assert chain == [] + assert cause is not None and "no separator after it" in cause + + +def test_a_subst_drive_splices_its_target_with_a_separator() -> None: + """The control: a subst working directory with a separator is walked under its target.""" + chain, cause = windows_chain("certs\\ca.pem", "S:\\w", _plain_probe, drive_target=_subst_s) + assert cause is None + assert chain[-1] == ("C:\\real\\w\\certs\\ca.pem", FILE) From 282f641ec9f36166ceebf3647d4f0e62fa2b70b8 Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Mon, 16 Nov 2026 19:13:21 -0600 Subject: [PATCH 02/23] A passkey response nested past json's depth limit is the audited refusal py_webauthn parses a ceremony response's text with its own json.loads and does not wrap the RecursionError a deeply nested response raises. That type was missing from _invalid_input_errors, so such a response would escape verify_registration (which the service hands the raw text) or the text form of verify_assertion as an unhandled 500 instead of the audited invalid-input refusal ADR 0068 requires. RecursionError joins the tuple; _refusal logs only its type, and its message names the depth, not the input. BACKLOG #5341 --- messagefoundry/auth/webauthn.py | 8 ++++++-- tests/test_webauthn.py | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/messagefoundry/auth/webauthn.py b/messagefoundry/auth/webauthn.py index 24e3836ef6..a8711f850c 100644 --- a/messagefoundry/auth/webauthn.py +++ b/messagefoundry/auth/webauthn.py @@ -169,7 +169,7 @@ def _supported_pub_key_algs() -> list[COSEAlgorithmIdentifier]: def _invalid_input_errors() -> tuple[type[Exception], ...]: """Every exception py_webauthn raises for bad input, which must all reach the audited path. - ``WebAuthnException`` is the library's own base class. The other three entries catch four raw + ``WebAuthnException`` is the library's own base class. The next three entries catch four raw exception types (``LookupError`` covers both ``KeyError`` and ``IndexError``) that it lets out when a COSE key is malformed rather than merely wrong: its key decoder indexes an untyped CBOR value, so a missing label is a ``KeyError``, a short array an ``IndexError``, a @@ -179,11 +179,15 @@ def _invalid_input_errors() -> tuple[type[Exception], ...]: 0068 decision 1 requires rather than escape as a 500. Use it only around library calls, never around our own logic, where the same exceptions would mean a bug. + ``RecursionError`` joins them for a response nested past json's depth limit, which the + library's own ``json.loads`` raises and does not wrap (vault BACKLOG #5341). Its message names + the depth, not the input. + A function rather than a constant because the library import is lazy (the extra is optional). """ from webauthn.helpers.exceptions import WebAuthnException - return (WebAuthnException, LookupError, TypeError, ValueError) + return (WebAuthnException, LookupError, TypeError, ValueError, RecursionError) def _refusal(exc: Exception, *, ceremony: _Ceremony) -> WebAuthnVerificationError: diff --git a/tests/test_webauthn.py b/tests/test_webauthn.py index a550a91fbf..f2bfbe84e3 100644 --- a/tests/test_webauthn.py +++ b/tests/test_webauthn.py @@ -1819,3 +1819,29 @@ async def test_a_passkey_registered_from_a_first_seen_address_records_nothing() assert await store.list_known_login_addresses(uid, since=0.0) == ["10.7.7.7"] finally: await store.close() + + +#: A ceremony response nested past json's depth limit, under a field a real response carries. +_TOO_DEEP = '{"id": ' + "[" * 100_000 + "]" * 100_000 + "}" + + +def test_a_deeply_nested_registration_response_is_the_audited_refusal() -> None: + """vault BACKLOG #5341. Red: json's RecursionError escaped verify_registration, a 500.""" + with pytest.raises(wa.WebAuthnVerificationError): + wa.verify_registration( + response_json=_TOO_DEEP, challenge=b"c" * 32, rp_id=RP, origin=ORIGIN + ) + + +def test_a_deeply_nested_assertion_response_is_the_audited_refusal() -> None: + """vault BACKLOG #5341, the text form ``verify_assertion`` accepts. Red: as above.""" + soft = SoftAuthenticator(rp_id=RP, origin=ORIGIN) + with pytest.raises(wa.WebAuthnVerificationError): + wa.verify_assertion( + response_json=_TOO_DEEP, + challenge=b"c" * 32, + rp_id=RP, + origin=ORIGIN, + public_key=soft.cose_public_key(), + current_sign_count=0, + ) From 8a864bd838cf9c4890f2232a828e717c87ed7304 Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Mon, 16 Nov 2026 19:24:49 -0600 Subject: [PATCH 03/23] A sign-in naming a control character is refused at the body, before the store LoginRequest bounded username and supersedes by length only. A value carrying a NUL would reach AuthService.login and the store's lookup, where a PostgreSQL store refuses it with an error the route does not catch, so the public sign-in route would answer 500 outside its failure-timing pad. Both fields now take the shared PRINTABLE_TEXT_PATTERN, which refuses C0, DEL and C1 controls and an empty value, with a 422 before the service runs. No account can hold such a name (username_refusal) and no session token carries one, so only a value that could never sign in or name a session is refused. provider is unchanged: the route already refuses an unknown one with a 400 before the service. BACKLOG #5342 --- messagefoundry/api/auth_models.py | 12 ++++++++-- tests/test_api_auth.py | 38 +++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/messagefoundry/api/auth_models.py b/messagefoundry/api/auth_models.py index 9b0327c9fd..12f5be6009 100644 --- a/messagefoundry/api/auth_models.py +++ b/messagefoundry/api/auth_models.py @@ -18,6 +18,7 @@ from messagefoundry.api.request_model import RequestModel from messagefoundry.api.validation import ( MAX_MAP_ENTRIES, + PRINTABLE_TEXT_PATTERN, ChannelScopeEntry, PermissionId, RoleId, @@ -55,14 +56,21 @@ class CredentialReply(BaseModel): class LoginRequest(RequestModel): - username: str = Field(max_length=_NAME_MAX) + # ``username`` and ``supersedes`` take the printable rule, which also refuses an empty value + # (vault BACKLOG #5342). Without it a NUL would reach the store's lookup, where PostgreSQL + # refuses it with an error the route does not catch, so the public sign-in route would answer + # 500 outside its failure-timing pad. No account can hold such a name (``username_refusal``) and no token + # carries one, so the 422 refuses only a value that could never sign in or name a session. + username: str = Field(max_length=_NAME_MAX, pattern=PRINTABLE_TEXT_PATTERN) password: str = Field(max_length=_PASSWORD_MAX) provider: str = Field(default="local", max_length=16) # 'local' | 'ad' #: The raw session token this sign-in REPLACES in the caller, if any (ASVS 7.2.4, BACKLOG #: #2096). Not the session ``id`` that ``/me/sessions`` shows, which is the token's hash: a #: value that names no session ends nothing, and the sign-in still succeeds. On success the #: engine ends it as ``AuthService._issue_session`` describes. - supersedes: str | None = Field(default=None, max_length=_TOKEN_MAX) + supersedes: str | None = Field( + default=None, max_length=_TOKEN_MAX, pattern=PRINTABLE_TEXT_PATTERN + ) # ADR 0197 (BACKLOG #1131): the optional authenticator code of the COMBINED sign-in, the password # and a TOTP code in one request. Absent means today's two-step flow, unchanged. Bounded to the # longest code any compiled-in enrolment gives (#3422); the engine treats a blank one as absent. diff --git a/tests/test_api_auth.py b/tests/test_api_auth.py index 66b223cf2d..064330c946 100644 --- a/tests/test_api_auth.py +++ b/tests/test_api_auth.py @@ -3672,3 +3672,41 @@ async def _live(c: httpx.AsyncClient, token: str) -> bool: "/auth/login", json={"username": "sup", "password": PW, "supersedes": "x" * 257} ) assert too_long.status_code == 422 + + +# --- vault BACKLOG #5342: a sign-in naming a control character is refused at the body ---------- + + +@pytest.mark.parametrize( + "fields", + [ + {"username": "sup\x00"}, + {"username": "s\x01up"}, + {"username": "sup\x9f"}, + {"username": ""}, + {"supersedes": "tok\x00en"}, + {"supersedes": "\x1b"}, + {"supersedes": ""}, + ], +) +async def test_login_refuses_a_control_character_before_the_store( + engine: Engine, fields: dict[str, str], monkeypatch: pytest.MonkeyPatch +) -> None: + """Red: each reached ``AuthService.login``, where on a PostgreSQL store a NUL would raise an + error the route does not catch, a 500 outside the failure-timing pad. Now each is a 422 before + the service runs. The control: the same body without the bad field still signs in.""" + service = await _service(engine, AuthSettings(require_mfa=False)) + await _add(service, "sup", Role.VIEWER) + async with _client(engine, service) as c: + ok = await c.post("/auth/login", json={"username": "sup", "password": PW}) + assert ok.status_code == 200 + reached: list[str] = [] + + async def _spy(*args: object, **kwargs: object) -> object: + reached.append("login") + raise AssertionError("the service ran") + + monkeypatch.setattr(service, "login", _spy) + r = await c.post("/auth/login", json={"username": "sup", "password": PW, **fields}) + assert r.status_code == 422 + assert reached == [] From 0f27da8acf025104deeb22fdaff32a6865200717 Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Mon, 16 Nov 2026 19:26:56 -0600 Subject: [PATCH 04/23] The subst splice leaves its drive-relative refusal to the loop top The per-pass drive-relative check added for BACKLOG #5337 already refuses a subst target joined to the rest as a drive-relative path, on the next pass, with the same cause, so the copy right after the splice is dropped. The test_odbc_path_walk_gaps item 12 subst case still pins it. BACKLOG #5337 --- messagefoundry/auth/anchor_path.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/messagefoundry/auth/anchor_path.py b/messagefoundry/auth/anchor_path.py index a780bb2712..311a8ff65f 100644 --- a/messagefoundry/auth/anchor_path.py +++ b/messagefoundry/auth/anchor_path.py @@ -615,9 +615,8 @@ def add(obj: str, kind: str) -> None: ) # The target with the rest of the path, not the target alone: a letter defined onto a # bare drive (``\??\C:``) makes ``S:\ca.pem`` read as ``C:\ca.pem``, which is rooted. + # A drive-relative result (``\??\C:certs``) is refused at the top of the next pass. pending = target + rest - if _drive_relative(pending): - return chain, WalkCause.plain(_DRIVE_RELATIVE) continue root = drive + "\\" why = volume_cause(root) From ed0f03efe73e3e8c438a4fed2be337d859de326a Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Mon, 16 Nov 2026 19:44:24 -0600 Subject: [PATCH 05/23] Review round 1: refuse only a NUL username at sign-in, and correct two docstrings The first cut applied the printable rule to LoginRequest.username and supersedes. Review found its premise false: provision-admin does not run username_refusal, so an account named with a tab or a C1 control could exist and the JSON route would 422 it; and supersedes is only ever hashed, never sent to the store, while an empty one meant "no prior token". The rule is now a NUL only, on username only: PostgreSQL text cannot hold one, so no account on that store has one. Controls pin that another control character, an empty name and a NUL supersedes still reach the service. The webauthn docstring said RecursionError's message names the depth; on 3.14 it names stack use. It now also says the catch does not cover a route that parses the body itself first. BACKLOG #5342 BACKLOG #5341 --- messagefoundry/api/auth_models.py | 20 +++++++------- messagefoundry/auth/webauthn.py | 4 ++- tests/test_api_auth.py | 46 +++++++++++++++++-------------- 3 files changed, 38 insertions(+), 32 deletions(-) diff --git a/messagefoundry/api/auth_models.py b/messagefoundry/api/auth_models.py index 12f5be6009..1e1d8cadd5 100644 --- a/messagefoundry/api/auth_models.py +++ b/messagefoundry/api/auth_models.py @@ -18,7 +18,6 @@ from messagefoundry.api.request_model import RequestModel from messagefoundry.api.validation import ( MAX_MAP_ENTRIES, - PRINTABLE_TEXT_PATTERN, ChannelScopeEntry, PermissionId, RoleId, @@ -35,6 +34,8 @@ _GROUP_MAX = 512 # A session token is 43 URL-safe characters (auth/tokens.py); the cap only refuses absurd input. _TOKEN_MAX = 256 +#: Any text but a NUL, empty included (vault BACKLOG #5342; ``LoginRequest`` says why). +_NO_NUL_PATTERN = r"^[^\x00]*$" #: The response header an ``oidc`` session's step-up refusal carries, set to #: :data:`STEP_UP_VIA_IDP`, so a client can branch on a header rather than parse the detail @@ -56,21 +57,20 @@ class CredentialReply(BaseModel): class LoginRequest(RequestModel): - # ``username`` and ``supersedes`` take the printable rule, which also refuses an empty value - # (vault BACKLOG #5342). Without it a NUL would reach the store's lookup, where PostgreSQL - # refuses it with an error the route does not catch, so the public sign-in route would answer - # 500 outside its failure-timing pad. No account can hold such a name (``username_refusal``) and no token - # carries one, so the 422 refuses only a value that could never sign in or name a session. - username: str = Field(max_length=_NAME_MAX, pattern=PRINTABLE_TEXT_PATTERN) + # A NUL in ``username`` is refused here (vault BACKLOG #5342). It would reach the store's + # lookup, where PostgreSQL refuses it with an error this route does not catch, so the route + # would answer 500 outside its failure-timing pad. Only the NUL: PostgreSQL text cannot hold + # one, so no account on that store has one, while other control characters can reach a stored + # name through paths ``username_refusal`` does not govern, such as provision-admin. Other + # sign-in routes are not covered by this model (``messagefoundry_webconsole``'s form login). + username: str = Field(max_length=_NAME_MAX, pattern=_NO_NUL_PATTERN) password: str = Field(max_length=_PASSWORD_MAX) provider: str = Field(default="local", max_length=16) # 'local' | 'ad' #: The raw session token this sign-in REPLACES in the caller, if any (ASVS 7.2.4, BACKLOG #: #2096). Not the session ``id`` that ``/me/sessions`` shows, which is the token's hash: a #: value that names no session ends nothing, and the sign-in still succeeds. On success the #: engine ends it as ``AuthService._issue_session`` describes. - supersedes: str | None = Field( - default=None, max_length=_TOKEN_MAX, pattern=PRINTABLE_TEXT_PATTERN - ) + supersedes: str | None = Field(default=None, max_length=_TOKEN_MAX) # ADR 0197 (BACKLOG #1131): the optional authenticator code of the COMBINED sign-in, the password # and a TOTP code in one request. Absent means today's two-step flow, unchanged. Bounded to the # longest code any compiled-in enrolment gives (#3422); the engine treats a blank one as absent. diff --git a/messagefoundry/auth/webauthn.py b/messagefoundry/auth/webauthn.py index a8711f850c..0e86ab7984 100644 --- a/messagefoundry/auth/webauthn.py +++ b/messagefoundry/auth/webauthn.py @@ -181,7 +181,9 @@ def _invalid_input_errors() -> tuple[type[Exception], ...]: ``RecursionError`` joins them for a response nested past json's depth limit, which the library's own ``json.loads`` raises and does not wrap (vault BACKLOG #5341). Its message names - the depth, not the input. + the stack use, not the input. It covers a response that reaches the library as text; a route + that parses the body itself first raises its own ``RecursionError`` there, which this does + not cover. A function rather than a constant because the library import is lazy (the extra is optional). """ diff --git a/tests/test_api_auth.py b/tests/test_api_auth.py index 064330c946..627ad4d836 100644 --- a/tests/test_api_auth.py +++ b/tests/test_api_auth.py @@ -3674,32 +3674,18 @@ async def _live(c: httpx.AsyncClient, token: str) -> bool: assert too_long.status_code == 422 -# --- vault BACKLOG #5342: a sign-in naming a control character is refused at the body ---------- +# --- vault BACKLOG #5342: a sign-in naming a NUL is refused at the body -------------------------- -@pytest.mark.parametrize( - "fields", - [ - {"username": "sup\x00"}, - {"username": "s\x01up"}, - {"username": "sup\x9f"}, - {"username": ""}, - {"supersedes": "tok\x00en"}, - {"supersedes": "\x1b"}, - {"supersedes": ""}, - ], -) -async def test_login_refuses_a_control_character_before_the_store( - engine: Engine, fields: dict[str, str], monkeypatch: pytest.MonkeyPatch +@pytest.mark.parametrize("username", ["sup\x00", "\x00", "s\x00up"]) +async def test_login_refuses_a_nul_username_before_the_store( + engine: Engine, username: str, monkeypatch: pytest.MonkeyPatch ) -> None: - """Red: each reached ``AuthService.login``, where on a PostgreSQL store a NUL would raise an + """Red: each reached ``AuthService.login``, where on a PostgreSQL store the NUL would raise an error the route does not catch, a 500 outside the failure-timing pad. Now each is a 422 before - the service runs. The control: the same body without the bad field still signs in.""" + the service runs.""" service = await _service(engine, AuthSettings(require_mfa=False)) - await _add(service, "sup", Role.VIEWER) async with _client(engine, service) as c: - ok = await c.post("/auth/login", json={"username": "sup", "password": PW}) - assert ok.status_code == 200 reached: list[str] = [] async def _spy(*args: object, **kwargs: object) -> object: @@ -3707,6 +3693,24 @@ async def _spy(*args: object, **kwargs: object) -> object: raise AssertionError("the service ran") monkeypatch.setattr(service, "login", _spy) - r = await c.post("/auth/login", json={"username": "sup", "password": PW, **fields}) + r = await c.post("/auth/login", json={"username": username, "password": PW}) assert r.status_code == 422 assert reached == [] + + +@pytest.mark.parametrize( + "fields", + [{"username": "s\x01up"}, {"username": ""}, {"supersedes": "tok\x00en"}, {"supersedes": ""}], +) +async def test_login_still_reaches_the_service_for_other_unusual_values( + engine: Engine, fields: dict[str, str] +) -> None: + """The controls: another control character, an empty name, and a ``supersedes`` holding a NUL + (only ever hashed, never sent to the store) are not refused by the body, so they are still + charged, padded and audited as a failed or plain sign-in, as before.""" + service = await _service(engine, AuthSettings(require_mfa=False)) + await _add(service, "sup", Role.VIEWER) + async with _client(engine, service) as c: + r = await c.post("/auth/login", json={"username": "sup", "password": PW, **fields}) + expected = 200 if "supersedes" in fields else 401 + assert r.status_code == expected From cc3f538d2e3bacff7ce2460d25d4a2f6b6482323 Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Mon, 16 Nov 2026 20:06:24 -0600 Subject: [PATCH 06/23] Review round 2: catch RecursionError around the response parse only Round 2 found RecursionError also caught around the stored-key check, where it would mean a low stack, not a bad credential, and turn a valid passkey into StoredKeyRefusedError. It now joins only at the two response verify calls, through _invalid_response_errors. _transports_from_response uses json_loads_or_refusal, so its own parse cannot let a RecursionError out after the library's succeeded. Docstrings and the auth_models comment say the module seam is what is covered: at least the web console's form login and passkey routes are not. The login control test now spies that the service ran; a test helper duplicating the default drive_target is dropped. Proposed PR title: Batch 613 C: anchor walk refuses a drive-relative working directory; passkey and sign-in refusals at the module seam Proposed vault banners (do not flip until merged): - BACKLOG #5337: windows_chain refuses a drive-relative spelling on every pass, so a drive-relative working directory is refused and a subst splice always has its separator. The literal C:anchor.pem case was already refused since PR 2815 (#5197 item 12). - BACKLOG #5341: PARTIAL. RecursionError from the library's response parse is the audited refusal in verify_registration and verify_assertion. The web console passkey routes parse the body first and still 500 on a deep body; that remainder is outside this module. - BACKLOG #5342: PARTIAL. POST /auth/login refuses a NUL username with a 422 before the service. supersedes is only hashed, so it needed no rule. The web console form login still reaches the store with a NUL; a guard at AuthService.login or the store lookup would cover every route. BACKLOG #5341 BACKLOG #5342 --- messagefoundry/api/auth_models.py | 4 ++-- messagefoundry/auth/webauthn.py | 34 +++++++++++++++++++------------ tests/test_anchor_path.py | 6 +----- tests/test_api_auth.py | 21 +++++++++++++------ tests/test_webauthn.py | 3 ++- 5 files changed, 41 insertions(+), 27 deletions(-) diff --git a/messagefoundry/api/auth_models.py b/messagefoundry/api/auth_models.py index 1e1d8cadd5..a9480c5616 100644 --- a/messagefoundry/api/auth_models.py +++ b/messagefoundry/api/auth_models.py @@ -61,8 +61,8 @@ class LoginRequest(RequestModel): # lookup, where PostgreSQL refuses it with an error this route does not catch, so the route # would answer 500 outside its failure-timing pad. Only the NUL: PostgreSQL text cannot hold # one, so no account on that store has one, while other control characters can reach a stored - # name through paths ``username_refusal`` does not govern, such as provision-admin. Other - # sign-in routes are not covered by this model (``messagefoundry_webconsole``'s form login). + # name through paths ``username_refusal`` does not govern, such as provision-admin. This covers + # only the JSON route; at least the web console's form login reaches the service without it. username: str = Field(max_length=_NAME_MAX, pattern=_NO_NUL_PATTERN) password: str = Field(max_length=_PASSWORD_MAX) provider: str = Field(default="local", max_length=16) # 'local' | 'ad' diff --git a/messagefoundry/auth/webauthn.py b/messagefoundry/auth/webauthn.py index 0e86ab7984..20b1b912c0 100644 --- a/messagefoundry/auth/webauthn.py +++ b/messagefoundry/auth/webauthn.py @@ -179,17 +179,23 @@ def _invalid_input_errors() -> tuple[type[Exception], ...]: 0068 decision 1 requires rather than escape as a 500. Use it only around library calls, never around our own logic, where the same exceptions would mean a bug. - ``RecursionError`` joins them for a response nested past json's depth limit, which the - library's own ``json.loads`` raises and does not wrap (vault BACKLOG #5341). Its message names - the stack use, not the input. It covers a response that reaches the library as text; a route - that parses the body itself first raises its own ``RecursionError`` there, which this does - not cover. - A function rather than a constant because the library import is lazy (the extra is optional). """ from webauthn.helpers.exceptions import WebAuthnException - return (WebAuthnException, LookupError, TypeError, ValueError, RecursionError) + return (WebAuthnException, LookupError, TypeError, ValueError) + + +def _invalid_response_errors() -> tuple[type[Exception], ...]: + """:func:`_invalid_input_errors` plus ``RecursionError``, for the library's parse of a ceremony + RESPONSE only (vault BACKLOG #5341). The library's own ``json.loads`` raises it, unwrapped, for + a response nested too deep for the stack. Its message names stack use, not the input. + + Not around the stored-key check: a key that registered once has no such nesting, so there a + ``RecursionError`` would mean a low stack, not a bad credential. Nor does it reach a caller + that parses the body itself before calling in: at least the web console's passkey routes do, + and their own parse raises it first.""" + return (*_invalid_input_errors(), RecursionError) def _refusal(exc: Exception, *, ceremony: _Ceremony) -> WebAuthnVerificationError: @@ -593,7 +599,7 @@ def verify_registration( expected_origin=origin, supported_pub_key_algs=_supported_pub_key_algs(), ) - except _invalid_input_errors() as exc: + except _invalid_response_errors() as exc: # The BASE class, deliberately (PR-A review HIGH): structurally-malformed browser input # raises siblings of InvalidRegistrationResponse (InvalidJSONStructure, InvalidCBORData, # ...) — every library-side rejection must land on the audited return-False/400 path, @@ -691,7 +697,7 @@ def verify_assertion( credential_public_key=public_key, credential_current_sign_count=current_sign_count, ) - except _invalid_input_errors() as exc: + except _invalid_response_errors() as exc: # The BASE class, deliberately (PR-A review HIGH): malformed input raises siblings of # InvalidAuthenticationResponse — every rejection lands audited, never a 500. The # sign-count regression message ("...sign count...") still rides through for the @@ -786,11 +792,13 @@ def resident_key_from_response(response_json: str) -> bool | None: def _transports_from_response(response_json: str) -> list[str] | None: """Best-effort ``response.transports`` extraction (browser hint — absence is fine).""" - try: - parsed = json.loads(response_json) - transports = parsed.get("response", {}).get("transports") - except (ValueError, AttributeError): # pragma: no cover - verify_registration already parsed + # The same refusal-safe parse as resident_key_from_response, so a RecursionError here cannot + # escape after the library's own parse succeeded (vault BACKLOG #5341). + parsed, refused = json_loads_or_refusal(response_json) + if refused is not None or not isinstance(parsed, dict): return None + response = parsed.get("response") + transports = response.get("transports") if isinstance(response, dict) else None if isinstance(transports, list) and all(isinstance(t, str) for t in transports): return list(transports) or None return None diff --git a/tests/test_anchor_path.py b/tests/test_anchor_path.py index 0b007c5094..8035ca31da 100644 --- a/tests/test_anchor_path.py +++ b/tests/test_anchor_path.py @@ -1214,15 +1214,11 @@ def _subst_s(drive: str) -> str | None: return "\\??\\C:\\real" if drive == "S:" else None -def _no_subst(_drive: str) -> str | None: - return None - - @pytest.mark.parametrize( ("anchor", "cwd", "drive_target"), [ # ``C:`` joined to ``ca.pem`` reads ``C:ca.pem``. Red: walked as C:\ca.pem, from the root. - ("ca.pem", "C:", _no_subst), + ("ca.pem", "C:", ap._no_drive_target), # A subst drive as that working directory. Red: spliced as C:\realcerts\ca.pem. ("certs\\ca.pem", "S:", _subst_s), ], diff --git a/tests/test_api_auth.py b/tests/test_api_auth.py index 627ad4d836..c42249277a 100644 --- a/tests/test_api_auth.py +++ b/tests/test_api_auth.py @@ -3703,14 +3703,23 @@ async def _spy(*args: object, **kwargs: object) -> object: [{"username": "s\x01up"}, {"username": ""}, {"supersedes": "tok\x00en"}, {"supersedes": ""}], ) async def test_login_still_reaches_the_service_for_other_unusual_values( - engine: Engine, fields: dict[str, str] + engine: Engine, fields: dict[str, str], monkeypatch: pytest.MonkeyPatch ) -> None: """The controls: another control character, an empty name, and a ``supersedes`` holding a NUL - (only ever hashed, never sent to the store) are not refused by the body, so they are still - charged, padded and audited as a failed or plain sign-in, as before.""" + (only ever hashed, never sent to the store) are not refused by the body. They reach + ``AuthService.login`` as before, and answer as it decides.""" service = await _service(engine, AuthSettings(require_mfa=False)) await _add(service, "sup", Role.VIEWER) + reached: list[str] = [] + real_login = service.login + + async def _spy(username: str, *args: Any, **kwargs: Any) -> Any: + reached.append(username) + return await real_login(username, *args, **kwargs) + + monkeypatch.setattr(service, "login", _spy) async with _client(engine, service) as c: - r = await c.post("/auth/login", json={"username": "sup", "password": PW, **fields}) - expected = 200 if "supersedes" in fields else 401 - assert r.status_code == expected + body = {"username": "sup", "password": PW, **fields} + r = await c.post("/auth/login", json=body) + assert reached == [body["username"]] + assert r.status_code == (200 if "supersedes" in fields else 401) diff --git a/tests/test_webauthn.py b/tests/test_webauthn.py index f2bfbe84e3..e3dde62094 100644 --- a/tests/test_webauthn.py +++ b/tests/test_webauthn.py @@ -1826,7 +1826,8 @@ async def test_a_passkey_registered_from_a_first_seen_address_records_nothing() def test_a_deeply_nested_registration_response_is_the_audited_refusal() -> None: - """vault BACKLOG #5341. Red: json's RecursionError escaped verify_registration, a 500.""" + """vault BACKLOG #5341, at this module's seam only. Red: the library's RecursionError escaped + verify_registration. A route that parses the body itself first is not covered here.""" with pytest.raises(wa.WebAuthnVerificationError): wa.verify_registration( response_json=_TOO_DEEP, challenge=b"c" * 32, rp_id=RP, origin=ORIGIN From 39a5e13e619bcf18de1f75fc70dabeb49b5a6cc1 Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Mon, 16 Nov 2026 20:18:36 -0600 Subject: [PATCH 07/23] The _invalid_response_errors docstring states the catch's real scope It said the RecursionError arm was not around the stored-key check. In verify_assertion the arm wraps the whole verify_authentication_response call, which also decodes the stored key, so a RecursionError there is refused as invalid input too. The docstring now says so, and that only our own _require_usable_public_key, which runs first, leaves it uncaught. Docstring only; no behaviour change. BACKLOG #5341 --- messagefoundry/auth/webauthn.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/messagefoundry/auth/webauthn.py b/messagefoundry/auth/webauthn.py index 20b1b912c0..4ccfc1b82c 100644 --- a/messagefoundry/auth/webauthn.py +++ b/messagefoundry/auth/webauthn.py @@ -187,14 +187,16 @@ def _invalid_input_errors() -> tuple[type[Exception], ...]: def _invalid_response_errors() -> tuple[type[Exception], ...]: - """:func:`_invalid_input_errors` plus ``RecursionError``, for the library's parse of a ceremony - RESPONSE only (vault BACKLOG #5341). The library's own ``json.loads`` raises it, unwrapped, for - a response nested too deep for the stack. Its message names stack use, not the input. - - Not around the stored-key check: a key that registered once has no such nesting, so there a - ``RecursionError`` would mean a low stack, not a bad credential. Nor does it reach a caller - that parses the body itself before calling in: at least the web console's passkey routes do, - and their own parse raises it first.""" + """:func:`_invalid_input_errors` plus ``RecursionError``, caught around the whole library verify + call of each ceremony (vault BACKLOG #5341). The library's own ``json.loads`` raises it, + unwrapped, for a response nested too deep for the stack. Its message names stack use, not the + input. + + In :func:`verify_assertion` that call also decodes the stored key, so a ``RecursionError`` + from that decode, for example on a low stack, is refused as invalid input too. Our own + stored-key check, :func:`_require_usable_public_key`, runs before that call and does not catch + it. Nor does this reach a caller that parses the body itself before calling in: at least the + web console's passkey routes do, and their own parse raises it first.""" return (*_invalid_input_errors(), RecursionError) From 83b6fcbf2772eab2a054c262578cf13bba15f7ef Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 00:27:59 -0600 Subject: [PATCH 08/23] Console passkey routes refuse a too-deep body; sign-in name rule covers the form login and /users/directory Review findings on b613-m-authmisc (vault BACKLOG #5341, #5342): - The console's POST /ui/reauth/webauthn and /ui/account/webauthn/verify parse their JSON body themselves, so a body nested too deep for the stack raised RecursionError out of the route. Both now answer 400 "malformed request" (BACKLOG #5341). - The NUL-username refusal moved to api/validation.py as SIGN_IN_NAME_PATTERN, listed in docs/API-INPUT-VALIDATION.md, and now also binds POST /users/directory and the console's POST /ui/login form, which carries the combined passkey sign-in too (BACKLOG #5342). The form's two refusals share one helper that clears the challenge cookie. - webauthn.py: the _invalid_response_errors docstring said the stored-key decode could raise RecursionError. Measured on webauthn 3.0.1: its parse_cbor wraps every Exception as InvalidCBORData, and cbor2 6.1.2 refuses nesting past 400 with CBORDecodeError. The docstring now says only the JSON parse needs the catch. - The module-seam deep-nesting tests now assert the precondition (json.loads raises RecursionError on this stack), the RecursionError cause and the raw-type WARNING, so they fail with the catch reverted. --- docs/API-INPUT-VALIDATION.md | 1 + messagefoundry/api/auth_models.py | 18 ++- messagefoundry/api/validation.py | 8 ++ messagefoundry/auth/webauthn.py | 15 +-- messagefoundry_webconsole/routes/account.py | 4 +- messagefoundry_webconsole/routes/core.py | 24 +++- .../tests/test_ui_passkey_body_refusals.py | 123 ++++++++++++++++++ tests/test_auth_federated_identity_routes.py | 20 +++ tests/test_webauthn.py | 34 ++++- 9 files changed, 217 insertions(+), 30 deletions(-) create mode 100644 packaging/messagefoundry-webconsole/tests/test_ui_passkey_body_refusals.py diff --git a/docs/API-INPUT-VALIDATION.md b/docs/API-INPUT-VALIDATION.md index 7946acceaf..e72546e56d 100644 --- a/docs/API-INPUT-VALIDATION.md +++ b/docs/API-INPUT-VALIDATION.md @@ -43,6 +43,7 @@ before the value reaches a database query, a filesystem path, a log line or a CS | Audit action filter | Printable text, no control characters, 1 to 128 characters | `action` on `GET /audit` and `GET /audit/export` | | Display label | Printable text, no control characters, 1 to 128 characters | `name` on `POST /search/presets` | | Filesystem path | Printable text, no control characters, 1 to 4096 characters | `config_dir` on `POST /config/reload`, and `archive` on `POST /dr/activate` | +| Sign-in name | Any text except NUL; on the two API routes, also up to 256 characters | `username` on `POST /auth/login` and `POST /users/directory`, and the `username` field of the web console's `POST /ui/login` form, which checks only the NUL | | Layered preset ids | Resource ids joined by single commas with no spaces, up to 1024 characters in all | `presets` on `GET /search/layered` | The email-address row covers `recipient_override` only. The account email fields follow a different diff --git a/messagefoundry/api/auth_models.py b/messagefoundry/api/auth_models.py index a9480c5616..559ca72dba 100644 --- a/messagefoundry/api/auth_models.py +++ b/messagefoundry/api/auth_models.py @@ -18,6 +18,7 @@ from messagefoundry.api.request_model import RequestModel from messagefoundry.api.validation import ( MAX_MAP_ENTRIES, + SIGN_IN_NAME_PATTERN, ChannelScopeEntry, PermissionId, RoleId, @@ -34,8 +35,6 @@ _GROUP_MAX = 512 # A session token is 43 URL-safe characters (auth/tokens.py); the cap only refuses absurd input. _TOKEN_MAX = 256 -#: Any text but a NUL, empty included (vault BACKLOG #5342; ``LoginRequest`` says why). -_NO_NUL_PATTERN = r"^[^\x00]*$" #: The response header an ``oidc`` session's step-up refusal carries, set to #: :data:`STEP_UP_VIA_IDP`, so a client can branch on a header rather than parse the detail @@ -57,13 +56,10 @@ class CredentialReply(BaseModel): class LoginRequest(RequestModel): - # A NUL in ``username`` is refused here (vault BACKLOG #5342). It would reach the store's - # lookup, where PostgreSQL refuses it with an error this route does not catch, so the route - # would answer 500 outside its failure-timing pad. Only the NUL: PostgreSQL text cannot hold - # one, so no account on that store has one, while other control characters can reach a stored - # name through paths ``username_refusal`` does not govern, such as provision-admin. This covers - # only the JSON route; at least the web console's form login reaches the service without it. - username: str = Field(max_length=_NAME_MAX, pattern=_NO_NUL_PATTERN) + # A NUL in ``username`` is refused here (vault BACKLOG #5342; ``SIGN_IN_NAME_PATTERN`` says + # why). Unrefused, it would reach the store's lookup, where PostgreSQL raises an error this + # route does not catch, so the route would answer 500 outside its failure-timing pad. + username: str = Field(max_length=_NAME_MAX, pattern=SIGN_IN_NAME_PATTERN) password: str = Field(max_length=_PASSWORD_MAX) provider: str = Field(default="local", max_length=16) # 'local' | 'ad' #: The raw session token this sign-in REPLACES in the caller, if any (ASVS 7.2.4, BACKLOG @@ -329,7 +325,9 @@ class DirectoryUserCreateRequest(RequestModel): cannot point the holder's notices away from the directory's (ASVS 6.3.7). """ - username: str = Field(min_length=1, max_length=_NAME_MAX) + # The sign-in name rule (vault BACKLOG #5342): the name goes to the directory lookup, and the + # name the directory returns to the store, so a NUL is refused before either sees it. + username: str = Field(min_length=1, max_length=_NAME_MAX, pattern=SIGN_IN_NAME_PATTERN) notify_email: str | None = Field(default=None, max_length=_NAME_MAX) diff --git a/messagefoundry/api/validation.py b/messagefoundry/api/validation.py index e73034d5ba..5588d4247f 100644 --- a/messagefoundry/api/validation.py +++ b/messagefoundry/api/validation.py @@ -203,6 +203,14 @@ #: Printable text: no C0 control, no DEL, no C1 control. One or more characters. PRINTABLE_TEXT_PATTERN = r"^[^\x00-\x1f\x7f-\x9f]+$" +#: A sign-in name: any text but a NUL, empty included (vault BACKLOG #5342). A NUL would reach the +#: store's lookup, and PostgreSQL text cannot hold one, so its driver raises rather than finding no +#: row. Only the NUL: no account on that store can hold one, while other control characters can +#: reach a stored name through paths ``auth.usernames.username_refusal`` does not govern, such as +#: provision-admin, so refusing them here could lock a holder out. The web console's form sign-in +#: applies the same rule by hand (``"\x00" in``), since it parses its form without pydantic. +SIGN_IN_NAME_PATTERN = r"^[^\x00]*$" + #: A member of one of the engine's own closed vocabularies -- ``MessageStatus``, ``OutboxStatus``, #: the connection-event kinds. Every member of all three is letters and underscores only. The #: vocabulary itself stays the store's to define; this is the shape a member can have. diff --git a/messagefoundry/auth/webauthn.py b/messagefoundry/auth/webauthn.py index 4ccfc1b82c..b612b633d2 100644 --- a/messagefoundry/auth/webauthn.py +++ b/messagefoundry/auth/webauthn.py @@ -189,14 +189,13 @@ def _invalid_input_errors() -> tuple[type[Exception], ...]: def _invalid_response_errors() -> tuple[type[Exception], ...]: """:func:`_invalid_input_errors` plus ``RecursionError``, caught around the whole library verify call of each ceremony (vault BACKLOG #5341). The library's own ``json.loads`` raises it, - unwrapped, for a response nested too deep for the stack. Its message names stack use, not the - input. - - In :func:`verify_assertion` that call also decodes the stored key, so a ``RecursionError`` - from that decode, for example on a low stack, is refused as invalid input too. Our own - stored-key check, :func:`_require_usable_public_key`, runs before that call and does not catch - it. Nor does this reach a caller that parses the body itself before calling in: at least the - web console's passkey routes do, and their own parse raises it first.""" + unwrapped, for a response nested too deep for the stack: its credential parsers catch only + ``JSONDecodeError``. Its message names stack use, not the input. + + Only that JSON parse needs it: the library's CBOR decodes, the stored key's included, go + through its ``parse_cbor``, which wraps every ``Exception`` as ``InvalidCBORData`` (read on + webauthn 3.0.1). A caller that parses the body itself is not reached by this catch: the web + console's two passkey JSON routes catch their own parse's ``RecursionError``.""" return (*_invalid_input_errors(), RecursionError) diff --git a/messagefoundry_webconsole/routes/account.py b/messagefoundry_webconsole/routes/account.py index 7bb465ac09..b0c32c2b4f 100644 --- a/messagefoundry_webconsole/routes/account.py +++ b/messagefoundry_webconsole/routes/account.py @@ -888,7 +888,9 @@ async def ui_webauthn_verify( enrolment_code = ( raw_code[:_ENROLMENT_CODE_MAX] if isinstance(raw_code, str) and raw_code else None ) - except (ValueError, KeyError, TypeError, AttributeError): + except (ValueError, KeyError, TypeError, AttributeError, RecursionError): + # RecursionError: json's own refusal of a body nested too deep for the stack, from the + # parse or the re-encode, is a malformed request too, not a 500 (vault BACKLOG #5341). return JSONResponse({"ok": False, "error": "malformed request"}, status_code=400) # BACKLOG #3014: ui_mfa_verify's per-actor budget, charged only on a request that reaches # the ceremony, and after the gate's admin-write charge (that order is BACKLOG #2652's, left diff --git a/messagefoundry_webconsole/routes/core.py b/messagefoundry_webconsole/routes/core.py index 87d20a2509..a0bfdd9273 100644 --- a/messagefoundry_webconsole/routes/core.py +++ b/messagefoundry_webconsole/routes/core.py @@ -768,6 +768,19 @@ async def ui_login(request: Request) -> Response: provider_value = form.get("provider", "local") if provider_value not in ("local", "ad"): return RedirectResponse("/ui/login?e=bad", status_code=303) + + def _refused() -> Response: + refused = RedirectResponse("/ui/login?e=bad", status_code=303) + if handle: + clear_passkey_sign_in_cookie(refused, request) + return refused + + username = form.get("username", "") + if "\x00" in username: + # The JSON route's sign-in name rule, by hand since this form is not a pydantic model + # (vault BACKLOG #5342; api/validation.py ``SIGN_IN_NAME_PATTERN`` says why). It covers + # the combined passkey sign-in too, which rides this form's username. + return _refused() # ADR 0197 Amendment A, wave 2: the combined passkey sign-in. The assertion rides the form, # the handle of its challenge the cookie the begin leg set; both or neither. Clamped, so an # oversized assertion is simply an invalid one. @@ -779,7 +792,7 @@ async def ui_login(request: Request) -> Response: else None ) outcome = await auth.login( - form.get("username", ""), + username, form.get("password", ""), provider=AuthProvider.AD if provider_value == "ad" else AuthProvider.LOCAL, client=client, @@ -795,10 +808,7 @@ async def ui_login(request: Request) -> Response: passkey_challenge=staged.challenge if staged is not None else None, ) if not outcome.ok or outcome.token is None: - refused = RedirectResponse("/ui/login?e=bad", status_code=303) - if handle: - clear_passkey_sign_in_cookie(refused, request) - return refused + return _refused() # A must-change account goes straight to the browser rotation page (L4b) — every other # /ui route would bounce it there anyway (require_ui). An MFA-pending session lands on the # second-factor page for the same reason (ASVS 6.3.3). must_change comes first for an @@ -2220,7 +2230,9 @@ async def ui_reauth_webauthn(request: Request) -> Response: try: body = await request.json() response_json = json.dumps(body["response"]) - except (ValueError, KeyError, TypeError): + except (ValueError, KeyError, TypeError, RecursionError): + # RecursionError: json's own refusal of a body nested too deep for the stack, from the + # parse or the re-encode, is a malformed request too, not a 500 (vault BACKLOG #5341). return JSONResponse({"ok": False, "error": "malformed request"}, status_code=400) elevation = await auth.finish_webauthn_assertion( token, response_json, client=client, rp_id=rp[0], origin=rp[1] diff --git a/packaging/messagefoundry-webconsole/tests/test_ui_passkey_body_refusals.py b/packaging/messagefoundry-webconsole/tests/test_ui_passkey_body_refusals.py new file mode 100644 index 0000000000..2494264426 --- /dev/null +++ b/packaging/messagefoundry-webconsole/tests/test_ui_passkey_body_refusals.py @@ -0,0 +1,123 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later +# Copyright (C) 2026 MessageFoundry Foundation, LLC and contributors +"""Two console refusals the engine's own models could not reach (vault BACKLOG #5341, #5342). + +``POST /ui/reauth/webauthn`` and ``POST /ui/account/webauthn/verify`` parse their JSON body +themselves, before the engine's ceremony code sees it, so a body nested too deep for the stack +raised ``RecursionError`` out of the route. ``POST /ui/login`` parses its form without pydantic, +so the JSON route's sign-in name rule (no NUL) did not cover it, nor the combined passkey sign-in +that rides the same form. All data here is synthetic. +""" + +from __future__ import annotations + +import json +from typing import Any + +import pytest +from _ui_clients import PW, SAME_ORIGIN, cookie_login, provision +from _webui_support import _client, _service + +from messagefoundry.auth import Role +from messagefoundry.auth.service import AuthService +from messagefoundry.pipeline import Engine + +#: Deep enough that json refuses it on any stack these tests run on, and inside the 1 MiB body cap. +_DEPTH = 100_000 +_TOO_DEEP = "[" * _DEPTH + "]" * _DEPTH + + +_DEEP_BODIES = { + "/ui/reauth/webauthn": '{"response": ' + _TOO_DEEP + "}", + "/ui/account/webauthn/verify": '{"label": "k", "response": ' + _TOO_DEEP + "}", +} + + +@pytest.mark.parametrize("route", sorted(_DEEP_BODIES)) +async def test_a_body_nested_too_deep_is_a_malformed_request_not_a_500( + engine: Engine, route: str +) -> None: + """vault BACKLOG #5341. Red: the route's own ``request.json()`` raised ``RecursionError``, + which its catch did not list, so it escaped the route (a 500 on a served engine).""" + body = _DEEP_BODIES[route] + # The precondition that makes this test about RecursionError: on this stack json refuses the + # body that way. Were the stack ever deep enough to decode it, this fails rather than passing on + # some other path. + with pytest.raises(RecursionError): + json.loads(body) + service = await _service(engine) + await provision(service, "op", [Role.OPERATOR.value]) + async with _client(engine, service) as c: + await cookie_login(c, "op") + r = await c.post( + route, + content=body, + headers={**SAME_ORIGIN, "Content-Type": "application/json"}, + ) + assert (r.status_code, r.json()) == (400, {"ok": False, "error": "malformed request"}) + + +def _spy_login(service: AuthService, monkeypatch: pytest.MonkeyPatch) -> list[str]: + reached: list[str] = [] + real_login = service.login + + async def _spy(username: str, *args: Any, **kwargs: Any) -> Any: + reached.append(username) + return await real_login(username, *args, **kwargs) + + monkeypatch.setattr(service, "login", _spy) + return reached + + +@pytest.mark.parametrize("username", ["op\x00", "\x00", "o\x00p"]) +async def test_the_form_sign_in_refuses_a_nul_username_before_the_service( + engine: Engine, monkeypatch: pytest.MonkeyPatch, username: str +) -> None: + """vault BACKLOG #5342, the form route. Red: the NUL reached ``AuthService.login``, where a + PostgreSQL store's lookup would raise an error the route does not catch.""" + service = await _service(engine) + reached = _spy_login(service, monkeypatch) + async with _client(engine, service) as c: + r = await c.post( + "/ui/login", data={"username": username, "password": PW}, headers=SAME_ORIGIN + ) + assert (r.status_code, r.headers["location"]) == (303, "/ui/login?e=bad") + assert reached == [] + + +async def test_the_combined_passkey_sign_in_refuses_a_nul_username_and_ends_its_challenge( + engine: Engine, monkeypatch: pytest.MonkeyPatch +) -> None: + """vault BACKLOG #5342, the combined passkey sign-in, which rides the same form. Red: as above. + The refusal also clears the challenge cookie, as every other refused sign-in does.""" + pytest.importorskip("webauthn") + service = await _service(engine) + reached = _spy_login(service, monkeypatch) + async with _client(engine, service) as c: + begun = await c.post("/ui/login/passkey", headers=SAME_ORIGIN) + assert begun.status_code == 200, begun.text + r = await c.post( + "/ui/login", + data={"username": "o\x00p", "password": "", "passkey_assertion": "{}"}, + headers=SAME_ORIGIN, + ) + assert (r.status_code, r.headers["location"]) == (303, "/ui/login?e=bad") + assert reached == [] + cleared = [h for h in r.headers.get_list("set-cookie") if "passkey" in h.lower()] + assert cleared and all("max-age=0" in h.lower() for h in cleared), cleared + + +@pytest.mark.parametrize("username", ["o\x01p", "nobody"]) +async def test_the_form_sign_in_still_reaches_the_service_for_other_names( + engine: Engine, monkeypatch: pytest.MonkeyPatch, username: str +) -> None: + """The control: another control character and an ordinary unknown name still reach + ``AuthService.login``, which refuses them as it decides.""" + service = await _service(engine) + reached = _spy_login(service, monkeypatch) + async with _client(engine, service) as c: + r = await c.post( + "/ui/login", data={"username": username, "password": PW}, headers=SAME_ORIGIN + ) + assert (r.status_code, r.headers["location"]) == (303, "/ui/login?e=bad") + assert reached == [username] diff --git a/tests/test_auth_federated_identity_routes.py b/tests/test_auth_federated_identity_routes.py index 57a19714e5..57a474eea6 100644 --- a/tests/test_auth_federated_identity_routes.py +++ b/tests/test_auth_federated_identity_routes.py @@ -924,6 +924,26 @@ async def test_a_directory_row_created_by_name_takes_its_id_from_the_directory_a assert await _pairs(engine, user.id) == {user.id: (ISSUER, "S-1-a")} +async def test_the_directory_create_refuses_a_nul_in_the_name_before_the_directory( + engine: Engine, +) -> None: + """vault BACKLOG #5342, the sign-in name rule on ``POST /users/directory``. Red: the NUL went + on to the directory lookup, and from there toward the store. THE CONTROL is the same name + without it, which the directory is asked about.""" + directory = _Directory(_principal("jdoe")) + service = await _directory_service(engine, directory) + async with _client(engine, service) as c: + tok = await _admin(c, service) + refused = await c.post( + "/users/directory", json={"username": "jd\x00oe"}, headers=_auth(tok) + ) + assert refused.status_code == 422, refused.text + assert directory.asked == [] + ok = await c.post("/users/directory", json={"username": "jdoe"}, headers=_auth(tok)) + assert ok.status_code == 201, ok.text + assert directory.asked != [] + + async def test_the_directory_create_needs_users_manage_and_a_fresh_step_up(engine: Engine) -> None: """Gated as ``POST /users``: a Viewer is refused, and so is an Administrator whose step-up window has lapsed. The control is the same Administrator after a re-authentication.""" diff --git a/tests/test_webauthn.py b/tests/test_webauthn.py index e3dde62094..3f758db97c 100644 --- a/tests/test_webauthn.py +++ b/tests/test_webauthn.py @@ -1825,19 +1825,42 @@ async def test_a_passkey_registered_from_a_first_seen_address_records_nothing() _TOO_DEEP = '{"id": ' + "[" * 100_000 + "]" * 100_000 + "}" -def test_a_deeply_nested_registration_response_is_the_audited_refusal() -> None: +def _refused_on_recursion( + excinfo: pytest.ExceptionInfo[wa.WebAuthnVerificationError], caplog: pytest.LogCaptureFixture +) -> None: + """The refusal came from the RecursionError catch, not from some other library refusal: the + precondition holds on this stack, the cause is the RecursionError, and its type was logged.""" + with pytest.raises(RecursionError): + json.loads(_TOO_DEEP) + assert isinstance(excinfo.value.__cause__, RecursionError), repr(excinfo.value.__cause__) + assert any("raw RecursionError" in r.getMessage() for r in caplog.records) + + +def test_a_deeply_nested_registration_response_is_the_audited_refusal( + caplog: pytest.LogCaptureFixture, +) -> None: """vault BACKLOG #5341, at this module's seam only. Red: the library's RecursionError escaped - verify_registration. A route that parses the body itself first is not covered here.""" - with pytest.raises(wa.WebAuthnVerificationError): + verify_registration. The web console's routes that parse the body first have their own test, + in the web console suite (``test_ui_passkey_body_refusals.py``).""" + with ( + caplog.at_level(logging.WARNING, logger=_WEBAUTHN_LOG), + pytest.raises(wa.WebAuthnVerificationError) as excinfo, + ): wa.verify_registration( response_json=_TOO_DEEP, challenge=b"c" * 32, rp_id=RP, origin=ORIGIN ) + _refused_on_recursion(excinfo, caplog) -def test_a_deeply_nested_assertion_response_is_the_audited_refusal() -> None: +def test_a_deeply_nested_assertion_response_is_the_audited_refusal( + caplog: pytest.LogCaptureFixture, +) -> None: """vault BACKLOG #5341, the text form ``verify_assertion`` accepts. Red: as above.""" soft = SoftAuthenticator(rp_id=RP, origin=ORIGIN) - with pytest.raises(wa.WebAuthnVerificationError): + with ( + caplog.at_level(logging.WARNING, logger=_WEBAUTHN_LOG), + pytest.raises(wa.WebAuthnVerificationError) as excinfo, + ): wa.verify_assertion( response_json=_TOO_DEEP, challenge=b"c" * 32, @@ -1846,3 +1869,4 @@ def test_a_deeply_nested_assertion_response_is_the_audited_refusal() -> None: public_key=soft.cose_public_key(), current_sign_count=0, ) + _refused_on_recursion(excinfo, caplog) From 73aa44d1b6d7e0bdf8fddd3fa70dfb86dce809e7 Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 01:01:34 -0600 Subject: [PATCH 09/23] Review round 1: log the form's NUL refusal; correct three control claims Code review of 83b6fcbf277 (vault BACKLOG #5341, #5342): - The console form sign-in's NUL refusal never reaches the service, so it wrote no audit row. It now logs a WARNING naming the client, never the name, and the test asserts that line. - webauthn.py: the docstring said only the credential JSON parse needs the RecursionError catch. The library's clientDataJSON parser also catches only JSONDecodeError, so the catch stays around the whole call. - auth_models.py: the /users/directory comment claimed the rule keeps a NUL from the store. It checks only the typed name, which goes to the directory; the store gets the directory's name. - docs/API-INPUT-VALIDATION.md: the Sign-in name row now gives the directory route's minimum, and the form route's redirect refusal is stated beside the table, since the page promises a 422. --- docs/API-INPUT-VALIDATION.md | 6 +++++- messagefoundry/api/auth_models.py | 5 +++-- messagefoundry/auth/webauthn.py | 7 ++++--- messagefoundry_webconsole/routes/core.py | 6 +++++- .../tests/test_ui_passkey_body_refusals.py | 17 +++++++++++------ tests/test_auth_federated_identity_routes.py | 4 ++-- 6 files changed, 30 insertions(+), 15 deletions(-) diff --git a/docs/API-INPUT-VALIDATION.md b/docs/API-INPUT-VALIDATION.md index e72546e56d..1c3ab3e393 100644 --- a/docs/API-INPUT-VALIDATION.md +++ b/docs/API-INPUT-VALIDATION.md @@ -43,12 +43,16 @@ before the value reaches a database query, a filesystem path, a log line or a CS | Audit action filter | Printable text, no control characters, 1 to 128 characters | `action` on `GET /audit` and `GET /audit/export` | | Display label | Printable text, no control characters, 1 to 128 characters | `name` on `POST /search/presets` | | Filesystem path | Printable text, no control characters, 1 to 4096 characters | `config_dir` on `POST /config/reload`, and `archive` on `POST /dr/activate` | -| Sign-in name | Any text except NUL; on the two API routes, also up to 256 characters | `username` on `POST /auth/login` and `POST /users/directory`, and the `username` field of the web console's `POST /ui/login` form, which checks only the NUL | +| Sign-in name | Any text except NUL, up to 256 characters (1 to 256 on `POST /users/directory`) | `username` on `POST /auth/login` and `POST /users/directory` | | Layered preset ids | Resource ids joined by single commas with no spaces, up to 1024 characters in all | `presets` on `GET /search/layered` | The email-address row covers `recipient_override` only. The account email fields follow a different rule, set out under [Items checked a second time](#items-checked-a-second-time-behind-the-route). +The web console's `POST /ui/login` form applies the sign-in name's NUL rule too, without its length +bound. It is not a JSON route, so it refuses with its usual redirect to the sign-in page rather +than a 422. + "Printable text, no control characters" means every character except the C0 range, DEL, and the C1 range. In practice: no NUL, no tab, no carriage return, no line feed. diff --git a/messagefoundry/api/auth_models.py b/messagefoundry/api/auth_models.py index 559ca72dba..c1e88134c8 100644 --- a/messagefoundry/api/auth_models.py +++ b/messagefoundry/api/auth_models.py @@ -325,8 +325,9 @@ class DirectoryUserCreateRequest(RequestModel): cannot point the holder's notices away from the directory's (ASVS 6.3.7). """ - # The sign-in name rule (vault BACKLOG #5342): the name goes to the directory lookup, and the - # name the directory returns to the store, so a NUL is refused before either sees it. + # The sign-in name rule (vault BACKLOG #5342), on the typed name, which goes only to the + # directory lookup. The name the store receives is the one the directory returns, and this + # rule does not check that one. username: str = Field(min_length=1, max_length=_NAME_MAX, pattern=SIGN_IN_NAME_PATTERN) notify_email: str | None = Field(default=None, max_length=_NAME_MAX) diff --git a/messagefoundry/auth/webauthn.py b/messagefoundry/auth/webauthn.py index b612b633d2..605492036e 100644 --- a/messagefoundry/auth/webauthn.py +++ b/messagefoundry/auth/webauthn.py @@ -189,10 +189,11 @@ def _invalid_input_errors() -> tuple[type[Exception], ...]: def _invalid_response_errors() -> tuple[type[Exception], ...]: """:func:`_invalid_input_errors` plus ``RecursionError``, caught around the whole library verify call of each ceremony (vault BACKLOG #5341). The library's own ``json.loads`` raises it, - unwrapped, for a response nested too deep for the stack: its credential parsers catch only - ``JSONDecodeError``. Its message names stack use, not the input. + unwrapped, for JSON nested too deep for the stack: at least its credential parsers and its + ``clientDataJSON`` parser catch only ``JSONDecodeError``. Its message names stack use, not the + input. So the catch stays around the whole call, not one parse. - Only that JSON parse needs it: the library's CBOR decodes, the stored key's included, go + The stored key is not such a source: the library's CBOR decodes, the stored key's included, go through its ``parse_cbor``, which wraps every ``Exception`` as ``InvalidCBORData`` (read on webauthn 3.0.1). A caller that parses the body itself is not reached by this catch: the web console's two passkey JSON routes catch their own parse's ``RecursionError``.""" diff --git a/messagefoundry_webconsole/routes/core.py b/messagefoundry_webconsole/routes/core.py index a0bfdd9273..652623c399 100644 --- a/messagefoundry_webconsole/routes/core.py +++ b/messagefoundry_webconsole/routes/core.py @@ -779,7 +779,11 @@ def _refused() -> Response: if "\x00" in username: # The JSON route's sign-in name rule, by hand since this form is not a pydantic model # (vault BACKLOG #5342; api/validation.py ``SIGN_IN_NAME_PATTERN`` says why). It covers - # the combined passkey sign-in too, which rides this form's username. + # the combined passkey sign-in too, which rides this form's username. The service never + # sees the name, so it writes no audit row; this line is the record. Never the name. + _log.warning( + "sign-in refused before the service: username held a NUL, client=%s", client + ) return _refused() # ADR 0197 Amendment A, wave 2: the combined passkey sign-in. The assertion rides the form, # the handle of its challenge the cookie the begin leg set; both or neither. Clamped, so an diff --git a/packaging/messagefoundry-webconsole/tests/test_ui_passkey_body_refusals.py b/packaging/messagefoundry-webconsole/tests/test_ui_passkey_body_refusals.py index 2494264426..49ef18ca28 100644 --- a/packaging/messagefoundry-webconsole/tests/test_ui_passkey_body_refusals.py +++ b/packaging/messagefoundry-webconsole/tests/test_ui_passkey_body_refusals.py @@ -12,6 +12,7 @@ from __future__ import annotations import json +import logging from typing import Any import pytest @@ -71,25 +72,29 @@ async def _spy(username: str, *args: Any, **kwargs: Any) -> Any: @pytest.mark.parametrize("username", ["op\x00", "\x00", "o\x00p"]) async def test_the_form_sign_in_refuses_a_nul_username_before_the_service( - engine: Engine, monkeypatch: pytest.MonkeyPatch, username: str + engine: Engine, monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, username: str ) -> None: """vault BACKLOG #5342, the form route. Red: the NUL reached ``AuthService.login``, where a - PostgreSQL store's lookup would raise an error the route does not catch.""" + PostgreSQL store's lookup would raise an error the route does not catch. The service writes no + audit row for it, so the route logs the refusal, without the name.""" service = await _service(engine) reached = _spy_login(service, monkeypatch) async with _client(engine, service) as c: - r = await c.post( - "/ui/login", data={"username": username, "password": PW}, headers=SAME_ORIGIN - ) + with caplog.at_level(logging.WARNING, logger="messagefoundry_webconsole.routes.core"): + r = await c.post( + "/ui/login", data={"username": username, "password": PW}, headers=SAME_ORIGIN + ) assert (r.status_code, r.headers["location"]) == (303, "/ui/login?e=bad") assert reached == [] + [line] = [m for m in (rec.getMessage() for rec in caplog.records) if "held a NUL" in m] + assert "\x00" not in line async def test_the_combined_passkey_sign_in_refuses_a_nul_username_and_ends_its_challenge( engine: Engine, monkeypatch: pytest.MonkeyPatch ) -> None: """vault BACKLOG #5342, the combined passkey sign-in, which rides the same form. Red: as above. - The refusal also clears the challenge cookie, as every other refused sign-in does.""" + The refusal also clears the challenge cookie, as a refused sign-in after the service does.""" pytest.importorskip("webauthn") service = await _service(engine) reached = _spy_login(service, monkeypatch) diff --git a/tests/test_auth_federated_identity_routes.py b/tests/test_auth_federated_identity_routes.py index 57a474eea6..4204c39d90 100644 --- a/tests/test_auth_federated_identity_routes.py +++ b/tests/test_auth_federated_identity_routes.py @@ -928,8 +928,8 @@ async def test_the_directory_create_refuses_a_nul_in_the_name_before_the_directo engine: Engine, ) -> None: """vault BACKLOG #5342, the sign-in name rule on ``POST /users/directory``. Red: the NUL went - on to the directory lookup, and from there toward the store. THE CONTROL is the same name - without it, which the directory is asked about.""" + on to the directory lookup. THE CONTROL is the same name without it, which the directory is + asked about.""" directory = _Directory(_principal("jdoe")) service = await _directory_service(engine, directory) async with _client(engine, service) as c: From b0a2d195082a999eef3d0e19813902f0941f8860 Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 01:41:08 -0600 Subject: [PATCH 10/23] Review round 2: every form sign-in refusal ends the passkey handle; claim fixes Code review of 73aa44d1b6d (vault BACKLOG #5341, #5342): - The console form's bad-provider refusal now uses the same helper as the NUL and service refusals, so it also clears the popped challenge's handle cookie. The provider comment says the NUL refusal precedes it. - webauthn.py: the console routes are named as "at least", not counted. - auth_models.py: the /users/directory comment says why the rule applies. - The NUL-refusal log test asserts the line's only argument is the client. Proposed PR title: Console passkey routes refuse a too-deep body; the sign-in name rule covers the console form and /users/directory Proposed vault banners: - #5337: Fixed (unchanged by this branch; built on b613-c-authmisc). - #5341: Fixed. verify_registration/verify_assertion refuse a too-deep response as audited invalid input, and the console's /ui/reauth/webauthn and /ui/account/webauthn/verify answer 400. Left: POST /ui/csp-report (unauthenticated) still lets RecursionError out of its json.loads; file as a new item or a step. - #5342: Fixed for the name. A NUL username is refused at POST /auth/login, POST /users/directory and the console POST /ui/login form, combined passkey sign-in included. Left: the console form still has no 256-char bound; the store-level guard (get_user_by_username returning no row for a NUL) that would cover every caller is unbuilt; supersedes is not refused by design (it is only hashed). --- messagefoundry/api/auth_models.py | 5 +++-- messagefoundry/auth/webauthn.py | 5 +++-- messagefoundry_webconsole/routes/core.py | 10 ++++++---- .../tests/test_ui_passkey_body_refusals.py | 5 +++-- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/messagefoundry/api/auth_models.py b/messagefoundry/api/auth_models.py index c1e88134c8..c7b59a6757 100644 --- a/messagefoundry/api/auth_models.py +++ b/messagefoundry/api/auth_models.py @@ -326,8 +326,9 @@ class DirectoryUserCreateRequest(RequestModel): """ # The sign-in name rule (vault BACKLOG #5342), on the typed name, which goes only to the - # directory lookup. The name the store receives is the one the directory returns, and this - # rule does not check that one. + # directory lookup. Applied here because no account can hold a NUL, so a name holding one can + # name no directory account either; the 422 says so before a directory round trip. The name + # the store receives is the one the directory returns, and this rule does not check that one. username: str = Field(min_length=1, max_length=_NAME_MAX, pattern=SIGN_IN_NAME_PATTERN) notify_email: str | None = Field(default=None, max_length=_NAME_MAX) diff --git a/messagefoundry/auth/webauthn.py b/messagefoundry/auth/webauthn.py index 605492036e..bdd2f5003b 100644 --- a/messagefoundry/auth/webauthn.py +++ b/messagefoundry/auth/webauthn.py @@ -195,8 +195,9 @@ def _invalid_response_errors() -> tuple[type[Exception], ...]: The stored key is not such a source: the library's CBOR decodes, the stored key's included, go through its ``parse_cbor``, which wraps every ``Exception`` as ``InvalidCBORData`` (read on - webauthn 3.0.1). A caller that parses the body itself is not reached by this catch: the web - console's two passkey JSON routes catch their own parse's ``RecursionError``.""" + webauthn 3.0.1). A caller that parses the body itself is not reached by this catch, so it + must catch its own parse's ``RecursionError``, as at least the web console's passkey routes + do.""" return (*_invalid_input_errors(), RecursionError) diff --git a/messagefoundry_webconsole/routes/core.py b/messagefoundry_webconsole/routes/core.py index 652623c399..14d1cd905a 100644 --- a/messagefoundry_webconsole/routes/core.py +++ b/messagefoundry_webconsole/routes/core.py @@ -764,17 +764,19 @@ async def ui_login(request: Request) -> Response: # selector, and "ad" stays in the allow-list only so the ENGINE (_dispatch_login) is the # single point that refuses and audits it. Directory accounts sign in by Windows SSO or # OIDC, and the AD bind as the user survives only as the step-up re-bind at /ui/reauth - # (and the JSON /me/reauth). - provider_value = form.get("provider", "local") - if provider_value not in ("local", "ad"): - return RedirectResponse("/ui/login?e=bad", status_code=303) + # (and the JSON /me/reauth). A name holding a NUL is the exception: it is refused below, + # whatever the provider, before the engine sees it. def _refused() -> Response: + # The challenge is already popped, so every refusal also ends its handle cookie. refused = RedirectResponse("/ui/login?e=bad", status_code=303) if handle: clear_passkey_sign_in_cookie(refused, request) return refused + provider_value = form.get("provider", "local") + if provider_value not in ("local", "ad"): + return _refused() username = form.get("username", "") if "\x00" in username: # The JSON route's sign-in name rule, by hand since this form is not a pydantic model diff --git a/packaging/messagefoundry-webconsole/tests/test_ui_passkey_body_refusals.py b/packaging/messagefoundry-webconsole/tests/test_ui_passkey_body_refusals.py index 49ef18ca28..00302e11fe 100644 --- a/packaging/messagefoundry-webconsole/tests/test_ui_passkey_body_refusals.py +++ b/packaging/messagefoundry-webconsole/tests/test_ui_passkey_body_refusals.py @@ -86,8 +86,9 @@ async def test_the_form_sign_in_refuses_a_nul_username_before_the_service( ) assert (r.status_code, r.headers["location"]) == (303, "/ui/login?e=bad") assert reached == [] - [line] = [m for m in (rec.getMessage() for rec in caplog.records) if "held a NUL" in m] - assert "\x00" not in line + [record] = [rec for rec in caplog.records if "held a NUL" in rec.getMessage()] + # The client is the line's only argument, so no form of the name reaches the general log. + assert record.args == ("127.0.0.1",) async def test_the_combined_passkey_sign_in_refuses_a_nul_username_and_ends_its_challenge( From 6441830ba9d0adf04aedea3354135a5a88eca77c Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 03:10:30 -0600 Subject: [PATCH 11/23] Correct four control claims in the auth-misc comments and docs Comment, docstring and doc text only; no behaviour change. Each line says only what was measured (vault BACKLOG #5341, #5342): - webauthn.py _invalid_response_errors: run on webauthn 3.0.1, both verify functions wrap parse_client_data_json in except Exception (a too-deep clientDataJSON comes back as InvalidRegistrationResponse / InvalidAuthenticationResponse, cause RecursionError). Only the credential-JSON parsers, and the android-safetynet JWS parse, raise RecursionError unwrapped. - auth_models.py / validation.py: "no account can hold a NUL" is PostgreSQL's alone (its documentation, not run here). The SQLite store stores and finds a NUL name (measured); SQL Server unmeasured. - Console form login: the shared refusal helper clears the passkey handle cookie; the login-budget 429 raises with it left set. - API-INPUT-VALIDATION.md: the sign-in name's 256 bound is in api/auth_models.py, not api/validation.py; the intro names the other out-of-module rules found (field path, layered preset bound). Proposed PR title: Console passkey routes refuse a too-deep body; the sign-in name rule covers the console form and /users/directory Proposed vault banners: - #5337: Fixed (unchanged by this branch; built on b613-c-authmisc). - #5341: Fixed. verify_registration/verify_assertion refuse a too-deep response as audited invalid input, and the console's /ui/reauth/webauthn and /ui/account/webauthn/verify answer 400. Left: POST /ui/csp-report (unauthenticated) still lets RecursionError out of its json.loads; file as a new item or a step. - #5342: Fixed for the name. A NUL username is refused at POST /auth/login, POST /users/directory and the console POST /ui/login form, combined passkey sign-in included. Left: the console form still has no 256-char bound; the store-level guard (get_user_by_username returning no row for a NUL) that would cover every caller is unbuilt; supersedes is not refused by design (it is only hashed). --- docs/API-INPUT-VALIDATION.md | 23 ++++++++++++++++------- messagefoundry/api/auth_models.py | 7 ++++--- messagefoundry/api/validation.py | 7 +++++-- messagefoundry/auth/webauthn.py | 19 +++++++++++++++---- messagefoundry_webconsole/routes/core.py | 4 +++- 5 files changed, 43 insertions(+), 17 deletions(-) diff --git a/docs/API-INPUT-VALIDATION.md b/docs/API-INPUT-VALIDATION.md index 1c3ab3e393..42010ea4bf 100644 --- a/docs/API-INPUT-VALIDATION.md +++ b/docs/API-INPUT-VALIDATION.md @@ -5,12 +5,21 @@ It covers the control plane: the ids, connection names, time bounds and search t client sends to the engine. It does not cover message payloads. Those are the data plane, and they have their own documents: [HL7-VALIDATION.md](HL7-VALIDATION.md) and [CODESETS.md](CODESETS.md). -The rules live in one module, [`messagefoundry/api/validation.py`](../messagefoundry/api/validation.py). -The rules table below is quoted from it. `tests/test_api_input_validation.py` checks that six -phrases on this page still match the code, and fails if the page drops one. It does not check every -number on this page. The connection-name pattern is the one exception to where it is written: -it is defined in [`messagefoundry/connection_names.py`](../messagefoundry/connection_names.py) and -imported by that module, because the config loader enforces the same rule. +Most of the rules live in one module, +[`messagefoundry/api/validation.py`](../messagefoundry/api/validation.py), and most of the rules +table below is quoted from it. `tests/test_api_input_validation.py` checks that six phrases on this +page still match the code, and fails if the page drops one. It does not check every number on this +page. At least four rules in the table are written, in whole or in part, elsewhere. The +connection-name pattern is defined in +[`messagefoundry/connection_names.py`](../messagefoundry/connection_names.py) and imported by that +module, because the config loader enforces the same rule. The sign-in name's NUL rule is in +`validation.py`, but its length bounds, 256 characters and the minimum of 1 on +`POST /users/directory`, are set on the request models in +[`messagefoundry/api/auth_models.py`](../messagefoundry/api/auth_models.py). +The HL7 field-path grammar is `parse_path` in +[`messagefoundry/parsing/peek.py`](../messagefoundry/parsing/peek.py). The layered preset ids' +pattern is in `validation.py`, but their 1024-character bound is set on the route in +[`messagefoundry/api/app.py`](../messagefoundry/api/app.py). Two later sections quote rules held in other files: the items checked behind a route, and the rules over two or more items. Each entry there names the code that holds its rule. No test compares those @@ -344,7 +353,7 @@ replace it.** The first two are below. The third is the DR archive path, under 2. A log `level` is checked against the engine's own level names, which is why a wrong one gets a 400. The shape rule only keeps an arbitrary-length string out of that error message. -**One rule is documented here but enforced elsewhere.** The HL7 field path grammar belongs to +**The field path is documented here but enforced elsewhere.** The HL7 field path grammar belongs to `messagefoundry.parsing.peek.parse_path`, and `messagefoundry.store.content_search.make_spec` applies it at every point the API accepts a `field_path`. A malformed path is already a 400. Copying that pattern into the API models would create a second definition of a rule that has one. diff --git a/messagefoundry/api/auth_models.py b/messagefoundry/api/auth_models.py index c7b59a6757..396e33e83d 100644 --- a/messagefoundry/api/auth_models.py +++ b/messagefoundry/api/auth_models.py @@ -326,9 +326,10 @@ class DirectoryUserCreateRequest(RequestModel): """ # The sign-in name rule (vault BACKLOG #5342), on the typed name, which goes only to the - # directory lookup. Applied here because no account can hold a NUL, so a name holding one can - # name no directory account either; the 422 says so before a directory round trip. The name - # the store receives is the one the directory returns, and this rule does not check that one. + # directory lookup. The LDAP filter escapes a NUL (``auth.ldap._escape_filter``), so such a + # name is not sent raw; the rule refuses it with a 422 before any directory round trip, as + # POST /auth/login refuses it before the store. The name the store receives is the one the + # directory returns, and this rule does not check that one. username: str = Field(min_length=1, max_length=_NAME_MAX, pattern=SIGN_IN_NAME_PATTERN) notify_email: str | None = Field(default=None, max_length=_NAME_MAX) diff --git a/messagefoundry/api/validation.py b/messagefoundry/api/validation.py index 5588d4247f..12075156d4 100644 --- a/messagefoundry/api/validation.py +++ b/messagefoundry/api/validation.py @@ -207,8 +207,11 @@ #: store's lookup, and PostgreSQL text cannot hold one, so its driver raises rather than finding no #: row. Only the NUL: no account on that store can hold one, while other control characters can #: reach a stored name through paths ``auth.usernames.username_refusal`` does not govern, such as -#: provision-admin, so refusing them here could lock a holder out. The web console's form sign-in -#: applies the same rule by hand (``"\x00" in``), since it parses its form without pydantic. +#: provision-admin, so refusing them here could lock a holder out. That holds on PostgreSQL alone, +#: and there only on its documentation, not run here. The SQLite store can hold a NUL name: +#: ``create_user`` stores one and ``get_user_by_username`` finds it. The SQL Server store is not +#: measured. The web console's form sign-in applies the same rule by hand (``"\x00" in``), since +#: it parses its form without pydantic. SIGN_IN_NAME_PATTERN = r"^[^\x00]*$" #: A member of one of the engine's own closed vocabularies -- ``MessageStatus``, ``OutboxStatus``, diff --git a/messagefoundry/auth/webauthn.py b/messagefoundry/auth/webauthn.py index bdd2f5003b..004b11c3be 100644 --- a/messagefoundry/auth/webauthn.py +++ b/messagefoundry/auth/webauthn.py @@ -188,10 +188,21 @@ def _invalid_input_errors() -> tuple[type[Exception], ...]: def _invalid_response_errors() -> tuple[type[Exception], ...]: """:func:`_invalid_input_errors` plus ``RecursionError``, caught around the whole library verify - call of each ceremony (vault BACKLOG #5341). The library's own ``json.loads`` raises it, - unwrapped, for JSON nested too deep for the stack: at least its credential parsers and its - ``clientDataJSON`` parser catch only ``JSONDecodeError``. Its message names stack use, not the - input. So the catch stays around the whole call, not one parse. + call of each ceremony (vault BACKLOG #5341). Read and run on the installed webauthn 3.0.1: + + * Its credential-JSON parsers (``parse_registration_credential_json``, + ``parse_authentication_credential_json``) catch only ``JSONDecodeError`` around their + ``json.loads``, so a credential handed to the library as text and nested too deep for the + stack raises ``RecursionError`` unwrapped. A caller that hands it an already-parsed dict + does not reach that parse. + * At least the ``android-safetynet`` attestation check also ``json.loads`` the JWS header and + payload with no guard, and ``verify_registration_response`` calls it outside any ``try``. + * The library's ``clientDataJSON`` parse is not a source: both verify functions call it inside + ``except Exception`` and re-raise ``InvalidRegistrationResponse`` or + ``InvalidAuthenticationResponse``, which :func:`_invalid_input_errors` already holds. + + A ``RecursionError`` message names stack use, not the input. Because a source sits inside the + call as well as at its entry, the catch stays around the whole call, not one parse. The stored key is not such a source: the library's CBOR decodes, the stored key's included, go through its ``parse_cbor``, which wraps every ``Exception`` as ``InvalidCBORData`` (read on diff --git a/messagefoundry_webconsole/routes/core.py b/messagefoundry_webconsole/routes/core.py index 14d1cd905a..87f3bc3880 100644 --- a/messagefoundry_webconsole/routes/core.py +++ b/messagefoundry_webconsole/routes/core.py @@ -768,7 +768,9 @@ async def ui_login(request: Request) -> Response: # whatever the provider, before the engine sees it. def _refused() -> Response: - # The challenge is already popped, so every refusal also ends its handle cookie. + # The challenge is already popped, so each refusal this helper builds also ends its + # handle cookie. Not every refusal after the pop comes here: the login-budget 429 above + # raises with the cookie left set. refused = RedirectResponse("/ui/login?e=bad", status_code=303) if handle: clear_passkey_sign_in_cookie(refused, request) From af28eb140858311259ffbe0f82d39613e37cd83e Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 03:43:57 -0600 Subject: [PATCH 12/23] A malformed android-safetynet statement is a clean passkey refusal webauthn 3.0.1's android-safetynet check calls .decode on the attestation statement's response and .get on the decoded JWS header and payload with no type check. A non-bytes response, or a header or payload decoding to null, a list or a number, raised AttributeError straight out of verify_registration, which neither error tuple held, so enrolment would have answered 500 with no audit row. verify_registration now also catches AttributeError, around the library call only: the supported-algorithm list is resolved before the try. The _invalid_input_errors docstring no longer claims to list every exception the library raises, and names the pyproject range and the locked 3.0.1. BACKLOG #5341 --- messagefoundry/auth/webauthn.py | 24 +++++++++++++++------ tests/test_webauthn.py | 38 +++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/messagefoundry/auth/webauthn.py b/messagefoundry/auth/webauthn.py index 004b11c3be..9ffee12adc 100644 --- a/messagefoundry/auth/webauthn.py +++ b/messagefoundry/auth/webauthn.py @@ -167,15 +167,20 @@ def _supported_pub_key_algs() -> list[COSEAlgorithmIdentifier]: def _invalid_input_errors() -> tuple[type[Exception], ...]: - """Every exception py_webauthn raises for bad input, which must all reach the audited path. + """Exceptions py_webauthn raises for bad input, which must reach the audited path. + + Not every one it raises: at least :func:`_invalid_response_errors` adds ``RecursionError`` + around both ceremonies' verify calls, and :func:`verify_registration` adds ``AttributeError`` + around its own (vault BACKLOG #5341). ``WebAuthnException`` is the library's own base class. The next three entries catch four raw exception types (``LookupError`` covers both ``KeyError`` and ``IndexError``) that it lets out when a COSE key is malformed rather than merely wrong: its key decoder indexes an untyped CBOR value, so a missing label is a ``KeyError``, a short array an ``IndexError``, a bare integer a ``TypeError``, and a point that is not on its stated curve a ``ValueError`` from - ``cryptography``. Measured at engine ``0076e3cec`` on the pinned ``webauthn==3.0.0`` (BACKLOG - #1166). Each is attacker-shaped input, so each must land on the audited invalid-input path ADR + ``cryptography``. Measured at engine ``0076e3cec`` on ``webauthn`` 3.0.0 (BACKLOG #1166); + ``pyproject.toml`` allows ``webauthn>=3.0.0,<4`` and ``requirements.lock`` pins 3.0.1. + Each is attacker-shaped input, so each must land on the audited invalid-input path ADR 0068 decision 1 requires rather than escape as a 500. Use it only around library calls, never around our own logic, where the same exceptions would mean a bug. @@ -605,20 +610,27 @@ def verify_registration( _require_webauthn() from webauthn import verify_registration_response + # Resolved before the ``try`` so the AttributeError catch below covers the library call only, + # where it is attacker input, and never our own code, where it would be a bug. + supported_algs = _supported_pub_key_algs() try: verified = verify_registration_response( credential=response_json, expected_challenge=challenge, expected_rp_id=rp_id, expected_origin=origin, - supported_pub_key_algs=_supported_pub_key_algs(), + supported_pub_key_algs=supported_algs, ) - except _invalid_response_errors() as exc: + except (*_invalid_response_errors(), AttributeError) as exc: # The BASE class, deliberately (PR-A review HIGH): structurally-malformed browser input # raises siblings of InvalidRegistrationResponse (InvalidJSONStructure, InvalidCBORData, # ...) — every library-side rejection must land on the audited return-False/400 path, # never an unhandled 500. The raw decode errors join it for the same reason (BACKLOG - # #1166): a malformed COSE key raised them straight out of this call. + # #1166): a malformed COSE key raised them straight out of this call. AttributeError + # joins only here (vault BACKLOG #5341): webauthn 3.0.1's android-safetynet check calls + # ``.decode`` on the attestation statement's ``response`` and ``.get`` on the decoded + # JWS header and payload with no type check, so a non-bytes ``response``, or a header or + # payload decoding to ``null``, a list or a number, raised it straight out of this call. raise _refusal(exc, ceremony="registration") from exc _require_usable_public_key(verified.credential_public_key, ceremony="registration") return RegistrationResult( diff --git a/tests/test_webauthn.py b/tests/test_webauthn.py index 3f758db97c..66ea262317 100644 --- a/tests/test_webauthn.py +++ b/tests/test_webauthn.py @@ -1870,3 +1870,41 @@ def test_a_deeply_nested_assertion_response_is_the_audited_refusal( current_sign_count=0, ) _refused_on_recursion(excinfo, caplog) + + +def _safetynet_jws(header: bytes, payload: bytes) -> bytes: + return f"{bytes_to_base64url(header)}.{bytes_to_base64url(payload)}.AA".encode() + + +@pytest.mark.parametrize( + "att_stmt", + [ + pytest.param({"ver": "1", "response": "a.b.c"}, id="response-is-text"), + pytest.param({"ver": "1", "response": 7}, id="response-is-a-number"), + pytest.param({"ver": "1", "response": _safetynet_jws(b"null", b"{}")}, id="header-null"), + pytest.param({"ver": "1", "response": _safetynet_jws(b"[]", b"{}")}, id="header-list"), + pytest.param({"ver": "1", "response": _safetynet_jws(b"{}", b"null")}, id="payload-null"), + ], +) +def test_a_malformed_safetynet_statement_is_the_audited_refusal( + att_stmt: dict[str, object], caplog: pytest.LogCaptureFixture +) -> None: + """vault BACKLOG #5341. Red: webauthn 3.0.1's android-safetynet check raised AttributeError on + these attacker-shaped statements, and it escaped verify_registration as itself.""" + from webauthn.helpers import encode_cbor, parse_cbor + + challenge = b"c" * 32 + response = json.loads(SoftAuthenticator(rp_id=RP, origin=ORIGIN).create_response(challenge)) + attestation = parse_cbor(base64url_to_bytes(response["response"]["attestationObject"])) + attestation["fmt"] = "android-safetynet" + attestation["attStmt"] = att_stmt + response["response"]["attestationObject"] = bytes_to_base64url(encode_cbor(attestation)) + with ( + caplog.at_level(logging.WARNING, logger=_WEBAUTHN_LOG), + pytest.raises(wa.WebAuthnVerificationError) as excinfo, + ): + wa.verify_registration( + response_json=json.dumps(response), challenge=challenge, rp_id=RP, origin=ORIGIN + ) + assert isinstance(excinfo.value.__cause__, AttributeError), repr(excinfo.value.__cause__) + assert any("raw AttributeError" in r.getMessage() for r in caplog.records) From daad647ac2b9fbfa89f720d4ae01630b23a24e49 Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 04:02:43 -0600 Subject: [PATCH 13/23] Name the registration catch's exception tuple so strict mypy accepts it mypy refuses a starred tuple display in an except clause. Same set of types as the previous commit, held in a typed local before the try. BACKLOG #5341 --- messagefoundry/auth/webauthn.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/messagefoundry/auth/webauthn.py b/messagefoundry/auth/webauthn.py index 9ffee12adc..10d8127988 100644 --- a/messagefoundry/auth/webauthn.py +++ b/messagefoundry/auth/webauthn.py @@ -613,6 +613,7 @@ def verify_registration( # Resolved before the ``try`` so the AttributeError catch below covers the library call only, # where it is attacker input, and never our own code, where it would be a bug. supported_algs = _supported_pub_key_algs() + refused: tuple[type[Exception], ...] = (*_invalid_response_errors(), AttributeError) try: verified = verify_registration_response( credential=response_json, @@ -621,7 +622,7 @@ def verify_registration( expected_origin=origin, supported_pub_key_algs=supported_algs, ) - except (*_invalid_response_errors(), AttributeError) as exc: + except refused as exc: # The BASE class, deliberately (PR-A review HIGH): structurally-malformed browser input # raises siblings of InvalidRegistrationResponse (InvalidJSONStructure, InvalidCBORData, # ...) — every library-side rejection must land on the audited return-False/400 path, From dfebf99e102f3d510bff2d9f7fc41eb12b27c9b7 Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 04:05:54 -0600 Subject: [PATCH 14/23] Correct the sign-in name, NUL and refusal-path claims in comments and docs Comment, docstring and doc text only (vault BACKLOG #5341, #5342): - api/auth_models.py, DirectoryUserCreateRequest: the typed name also reaches the store when the directory entry has no sAMAccountName, since auth/ldap.py then falls back to the searched name. - api/validation.py, SIGN_IN_NAME_PATTERN: the one statement of which stores can hold a NUL name (PostgreSQL by its documentation, SQLite measured, SQL Server not measured); the ambiguous "That holds on PostgreSQL alone" is gone. LoginRequest and auth/usernames.py point there instead of a flat "no account can hold a NUL". - web console routes/core.py, _refused: at least the 429 skips the helper; an exception from request.body() or auth.login does too. - docs/API-INPUT-VALIDATION.md: a malformed field_path is a 400, not 422. Proposed PR title: Console passkey routes refuse a too-deep body or a malformed safetynet statement; the sign-in name rule covers the console form and /users/directory Proposed vault banners: - #5337: Fixed (unchanged by this branch; built on b613-c-authmisc). - #5341: Fixed. verify_registration/verify_assertion refuse a too-deep response as audited invalid input, and the console's /ui/reauth/webauthn and /ui/account/webauthn/verify answer 400. verify_registration also refuses a malformed android-safetynet statement (a non-bytes response, or a JWS header or payload decoding to null, a list or a number), which raised AttributeError out of webauthn 3.0.1, as audited invalid input. Left: POST /ui/csp-report (unauthenticated) still lets RecursionError out of its json.loads; file as a new item or a step. - #5342: Fixed for the name. A NUL username is refused at POST /auth/login, POST /users/directory and the console POST /ui/login form, combined passkey sign-in included. Left: the console form still has no 256-char bound; the store-level guard (get_user_by_username returning no row for a NUL) that would cover every caller is unbuilt; supersedes is not refused by design (it is only hashed). --- docs/API-INPUT-VALIDATION.md | 7 +++++-- messagefoundry/api/auth_models.py | 19 +++++++++++-------- messagefoundry/api/validation.py | 18 ++++++++++-------- messagefoundry/auth/usernames.py | 5 ++++- messagefoundry_webconsole/routes/core.py | 5 +++-- 5 files changed, 33 insertions(+), 21 deletions(-) diff --git a/docs/API-INPUT-VALIDATION.md b/docs/API-INPUT-VALIDATION.md index 42010ea4bf..143d6c0234 100644 --- a/docs/API-INPUT-VALIDATION.md +++ b/docs/API-INPUT-VALIDATION.md @@ -25,8 +25,11 @@ Two later sections quote rules held in other files: the items checked behind a r over two or more items. Each entry there names the code that holds its rule. No test compares those sections with the code, so read the named code when a number matters. -A value that breaks one of these rules gets an HTTP 422 with the field named. The engine refuses it -before the value reaches a database query, a filesystem path, a log line or a CSV export. +A value that breaks one of these rules gets an HTTP 422 with the field named, except a malformed HL7 +field path, which gets a 400: its rule is enforced outside the API models, as the note on the field +path below says. The web console's sign-in form is another exception, described under the table. +The engine refuses such a value +before it reaches a database query, a filesystem path, a log line or a CSV export. --- diff --git a/messagefoundry/api/auth_models.py b/messagefoundry/api/auth_models.py index 396e33e83d..dbc1d1c6c6 100644 --- a/messagefoundry/api/auth_models.py +++ b/messagefoundry/api/auth_models.py @@ -56,9 +56,10 @@ class CredentialReply(BaseModel): class LoginRequest(RequestModel): - # A NUL in ``username`` is refused here (vault BACKLOG #5342; ``SIGN_IN_NAME_PATTERN`` says - # why). Unrefused, it would reach the store's lookup, where PostgreSQL raises an error this - # route does not catch, so the route would answer 500 outside its failure-timing pad. + # A NUL in ``username`` is refused here (vault BACKLOG #5342). ``SIGN_IN_NAME_PATTERN`` says + # why, and which stores can hold such a name. Unrefused on PostgreSQL, the driver's error at + # the store's lookup is not one this route catches, so the route would answer 500 outside its + # failure-timing pad. username: str = Field(max_length=_NAME_MAX, pattern=SIGN_IN_NAME_PATTERN) password: str = Field(max_length=_PASSWORD_MAX) provider: str = Field(default="local", max_length=16) # 'local' | 'ad' @@ -325,11 +326,13 @@ class DirectoryUserCreateRequest(RequestModel): cannot point the holder's notices away from the directory's (ASVS 6.3.7). """ - # The sign-in name rule (vault BACKLOG #5342), on the typed name, which goes only to the - # directory lookup. The LDAP filter escapes a NUL (``auth.ldap._escape_filter``), so such a - # name is not sent raw; the rule refuses it with a 422 before any directory round trip, as - # POST /auth/login refuses it before the store. The name the store receives is the one the - # directory returns, and this rule does not check that one. + # The sign-in name rule (vault BACKLOG #5342), on the typed name. The typed name goes to the + # directory lookup, and also to the store when the entry found carries no ``sAMAccountName``: + # the lookup then names the account by the name it searched for, the typed name with its + # surrounding white space stripped (``fallback_username`` in ``auth.ldap``). The LDAP filter escapes a NUL (``auth.ldap._escape_filter``), so such a name + # is not sent raw; the rule refuses it with a 422 before any directory round trip, as + # POST /auth/login refuses it before the store. When the entry carries a ``sAMAccountName``, + # that is the name the store receives, and this rule does not check it. username: str = Field(min_length=1, max_length=_NAME_MAX, pattern=SIGN_IN_NAME_PATTERN) notify_email: str | None = Field(default=None, max_length=_NAME_MAX) diff --git a/messagefoundry/api/validation.py b/messagefoundry/api/validation.py index 12075156d4..5bb332efe1 100644 --- a/messagefoundry/api/validation.py +++ b/messagefoundry/api/validation.py @@ -204,14 +204,16 @@ PRINTABLE_TEXT_PATTERN = r"^[^\x00-\x1f\x7f-\x9f]+$" #: A sign-in name: any text but a NUL, empty included (vault BACKLOG #5342). A NUL would reach the -#: store's lookup, and PostgreSQL text cannot hold one, so its driver raises rather than finding no -#: row. Only the NUL: no account on that store can hold one, while other control characters can -#: reach a stored name through paths ``auth.usernames.username_refusal`` does not govern, such as -#: provision-admin, so refusing them here could lock a holder out. That holds on PostgreSQL alone, -#: and there only on its documentation, not run here. The SQLite store can hold a NUL name: -#: ``create_user`` stores one and ``get_user_by_username`` finds it. The SQL Server store is not -#: measured. The web console's form sign-in applies the same rule by hand (``"\x00" in``), since -#: it parses its form without pydantic. +#: store's lookup, and PostgreSQL text cannot hold one, so its driver would raise rather than find +#: no row. Only the NUL: other control characters can reach a stored name through paths +#: ``auth.usernames.username_refusal`` does not govern, such as provision-admin, so refusing them +#: here could lock a holder out. Refusing a NUL locks no one out only where no account can hold +#: one. PostgreSQL is such a store, read from its documentation and not run here. The SQLite store +#: is not: ``create_user`` stores a NUL name and ``get_user_by_username`` finds it, so on SQLite an +#: account created with one through a path that rule does not govern could not sign in. The SQL +#: Server store is not measured. This is the one statement of these facts; the other sites point +#: here. The web console's form sign-in applies the same rule by hand (``"\x00" in``), since it +#: parses its form without pydantic. SIGN_IN_NAME_PATTERN = r"^[^\x00]*$" #: A member of one of the engine's own closed vocabularies -- ``MessageStatus``, ``OutboxStatus``, diff --git a/messagefoundry/auth/usernames.py b/messagefoundry/auth/usernames.py index 853e585b1f..993adb6853 100644 --- a/messagefoundry/auth/usernames.py +++ b/messagefoundry/auth/usernames.py @@ -17,7 +17,10 @@ U+2800 among them), or when it takes one of the forms the audit trail writes for its own synthetic actors (:data:`RESERVED_ACTOR_FORMS`). An account named ``system`` would otherwise be indistinguishable from the engine in ``GET /audit?actor=system`` -and in an audit export, and a NUL would reach a store that cannot hold one. +and in an audit export, and a NUL name could not sign in with its password, since +``POST /auth/login`` and the web console's form sign-in both refuse a NUL. Which stores can hold +such a name at all is stated once, at +:data:`messagefoundry.api.validation.SIGN_IN_NAME_PATTERN`. The ``directory`` name is NOT reserved: nothing at HEAD writes it as an audit actor. diff --git a/messagefoundry_webconsole/routes/core.py b/messagefoundry_webconsole/routes/core.py index 87f3bc3880..fc0c2a71bf 100644 --- a/messagefoundry_webconsole/routes/core.py +++ b/messagefoundry_webconsole/routes/core.py @@ -769,8 +769,9 @@ async def ui_login(request: Request) -> Response: def _refused() -> Response: # The challenge is already popped, so each refusal this helper builds also ends its - # handle cookie. Not every refusal after the pop comes here: the login-budget 429 above - # raises with the cookie left set. + # handle cookie. Not every refusal after the pop comes here: at least the login-budget + # 429 above raises with the cookie left set, and so does an exception out of + # ``request.body()`` or ``auth.login``. refused = RedirectResponse("/ui/login?e=bad", status_code=303) if handle: clear_passkey_sign_in_cookie(refused, request) From 5e150df090df17c952207554ff8819f81ecffc6f Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 04:35:13 -0600 Subject: [PATCH 15/23] Review round 1: narrow two webauthn claims, one source for the NUL fact Code review of dfebf99e102 (vault BACKLOG #5341, #5342). Comments, doc and one test case; no behaviour change: - webauthn.py: the registration catch's comment no longer reads as a proof that no raw library error escapes; it names cryptography's UnsupportedAlgorithm as uncaught (reported by review, not reproduced). The AttributeError note says it would also catch a bad argument of ours inside the call, which _refusal logs. - auth_models.py: LoginRequest no longer restates the PostgreSQL fact that SIGN_IN_NAME_PATTERN holds; a 157-character comment line rewrapped. - API-INPUT-VALIDATION.md: the refusal sentence rewrapped and made exact. - test_webauthn.py: the header-is-a-number case the comment names. Proposed PR title: Console passkey routes refuse a too-deep body or a malformed safetynet statement; the sign-in name rule covers the console form and /users/directory Proposed vault banners: - #5337: Fixed (unchanged by this branch; built on b613-c-authmisc). - #5341: Fixed. verify_registration/verify_assertion refuse a too-deep response as audited invalid input, and the console's /ui/reauth/webauthn and /ui/account/webauthn/verify answer 400. verify_registration also refuses a malformed android-safetynet statement (a non-bytes response, or a JWS header or payload decoding to null, a list or a number), which raised AttributeError out of webauthn 3.0.1, as audited invalid input. Left: POST /ui/csp-report (unauthenticated) still lets RecursionError out of its json.loads; file as a new item or a step. - #5342: Fixed for the name. A NUL username is refused at POST /auth/login, POST /users/directory and the console POST /ui/login form, combined passkey sign-in included. Left: the console form still has no 256-char bound; the store-level guard (get_user_by_username returning no row for a NUL) that would cover every caller is unbuilt; supersedes is not refused by design (it is only hashed). --- docs/API-INPUT-VALIDATION.md | 4 ++-- messagefoundry/api/auth_models.py | 10 +++++----- messagefoundry/auth/webauthn.py | 12 +++++++++--- tests/test_webauthn.py | 6 ++++-- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/docs/API-INPUT-VALIDATION.md b/docs/API-INPUT-VALIDATION.md index 143d6c0234..807fdbb687 100644 --- a/docs/API-INPUT-VALIDATION.md +++ b/docs/API-INPUT-VALIDATION.md @@ -28,8 +28,8 @@ sections with the code, so read the named code when a number matters. A value that breaks one of these rules gets an HTTP 422 with the field named, except a malformed HL7 field path, which gets a 400: its rule is enforced outside the API models, as the note on the field path below says. The web console's sign-in form is another exception, described under the table. -The engine refuses such a value -before it reaches a database query, a filesystem path, a log line or a CSV export. +Whatever the status, the value that breaks a rule is refused before it reaches a database query, a +filesystem path, a log line or a CSV export. --- diff --git a/messagefoundry/api/auth_models.py b/messagefoundry/api/auth_models.py index dbc1d1c6c6..aea23fca3c 100644 --- a/messagefoundry/api/auth_models.py +++ b/messagefoundry/api/auth_models.py @@ -57,9 +57,9 @@ class CredentialReply(BaseModel): class LoginRequest(RequestModel): # A NUL in ``username`` is refused here (vault BACKLOG #5342). ``SIGN_IN_NAME_PATTERN`` says - # why, and which stores can hold such a name. Unrefused on PostgreSQL, the driver's error at - # the store's lookup is not one this route catches, so the route would answer 500 outside its - # failure-timing pad. + # why, and which stores can hold such a name. On a store whose driver raises on one, the + # error at the lookup is not one this route catches, so unrefused the route would answer 500 + # outside its failure-timing pad. username: str = Field(max_length=_NAME_MAX, pattern=SIGN_IN_NAME_PATTERN) password: str = Field(max_length=_PASSWORD_MAX) provider: str = Field(default="local", max_length=16) # 'local' | 'ad' @@ -329,8 +329,8 @@ class DirectoryUserCreateRequest(RequestModel): # The sign-in name rule (vault BACKLOG #5342), on the typed name. The typed name goes to the # directory lookup, and also to the store when the entry found carries no ``sAMAccountName``: # the lookup then names the account by the name it searched for, the typed name with its - # surrounding white space stripped (``fallback_username`` in ``auth.ldap``). The LDAP filter escapes a NUL (``auth.ldap._escape_filter``), so such a name - # is not sent raw; the rule refuses it with a 422 before any directory round trip, as + # surrounding white space stripped (``fallback_username`` in ``auth.ldap``). The LDAP filter + # escapes a NUL (``auth.ldap._escape_filter``), so such a name is not sent raw; the rule refuses it with a 422 before any directory round trip, as # POST /auth/login refuses it before the store. When the entry carries a ``sAMAccountName``, # that is the name the store receives, and this rule does not check it. username: str = Field(min_length=1, max_length=_NAME_MAX, pattern=SIGN_IN_NAME_PATTERN) diff --git a/messagefoundry/auth/webauthn.py b/messagefoundry/auth/webauthn.py index 10d8127988..9b70d9fdd0 100644 --- a/messagefoundry/auth/webauthn.py +++ b/messagefoundry/auth/webauthn.py @@ -610,8 +610,10 @@ def verify_registration( _require_webauthn() from webauthn import verify_registration_response - # Resolved before the ``try`` so the AttributeError catch below covers the library call only, - # where it is attacker input, and never our own code, where it would be a bug. + # Resolved before the ``try`` so the AttributeError catch below covers the library call alone + # and not our code before it. Inside the call it would also catch a bad argument of ours + # reaching the library (a non-str ``rp_id`` or ``origin``); the arguments are typed ``str``, + # and :func:`_refusal` logs the type at WARNING, so such a bug is not silent. supported_algs = _supported_pub_key_algs() refused: tuple[type[Exception], ...] = (*_invalid_response_errors(), AttributeError) try: @@ -626,7 +628,11 @@ def verify_registration( # The BASE class, deliberately (PR-A review HIGH): structurally-malformed browser input # raises siblings of InvalidRegistrationResponse (InvalidJSONStructure, InvalidCBORData, # ...) — every library-side rejection must land on the audited return-False/400 path, - # never an unhandled 500. The raw decode errors join it for the same reason (BACKLOG + # never an unhandled 500. That is the aim, not a proof: a raw type outside this tuple + # still escapes. cryptography's UnsupportedAlgorithm derives from Exception alone, so it is + # not caught here; code review reported it raised by an attestation certificate whose key + # algorithm cryptography does not know, which this branch did not reproduce. The raw + # decode errors join it for the same reason (BACKLOG # #1166): a malformed COSE key raised them straight out of this call. AttributeError # joins only here (vault BACKLOG #5341): webauthn 3.0.1's android-safetynet check calls # ``.decode`` on the attestation statement's ``response`` and ``.get`` on the decoded diff --git a/tests/test_webauthn.py b/tests/test_webauthn.py index 66ea262317..36889af7ae 100644 --- a/tests/test_webauthn.py +++ b/tests/test_webauthn.py @@ -1884,13 +1884,15 @@ def _safetynet_jws(header: bytes, payload: bytes) -> bytes: pytest.param({"ver": "1", "response": _safetynet_jws(b"null", b"{}")}, id="header-null"), pytest.param({"ver": "1", "response": _safetynet_jws(b"[]", b"{}")}, id="header-list"), pytest.param({"ver": "1", "response": _safetynet_jws(b"{}", b"null")}, id="payload-null"), + pytest.param({"ver": "1", "response": _safetynet_jws(b"1", b"{}")}, id="header-number"), ], ) def test_a_malformed_safetynet_statement_is_the_audited_refusal( att_stmt: dict[str, object], caplog: pytest.LogCaptureFixture ) -> None: - """vault BACKLOG #5341. Red: webauthn 3.0.1's android-safetynet check raised AttributeError on - these attacker-shaped statements, and it escaped verify_registration as itself.""" + """vault BACKLOG #5341, at this module's seam only: the refusal the service audits. Red: + webauthn 3.0.1's android-safetynet check raised AttributeError on these attacker-shaped + statements, and it escaped verify_registration as itself.""" from webauthn.helpers import encode_cbor, parse_cbor challenge = b"c" * 32 From 6c93192f9384e41b6d29cf0a606de29bf6e50b72 Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 05:55:32 -0600 Subject: [PATCH 16/23] The deep-nesting tests parse on a fixed 2 MiB thread stack The four RecursionError tests nested 100_000 levels on the main thread, whose stack is the platform's: Windows raised RecursionError, the Linux runner decoded it, and the precondition failed there ("DID NOT RAISE"). Each now runs its parse, and the precondition, on a thread with a 2 MiB stack at 400_000 levels (inside the 1 MiB body cap), so json's C decoder refuses it on any build. The web console test builds its engine and event loop on that thread, since the route parses on the loop's thread. Each still fails with its RecursionError catch reverted (measured: 4 failed with the three catches removed, 4 passed restored). vault BACKLOG #5341 --- .../tests/test_ui_passkey_body_refusals.py | 83 +++++++++++++++---- tests/test_webauthn.py | 61 +++++++++++--- 2 files changed, 116 insertions(+), 28 deletions(-) diff --git a/packaging/messagefoundry-webconsole/tests/test_ui_passkey_body_refusals.py b/packaging/messagefoundry-webconsole/tests/test_ui_passkey_body_refusals.py index 00302e11fe..4337c9fd62 100644 --- a/packaging/messagefoundry-webconsole/tests/test_ui_passkey_body_refusals.py +++ b/packaging/messagefoundry-webconsole/tests/test_ui_passkey_body_refusals.py @@ -11,20 +11,30 @@ from __future__ import annotations +import asyncio import json import logging +import threading +from collections.abc import Callable, Coroutine +from pathlib import Path from typing import Any +import httpx import pytest from _ui_clients import PW, SAME_ORIGIN, cookie_login, provision from _webui_support import _client, _service from messagefoundry.auth import Role from messagefoundry.auth.service import AuthService +from messagefoundry.config.settings import EgressSettings from messagefoundry.pipeline import Engine -#: Deep enough that json refuses it on any stack these tests run on, and inside the 1 MiB body cap. -_DEPTH = 100_000 +#: How deep the too-deep body nests: inside the 1 MiB body cap, and far past what json's C decoder +#: can reach on :data:`_SMALL_STACK`, since each level costs it at least two C frames. +_DEPTH = 400_000 +#: The stack the request runs on. A fixed thread stack, because a main thread's stack is the +#: platform's: 100_000 levels raised RecursionError on Windows and decoded on the Linux runner. +_SMALL_STACK = 2 * 1024 * 1024 _TOO_DEEP = "[" * _DEPTH + "]" * _DEPTH @@ -34,27 +44,68 @@ } +def _on_a_small_stack[T](call: Callable[[], Coroutine[Any, Any, T]]) -> T: + """Run ``call`` in its own event loop on a thread whose stack is :data:`_SMALL_STACK`, + re-raising what it raised. The route parses the body on the loop's thread, so the loop and the + engine it serves are built there too.""" + result: list[T] = [] + raised: list[BaseException] = [] + + def _run() -> None: + try: + result.append(asyncio.run(call())) + except BaseException as exc: # handed back to the test thread, which re-raises it + raised.append(exc) + + previous = threading.stack_size(_SMALL_STACK) + try: + worker = threading.Thread(target=_run) + worker.start() + finally: + threading.stack_size(previous) + worker.join() + if raised: + raise raised[0] + return result[0] + + @pytest.mark.parametrize("route", sorted(_DEEP_BODIES)) -async def test_a_body_nested_too_deep_is_a_malformed_request_not_a_500( - engine: Engine, route: str +def test_a_body_nested_too_deep_is_a_malformed_request_not_a_500( + tmp_path: Path, route: str ) -> None: """vault BACKLOG #5341. Red: the route's own ``request.json()`` raised ``RecursionError``, which its catch did not list, so it escaped the route (a 500 on a served engine).""" body = _DEEP_BODIES[route] - # The precondition that makes this test about RecursionError: on this stack json refuses the - # body that way. Were the stack ever deep enough to decode it, this fails rather than passing on - # some other path. + + async def _parse() -> object: + return json.loads(body) + + # The precondition that makes this test about RecursionError: on the stack the route runs on, + # json refuses the body that way. Were that stack ever deep enough to decode it, this fails + # rather than passing on some other path. with pytest.raises(RecursionError): - json.loads(body) - service = await _service(engine) - await provision(service, "op", [Role.OPERATOR.value]) - async with _client(engine, service) as c: - await cookie_login(c, "op") - r = await c.post( - route, - content=body, - headers={**SAME_ORIGIN, "Content-Type": "application/json"}, + _on_a_small_stack(_parse) + + async def _post() -> httpx.Response: + engine = await Engine.create( + tmp_path / "webui.db", + poll_interval=0.02, + egress_settings=EgressSettings(deny_by_default=False), ) + try: + service = await _service(engine) + await provision(service, "op", [Role.OPERATOR.value]) + async with _client(engine, service) as c: + await cookie_login(c, "op") + return await c.post( + route, + content=body, + headers={**SAME_ORIGIN, "Content-Type": "application/json"}, + ) + finally: + await engine.stop() + + r = _on_a_small_stack(_post) assert (r.status_code, r.json()) == (400, {"ok": False, "error": "malformed request"}) diff --git a/tests/test_webauthn.py b/tests/test_webauthn.py index 36889af7ae..df4cf7beb5 100644 --- a/tests/test_webauthn.py +++ b/tests/test_webauthn.py @@ -13,7 +13,9 @@ import dataclasses import json import logging +import threading import time +from collections.abc import Callable from typing import Any import pytest @@ -1821,17 +1823,48 @@ async def test_a_passkey_registered_from_a_first_seen_address_records_nothing() await store.close() +#: How deep the too-deep response nests. Each level costs json's C decoder at least two C frames, so +#: this many levels need far more than :data:`_SMALL_STACK` bytes on any build these tests run on. +_DEPTH = 400_000 +#: The stack the deep parse runs on. A fixed thread stack, because a main thread's stack is the +#: platform's: 100_000 levels raised RecursionError on Windows and decoded on the Linux runner. +_SMALL_STACK = 2 * 1024 * 1024 + #: A ceremony response nested past json's depth limit, under a field a real response carries. -_TOO_DEEP = '{"id": ' + "[" * 100_000 + "]" * 100_000 + "}" +_TOO_DEEP = '{"id": ' + "[" * _DEPTH + "]" * _DEPTH + "}" + + +def _on_a_small_stack[T](call: Callable[[], T]) -> T: + """Run ``call`` on a thread whose stack is :data:`_SMALL_STACK`, re-raising what it raised.""" + result: list[T] = [] + raised: list[BaseException] = [] + + def _run() -> None: + try: + result.append(call()) + except BaseException as exc: # handed back to the test thread, which re-raises it + raised.append(exc) + + previous = threading.stack_size(_SMALL_STACK) + try: + worker = threading.Thread(target=_run) + worker.start() + finally: + threading.stack_size(previous) + worker.join() + if raised: + raise raised[0] + return result[0] def _refused_on_recursion( excinfo: pytest.ExceptionInfo[wa.WebAuthnVerificationError], caplog: pytest.LogCaptureFixture ) -> None: """The refusal came from the RecursionError catch, not from some other library refusal: the - precondition holds on this stack, the cause is the RecursionError, and its type was logged.""" + precondition holds on the same stack, the cause is the RecursionError, and its type was + logged.""" with pytest.raises(RecursionError): - json.loads(_TOO_DEEP) + _on_a_small_stack(lambda: json.loads(_TOO_DEEP)) assert isinstance(excinfo.value.__cause__, RecursionError), repr(excinfo.value.__cause__) assert any("raw RecursionError" in r.getMessage() for r in caplog.records) @@ -1846,8 +1879,10 @@ def test_a_deeply_nested_registration_response_is_the_audited_refusal( caplog.at_level(logging.WARNING, logger=_WEBAUTHN_LOG), pytest.raises(wa.WebAuthnVerificationError) as excinfo, ): - wa.verify_registration( - response_json=_TOO_DEEP, challenge=b"c" * 32, rp_id=RP, origin=ORIGIN + _on_a_small_stack( + lambda: wa.verify_registration( + response_json=_TOO_DEEP, challenge=b"c" * 32, rp_id=RP, origin=ORIGIN + ) ) _refused_on_recursion(excinfo, caplog) @@ -1861,13 +1896,15 @@ def test_a_deeply_nested_assertion_response_is_the_audited_refusal( caplog.at_level(logging.WARNING, logger=_WEBAUTHN_LOG), pytest.raises(wa.WebAuthnVerificationError) as excinfo, ): - wa.verify_assertion( - response_json=_TOO_DEEP, - challenge=b"c" * 32, - rp_id=RP, - origin=ORIGIN, - public_key=soft.cose_public_key(), - current_sign_count=0, + _on_a_small_stack( + lambda: wa.verify_assertion( + response_json=_TOO_DEEP, + challenge=b"c" * 32, + rp_id=RP, + origin=ORIGIN, + public_key=soft.cose_public_key(), + current_sign_count=0, + ) ) _refused_on_recursion(excinfo, caplog) From 560fa02fa9822c708d030c3ed88905e49771d7c8 Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 06:07:47 -0600 Subject: [PATCH 17/23] The too-deep re-auth body expects the service's audited refusal After the merge with main, /ui/reauth/webauthn hands a body nested too deep for json to the service, which answers "passkey verification failed" (main's audited refusal), not the route's "malformed request". The test now holds each route's own refusal; the enrolment route still answers "malformed request", since its label cannot be read. vault BACKLOG #5341 --- .../tests/test_ui_passkey_body_refusals.py | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/packaging/messagefoundry-webconsole/tests/test_ui_passkey_body_refusals.py b/packaging/messagefoundry-webconsole/tests/test_ui_passkey_body_refusals.py index 4337c9fd62..a924c1211e 100644 --- a/packaging/messagefoundry-webconsole/tests/test_ui_passkey_body_refusals.py +++ b/packaging/messagefoundry-webconsole/tests/test_ui_passkey_body_refusals.py @@ -38,9 +38,18 @@ _TOO_DEEP = "[" * _DEPTH + "]" * _DEPTH +#: Each route's too-deep body, and the refusal it answers. The re-auth route hands the body to the +#: service, whose audited refusal answers; the enrolment route cannot read the label, so it answers +#: its own malformed-body refusal. _DEEP_BODIES = { - "/ui/reauth/webauthn": '{"response": ' + _TOO_DEEP + "}", - "/ui/account/webauthn/verify": '{"label": "k", "response": ' + _TOO_DEEP + "}", + "/ui/reauth/webauthn": ( + '{"response": ' + _TOO_DEEP + "}", + {"ok": False, "error": "passkey verification failed"}, + ), + "/ui/account/webauthn/verify": ( + '{"label": "k", "response": ' + _TOO_DEEP + "}", + {"ok": False, "error": "malformed request"}, + ), } @@ -70,12 +79,10 @@ def _run() -> None: @pytest.mark.parametrize("route", sorted(_DEEP_BODIES)) -def test_a_body_nested_too_deep_is_a_malformed_request_not_a_500( - tmp_path: Path, route: str -) -> None: +def test_a_body_nested_too_deep_is_a_refusal_not_a_500(tmp_path: Path, route: str) -> None: """vault BACKLOG #5341. Red: the route's own ``request.json()`` raised ``RecursionError``, which its catch did not list, so it escaped the route (a 500 on a served engine).""" - body = _DEEP_BODIES[route] + body, refusal = _DEEP_BODIES[route] async def _parse() -> object: return json.loads(body) @@ -106,7 +113,7 @@ async def _post() -> httpx.Response: await engine.stop() r = _on_a_small_stack(_post) - assert (r.status_code, r.json()) == (400, {"ok": False, "error": "malformed request"}) + assert (r.status_code, r.json()) == (400, refusal) def _spy_login(service: AuthService, monkeypatch: pytest.MonkeyPatch) -> list[str]: From f2bc6890b3a24cb8b5f45a54eb92e7428cb4ac58 Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 06:54:25 -0600 Subject: [PATCH 18/23] The form sign-in leaves a NUL name to the service's audited refusal Main (PR 3020) made AuthService refuse a NUL sign-in name as an unknown name before the account lookup: dummy verify, limiter charge stands, and an auth.login_failed row with the NUL escaped. This branch's form check refused the same name earlier, with only a WARNING log line, so the console surface lost its audit row and main's test_a_username_with_a_nul_is_an_unknown_name_on_both_sign_in_surfaces failed. The form check goes; the name is still refused and still never reaches the store's lookup, now audited. The console tests pin that. The comments and docs that said a NUL would reach the store's lookup, or that the form refuses it by hand, are corrected to the service refusal (validation.py, auth_models.py, docs/API-INPUT-VALIDATION.md, the test docstrings). The JSON route's 422 is unchanged. vault BACKLOG #5342 --- docs/API-INPUT-VALIDATION.md | 9 +-- messagefoundry/api/auth_models.py | 5 +- messagefoundry/api/validation.py | 13 +++-- messagefoundry_webconsole/routes/core.py | 16 +----- .../tests/test_ui_passkey_body_refusals.py | 57 ++++++++++++------- tests/test_api_auth.py | 5 +- 6 files changed, 57 insertions(+), 48 deletions(-) diff --git a/docs/API-INPUT-VALIDATION.md b/docs/API-INPUT-VALIDATION.md index 807fdbb687..7f4a47d8ca 100644 --- a/docs/API-INPUT-VALIDATION.md +++ b/docs/API-INPUT-VALIDATION.md @@ -27,7 +27,7 @@ sections with the code, so read the named code when a number matters. A value that breaks one of these rules gets an HTTP 422 with the field named, except a malformed HL7 field path, which gets a 400: its rule is enforced outside the API models, as the note on the field -path below says. The web console's sign-in form is another exception, described under the table. +path below says. Whatever the status, the value that breaks a rule is refused before it reaches a database query, a filesystem path, a log line or a CSV export. @@ -61,9 +61,10 @@ filesystem path, a log line or a CSV export. The email-address row covers `recipient_override` only. The account email fields follow a different rule, set out under [Items checked a second time](#items-checked-a-second-time-behind-the-route). -The web console's `POST /ui/login` form applies the sign-in name's NUL rule too, without its length -bound. It is not a JSON route, so it refuses with its usual redirect to the sign-in page rather -than a 422. +The web console's `POST /ui/login` form has no body model, so these rules do not run on it. A +sign-in name holding a NUL reaches the sign-in service there, which refuses it as an unknown name +before the account lookup and audits the attempt, and the form answers with its usual redirect to +the sign-in page. "Printable text, no control characters" means every character except the C0 range, DEL, and the C1 range. In practice: no NUL, no tab, no carriage return, no line feed. diff --git a/messagefoundry/api/auth_models.py b/messagefoundry/api/auth_models.py index aea23fca3c..7f6ae45096 100644 --- a/messagefoundry/api/auth_models.py +++ b/messagefoundry/api/auth_models.py @@ -57,9 +57,8 @@ class CredentialReply(BaseModel): class LoginRequest(RequestModel): # A NUL in ``username`` is refused here (vault BACKLOG #5342). ``SIGN_IN_NAME_PATTERN`` says - # why, and which stores can hold such a name. On a store whose driver raises on one, the - # error at the lookup is not one this route catches, so unrefused the route would answer 500 - # outside its failure-timing pad. + # why, and which stores can hold such a name. Unrefused here, the sign-in service would still + # refuse it as an unknown name before the account lookup. username: str = Field(max_length=_NAME_MAX, pattern=SIGN_IN_NAME_PATTERN) password: str = Field(max_length=_PASSWORD_MAX) provider: str = Field(default="local", max_length=16) # 'local' | 'ad' diff --git a/messagefoundry/api/validation.py b/messagefoundry/api/validation.py index 5bb332efe1..96eb6fade4 100644 --- a/messagefoundry/api/validation.py +++ b/messagefoundry/api/validation.py @@ -203,17 +203,18 @@ #: Printable text: no C0 control, no DEL, no C1 control. One or more characters. PRINTABLE_TEXT_PATTERN = r"^[^\x00-\x1f\x7f-\x9f]+$" -#: A sign-in name: any text but a NUL, empty included (vault BACKLOG #5342). A NUL would reach the -#: store's lookup, and PostgreSQL text cannot hold one, so its driver would raise rather than find -#: no row. Only the NUL: other control characters can reach a stored name through paths +#: A sign-in name: any text but a NUL, empty included (vault BACKLOG #5342). PostgreSQL text +#: cannot hold one, so a NUL at the store's lookup would make its driver raise rather than find no +#: row; the sign-in service refuses such a name as an unknown one before that lookup, and this rule +#: refuses it earlier, at the JSON body. Only the NUL: other control characters can reach a stored +#: name through paths #: ``auth.usernames.username_refusal`` does not govern, such as provision-admin, so refusing them #: here could lock a holder out. Refusing a NUL locks no one out only where no account can hold #: one. PostgreSQL is such a store, read from its documentation and not run here. The SQLite store #: is not: ``create_user`` stores a NUL name and ``get_user_by_username`` finds it, so on SQLite an #: account created with one through a path that rule does not govern could not sign in. The SQL -#: Server store is not measured. This is the one statement of these facts; the other sites point -#: here. The web console's form sign-in applies the same rule by hand (``"\x00" in``), since it -#: parses its form without pydantic. +#: Server store is not measured. The web console's form sign-in has no body model, so it has no +#: such rule; the service's refusal covers it. SIGN_IN_NAME_PATTERN = r"^[^\x00]*$" #: A member of one of the engine's own closed vocabularies -- ``MessageStatus``, ``OutboxStatus``, diff --git a/messagefoundry_webconsole/routes/core.py b/messagefoundry_webconsole/routes/core.py index 3fa0fdf218..75ca8ad9a2 100644 --- a/messagefoundry_webconsole/routes/core.py +++ b/messagefoundry_webconsole/routes/core.py @@ -764,8 +764,8 @@ async def ui_login(request: Request) -> Response: # selector, and "ad" stays in the allow-list only so the ENGINE (_dispatch_login) is the # single point that refuses and audits it. Directory accounts sign in by Windows SSO or # OIDC, and the AD bind as the user survives only as the step-up re-bind at /ui/reauth - # (and the JSON /me/reauth). A name holding a NUL is the exception: it is refused below, - # whatever the provider, before the engine sees it. + # (and the JSON /me/reauth). A name holding a NUL reaches the service too, which refuses it + # as an unknown name before the account lookup, audited (BACKLOG #5342). def _refused() -> Response: # The challenge is already popped, so each refusal this helper builds also ends its @@ -780,16 +780,6 @@ def _refused() -> Response: provider_value = form.get("provider", "local") if provider_value not in ("local", "ad"): return _refused() - username = form.get("username", "") - if "\x00" in username: - # The JSON route's sign-in name rule, by hand since this form is not a pydantic model - # (vault BACKLOG #5342; api/validation.py ``SIGN_IN_NAME_PATTERN`` says why). It covers - # the combined passkey sign-in too, which rides this form's username. The service never - # sees the name, so it writes no audit row; this line is the record. Never the name. - _log.warning( - "sign-in refused before the service: username held a NUL, client=%s", client - ) - return _refused() # ADR 0197 Amendment A, wave 2: the combined passkey sign-in. The assertion rides the form, # the handle of its challenge the cookie the begin leg set; both or neither. Clamped, so an # oversized assertion is simply an invalid one. @@ -801,7 +791,7 @@ def _refused() -> Response: else None ) outcome = await auth.login( - username, + form.get("username", ""), form.get("password", ""), provider=AuthProvider.AD if provider_value == "ad" else AuthProvider.LOCAL, client=client, diff --git a/packaging/messagefoundry-webconsole/tests/test_ui_passkey_body_refusals.py b/packaging/messagefoundry-webconsole/tests/test_ui_passkey_body_refusals.py index a924c1211e..2e9c99676d 100644 --- a/packaging/messagefoundry-webconsole/tests/test_ui_passkey_body_refusals.py +++ b/packaging/messagefoundry-webconsole/tests/test_ui_passkey_body_refusals.py @@ -5,15 +5,15 @@ ``POST /ui/reauth/webauthn`` and ``POST /ui/account/webauthn/verify`` parse their JSON body themselves, before the engine's ceremony code sees it, so a body nested too deep for the stack raised ``RecursionError`` out of the route. ``POST /ui/login`` parses its form without pydantic, -so the JSON route's sign-in name rule (no NUL) did not cover it, nor the combined passkey sign-in -that rides the same form. All data here is synthetic. +so the JSON route's sign-in name rule (no NUL) does not cover it, nor the combined passkey sign-in +that rides the same form: the service's own refusal, before the account lookup, does. All data here +is synthetic. """ from __future__ import annotations import asyncio import json -import logging import threading from collections.abc import Callable, Coroutine from pathlib import Path @@ -128,35 +128,54 @@ async def _spy(username: str, *args: Any, **kwargs: Any) -> Any: return reached +def _spy_lookup(engine: Engine, monkeypatch: pytest.MonkeyPatch) -> list[str]: + looked_up: list[str] = [] + real_lookup = engine.store.get_user_by_username + + async def _spy(username: str) -> Any: + looked_up.append(username) + return await real_lookup(username) + + monkeypatch.setattr(engine.store, "get_user_by_username", _spy) + return looked_up + + +async def _failed_actors(engine: Engine) -> list[str]: + return [ + str(row["actor"]) + for row in await engine.store.list_audit() + if row["action"] == "auth.login_failed" + ] + + @pytest.mark.parametrize("username", ["op\x00", "\x00", "o\x00p"]) -async def test_the_form_sign_in_refuses_a_nul_username_before_the_service( - engine: Engine, monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, username: str +async def test_the_form_sign_in_refuses_a_nul_username_before_the_store( + engine: Engine, monkeypatch: pytest.MonkeyPatch, username: str ) -> None: - """vault BACKLOG #5342, the form route. Red: the NUL reached ``AuthService.login``, where a - PostgreSQL store's lookup would raise an error the route does not catch. The service writes no - audit row for it, so the route logs the refusal, without the name.""" + """vault BACKLOG #5342, the form route. Red: the NUL reached the store's account lookup, where + at least a PostgreSQL store refuses a NUL in a text bind. The service refuses it as an unknown + name before the lookup, with the audit row naming it escaped.""" service = await _service(engine) reached = _spy_login(service, monkeypatch) + looked_up = _spy_lookup(engine, monkeypatch) async with _client(engine, service) as c: - with caplog.at_level(logging.WARNING, logger="messagefoundry_webconsole.routes.core"): - r = await c.post( - "/ui/login", data={"username": username, "password": PW}, headers=SAME_ORIGIN - ) + r = await c.post( + "/ui/login", data={"username": username, "password": PW}, headers=SAME_ORIGIN + ) assert (r.status_code, r.headers["location"]) == (303, "/ui/login?e=bad") - assert reached == [] - [record] = [rec for rec in caplog.records if "held a NUL" in rec.getMessage()] - # The client is the line's only argument, so no form of the name reaches the general log. - assert record.args == ("127.0.0.1",) + assert reached == [username] + assert all("\x00" not in name for name in looked_up), looked_up + assert await _failed_actors(engine) == [username.replace("\x00", "\\u0000")] async def test_the_combined_passkey_sign_in_refuses_a_nul_username_and_ends_its_challenge( engine: Engine, monkeypatch: pytest.MonkeyPatch ) -> None: """vault BACKLOG #5342, the combined passkey sign-in, which rides the same form. Red: as above. - The refusal also clears the challenge cookie, as a refused sign-in after the service does.""" + The refusal also clears the challenge cookie, as any refused sign-in after the pop does.""" pytest.importorskip("webauthn") service = await _service(engine) - reached = _spy_login(service, monkeypatch) + looked_up = _spy_lookup(engine, monkeypatch) async with _client(engine, service) as c: begun = await c.post("/ui/login/passkey", headers=SAME_ORIGIN) assert begun.status_code == 200, begun.text @@ -166,7 +185,7 @@ async def test_the_combined_passkey_sign_in_refuses_a_nul_username_and_ends_its_ headers=SAME_ORIGIN, ) assert (r.status_code, r.headers["location"]) == (303, "/ui/login?e=bad") - assert reached == [] + assert all("\x00" not in name for name in looked_up), looked_up cleared = [h for h in r.headers.get_list("set-cookie") if "passkey" in h.lower()] assert cleared and all("max-age=0" in h.lower() for h in cleared), cleared diff --git a/tests/test_api_auth.py b/tests/test_api_auth.py index c42249277a..b327baa6ca 100644 --- a/tests/test_api_auth.py +++ b/tests/test_api_auth.py @@ -3681,9 +3681,8 @@ async def _live(c: httpx.AsyncClient, token: str) -> bool: async def test_login_refuses_a_nul_username_before_the_store( engine: Engine, username: str, monkeypatch: pytest.MonkeyPatch ) -> None: - """Red: each reached ``AuthService.login``, where on a PostgreSQL store the NUL would raise an - error the route does not catch, a 500 outside the failure-timing pad. Now each is a 422 before - the service runs.""" + """Red: each reached ``AuthService.login``, which refuses a NUL name as an unknown one before + the account lookup. Now each is a 422 at the body, before the service runs.""" service = await _service(engine, AuthSettings(require_mfa=False)) async with _client(engine, service) as c: reached: list[str] = [] From 8ab5daf15f9cf8c14df568f1e5cd787cf365e752 Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 07:41:24 -0600 Subject: [PATCH 19/23] A malformed attestation certificate is the audited passkey refusal, not a 500 verify_registration now catches three raw cryptography exceptions an attacker-supplied attestation leaf raised straight out of verify_registration_response on webauthn 3.0.1 and cryptography 50.0.2: UnsupportedAlgorithm (packed leaf naming an unknown key algorithm), InvalidVersion (packed or tpm leaf with an undefined version) and DuplicateExtension (tpm leaf carrying one extension twice). Each would have been a 500 with no auth.webauthn_failed row on first deployment; each now lands on the same refusal path as the AttributeError catch. Also removes the false comment that the UnsupportedAlgorithm escape did not reproduce; the comments now state what was measured. The crypto inventory records the new cryptography import (exception classes only). Tests: four patched-leaf cases, red with the new catch removed (4 failed), and a positive control that the intact leaf is parsed past each failure point and refused on purpose. BACKLOG #5341 --- messagefoundry/auth/webauthn.py | 45 ++++-- scripts/security/crypto_inventory_check.py | 5 +- tests/test_webauthn.py | 160 +++++++++++++++++++++ 3 files changed, 199 insertions(+), 11 deletions(-) diff --git a/messagefoundry/auth/webauthn.py b/messagefoundry/auth/webauthn.py index 9b70d9fdd0..7a4405b3ba 100644 --- a/messagefoundry/auth/webauthn.py +++ b/messagefoundry/auth/webauthn.py @@ -217,6 +217,28 @@ def _invalid_response_errors() -> tuple[type[Exception], ...]: return (*_invalid_input_errors(), RecursionError) +def _attestation_certificate_errors() -> tuple[type[Exception], ...]: + """Raw ``cryptography`` exceptions an attacker-supplied attestation certificate raised straight + out of ``verify_registration_response`` (BACKLOG #5341). Each derives from ``Exception`` alone, + so :func:`_invalid_input_errors` does not hold it. Reproduced on webauthn 3.0.1 and + cryptography 50.0.2. :func:`verify_registration` passes the library no root certificates, so + its chain check passes and the certificate is parsed as sent: + + * ``UnsupportedAlgorithm``: a ``packed`` statement whose ``x5c`` leaf names a key algorithm + cryptography does not know. The library calls ``public_key()`` on it. + * ``InvalidVersion``: a ``packed`` or ``tpm`` leaf whose version field is not one X.509 + defines. ``load_der_x509_certificate`` raises it. + * ``DuplicateExtension``: a ``tpm`` leaf carrying one extension twice, once its signature over + ``certInfo`` verifies. The library reads ``extensions`` on it. + + Registration only: the assertion ceremony parses no certificate. A function, so this module + keeps its third-party imports inside the functions that use them.""" + from cryptography.exceptions import UnsupportedAlgorithm + from cryptography.x509 import DuplicateExtension, InvalidVersion + + return (UnsupportedAlgorithm, InvalidVersion, DuplicateExtension) + + def _refusal(exc: Exception, *, ceremony: _Ceremony) -> WebAuthnVerificationError: """Turn a caught library exception into the audited refusal, and log the raw ones. @@ -615,7 +637,11 @@ def verify_registration( # reaching the library (a non-str ``rp_id`` or ``origin``); the arguments are typed ``str``, # and :func:`_refusal` logs the type at WARNING, so such a bug is not silent. supported_algs = _supported_pub_key_algs() - refused: tuple[type[Exception], ...] = (*_invalid_response_errors(), AttributeError) + refused: tuple[type[Exception], ...] = ( + *_invalid_response_errors(), + AttributeError, + *_attestation_certificate_errors(), + ) try: verified = verify_registration_response( credential=response_json, @@ -629,15 +655,14 @@ def verify_registration( # raises siblings of InvalidRegistrationResponse (InvalidJSONStructure, InvalidCBORData, # ...) — every library-side rejection must land on the audited return-False/400 path, # never an unhandled 500. That is the aim, not a proof: a raw type outside this tuple - # still escapes. cryptography's UnsupportedAlgorithm derives from Exception alone, so it is - # not caught here; code review reported it raised by an attestation certificate whose key - # algorithm cryptography does not know, which this branch did not reproduce. The raw - # decode errors join it for the same reason (BACKLOG - # #1166): a malformed COSE key raised them straight out of this call. AttributeError - # joins only here (vault BACKLOG #5341): webauthn 3.0.1's android-safetynet check calls - # ``.decode`` on the attestation statement's ``response`` and ``.get`` on the decoded - # JWS header and payload with no type check, so a non-bytes ``response``, or a header or - # payload decoding to ``null``, a list or a number, raised it straight out of this call. + # still escapes. The raw decode errors join it for the same reason (BACKLOG #1166): a + # malformed COSE key raised them straight out of this call. More join only here, at + # registration (vault BACKLOG #5341). The attestation-certificate errors, and what raised + # each, are in :func:`_attestation_certificate_errors`. AttributeError: webauthn 3.0.1's + # android-safetynet check calls ``.decode`` on the attestation statement's ``response`` + # and ``.get`` on the decoded JWS header and payload with no type check, so a non-bytes + # ``response``, or a header or payload decoding to ``null``, a list or a number, raised it + # straight out of this call. raise _refusal(exc, ceremony="registration") from exc _require_usable_public_key(verified.credential_public_key, ceremony="registration") return RegistrationResult( diff --git a/scripts/security/crypto_inventory_check.py b/scripts/security/crypto_inventory_check.py index 092ae36505..7e88894f95 100644 --- a/scripts/security/crypto_inventory_check.py +++ b/scripts/security/crypto_inventory_check.py @@ -369,7 +369,10 @@ # [webauthn] extra, lazy-imported inside this module's functions) performs the COSE signature # verification via cryptography — a CRYPTO_LIBRARY_MODULES trigger, so the delegated verification # is a first-class inventory token here, not merely §4 prose. - "messagefoundry/auth/webauthn.py": frozenset({"secrets", "webauthn"}), + # vault BACKLOG #5341: cryptography for three exception classes alone (UnsupportedAlgorithm, + # x509.InvalidVersion, x509.DuplicateExtension), caught around verify_registration_response so a + # malformed attestation certificate is the audited refusal. It performs no crypto operation. + "messagefoundry/auth/webauthn.py": frozenset({"cryptography", "secrets", "webauthn"}), # ADR 0041 (D1): SHA-256 content fingerprint of a loaded config bundle, recorded in the # config_reload audit to bind reviewed-commit -> loaded-bytes (integrity/attribution, not a secret). "messagefoundry/config/fingerprint.py": frozenset({"hashlib"}), diff --git a/tests/test_webauthn.py b/tests/test_webauthn.py index df4cf7beb5..0b1232dc50 100644 --- a/tests/test_webauthn.py +++ b/tests/test_webauthn.py @@ -1947,3 +1947,163 @@ def test_a_malformed_safetynet_statement_is_the_audited_refusal( ) assert isinstance(excinfo.value.__cause__, AttributeError), repr(excinfo.value.__cause__) assert any("raw AttributeError" in r.getMessage() for r in caplog.records) + + +#: id-ecPublicKey (1.2.840.10045.2.1), and the same arc ending in 9, which names no algorithm. +_EC_PUBLIC_KEY_OID, _UNKNOWN_KEY_OID = "06072a8648ce3d0201", "06072a8648ce3d0209" +#: The explicit version field reading v3, and the same field reading 5, which X.509 does not define. +_VERSION_3, _VERSION_5 = "a003020102", "a003020105" +#: The id-ce-keyUsage OID and the id-ce-basicConstraints OID (id-ce arcs 15 and 19), one length. +_KEY_USAGE_OID, _BASIC_CONSTRAINTS_OID = "0603551d0f", "0603551d13" + + +def _attestation_cert() -> tuple[bytes, Any]: + """A self-signed P-256 leaf in DER, and its private key. Its subject is empty and it carries + BasicConstraints then KeyUsage, so the library's tpm check reads it as far as its extensions.""" + import datetime + + from cryptography import x509 + from cryptography.hazmat.primitives import hashes + from cryptography.hazmat.primitives.asymmetric import ec + from cryptography.hazmat.primitives.serialization import Encoding + from cryptography.x509.oid import NameOID + + key = ec.generate_private_key(ec.SECP256R1()) + start = datetime.datetime(2026, 1, 1, tzinfo=datetime.UTC) + cert = ( + x509.CertificateBuilder() + .subject_name(x509.Name([])) + .issuer_name(x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "t")])) + .public_key(key.public_key()) + .serial_number(1) + .not_valid_before(start) + .not_valid_after(start + datetime.timedelta(days=1)) + .add_extension(x509.BasicConstraints(ca=False, path_length=None), critical=True) + .add_extension( + x509.KeyUsage(True, False, False, False, False, False, False, False, False), + critical=False, + ) + .sign(key, hashes.SHA256()) + ) + return cert.public_bytes(Encoding.DER), key + + +def _patched(der: bytes, old: str, new: str) -> bytes: + """``der`` with its one occurrence of the hex bytes ``old`` replaced by ``new``.""" + assert der.count(bytes.fromhex(old)) == 1 + return der.replace(bytes.fromhex(old), bytes.fromhex(new)) + + +def _attested_registration(fmt: str, patch: tuple[str, str] | None) -> tuple[str, bytes]: + """A registration response re-labelled ``fmt``, its statement carrying a leaf certificate + patched by ``patch`` (``None`` leaves it intact), and its challenge. + + The ``tpm`` statement is built to pass every check the library runs before it parses the + leaf: ``pubArea`` names the credential's own P-256 point, ``certInfo`` carries the hashes the + library recomputes, and the leaf's key signs ``certInfo``. The ``packed`` statement carries a + signature that does not verify, which the library finds only after it parses the leaf.""" + import hashlib + + from cryptography.hazmat.primitives import hashes + from cryptography.hazmat.primitives.asymmetric import ec + from webauthn.helpers import decode_credential_public_key, encode_cbor, parse_cbor + from webauthn.helpers.decode_credential_public_key import DecodedEC2PublicKey + + challenge = b"c" * 32 + soft = SoftAuthenticator(rp_id=RP, origin=ORIGIN) + response = json.loads(soft.create_response(challenge)) + attestation = parse_cbor(base64url_to_bytes(response["response"]["attestationObject"])) + der, key = _attestation_cert() + leaf = der if patch is None else _patched(der, *patch) + if fmt == "packed": + statement: dict[str, Any] = {"alg": -7, "sig": b"\x30\x00", "x5c": [leaf]} + else: + point = decode_credential_public_key(soft.cose_public_key()) + assert isinstance(point, DecodedEC2PublicKey) + # TPM_ALG_ECC, nameAlg SHA-256, no attributes or policy, NULL scheme on NIST P-256. + pub_area = bytes.fromhex("0023000b00000000000000100010000300100020") + point.x + pub_area += b"\x00\x20" + point.y + client_data = base64url_to_bytes(response["response"]["clientDataJSON"]) + extra = hashlib.sha256( + attestation["authData"] + hashlib.sha256(client_data).digest() + ).digest() + name = b"\x00\x0b" + hashlib.sha256(pub_area).digest() + cert_info = ( + bytes.fromhex("ff544347801700000020") # magic, ATTEST_CERTIFY, no signer + + extra + + bytes(17 + 8) # clock info and firmware version + + len(name).to_bytes(2, "big") + + name + + b"\x00\x00" # no qualified name + ) + statement = { + "ver": "2.0", + "alg": -7, + "x5c": [leaf], + "sig": key.sign(cert_info, ec.ECDSA(hashes.SHA256())), + "certInfo": cert_info, + "pubArea": pub_area, + } + attestation["fmt"] = fmt + attestation["attStmt"] = statement + response["response"]["attestationObject"] = bytes_to_base64url(encode_cbor(attestation)) + return json.dumps(response), challenge + + +@pytest.mark.parametrize( + ("fmt", "patch", "raised"), + [ + pytest.param( + "packed", + (_EC_PUBLIC_KEY_OID, _UNKNOWN_KEY_OID), + "UnsupportedAlgorithm", + id="packed-unknown-key-algorithm", + ), + pytest.param("packed", (_VERSION_3, _VERSION_5), "InvalidVersion", id="packed-version"), + pytest.param("tpm", (_VERSION_3, _VERSION_5), "InvalidVersion", id="tpm-version"), + pytest.param( + "tpm", + (_KEY_USAGE_OID, _BASIC_CONSTRAINTS_OID), + "DuplicateExtension", + id="tpm-duplicate-extension", + ), + ], +) +def test_a_malformed_attestation_certificate_is_the_audited_refusal( + fmt: str, patch: tuple[str, str], raised: str, caplog: pytest.LogCaptureFixture +) -> None: + """vault BACKLOG #5341, at this module's seam only: the refusal the service audits. Red: + cryptography raised each of these on an attacker-supplied leaf, and it escaped + verify_registration as itself, which the route would have served as a 500.""" + response_json, challenge = _attested_registration(fmt, patch) + with ( + caplog.at_level(logging.WARNING, logger=_WEBAUTHN_LOG), + pytest.raises(wa.WebAuthnVerificationError) as excinfo, + ): + wa.verify_registration( + response_json=response_json, challenge=challenge, rp_id=RP, origin=ORIGIN + ) + assert type(excinfo.value.__cause__).__name__ == raised, repr(excinfo.value.__cause__) + assert any(f"raw {raised}" in r.getMessage() for r in caplog.records) + + +@pytest.mark.parametrize( + ("fmt", "refusal"), + [ + pytest.param("packed", "Could not verify attestation statement signature", id="packed"), + pytest.param("tpm", "subjectAltName", id="tpm"), + ], +) +def test_an_intact_attestation_certificate_is_parsed_past_where_the_patched_ones_fail( + fmt: str, refusal: str +) -> None: + """Positive control for the test above: with the leaf intact, the library parses it and + refuses later, on purpose. So each patched leaf, not the statement around it, raised there.""" + response_json, challenge = _attested_registration(fmt, None) + with pytest.raises(wa.WebAuthnVerificationError, match=refusal) as excinfo: + wa.verify_registration( + response_json=response_json, challenge=challenge, rp_id=RP, origin=ORIGIN + ) + from webauthn.helpers.exceptions import InvalidRegistrationResponse + + assert isinstance(excinfo.value.__cause__, InvalidRegistrationResponse) From 46a4190d81b9c7c275bb73178490768bffd35d0a Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 07:57:52 -0600 Subject: [PATCH 20/23] The JSON sign-in leaves a NUL name to the service's audited, charged refusal The NUL rule on LoginRequest.username made a NUL sign-in on POST /auth/login a 422 that was neither charged to the sign-in limiter nor audited. The sign-in service already refuses a NUL name as an unknown one before the account lookup, and the route charges the limiter first. The rule is removed from LoginRequest only; DirectoryUserCreateRequest keeps it. The JSON route now takes the same path the web console form takes. Test: a JSON login with a NUL name answers as for an unknown name, is charged, writes one auth.login_failed row, and never reaches the store's account lookup; the positive control is an unknown name the same lookup spy records. Red with the rule restored (3 failed). Comments and docs/API-INPUT-VALIDATION.md now say the rule is on POST /users/directory alone; two test docstrings that said the JSON body may refuse a NUL are corrected. BACKLOG #5342 Proposed PR title (unchanged scope): batch 613 auth hardening, passkey and sign-in refusals (BACKLOG #5341, #5342). Proposed #5341 banner: a too-deep ceremony body, a malformed android-safetynet statement, and a malformed attestation certificate (unknown key algorithm, undefined version, duplicate extension) are each the audited passkey refusal, not a 500. Proposed #5342 banner: a NUL sign-in name is refused as an unknown name, charged and audited, on both sign-in surfaces; POST /users/directory refuses it with a 422. --- docs/API-INPUT-VALIDATION.md | 16 ++++--- messagefoundry/api/auth_models.py | 14 +++--- messagefoundry/api/validation.py | 15 ++++--- .../tests/test_ui_passkey_body_refusals.py | 6 +-- tests/test_api_auth.py | 45 ++++++++++++++----- tests/test_auth_hardening.py | 2 +- 6 files changed, 61 insertions(+), 37 deletions(-) diff --git a/docs/API-INPUT-VALIDATION.md b/docs/API-INPUT-VALIDATION.md index 7f4a47d8ca..4203ebb516 100644 --- a/docs/API-INPUT-VALIDATION.md +++ b/docs/API-INPUT-VALIDATION.md @@ -12,9 +12,8 @@ page still match the code, and fails if the page drops one. It does not check ev page. At least four rules in the table are written, in whole or in part, elsewhere. The connection-name pattern is defined in [`messagefoundry/connection_names.py`](../messagefoundry/connection_names.py) and imported by that -module, because the config loader enforces the same rule. The sign-in name's NUL rule is in -`validation.py`, but its length bounds, 256 characters and the minimum of 1 on -`POST /users/directory`, are set on the request models in +module, because the config loader enforces the same rule. The directory user name's NUL rule is +in `validation.py`, but its length bounds, 1 to 256 characters, are set on the request model in [`messagefoundry/api/auth_models.py`](../messagefoundry/api/auth_models.py). The HL7 field-path grammar is `parse_path` in [`messagefoundry/parsing/peek.py`](../messagefoundry/parsing/peek.py). The layered preset ids' @@ -55,15 +54,18 @@ filesystem path, a log line or a CSV export. | Audit action filter | Printable text, no control characters, 1 to 128 characters | `action` on `GET /audit` and `GET /audit/export` | | Display label | Printable text, no control characters, 1 to 128 characters | `name` on `POST /search/presets` | | Filesystem path | Printable text, no control characters, 1 to 4096 characters | `config_dir` on `POST /config/reload`, and `archive` on `POST /dr/activate` | -| Sign-in name | Any text except NUL, up to 256 characters (1 to 256 on `POST /users/directory`) | `username` on `POST /auth/login` and `POST /users/directory` | +| Directory user name | Any text except NUL, 1 to 256 characters | `username` on `POST /users/directory` | | Layered preset ids | Resource ids joined by single commas with no spaces, up to 1024 characters in all | `presets` on `GET /search/layered` | The email-address row covers `recipient_override` only. The account email fields follow a different rule, set out under [Items checked a second time](#items-checked-a-second-time-behind-the-route). -The web console's `POST /ui/login` form has no body model, so these rules do not run on it. A -sign-in name holding a NUL reaches the sign-in service there, which refuses it as an unknown name -before the account lookup and audits the attempt, and the form answers with its usual redirect to +Neither sign-in surface refuses a NUL in the name at the body: not `username` on +`POST /auth/login`, and not the web console's `POST /ui/login` form, which has no body model. The +NUL rule is left off the sign-in body on purpose. A 422 there would be neither charged to the +sign-in limiter nor audited. On both surfaces the route charges the limiter, and the sign-in +service refuses such a name as an unknown one before the account lookup and audits the attempt. +`POST /auth/login` answers with its usual failed sign-in, and the form with its usual redirect to the sign-in page. "Printable text, no control characters" means every character except the C0 range, DEL, and the C1 diff --git a/messagefoundry/api/auth_models.py b/messagefoundry/api/auth_models.py index 7f6ae45096..df8f31b287 100644 --- a/messagefoundry/api/auth_models.py +++ b/messagefoundry/api/auth_models.py @@ -56,10 +56,10 @@ class CredentialReply(BaseModel): class LoginRequest(RequestModel): - # A NUL in ``username`` is refused here (vault BACKLOG #5342). ``SIGN_IN_NAME_PATTERN`` says - # why, and which stores can hold such a name. Unrefused here, the sign-in service would still - # refuse it as an unknown name before the account lookup. - username: str = Field(max_length=_NAME_MAX, pattern=SIGN_IN_NAME_PATTERN) + # No NUL rule here (vault BACKLOG #5342), on purpose: the route charges the sign-in limiter + # and the sign-in service refuses a NUL name as an unknown one before the account lookup, and + # audits it. A 422 at this body would be neither charged nor audited. + username: str = Field(max_length=_NAME_MAX) password: str = Field(max_length=_PASSWORD_MAX) provider: str = Field(default="local", max_length=16) # 'local' | 'ad' #: The raw session token this sign-in REPLACES in the caller, if any (ASVS 7.2.4, BACKLOG @@ -329,9 +329,9 @@ class DirectoryUserCreateRequest(RequestModel): # directory lookup, and also to the store when the entry found carries no ``sAMAccountName``: # the lookup then names the account by the name it searched for, the typed name with its # surrounding white space stripped (``fallback_username`` in ``auth.ldap``). The LDAP filter - # escapes a NUL (``auth.ldap._escape_filter``), so such a name is not sent raw; the rule refuses it with a 422 before any directory round trip, as - # POST /auth/login refuses it before the store. When the entry carries a ``sAMAccountName``, - # that is the name the store receives, and this rule does not check it. + # escapes a NUL (``auth.ldap._escape_filter``), so such a name is not sent raw; the rule + # refuses it with a 422 before any directory round trip. When the entry carries a + # ``sAMAccountName``, that is the name the store receives, and this rule does not check it. username: str = Field(min_length=1, max_length=_NAME_MAX, pattern=SIGN_IN_NAME_PATTERN) notify_email: str | None = Field(default=None, max_length=_NAME_MAX) diff --git a/messagefoundry/api/validation.py b/messagefoundry/api/validation.py index 96eb6fade4..5756cb9a41 100644 --- a/messagefoundry/api/validation.py +++ b/messagefoundry/api/validation.py @@ -205,16 +205,17 @@ #: A sign-in name: any text but a NUL, empty included (vault BACKLOG #5342). PostgreSQL text #: cannot hold one, so a NUL at the store's lookup would make its driver raise rather than find no -#: row; the sign-in service refuses such a name as an unknown one before that lookup, and this rule -#: refuses it earlier, at the JSON body. Only the NUL: other control characters can reach a stored -#: name through paths -#: ``auth.usernames.username_refusal`` does not govern, such as provision-admin, so refusing them -#: here could lock a holder out. Refusing a NUL locks no one out only where no account can hold +#: row; the sign-in service refuses such a name as an unknown one before that lookup. This rule +#: is on ``POST /users/directory`` alone. Neither sign-in surface carries it: the JSON body and the +#: web console's form both leave a NUL name to that refusal, which each route charges to the +#: sign-in limiter and the service audits. Only the NUL: other control characters can reach a +#: stored name through paths ``auth.usernames.username_refusal`` does not govern, such as +#: provision-admin, so refusing them here could lock a holder out. Refusing a NUL locks no one +#: out only where no account can hold #: one. PostgreSQL is such a store, read from its documentation and not run here. The SQLite store #: is not: ``create_user`` stores a NUL name and ``get_user_by_username`` finds it, so on SQLite an #: account created with one through a path that rule does not govern could not sign in. The SQL -#: Server store is not measured. The web console's form sign-in has no body model, so it has no -#: such rule; the service's refusal covers it. +#: Server store is not measured. SIGN_IN_NAME_PATTERN = r"^[^\x00]*$" #: A member of one of the engine's own closed vocabularies -- ``MessageStatus``, ``OutboxStatus``, diff --git a/packaging/messagefoundry-webconsole/tests/test_ui_passkey_body_refusals.py b/packaging/messagefoundry-webconsole/tests/test_ui_passkey_body_refusals.py index 2e9c99676d..d6321a3850 100644 --- a/packaging/messagefoundry-webconsole/tests/test_ui_passkey_body_refusals.py +++ b/packaging/messagefoundry-webconsole/tests/test_ui_passkey_body_refusals.py @@ -5,9 +5,9 @@ ``POST /ui/reauth/webauthn`` and ``POST /ui/account/webauthn/verify`` parse their JSON body themselves, before the engine's ceremony code sees it, so a body nested too deep for the stack raised ``RecursionError`` out of the route. ``POST /ui/login`` parses its form without pydantic, -so the JSON route's sign-in name rule (no NUL) does not cover it, nor the combined passkey sign-in -that rides the same form: the service's own refusal, before the account lookup, does. All data here -is synthetic. +and neither it nor the combined passkey sign-in that rides the same form refuses a NUL name +itself: the service's own refusal, before the account lookup, covers both, as it covers the JSON +``POST /auth/login``. All data here is synthetic. """ from __future__ import annotations diff --git a/tests/test_api_auth.py b/tests/test_api_auth.py index b327baa6ca..7281b30180 100644 --- a/tests/test_api_auth.py +++ b/tests/test_api_auth.py @@ -3674,27 +3674,48 @@ async def _live(c: httpx.AsyncClient, token: str) -> bool: assert too_long.status_code == 422 -# --- vault BACKLOG #5342: a sign-in naming a NUL is refused at the body -------------------------- +# --- vault BACKLOG #5342: a sign-in naming a NUL takes the service's audited refusal -------------- @pytest.mark.parametrize("username", ["sup\x00", "\x00", "s\x00up"]) -async def test_login_refuses_a_nul_username_before_the_store( +async def test_login_answers_a_nul_username_as_an_audited_unknown_name( engine: Engine, username: str, monkeypatch: pytest.MonkeyPatch ) -> None: - """Red: each reached ``AuthService.login``, which refuses a NUL name as an unknown one before - the account lookup. Now each is a 422 at the body, before the service runs.""" + """Red: a NUL rule on the body answered 422, which was neither charged to the sign-in limiter + nor audited. Now the JSON sign-in answers as for an unknown name, is charged, writes an + ``auth.login_failed`` row, and never reaches the store's account lookup. The positive control + is the unknown name sent first: the same lookup spy records it.""" service = await _service(engine, AuthSettings(require_mfa=False)) - async with _client(engine, service) as c: - reached: list[str] = [] + await _add(service, "sup", Role.VIEWER) + looked_up: list[str] = [] + real_lookup = engine.store.get_user_by_username + + async def _lookup(name: str) -> Any: + looked_up.append(name) + return await real_lookup(name) - async def _spy(*args: object, **kwargs: object) -> object: - reached.append("login") - raise AssertionError("the service ran") + monkeypatch.setattr(engine.store, "get_user_by_username", _lookup) + charged: list[str | None] = [] + real_allow = service.allow_login_attempt - monkeypatch.setattr(service, "login", _spy) + def _allow(client: str | None) -> bool: + charged.append(client) + return real_allow(client) + + monkeypatch.setattr(service, "allow_login_attempt", _allow) + async with _client(engine, service) as c: + unknown = await c.post("/auth/login", json={"username": "ghost", "password": PW}) + assert "ghost" in looked_up, looked_up # the spy sees a lookup that happens + looked_up.clear() r = await c.post("/auth/login", json={"username": username, "password": PW}) - assert r.status_code == 422 - assert reached == [] + assert looked_up == [], looked_up + assert unknown.status_code == 401 + assert (r.status_code, r.json()) == (unknown.status_code, unknown.json()) + assert len(charged) == 2, "the NUL sign-in was not charged to the limiter" + actors = [ + dict(row)["actor"] for row in await engine.store.list_audit(action="auth.login_failed") + ] + assert actors.count(username.replace("\x00", "\\u0000")) == 1, actors @pytest.mark.parametrize( diff --git a/tests/test_auth_hardening.py b/tests/test_auth_hardening.py index a381fb2429..2d378037a6 100644 --- a/tests/test_auth_hardening.py +++ b/tests/test_auth_hardening.py @@ -624,7 +624,7 @@ async def test_a_username_with_a_nul_is_an_unknown_name_on_both_sign_in_surfaces service refuses it as an unknown name before the account lookup: at the seam itself, and through the console form, which charges the limiter first. The answer is an unknown name's, and the ``auth.login_failed`` row names it with the NUL escaped. The JSON route is driven at - the seam, not over HTTP, because its body model may refuse a NUL before the service.""" + the seam here; ``tests/test_api_auth.py`` drives it over HTTP.""" service = await _service(engine) await _add(service, "bob") looked_up: list[str] = [] From 593b49facfc49d7e562b28eb3e235fe0505bc4f6 Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 08:31:13 -0600 Subject: [PATCH 21/23] Both WebAuthn verify calls refuse on any exception from the library Replace the per-type catches around verify_registration_response and verify_authentication_response with a catch of Exception around the library call alone, so an exception type nobody listed (the latest being cryptography's UnsupportedGeneralNameType, from a tpm leaf whose subjectAltName holds an x400Address or EDIPartyName) still lands on the audited refusal path. _refusal logs the raw type at WARNING, never the message. _invalid_response_errors and _attestation_certificate_errors are removed; _invalid_input_errors stays for the key-decoding calls in _require_usable_public_key. The cryptography import they needed is gone, so the crypto inventory entry for auth/webauthn.py is back to main's. New test cases: a tpm leaf whose SAN holds an x400Address or an EDIPartyName, with a positive control for the intact SAN-bearing leaf. BACKLOG #5341 --- messagefoundry/auth/webauthn.py | 94 +++------------------- scripts/security/crypto_inventory_check.py | 5 +- tests/test_webauthn.py | 53 +++++++++--- 3 files changed, 54 insertions(+), 98 deletions(-) diff --git a/messagefoundry/auth/webauthn.py b/messagefoundry/auth/webauthn.py index 7a4405b3ba..3b394530c3 100644 --- a/messagefoundry/auth/webauthn.py +++ b/messagefoundry/auth/webauthn.py @@ -167,11 +167,9 @@ def _supported_pub_key_algs() -> list[COSEAlgorithmIdentifier]: def _invalid_input_errors() -> tuple[type[Exception], ...]: - """Exceptions py_webauthn raises for bad input, which must reach the audited path. - - Not every one it raises: at least :func:`_invalid_response_errors` adds ``RecursionError`` - around both ceremonies' verify calls, and :func:`verify_registration` adds ``AttributeError`` - around its own (vault BACKLOG #5341). + """Exceptions py_webauthn raises for bad input, caught around its key-decoding calls in + :func:`_require_usable_public_key`. Both ceremonies' verify calls catch every ``Exception`` + instead (vault BACKLOG #5341). ``WebAuthnException`` is the library's own base class. The next three entries catch four raw exception types (``LookupError`` covers both ``KeyError`` and ``IndexError``) that it lets @@ -191,54 +189,6 @@ def _invalid_input_errors() -> tuple[type[Exception], ...]: return (WebAuthnException, LookupError, TypeError, ValueError) -def _invalid_response_errors() -> tuple[type[Exception], ...]: - """:func:`_invalid_input_errors` plus ``RecursionError``, caught around the whole library verify - call of each ceremony (vault BACKLOG #5341). Read and run on the installed webauthn 3.0.1: - - * Its credential-JSON parsers (``parse_registration_credential_json``, - ``parse_authentication_credential_json``) catch only ``JSONDecodeError`` around their - ``json.loads``, so a credential handed to the library as text and nested too deep for the - stack raises ``RecursionError`` unwrapped. A caller that hands it an already-parsed dict - does not reach that parse. - * At least the ``android-safetynet`` attestation check also ``json.loads`` the JWS header and - payload with no guard, and ``verify_registration_response`` calls it outside any ``try``. - * The library's ``clientDataJSON`` parse is not a source: both verify functions call it inside - ``except Exception`` and re-raise ``InvalidRegistrationResponse`` or - ``InvalidAuthenticationResponse``, which :func:`_invalid_input_errors` already holds. - - A ``RecursionError`` message names stack use, not the input. Because a source sits inside the - call as well as at its entry, the catch stays around the whole call, not one parse. - - The stored key is not such a source: the library's CBOR decodes, the stored key's included, go - through its ``parse_cbor``, which wraps every ``Exception`` as ``InvalidCBORData`` (read on - webauthn 3.0.1). A caller that parses the body itself is not reached by this catch, so it - must catch its own parse's ``RecursionError``, as at least the web console's passkey routes - do.""" - return (*_invalid_input_errors(), RecursionError) - - -def _attestation_certificate_errors() -> tuple[type[Exception], ...]: - """Raw ``cryptography`` exceptions an attacker-supplied attestation certificate raised straight - out of ``verify_registration_response`` (BACKLOG #5341). Each derives from ``Exception`` alone, - so :func:`_invalid_input_errors` does not hold it. Reproduced on webauthn 3.0.1 and - cryptography 50.0.2. :func:`verify_registration` passes the library no root certificates, so - its chain check passes and the certificate is parsed as sent: - - * ``UnsupportedAlgorithm``: a ``packed`` statement whose ``x5c`` leaf names a key algorithm - cryptography does not know. The library calls ``public_key()`` on it. - * ``InvalidVersion``: a ``packed`` or ``tpm`` leaf whose version field is not one X.509 - defines. ``load_der_x509_certificate`` raises it. - * ``DuplicateExtension``: a ``tpm`` leaf carrying one extension twice, once its signature over - ``certInfo`` verifies. The library reads ``extensions`` on it. - - Registration only: the assertion ceremony parses no certificate. A function, so this module - keeps its third-party imports inside the functions that use them.""" - from cryptography.exceptions import UnsupportedAlgorithm - from cryptography.x509 import DuplicateExtension, InvalidVersion - - return (UnsupportedAlgorithm, InvalidVersion, DuplicateExtension) - - def _refusal(exc: Exception, *, ceremony: _Ceremony) -> WebAuthnVerificationError: """Turn a caught library exception into the audited refusal, and log the raw ones. @@ -632,16 +582,9 @@ def verify_registration( _require_webauthn() from webauthn import verify_registration_response - # Resolved before the ``try`` so the AttributeError catch below covers the library call alone - # and not our code before it. Inside the call it would also catch a bad argument of ours - # reaching the library (a non-str ``rp_id`` or ``origin``); the arguments are typed ``str``, - # and :func:`_refusal` logs the type at WARNING, so such a bug is not silent. + # Resolved before the ``try`` so the catch below covers the library call alone and not our + # code before it. supported_algs = _supported_pub_key_algs() - refused: tuple[type[Exception], ...] = ( - *_invalid_response_errors(), - AttributeError, - *_attestation_certificate_errors(), - ) try: verified = verify_registration_response( credential=response_json, @@ -650,19 +593,9 @@ def verify_registration( expected_origin=origin, supported_pub_key_algs=supported_algs, ) - except refused as exc: - # The BASE class, deliberately (PR-A review HIGH): structurally-malformed browser input - # raises siblings of InvalidRegistrationResponse (InvalidJSONStructure, InvalidCBORData, - # ...) — every library-side rejection must land on the audited return-False/400 path, - # never an unhandled 500. That is the aim, not a proof: a raw type outside this tuple - # still escapes. The raw decode errors join it for the same reason (BACKLOG #1166): a - # malformed COSE key raised them straight out of this call. More join only here, at - # registration (vault BACKLOG #5341). The attestation-certificate errors, and what raised - # each, are in :func:`_attestation_certificate_errors`. AttributeError: webauthn 3.0.1's - # android-safetynet check calls ``.decode`` on the attestation statement's ``response`` - # and ``.get`` on the decoded JWS header and payload with no type check, so a non-bytes - # ``response``, or a header or payload decoding to ``null``, a list or a number, raised it - # straight out of this call. + except Exception as exc: + # Every exception from the library call is the audited refusal (vault BACKLOG #5341), and + # :func:`_refusal` logs the type of any raw one at WARNING. raise _refusal(exc, ceremony="registration") from exc _require_usable_public_key(verified.credential_public_key, ceremony="registration") return RegistrationResult( @@ -755,12 +688,11 @@ def verify_assertion( credential_public_key=public_key, credential_current_sign_count=current_sign_count, ) - except _invalid_response_errors() as exc: - # The BASE class, deliberately (PR-A review HIGH): malformed input raises siblings of - # InvalidAuthenticationResponse — every rejection lands audited, never a 500. The - # sign-count regression message ("...sign count...") still rides through for the - # service's clone-signal classification. The raw types stay caught although the stored - # key was screened above: the response itself is attacker input too. + except Exception as exc: + # Every exception from the library call is the audited refusal (vault BACKLOG #5341), and + # :func:`_refusal` logs the type of any raw one at WARNING. The sign-count regression + # message ("...sign count...") still rides through for the service's clone-signal + # classification. raise _refusal(exc, ceremony="assertion") from exc return AssertionResult(verified.new_sign_count, bool(verified.user_verified)) diff --git a/scripts/security/crypto_inventory_check.py b/scripts/security/crypto_inventory_check.py index 7e88894f95..092ae36505 100644 --- a/scripts/security/crypto_inventory_check.py +++ b/scripts/security/crypto_inventory_check.py @@ -369,10 +369,7 @@ # [webauthn] extra, lazy-imported inside this module's functions) performs the COSE signature # verification via cryptography — a CRYPTO_LIBRARY_MODULES trigger, so the delegated verification # is a first-class inventory token here, not merely §4 prose. - # vault BACKLOG #5341: cryptography for three exception classes alone (UnsupportedAlgorithm, - # x509.InvalidVersion, x509.DuplicateExtension), caught around verify_registration_response so a - # malformed attestation certificate is the audited refusal. It performs no crypto operation. - "messagefoundry/auth/webauthn.py": frozenset({"cryptography", "secrets", "webauthn"}), + "messagefoundry/auth/webauthn.py": frozenset({"secrets", "webauthn"}), # ADR 0041 (D1): SHA-256 content fingerprint of a loaded config bundle, recorded in the # config_reload audit to bind reviewed-commit -> loaded-bytes (integrity/attribution, not a secret). "messagefoundry/config/fingerprint.py": frozenset({"hashlib"}), diff --git a/tests/test_webauthn.py b/tests/test_webauthn.py index 0b1232dc50..52eab4a6e8 100644 --- a/tests/test_webauthn.py +++ b/tests/test_webauthn.py @@ -1955,11 +1955,15 @@ def test_a_malformed_safetynet_statement_is_the_audited_refusal( _VERSION_3, _VERSION_5 = "a003020102", "a003020105" #: The id-ce-keyUsage OID and the id-ce-basicConstraints OID (id-ce arcs 15 and 19), one length. _KEY_USAGE_OID, _BASIC_CONSTRAINTS_OID = "0603551d0f", "0603551d13" +#: A subjectAltName GeneralName: a directoryName holding an empty Name, and the same bytes tagged +#: x400Address and ediPartyName, two forms cryptography does not support. +_DIRECTORY_NAME, _X400_ADDRESS, _EDI_PARTY_NAME = "a4023000", "a3023000", "a5023000" -def _attestation_cert() -> tuple[bytes, Any]: +def _attestation_cert(*, san: bool = False) -> tuple[bytes, Any]: """A self-signed P-256 leaf in DER, and its private key. Its subject is empty and it carries - BasicConstraints then KeyUsage, so the library's tpm check reads it as far as its extensions.""" + BasicConstraints then KeyUsage, so the library's tpm check reads it as far as its extensions. + With ``san`` it also carries a subjectAltName holding one empty directoryName.""" import datetime from cryptography import x509 @@ -1970,7 +1974,7 @@ def _attestation_cert() -> tuple[bytes, Any]: key = ec.generate_private_key(ec.SECP256R1()) start = datetime.datetime(2026, 1, 1, tzinfo=datetime.UTC) - cert = ( + builder = ( x509.CertificateBuilder() .subject_name(x509.Name([])) .issuer_name(x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "t")])) @@ -1983,8 +1987,12 @@ def _attestation_cert() -> tuple[bytes, Any]: x509.KeyUsage(True, False, False, False, False, False, False, False, False), critical=False, ) - .sign(key, hashes.SHA256()) ) + if san: + builder = builder.add_extension( + x509.SubjectAlternativeName([x509.DirectoryName(x509.Name([]))]), critical=True + ) + cert = builder.sign(key, hashes.SHA256()) return cert.public_bytes(Encoding.DER), key @@ -1994,9 +2002,12 @@ def _patched(der: bytes, old: str, new: str) -> bytes: return der.replace(bytes.fromhex(old), bytes.fromhex(new)) -def _attested_registration(fmt: str, patch: tuple[str, str] | None) -> tuple[str, bytes]: +def _attested_registration( + fmt: str, patch: tuple[str, str] | None, *, san: bool = False +) -> tuple[str, bytes]: """A registration response re-labelled ``fmt``, its statement carrying a leaf certificate - patched by ``patch`` (``None`` leaves it intact), and its challenge. + patched by ``patch`` (``None`` leaves it intact), and its challenge. ``san`` is passed to + :func:`_attestation_cert`. The ``tpm`` statement is built to pass every check the library runs before it parses the leaf: ``pubArea`` names the credential's own P-256 point, ``certInfo`` carries the hashes the @@ -2013,7 +2024,7 @@ def _attested_registration(fmt: str, patch: tuple[str, str] | None) -> tuple[str soft = SoftAuthenticator(rp_id=RP, origin=ORIGIN) response = json.loads(soft.create_response(challenge)) attestation = parse_cbor(base64url_to_bytes(response["response"]["attestationObject"])) - der, key = _attestation_cert() + der, key = _attestation_cert(san=san) leaf = der if patch is None else _patched(der, *patch) if fmt == "packed": statement: dict[str, Any] = {"alg": -7, "sig": b"\x30\x00", "x5c": [leaf]} @@ -2067,6 +2078,18 @@ def _attested_registration(fmt: str, patch: tuple[str, str] | None) -> tuple[str "DuplicateExtension", id="tpm-duplicate-extension", ), + pytest.param( + "tpm", + (_DIRECTORY_NAME, _X400_ADDRESS), + "UnsupportedGeneralNameType", + id="tpm-san-x400-address", + ), + pytest.param( + "tpm", + (_DIRECTORY_NAME, _EDI_PARTY_NAME), + "UnsupportedGeneralNameType", + id="tpm-san-edi-party-name", + ), ], ) def test_a_malformed_attestation_certificate_is_the_audited_refusal( @@ -2075,7 +2098,8 @@ def test_a_malformed_attestation_certificate_is_the_audited_refusal( """vault BACKLOG #5341, at this module's seam only: the refusal the service audits. Red: cryptography raised each of these on an attacker-supplied leaf, and it escaped verify_registration as itself, which the route would have served as a 500.""" - response_json, challenge = _attested_registration(fmt, patch) + san = patch[0] == _DIRECTORY_NAME + response_json, challenge = _attested_registration(fmt, patch, san=san) with ( caplog.at_level(logging.WARNING, logger=_WEBAUTHN_LOG), pytest.raises(wa.WebAuthnVerificationError) as excinfo, @@ -2088,18 +2112,21 @@ def test_a_malformed_attestation_certificate_is_the_audited_refusal( @pytest.mark.parametrize( - ("fmt", "refusal"), + ("fmt", "san", "refusal"), [ - pytest.param("packed", "Could not verify attestation statement signature", id="packed"), - pytest.param("tpm", "subjectAltName", id="tpm"), + pytest.param( + "packed", False, "Could not verify attestation statement signature", id="packed" + ), + pytest.param("tpm", False, "subjectAltName", id="tpm"), + pytest.param("tpm", True, "Subject Alt Name was invalid", id="tpm-san"), ], ) def test_an_intact_attestation_certificate_is_parsed_past_where_the_patched_ones_fail( - fmt: str, refusal: str + fmt: str, san: bool, refusal: str ) -> None: """Positive control for the test above: with the leaf intact, the library parses it and refuses later, on purpose. So each patched leaf, not the statement around it, raised there.""" - response_json, challenge = _attested_registration(fmt, None) + response_json, challenge = _attested_registration(fmt, None, san=san) with pytest.raises(wa.WebAuthnVerificationError, match=refusal) as excinfo: wa.verify_registration( response_json=response_json, challenge=challenge, rp_id=RP, origin=ORIGIN From a22238d690c6a9cd123ab2058bb7386b140d2cbd Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 08:44:44 -0600 Subject: [PATCH 22/23] Sign-in NUL text names where the combined passkey sign-in is charged docs/API-INPUT-VALIDATION.md and the SIGN_IN_NAME_PATTERN comment said the route charges the limiter on both surfaces. For the combined passkey form sign-in, POST /ui/login skips its own charge when the staged challenge is paid, because the charge was taken at /ui/login/passkey. Both now say so. A test helper docstring that named a RecursionError catch now names the RecursionError alone. BACKLOG #5342 Proposed PR title: WebAuthn verify refuses any library exception; sign-in NUL name and anchor-path fixes (BACKLOG #5341, #5342) Proposed vault banners: - #5341: Both WebAuthn verify calls catch Exception around the library call alone and take the audited refusal (auth.webauthn_failed), logging only the raw type at WARNING. Covers cryptography's UnsupportedGeneralNameType from a tpm leaf whose subjectAltName holds an x400Address or EDIPartyName, with a test red under the old catch. - #5342: A NUL sign-in name reaches the service on both surfaces, which refuses it as an unknown name before the account lookup and audits it. The attempt is charged to the limiter; for the combined passkey sign-in the charge is taken at /ui/login/passkey. --- docs/API-INPUT-VALIDATION.md | 7 ++++--- messagefoundry/api/validation.py | 5 +++-- tests/test_webauthn.py | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/docs/API-INPUT-VALIDATION.md b/docs/API-INPUT-VALIDATION.md index 4203ebb516..7e1f5a8cc6 100644 --- a/docs/API-INPUT-VALIDATION.md +++ b/docs/API-INPUT-VALIDATION.md @@ -63,9 +63,10 @@ rule, set out under [Items checked a second time](#items-checked-a-second-time-b Neither sign-in surface refuses a NUL in the name at the body: not `username` on `POST /auth/login`, and not the web console's `POST /ui/login` form, which has no body model. The NUL rule is left off the sign-in body on purpose. A 422 there would be neither charged to the -sign-in limiter nor audited. On both surfaces the route charges the limiter, and the sign-in -service refuses such a name as an unknown one before the account lookup and audits the attempt. -`POST /auth/login` answers with its usual failed sign-in, and the form with its usual redirect to +sign-in limiter nor audited. On both surfaces the attempt is charged to the limiter, at +`POST /ui/login/passkey` for the combined passkey sign-in and at the sign-in route otherwise, and +the sign-in service refuses such a name as an unknown one before the account lookup and audits +the attempt. `POST /auth/login` answers with its usual failed sign-in, and the form with its usual redirect to the sign-in page. "Printable text, no control characters" means every character except the C0 range, DEL, and the C1 diff --git a/messagefoundry/api/validation.py b/messagefoundry/api/validation.py index 5756cb9a41..66e0d6ca4b 100644 --- a/messagefoundry/api/validation.py +++ b/messagefoundry/api/validation.py @@ -207,8 +207,9 @@ #: cannot hold one, so a NUL at the store's lookup would make its driver raise rather than find no #: row; the sign-in service refuses such a name as an unknown one before that lookup. This rule #: is on ``POST /users/directory`` alone. Neither sign-in surface carries it: the JSON body and the -#: web console's form both leave a NUL name to that refusal, which each route charges to the -#: sign-in limiter and the service audits. Only the NUL: other control characters can reach a +#: web console's form both leave a NUL name to that refusal, which the service audits. The +#: attempt is charged to the sign-in limiter, at ``/ui/login/passkey`` for the combined passkey +#: sign-in and at the sign-in route otherwise. Only the NUL: other control characters can reach a #: stored name through paths ``auth.usernames.username_refusal`` does not govern, such as #: provision-admin, so refusing them here could lock a holder out. Refusing a NUL locks no one #: out only where no account can hold diff --git a/tests/test_webauthn.py b/tests/test_webauthn.py index 52eab4a6e8..8c5de13388 100644 --- a/tests/test_webauthn.py +++ b/tests/test_webauthn.py @@ -1860,7 +1860,7 @@ def _run() -> None: def _refused_on_recursion( excinfo: pytest.ExceptionInfo[wa.WebAuthnVerificationError], caplog: pytest.LogCaptureFixture ) -> None: - """The refusal came from the RecursionError catch, not from some other library refusal: the + """The refusal came from a RecursionError, not from some other library refusal: the precondition holds on the same stack, the cause is the RecursionError, and its type was logged.""" with pytest.raises(RecursionError): From b7b65e3a10a2bee9a44e7febb3e7f403aa21f58a Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Tue, 17 Nov 2026 09:31:16 -0600 Subject: [PATCH 23/23] Sign-in NUL and passkey refusal prose cut to test pointers Owner ruling 2026-11-17, in the Manager session, asked how PR 3024 should proceed after six rounds each found new false prose about auth controls: "Strip prose to pointers". Every comment, docstring and doc sentence this PR adds or changes about sign-in name validation, NUL refusal, limiter charging, audit rows, passkey body refusal and the webauthn exception handling is now a short pointer to the test that pins it. Test docstrings this PR added are one plain line naming what the test asserts. No code or test logic changed. BACKLOG #5341 BACKLOG #5342 Proposed PR title: WebAuthn verify refuses any library exception; sign-in NUL name and anchor-path fixes (BACKLOG #5341, #5342) Proposed vault banners: - #5341: Both WebAuthn verify calls catch Exception around the library call alone and take the audited refusal (auth.webauthn_failed), logging only the raw type at WARNING. Covers cryptography's UnsupportedGeneralNameType from a tpm leaf whose subjectAltName holds an x400Address or EDIPartyName, with a test red under the old catch. - #5342: A NUL sign-in name reaches the service on both surfaces, which refuses it as an unknown name before the account lookup and audits it. The attempt is charged to the limiter; for the combined passkey sign-in the charge is taken at /ui/login/passkey. --- docs/API-INPUT-VALIDATION.md | 15 +++---- messagefoundry/api/auth_models.py | 14 ++----- messagefoundry/api/validation.py | 17 ++------ messagefoundry/auth/usernames.py | 6 +-- messagefoundry/auth/webauthn.py | 22 +++++----- messagefoundry_webconsole/routes/core.py | 10 ++--- .../tests/test_ui_passkey_body_refusals.py | 33 +++++---------- tests/test_api_auth.py | 13 +++--- tests/test_auth_federated_identity_routes.py | 5 +-- tests/test_auth_hardening.py | 3 +- tests/test_webauthn.py | 40 ++++++++----------- 11 files changed, 64 insertions(+), 114 deletions(-) diff --git a/docs/API-INPUT-VALIDATION.md b/docs/API-INPUT-VALIDATION.md index 7e1f5a8cc6..a68c44f06d 100644 --- a/docs/API-INPUT-VALIDATION.md +++ b/docs/API-INPUT-VALIDATION.md @@ -54,20 +54,17 @@ filesystem path, a log line or a CSV export. | Audit action filter | Printable text, no control characters, 1 to 128 characters | `action` on `GET /audit` and `GET /audit/export` | | Display label | Printable text, no control characters, 1 to 128 characters | `name` on `POST /search/presets` | | Filesystem path | Printable text, no control characters, 1 to 4096 characters | `config_dir` on `POST /config/reload`, and `archive` on `POST /dr/activate` | -| Directory user name | Any text except NUL, 1 to 256 characters | `username` on `POST /users/directory` | +| Directory user name | No NUL, 1 to 256 characters. Pinned by `tests/test_auth_federated_identity_routes.py::test_the_directory_create_refuses_a_nul_in_the_name_before_the_directory` | `username` on `POST /users/directory` | | Layered preset ids | Resource ids joined by single commas with no spaces, up to 1024 characters in all | `presets` on `GET /search/layered` | The email-address row covers `recipient_override` only. The account email fields follow a different rule, set out under [Items checked a second time](#items-checked-a-second-time-behind-the-route). -Neither sign-in surface refuses a NUL in the name at the body: not `username` on -`POST /auth/login`, and not the web console's `POST /ui/login` form, which has no body model. The -NUL rule is left off the sign-in body on purpose. A 422 there would be neither charged to the -sign-in limiter nor audited. On both surfaces the attempt is charged to the limiter, at -`POST /ui/login/passkey` for the combined passkey sign-in and at the sign-in route otherwise, and -the sign-in service refuses such a name as an unknown one before the account lookup and audits -the attempt. `POST /auth/login` answers with its usual failed sign-in, and the form with its usual redirect to -the sign-in page. +A NUL in a sign-in name (vault BACKLOG #5342): behaviour pinned by +`tests/test_api_auth.py::test_login_answers_a_nul_username_as_an_audited_unknown_name`, +`tests/test_auth_hardening.py::test_a_username_with_a_nul_is_an_unknown_name_on_both_sign_in_surfaces`, +and, in the web console suite, +`packaging/messagefoundry-webconsole/tests/test_ui_passkey_body_refusals.py`. "Printable text, no control characters" means every character except the C0 range, DEL, and the C1 range. In practice: no NUL, no tab, no carriage return, no line feed. diff --git a/messagefoundry/api/auth_models.py b/messagefoundry/api/auth_models.py index df8f31b287..043753e973 100644 --- a/messagefoundry/api/auth_models.py +++ b/messagefoundry/api/auth_models.py @@ -56,9 +56,8 @@ class CredentialReply(BaseModel): class LoginRequest(RequestModel): - # No NUL rule here (vault BACKLOG #5342), on purpose: the route charges the sign-in limiter - # and the sign-in service refuses a NUL name as an unknown one before the account lookup, and - # audits it. A 422 at this body would be neither charged nor audited. + # A NUL in ``username`` (vault BACKLOG #5342): behaviour pinned by + # tests/test_api_auth.py::test_login_answers_a_nul_username_as_an_audited_unknown_name. username: str = Field(max_length=_NAME_MAX) password: str = Field(max_length=_PASSWORD_MAX) provider: str = Field(default="local", max_length=16) # 'local' | 'ad' @@ -325,13 +324,8 @@ class DirectoryUserCreateRequest(RequestModel): cannot point the holder's notices away from the directory's (ASVS 6.3.7). """ - # The sign-in name rule (vault BACKLOG #5342), on the typed name. The typed name goes to the - # directory lookup, and also to the store when the entry found carries no ``sAMAccountName``: - # the lookup then names the account by the name it searched for, the typed name with its - # surrounding white space stripped (``fallback_username`` in ``auth.ldap``). The LDAP filter - # escapes a NUL (``auth.ldap._escape_filter``), so such a name is not sent raw; the rule - # refuses it with a 422 before any directory round trip. When the entry carries a - # ``sAMAccountName``, that is the name the store receives, and this rule does not check it. + # vault BACKLOG #5342. Pinned by tests/test_auth_federated_identity_routes.py:: + # test_the_directory_create_refuses_a_nul_in_the_name_before_the_directory. username: str = Field(min_length=1, max_length=_NAME_MAX, pattern=SIGN_IN_NAME_PATTERN) notify_email: str | None = Field(default=None, max_length=_NAME_MAX) diff --git a/messagefoundry/api/validation.py b/messagefoundry/api/validation.py index 66e0d6ca4b..b8f5681fda 100644 --- a/messagefoundry/api/validation.py +++ b/messagefoundry/api/validation.py @@ -203,20 +203,9 @@ #: Printable text: no C0 control, no DEL, no C1 control. One or more characters. PRINTABLE_TEXT_PATTERN = r"^[^\x00-\x1f\x7f-\x9f]+$" -#: A sign-in name: any text but a NUL, empty included (vault BACKLOG #5342). PostgreSQL text -#: cannot hold one, so a NUL at the store's lookup would make its driver raise rather than find no -#: row; the sign-in service refuses such a name as an unknown one before that lookup. This rule -#: is on ``POST /users/directory`` alone. Neither sign-in surface carries it: the JSON body and the -#: web console's form both leave a NUL name to that refusal, which the service audits. The -#: attempt is charged to the sign-in limiter, at ``/ui/login/passkey`` for the combined passkey -#: sign-in and at the sign-in route otherwise. Only the NUL: other control characters can reach a -#: stored name through paths ``auth.usernames.username_refusal`` does not govern, such as -#: provision-admin, so refusing them here could lock a holder out. Refusing a NUL locks no one -#: out only where no account can hold -#: one. PostgreSQL is such a store, read from its documentation and not run here. The SQLite store -#: is not: ``create_user`` stores a NUL name and ``get_user_by_username`` finds it, so on SQLite an -#: account created with one through a path that rule does not govern could not sign in. The SQL -#: Server store is not measured. +#: Any text without a NUL, empty included (vault BACKLOG #5342). Pinned by +#: tests/test_auth_federated_identity_routes.py:: +#: test_the_directory_create_refuses_a_nul_in_the_name_before_the_directory. SIGN_IN_NAME_PATTERN = r"^[^\x00]*$" #: A member of one of the engine's own closed vocabularies -- ``MessageStatus``, ``OutboxStatus``, diff --git a/messagefoundry/auth/usernames.py b/messagefoundry/auth/usernames.py index 993adb6853..8843ee5ee1 100644 --- a/messagefoundry/auth/usernames.py +++ b/messagefoundry/auth/usernames.py @@ -17,10 +17,8 @@ U+2800 among them), or when it takes one of the forms the audit trail writes for its own synthetic actors (:data:`RESERVED_ACTOR_FORMS`). An account named ``system`` would otherwise be indistinguishable from the engine in ``GET /audit?actor=system`` -and in an audit export, and a NUL name could not sign in with its password, since -``POST /auth/login`` and the web console's form sign-in both refuse a NUL. Which stores can hold -such a name at all is stated once, at -:data:`messagefoundry.api.validation.SIGN_IN_NAME_PATTERN`. +and in an audit export. The NUL case is pinned by +``tests/test_username_rule.py::test_the_rule_refuses``. The ``directory`` name is NOT reserved: nothing at HEAD writes it as an audit actor. diff --git a/messagefoundry/auth/webauthn.py b/messagefoundry/auth/webauthn.py index 3b394530c3..3fb28d5fe6 100644 --- a/messagefoundry/auth/webauthn.py +++ b/messagefoundry/auth/webauthn.py @@ -167,9 +167,8 @@ def _supported_pub_key_algs() -> list[COSEAlgorithmIdentifier]: def _invalid_input_errors() -> tuple[type[Exception], ...]: - """Exceptions py_webauthn raises for bad input, caught around its key-decoding calls in - :func:`_require_usable_public_key`. Both ceremonies' verify calls catch every ``Exception`` - instead (vault BACKLOG #5341). + """Exceptions py_webauthn raises for bad input. Behaviour pinned by tests/test_webauthn.py:: + test_an_unusable_stored_key_fails_the_assertion_audited_not_raised. ``WebAuthnException`` is the library's own base class. The next three entries catch four raw exception types (``LookupError`` covers both ``KeyError`` and ``IndexError``) that it lets @@ -582,8 +581,6 @@ def verify_registration( _require_webauthn() from webauthn import verify_registration_response - # Resolved before the ``try`` so the catch below covers the library call alone and not our - # code before it. supported_algs = _supported_pub_key_algs() try: verified = verify_registration_response( @@ -594,8 +591,10 @@ def verify_registration( supported_pub_key_algs=supported_algs, ) except Exception as exc: - # Every exception from the library call is the audited refusal (vault BACKLOG #5341), and - # :func:`_refusal` logs the type of any raw one at WARNING. + # vault BACKLOG #5341. Pinned by tests/test_webauthn.py:: + # test_a_deeply_nested_registration_response_is_the_audited_refusal, + # test_a_malformed_safetynet_statement_is_the_audited_refusal and + # test_a_malformed_attestation_certificate_is_the_audited_refusal. raise _refusal(exc, ceremony="registration") from exc _require_usable_public_key(verified.credential_public_key, ceremony="registration") return RegistrationResult( @@ -689,10 +688,8 @@ def verify_assertion( credential_current_sign_count=current_sign_count, ) except Exception as exc: - # Every exception from the library call is the audited refusal (vault BACKLOG #5341), and - # :func:`_refusal` logs the type of any raw one at WARNING. The sign-count regression - # message ("...sign count...") still rides through for the service's clone-signal - # classification. + # vault BACKLOG #5341. Pinned by tests/test_webauthn.py:: + # test_a_deeply_nested_assertion_response_is_the_audited_refusal. raise _refusal(exc, ceremony="assertion") from exc return AssertionResult(verified.new_sign_count, bool(verified.user_verified)) @@ -782,8 +779,7 @@ def resident_key_from_response(response_json: str) -> bool | None: def _transports_from_response(response_json: str) -> list[str] | None: """Best-effort ``response.transports`` extraction (browser hint — absence is fine).""" - # The same refusal-safe parse as resident_key_from_response, so a RecursionError here cannot - # escape after the library's own parse succeeded (vault BACKLOG #5341). + # vault BACKLOG #5341. parsed, refused = json_loads_or_refusal(response_json) if refused is not None or not isinstance(parsed, dict): return None diff --git a/messagefoundry_webconsole/routes/core.py b/messagefoundry_webconsole/routes/core.py index 75ca8ad9a2..0bb6d711cc 100644 --- a/messagefoundry_webconsole/routes/core.py +++ b/messagefoundry_webconsole/routes/core.py @@ -764,14 +764,12 @@ async def ui_login(request: Request) -> Response: # selector, and "ad" stays in the allow-list only so the ENGINE (_dispatch_login) is the # single point that refuses and audits it. Directory accounts sign in by Windows SSO or # OIDC, and the AD bind as the user survives only as the step-up re-bind at /ui/reauth - # (and the JSON /me/reauth). A name holding a NUL reaches the service too, which refuses it - # as an unknown name before the account lookup, audited (BACKLOG #5342). + # (and the JSON /me/reauth). A NUL name (BACKLOG #5342): pinned by + # test_ui_passkey_body_refusals.py::test_the_form_sign_in_refuses_a_nul_username_before_the_store. def _refused() -> Response: - # The challenge is already popped, so each refusal this helper builds also ends its - # handle cookie. Not every refusal after the pop comes here: at least the login-budget - # 429 above raises with the cookie left set, and so does an exception out of - # ``request.body()`` or ``auth.login``. + # Pinned by test_ui_passkey_body_refusals.py:: + # test_the_combined_passkey_sign_in_refuses_a_nul_username_and_ends_its_challenge. refused = RedirectResponse("/ui/login?e=bad", status_code=303) if handle: clear_passkey_sign_in_cookie(refused, request) diff --git a/packaging/messagefoundry-webconsole/tests/test_ui_passkey_body_refusals.py b/packaging/messagefoundry-webconsole/tests/test_ui_passkey_body_refusals.py index d6321a3850..74e4f23c02 100644 --- a/packaging/messagefoundry-webconsole/tests/test_ui_passkey_body_refusals.py +++ b/packaging/messagefoundry-webconsole/tests/test_ui_passkey_body_refusals.py @@ -1,13 +1,7 @@ # SPDX-License-Identifier: AGPL-3.0-or-later # Copyright (C) 2026 MessageFoundry Foundation, LLC and contributors -"""Two console refusals the engine's own models could not reach (vault BACKLOG #5341, #5342). - -``POST /ui/reauth/webauthn`` and ``POST /ui/account/webauthn/verify`` parse their JSON body -themselves, before the engine's ceremony code sees it, so a body nested too deep for the stack -raised ``RecursionError`` out of the route. ``POST /ui/login`` parses its form without pydantic, -and neither it nor the combined passkey sign-in that rides the same form refuses a NUL name -itself: the service's own refusal, before the account lookup, covers both, as it covers the JSON -``POST /auth/login``. All data here is synthetic. +"""Web console tests for too-deep passkey bodies and NUL sign-in names (vault BACKLOG #5341, +#5342). All data here is synthetic. """ from __future__ import annotations @@ -38,9 +32,7 @@ _TOO_DEEP = "[" * _DEPTH + "]" * _DEPTH -#: Each route's too-deep body, and the refusal it answers. The re-auth route hands the body to the -#: service, whose audited refusal answers; the enrolment route cannot read the label, so it answers -#: its own malformed-body refusal. +#: Each route's too-deep body, and the response the test expects from it. _DEEP_BODIES = { "/ui/reauth/webauthn": ( '{"response": ' + _TOO_DEEP + "}", @@ -55,8 +47,7 @@ def _on_a_small_stack[T](call: Callable[[], Coroutine[Any, Any, T]]) -> T: """Run ``call`` in its own event loop on a thread whose stack is :data:`_SMALL_STACK`, - re-raising what it raised. The route parses the body on the loop's thread, so the loop and the - engine it serves are built there too.""" + re-raising what it raised.""" result: list[T] = [] raised: list[BaseException] = [] @@ -80,8 +71,7 @@ def _run() -> None: @pytest.mark.parametrize("route", sorted(_DEEP_BODIES)) def test_a_body_nested_too_deep_is_a_refusal_not_a_500(tmp_path: Path, route: str) -> None: - """vault BACKLOG #5341. Red: the route's own ``request.json()`` raised ``RecursionError``, - which its catch did not list, so it escaped the route (a 500 on a served engine).""" + """A too-deep body answers 400 with the route's expected JSON (vault BACKLOG #5341).""" body, refusal = _DEEP_BODIES[route] async def _parse() -> object: @@ -152,9 +142,8 @@ async def _failed_actors(engine: Engine) -> list[str]: async def test_the_form_sign_in_refuses_a_nul_username_before_the_store( engine: Engine, monkeypatch: pytest.MonkeyPatch, username: str ) -> None: - """vault BACKLOG #5342, the form route. Red: the NUL reached the store's account lookup, where - at least a PostgreSQL store refuses a NUL in a text bind. The service refuses it as an unknown - name before the lookup, with the audit row naming it escaped.""" + """A NUL name redirects to the error page, reaches the service, is never looked up, and + writes one escaped ``auth.login_failed`` row (vault BACKLOG #5342).""" service = await _service(engine) reached = _spy_login(service, monkeypatch) looked_up = _spy_lookup(engine, monkeypatch) @@ -171,8 +160,8 @@ async def test_the_form_sign_in_refuses_a_nul_username_before_the_store( async def test_the_combined_passkey_sign_in_refuses_a_nul_username_and_ends_its_challenge( engine: Engine, monkeypatch: pytest.MonkeyPatch ) -> None: - """vault BACKLOG #5342, the combined passkey sign-in, which rides the same form. Red: as above. - The refusal also clears the challenge cookie, as any refused sign-in after the pop does.""" + """A NUL name with a passkey assertion redirects to the error page, is never looked up, and + clears the passkey cookie (vault BACKLOG #5342).""" pytest.importorskip("webauthn") service = await _service(engine) looked_up = _spy_lookup(engine, monkeypatch) @@ -194,8 +183,8 @@ async def test_the_combined_passkey_sign_in_refuses_a_nul_username_and_ends_its_ async def test_the_form_sign_in_still_reaches_the_service_for_other_names( engine: Engine, monkeypatch: pytest.MonkeyPatch, username: str ) -> None: - """The control: another control character and an ordinary unknown name still reach - ``AuthService.login``, which refuses them as it decides.""" + """Another control character and an unknown name reach the service and redirect to the error + page.""" service = await _service(engine) reached = _spy_login(service, monkeypatch) async with _client(engine, service) as c: diff --git a/tests/test_api_auth.py b/tests/test_api_auth.py index 7281b30180..47c30b1707 100644 --- a/tests/test_api_auth.py +++ b/tests/test_api_auth.py @@ -3674,17 +3674,15 @@ async def _live(c: httpx.AsyncClient, token: str) -> bool: assert too_long.status_code == 422 -# --- vault BACKLOG #5342: a sign-in naming a NUL takes the service's audited refusal -------------- +# --- vault BACKLOG #5342: a NUL in the sign-in name --------------------------------------------- @pytest.mark.parametrize("username", ["sup\x00", "\x00", "s\x00up"]) async def test_login_answers_a_nul_username_as_an_audited_unknown_name( engine: Engine, username: str, monkeypatch: pytest.MonkeyPatch ) -> None: - """Red: a NUL rule on the body answered 422, which was neither charged to the sign-in limiter - nor audited. Now the JSON sign-in answers as for an unknown name, is charged, writes an - ``auth.login_failed`` row, and never reaches the store's account lookup. The positive control - is the unknown name sent first: the same lookup spy records it.""" + """A NUL name answers as an unknown name does, both sign-ins are charged, it writes one + escaped ``auth.login_failed`` row, and it is never looked up.""" service = await _service(engine, AuthSettings(require_mfa=False)) await _add(service, "sup", Role.VIEWER) looked_up: list[str] = [] @@ -3725,9 +3723,8 @@ def _allow(client: str | None) -> bool: async def test_login_still_reaches_the_service_for_other_unusual_values( engine: Engine, fields: dict[str, str], monkeypatch: pytest.MonkeyPatch ) -> None: - """The controls: another control character, an empty name, and a ``supersedes`` holding a NUL - (only ever hashed, never sent to the store) are not refused by the body. They reach - ``AuthService.login`` as before, and answer as it decides.""" + """Another control character, an empty name, and a NUL or empty ``supersedes`` reach the + service and answer 200 or 401.""" service = await _service(engine, AuthSettings(require_mfa=False)) await _add(service, "sup", Role.VIEWER) reached: list[str] = [] diff --git a/tests/test_auth_federated_identity_routes.py b/tests/test_auth_federated_identity_routes.py index 4204c39d90..cfb44bf077 100644 --- a/tests/test_auth_federated_identity_routes.py +++ b/tests/test_auth_federated_identity_routes.py @@ -927,9 +927,8 @@ async def test_a_directory_row_created_by_name_takes_its_id_from_the_directory_a async def test_the_directory_create_refuses_a_nul_in_the_name_before_the_directory( engine: Engine, ) -> None: - """vault BACKLOG #5342, the sign-in name rule on ``POST /users/directory``. Red: the NUL went - on to the directory lookup. THE CONTROL is the same name without it, which the directory is - asked about.""" + """A NUL name answers 422 and the directory is not asked; the same name without it is asked + and answers 201 (vault BACKLOG #5342).""" directory = _Directory(_principal("jdoe")) service = await _directory_service(engine, directory) async with _client(engine, service) as c: diff --git a/tests/test_auth_hardening.py b/tests/test_auth_hardening.py index 2d378037a6..97ce6cb299 100644 --- a/tests/test_auth_hardening.py +++ b/tests/test_auth_hardening.py @@ -624,7 +624,8 @@ async def test_a_username_with_a_nul_is_an_unknown_name_on_both_sign_in_surfaces service refuses it as an unknown name before the account lookup: at the seam itself, and through the console form, which charges the limiter first. The answer is an unknown name's, and the ``auth.login_failed`` row names it with the NUL escaped. The JSON route is driven at - the seam here; ``tests/test_api_auth.py`` drives it over HTTP.""" + the seam here; over HTTP: ``tests/test_api_auth.py:: + test_login_answers_a_nul_username_as_an_audited_unknown_name``.""" service = await _service(engine) await _add(service, "bob") looked_up: list[str] = [] diff --git a/tests/test_webauthn.py b/tests/test_webauthn.py index 8c5de13388..90b92b7952 100644 --- a/tests/test_webauthn.py +++ b/tests/test_webauthn.py @@ -1860,9 +1860,8 @@ def _run() -> None: def _refused_on_recursion( excinfo: pytest.ExceptionInfo[wa.WebAuthnVerificationError], caplog: pytest.LogCaptureFixture ) -> None: - """The refusal came from a RecursionError, not from some other library refusal: the - precondition holds on the same stack, the cause is the RecursionError, and its type was - logged.""" + """json raises RecursionError on the same stack, the refusal's cause is a RecursionError, + and a WARNING names its type.""" with pytest.raises(RecursionError): _on_a_small_stack(lambda: json.loads(_TOO_DEEP)) assert isinstance(excinfo.value.__cause__, RecursionError), repr(excinfo.value.__cause__) @@ -1872,9 +1871,8 @@ def _refused_on_recursion( def test_a_deeply_nested_registration_response_is_the_audited_refusal( caplog: pytest.LogCaptureFixture, ) -> None: - """vault BACKLOG #5341, at this module's seam only. Red: the library's RecursionError escaped - verify_registration. The web console's routes that parse the body first have their own test, - in the web console suite (``test_ui_passkey_body_refusals.py``).""" + """verify_registration raises WebAuthnVerificationError caused by RecursionError on a too-deep + response (vault BACKLOG #5341).""" with ( caplog.at_level(logging.WARNING, logger=_WEBAUTHN_LOG), pytest.raises(wa.WebAuthnVerificationError) as excinfo, @@ -1890,7 +1888,8 @@ def test_a_deeply_nested_registration_response_is_the_audited_refusal( def test_a_deeply_nested_assertion_response_is_the_audited_refusal( caplog: pytest.LogCaptureFixture, ) -> None: - """vault BACKLOG #5341, the text form ``verify_assertion`` accepts. Red: as above.""" + """verify_assertion raises WebAuthnVerificationError caused by RecursionError on a too-deep + response (vault BACKLOG #5341).""" soft = SoftAuthenticator(rp_id=RP, origin=ORIGIN) with ( caplog.at_level(logging.WARNING, logger=_WEBAUTHN_LOG), @@ -1927,9 +1926,8 @@ def _safetynet_jws(header: bytes, payload: bytes) -> bytes: def test_a_malformed_safetynet_statement_is_the_audited_refusal( att_stmt: dict[str, object], caplog: pytest.LogCaptureFixture ) -> None: - """vault BACKLOG #5341, at this module's seam only: the refusal the service audits. Red: - webauthn 3.0.1's android-safetynet check raised AttributeError on these attacker-shaped - statements, and it escaped verify_registration as itself.""" + """verify_registration raises WebAuthnVerificationError caused by AttributeError on each + statement, and a WARNING names its type (vault BACKLOG #5341).""" from webauthn.helpers import encode_cbor, parse_cbor challenge = b"c" * 32 @@ -1961,9 +1959,9 @@ def test_a_malformed_safetynet_statement_is_the_audited_refusal( def _attestation_cert(*, san: bool = False) -> tuple[bytes, Any]: - """A self-signed P-256 leaf in DER, and its private key. Its subject is empty and it carries - BasicConstraints then KeyUsage, so the library's tpm check reads it as far as its extensions. - With ``san`` it also carries a subjectAltName holding one empty directoryName.""" + """A self-signed P-256 leaf in DER with an empty subject, BasicConstraints then KeyUsage, and + its private key. With ``san`` it also carries a subjectAltName holding one empty + directoryName.""" import datetime from cryptography import x509 @@ -2007,12 +2005,7 @@ def _attested_registration( ) -> tuple[str, bytes]: """A registration response re-labelled ``fmt``, its statement carrying a leaf certificate patched by ``patch`` (``None`` leaves it intact), and its challenge. ``san`` is passed to - :func:`_attestation_cert`. - - The ``tpm`` statement is built to pass every check the library runs before it parses the - leaf: ``pubArea`` names the credential's own P-256 point, ``certInfo`` carries the hashes the - library recomputes, and the leaf's key signs ``certInfo``. The ``packed`` statement carries a - signature that does not verify, which the library finds only after it parses the leaf.""" + :func:`_attestation_cert`.""" import hashlib from cryptography.hazmat.primitives import hashes @@ -2095,9 +2088,8 @@ def _attested_registration( def test_a_malformed_attestation_certificate_is_the_audited_refusal( fmt: str, patch: tuple[str, str], raised: str, caplog: pytest.LogCaptureFixture ) -> None: - """vault BACKLOG #5341, at this module's seam only: the refusal the service audits. Red: - cryptography raised each of these on an attacker-supplied leaf, and it escaped - verify_registration as itself, which the route would have served as a 500.""" + """verify_registration raises WebAuthnVerificationError caused by the listed exception on + each patched leaf, and a WARNING names its type (vault BACKLOG #5341).""" san = patch[0] == _DIRECTORY_NAME response_json, challenge = _attested_registration(fmt, patch, san=san) with ( @@ -2124,8 +2116,8 @@ def test_a_malformed_attestation_certificate_is_the_audited_refusal( def test_an_intact_attestation_certificate_is_parsed_past_where_the_patched_ones_fail( fmt: str, san: bool, refusal: str ) -> None: - """Positive control for the test above: with the leaf intact, the library parses it and - refuses later, on purpose. So each patched leaf, not the statement around it, raised there.""" + """With the leaf intact, verify_registration raises WebAuthnVerificationError with the listed + message, caused by InvalidRegistrationResponse.""" response_json, challenge = _attested_registration(fmt, None, san=san) with pytest.raises(wa.WebAuthnVerificationError, match=refusal) as excinfo: wa.verify_registration(