diff --git a/authentik/stages/authenticator_validate/challenge.py b/authentik/stages/authenticator_validate/challenge.py index 61afbaeab731..105d38d172f7 100644 --- a/authentik/stages/authenticator_validate/challenge.py +++ b/authentik/stages/authenticator_validate/challenge.py @@ -223,9 +223,7 @@ def validate_challenge_webauthn( request = stage_view.request plan = cast(FlowPlan, stage_view.executor.plan) challenge = plan.context.get(PLAN_CONTEXT_WEBAUTHN_CHALLENGE) - # TODO(ty): the fallback assumes the current stage is an AuthenticatorValidateStage. The - # identification stage passes its `webauthn_stage`, which is None when not configured, so - # a posted passkey there falls back to the IdentificationStage and raises AttributeError. + # Without an explicit stage, this is called from the AuthenticatorValidateStage itself stage = stage or cast(AuthenticatorValidateStage, stage_view.executor.current_stage) if "MinuteMaid" in request.META.get("HTTP_USER_AGENT", ""): diff --git a/authentik/stages/authenticator_webauthn/tasks.py b/authentik/stages/authenticator_webauthn/tasks.py index fd9e355f31ad..20aa8db6ab0e 100644 --- a/authentik/stages/authenticator_webauthn/tasks.py +++ b/authentik/stages/authenticator_webauthn/tasks.py @@ -4,7 +4,6 @@ from functools import lru_cache from json import loads from pathlib import Path -from typing import cast from cryptography import x509 from django.core.cache import cache @@ -13,7 +12,7 @@ from dramatiq.actor import actor from fido2.attestation import verify_x509_chain from fido2.cose import CoseKey -from fido2.mds3 import MetadataBlobPayload, MetadataStatement, filter_revoked +from fido2.mds3 import MetadataBlobPayload, filter_revoked from fido2.utils import websafe_decode from authentik.stages.authenticator_webauthn.models import ( @@ -86,9 +85,10 @@ def webauthn_mds_import(force=False): if not filter_revoked(entry): to_delete.append(str(aaguid)) continue - # TODO(ty): MDS3 entries may omit metadataStatement (parse_mds_blob skips those); - # such an entry with an aaguid would raise AttributeError and abort the whole import - metadata = cast(MetadataStatement, entry.metadata_statement) + # MDS3 entries may omit metadataStatement + metadata = entry.metadata_statement + if metadata is None: + continue to_create_update.append( WebAuthnDeviceType( aaguid=str(aaguid), diff --git a/authentik/stages/identification/stage.py b/authentik/stages/identification/stage.py index ffc19f67af61..11e156063684 100644 --- a/authentik/stages/identification/stage.py +++ b/authentik/stages/identification/stage.py @@ -156,6 +156,9 @@ def _validate_passkey_response(self, passkey: dict) -> WebAuthnDevice: current_stage: IdentificationStage = IdentificationStage.objects.get( pk=self.stage.executor.current_stage.pk ) + # Passkeys are only offered when a WebAuthn stage is configured + if not current_stage.webauthn_stage: + raise ValidationError(_("Invalid device"), "invalid") return validate_challenge_webauthn( passkey, self.stage, self.stage.get_pending_user(), current_stage.webauthn_stage ) diff --git a/authentik/stages/identification/tests.py b/authentik/stages/identification/tests.py index d6b70d4724ce..e5d0e45dff04 100644 --- a/authentik/stages/identification/tests.py +++ b/authentik/stages/identification/tests.py @@ -1,6 +1,7 @@ """identification tests""" from json import loads +from unittest.mock import patch from django.urls import reverse from requests_mock import Mocker @@ -107,6 +108,23 @@ def test_passkey_challenge_generation(self): data = response.json() self.assertIsNotNone(data.get("passkey_challenge")) + def test_passkey_without_webauthn_stage(self): + """A posted passkey is rejected when no WebAuthn stage is configured""" + self.stage.webauthn_stage = None + self.stage.save() + url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": self.flow.slug}) + self.client.get(url) + with patch( + "authentik.stages.identification.stage.validate_challenge_webauthn", + return_value=self.device, + ) as validate: + response = self.client.post( + url, {"passkey": {"id": "test"}}, content_type="application/json" + ) + validate.assert_not_called() + self.assertEqual(response.status_code, 200) + self.assertIn("response_errors", response.json()) + def test_passkey_no_uid_field_required(self): """Test that uid_field is not required when passkey is provided""" # Get the challenge first to set up the session