Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions authentik/stages/authenticator_validate/challenge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
80 changes: 80 additions & 0 deletions authentik/stages/authenticator_validate/tests/test_webauthn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading