diff --git a/docs/API-INPUT-VALIDATION.md b/docs/API-INPUT-VALIDATION.md index 7946acceaf..a68c44f06d 100644 --- a/docs/API-INPUT-VALIDATION.md +++ b/docs/API-INPUT-VALIDATION.md @@ -5,19 +5,30 @@ 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 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' +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 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. +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. --- @@ -43,11 +54,18 @@ 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` | +| 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). +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. @@ -339,7 +357,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 9b0327c9fd..043753e973 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, @@ -55,6 +56,8 @@ class CredentialReply(BaseModel): class LoginRequest(RequestModel): + # 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' @@ -321,7 +324,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) + # 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 e73034d5ba..b8f5681fda 100644 --- a/messagefoundry/api/validation.py +++ b/messagefoundry/api/validation.py @@ -203,6 +203,11 @@ #: Printable text: no C0 control, no DEL, no C1 control. One or more characters. PRINTABLE_TEXT_PATTERN = r"^[^\x00-\x1f\x7f-\x9f]+$" +#: 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``, #: 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/anchor_path.py b/messagefoundry/auth/anchor_path.py index b4d7baf8e8..03bbbdf482 100644 --- a/messagefoundry/auth/anchor_path.py +++ b/messagefoundry/auth/anchor_path.py @@ -606,7 +606,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; 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. @@ -680,6 +681,12 @@ def add(obj: str, kind: str) -> None: if len(drive) != 2 and not rest: # A volume GUID with no separator after it (``//?/Volume{x}``) opens the volume device. return chain, WalkCause.plain(_DEVICE_PATH) + 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: @@ -700,9 +707,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) diff --git a/messagefoundry/auth/usernames.py b/messagefoundry/auth/usernames.py index 853e585b1f..8843ee5ee1 100644 --- a/messagefoundry/auth/usernames.py +++ b/messagefoundry/auth/usernames.py @@ -17,7 +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 would reach a store that cannot hold one. +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 24e3836ef6..3fb28d5fe6 100644 --- a/messagefoundry/auth/webauthn.py +++ b/messagefoundry/auth/webauthn.py @@ -167,15 +167,17 @@ 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. 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 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 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. @@ -579,20 +581,20 @@ def verify_registration( _require_webauthn() from webauthn import verify_registration_response + 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_input_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, - # 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. + except Exception as exc: + # 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( @@ -685,12 +687,9 @@ def verify_assertion( credential_public_key=public_key, credential_current_sign_count=current_sign_count, ) - except _invalid_input_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: + # 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)) @@ -780,11 +779,12 @@ 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 + # 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/messagefoundry_webconsole/routes/core.py b/messagefoundry_webconsole/routes/core.py index cdfdee9d6c..0bb6d711cc 100644 --- a/messagefoundry_webconsole/routes/core.py +++ b/messagefoundry_webconsole/routes/core.py @@ -764,10 +764,20 @@ 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). + # (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: + # 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) + return refused + provider_value = form.get("provider", "local") if provider_value not in ("local", "ad"): - return RedirectResponse("/ui/login?e=bad", status_code=303) + 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. @@ -795,10 +805,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 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..74e4f23c02 --- /dev/null +++ b/packaging/messagefoundry-webconsole/tests/test_ui_passkey_body_refusals.py @@ -0,0 +1,195 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later +# Copyright (C) 2026 MessageFoundry Foundation, LLC and contributors +"""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 + +import asyncio +import json +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 + +#: 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 + + +#: Each route's too-deep body, and the response the test expects from it. +_DEEP_BODIES = { + "/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"}, + ), +} + + +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.""" + 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)) +def test_a_body_nested_too_deep_is_a_refusal_not_a_500(tmp_path: Path, route: str) -> None: + """A too-deep body answers 400 with the route's expected JSON (vault BACKLOG #5341).""" + body, refusal = _DEEP_BODIES[route] + + 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): + _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, refusal) + + +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 + + +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_store( + engine: Engine, monkeypatch: pytest.MonkeyPatch, username: str +) -> None: + """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) + 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] + 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: + """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) + 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 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 + + +@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: + """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: + 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_anchor_path.py b/tests/test_anchor_path.py index 3f1d9878bc..ec218db14b 100644 --- a/tests/test_anchor_path.py +++ b/tests/test_anchor_path.py @@ -1377,3 +1377,41 @@ 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 + + +@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:", ap._no_drive_target), + # 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) diff --git a/tests/test_api_auth.py b/tests/test_api_auth.py index 66b223cf2d..47c30b1707 100644 --- a/tests/test_api_auth.py +++ b/tests/test_api_auth.py @@ -3672,3 +3672,71 @@ 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 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: + """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] = [] + real_lookup = engine.store.get_user_by_username + + async def _lookup(name: str) -> Any: + looked_up.append(name) + return await real_lookup(name) + + monkeypatch.setattr(engine.store, "get_user_by_username", _lookup) + charged: list[str | None] = [] + real_allow = service.allow_login_attempt + + 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 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( + "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], monkeypatch: pytest.MonkeyPatch +) -> None: + """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] = [] + 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: + 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_auth_federated_identity_routes.py b/tests/test_auth_federated_identity_routes.py index 57a19714e5..cfb44bf077 100644 --- a/tests/test_auth_federated_identity_routes.py +++ b/tests/test_auth_federated_identity_routes.py @@ -924,6 +924,25 @@ 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: + """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: + 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_auth_hardening.py b/tests/test_auth_hardening.py index a381fb2429..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, not over HTTP, because its body model may refuse a NUL before the service.""" + 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 a550a91fbf..90b92b7952 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 @@ -1819,3 +1821,308 @@ 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() + + +#: 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": ' + "[" * _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: + """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__) + 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: + """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, + ): + _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) + + +def test_a_deeply_nested_assertion_response_is_the_audited_refusal( + caplog: pytest.LogCaptureFixture, +) -> None: + """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), + pytest.raises(wa.WebAuthnVerificationError) as excinfo, + ): + _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) + + +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"), + 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: + """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 + 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) + + +#: 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" +#: 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(*, san: bool = False) -> tuple[bytes, Any]: + """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 + 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) + builder = ( + 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, + ) + ) + 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 + + +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, *, 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. ``san`` is passed to + :func:`_attestation_cert`.""" + 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(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]} + 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", + ), + 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( + fmt: str, patch: tuple[str, str], raised: str, caplog: pytest.LogCaptureFixture +) -> None: + """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 ( + 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", "san", "refusal"), + [ + 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, san: bool, refusal: str +) -> None: + """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( + response_json=response_json, challenge=challenge, rp_id=RP, origin=ORIGIN + ) + from webauthn.helpers.exceptions import InvalidRegistrationResponse + + assert isinstance(excinfo.value.__cause__, InvalidRegistrationResponse)