From c7bfeb67c55871748c1efa9cd973054c15085f29 Mon Sep 17 00:00:00 2001 From: Ryan Pesek Date: Mon, 2 Nov 2026 20:54:47 -0600 Subject: [PATCH] stages/authenticator_validate: reject malformed WebAuthn assertions as an invalid device validate_challenge_webauthn() only caught InvalidJSONStructure from parse_authentication_credential_json(). py_webauthn raises InvalidAuthenticationResponse when a credential field isn't valid base64url, and a bare ValueError (binascii.Error) when userHandle isn't, so such an assertion, for example a forged passwordless login, failed the flow with the error screen and a system_exception event instead of a validation error. Catch WebAuthnException and ValueError around the parse, as the WebAuthn setup stage does around verify_registration_response(). This covers both callers: the authenticator validation stage and the identification stage's passkey login. --- .../authenticator_validate/challenge.py | 10 +-- .../tests/test_webauthn.py | 80 +++++++++++++++++++ 2 files changed, 84 insertions(+), 6 deletions(-) diff --git a/authentik/stages/authenticator_validate/challenge.py b/authentik/stages/authenticator_validate/challenge.py index 4f73ef2fae39..1096f0399106 100644 --- a/authentik/stages/authenticator_validate/challenge.py +++ b/authentik/stages/authenticator_validate/challenge.py @@ -17,12 +17,8 @@ from webauthn.authentication.verify_authentication_response import verify_authentication_response from webauthn.helpers import parse_authentication_credential_json from webauthn.helpers.base64url_to_bytes import base64url_to_bytes -from webauthn.helpers.exceptions import ( - InvalidJSONStructure, - UnsupportedPublicKey, - WebAuthnException, -) from webauthn.helpers.exceptions import UnsupportedAlgorithm as WebAuthnUnsupportedAlgorithm +from webauthn.helpers.exceptions import UnsupportedPublicKey, WebAuthnException from webauthn.helpers.options_to_json_dict import options_to_json_dict from webauthn.helpers.structs import PublicKeyCredentialType, UserVerificationRequirement @@ -233,7 +229,9 @@ def validate_challenge_webauthn( data.setdefault("type", PublicKeyCredentialType.PUBLIC_KEY) try: credential = parse_authentication_credential_json(data) - except InvalidJSONStructure as exc: + # py_webauthn wraps most decoding errors in InvalidAuthenticationResponse, but not those of + # userHandle, which raise ValueError + except (WebAuthnException, ValueError) as exc: LOGGER.warning("Invalid WebAuthn challenge response", exc=exc) raise ValidationError("Invalid device", "invalid") from None diff --git a/authentik/stages/authenticator_validate/tests/test_webauthn.py b/authentik/stages/authenticator_validate/tests/test_webauthn.py index 48653626a7c5..9fb68df61204 100644 --- a/authentik/stages/authenticator_validate/tests/test_webauthn.py +++ b/authentik/stages/authenticator_validate/tests/test_webauthn.py @@ -524,6 +524,86 @@ def test_validate_challenge_restricted(self): }, ) + def test_validate_challenge_malformed(self): + """Test that an assertion whose fields can't be decoded is rejected as an invalid + device""" + flow = create_test_flow() + stage = AuthenticatorValidateStage.objects.create( + name=generate_id(), + not_configured_action=NotConfiguredAction.CONFIGURE, + device_classes=[DeviceClasses.WEBAUTHN], + ) + plan = FlowPlan(flow.pk.hex) + plan.context[PLAN_CONTEXT_WEBAUTHN_CHALLENGE] = b"\x01" * 64 + stage_view = AuthenticatorValidateStageView( + FlowExecutorView(flow=flow, current_stage=stage, plan=plan), + request=self.request_factory.get("/"), + ) + for field, value in ( + ("rawId", "a"), + ("clientDataJSON", "a"), + ("authenticatorData", "a"), + ("signature", "\u00e9"), + ("userHandle", "a"), + ("userHandle", "\u00e9"), + ): + assertion = { + "id": "QKZ97ASJAOIDyipAs6mKUxDUZgDrWrbAsUb5leL7-oU", + "rawId": "QKZ97ASJAOIDyipAs6mKUxDUZgDrWrbAsUb5leL7-oU", + "type": "public-key", + "response": { + "clientDataJSON": "eyJ0eXBlIjoid2ViYXV0aG4uZ2V0In0", + "authenticatorData": "SZYN5YgOjGh0NBcPZHZgW4_krrmihjLHmVzzuoMdl2MFAAAABQ", + "signature": "MEQCIFNlrHf9ablJAalXLWkrqvHB8oIu8kwvRpH3X3rbJVpI", + "userHandle": None, + }, + } + if field == "rawId": + assertion[field] = value + else: + assertion["response"][field] = value + with self.subTest(field=field, value=value): + with self.assertRaises(ValidationError) as cm: + validate_challenge_webauthn(assertion, stage_view, self.user) + self.assertEqual(str(cm.exception.detail[0]), "Invalid device") + self.assertEqual(cm.exception.detail[0].code, "invalid") + + def test_validate_challenge_malformed_userless(self): + """Test that a malformed assertion in a passwordless flow gets a validation error""" + flow = create_test_flow(FlowDesignation.AUTHENTICATION) + stage = AuthenticatorValidateStage.objects.create( + name=generate_id(), + not_configured_action=NotConfiguredAction.DENY, + device_classes=[DeviceClasses.WEBAUTHN], + ) + FlowStageBinding.objects.create(target=flow, stage=stage, order=0) + url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}) + response = self.client.get(url) + self.assertStageResponse(response, flow, component="ak-stage-authenticator-validate") + + response = self.client.post( + url, + data={ + "webauthn": { + "id": "QKZ97ASJAOIDyipAs6mKUxDUZgDrWrbAsUb5leL7-oU", + "rawId": "a", + "type": "public-key", + "response": { + "clientDataJSON": "eyJ0eXBlIjoid2ViYXV0aG4uZ2V0In0", + "authenticatorData": "SZYN5YgOjGh0NBcPZHZgW4_krrmihjLHmVzzuoMdl2MFAAAABQ", + "signature": "MEQCIFNlrHf9ablJAalXLWkrqvHB8oIu8kwvRpH3X3rbJVpI", + "userHandle": None, + }, + }, + }, + ) + self.assertStageResponse( + response, + flow, + component="ak-stage-authenticator-validate", + response_errors={"webauthn": [{"string": "Invalid device", "code": "invalid"}]}, + ) + def test_validate_challenge_userless(self): """Test webauthn""" device = WebAuthnDevice.objects.create(