@@ -32,7 +32,7 @@ use ceremonies::{
3232 ManagementCeremony , RegistrationCeremony ,
3333} ;
3434pub use error:: PasskeyConfigError ;
35- use error:: PasskeyError ;
35+ use error:: { CredentialRejection , PasskeyError } ;
3636use types:: {
3737 AuthenticationFinish , DisplayName , EnrollmentStart , LoginStart , ManagementResponse ,
3838 PasskeyKind , PasskeyName , PasskeyView , RegistrationFinish , RegistrationStart , Rename ,
@@ -59,6 +59,8 @@ const RELYING_PARTY_NAME: &str = "Piquel";
5959const ANDROID_USER_AGENT : & str = "Android" ;
6060/// Signature counter location in webauthn-rs's serialized passkey representation.
6161const CREDENTIAL_COUNTER_POINTER : & str = "/cred/counter" ;
62+ /// Stable telemetry event name for a signature counter regression, a sign of a copied passkey.
63+ const COUNTER_REGRESSION_EVENT : & str = "auth.passkey_counter_regression" ;
6264
6365impl PasskeyApplication {
6466 /// Builds `WebAuthn` from the already validated origin and admission policy.
@@ -226,7 +228,7 @@ impl PasskeyApplication {
226228 result : & AuthenticationResult ,
227229 ) -> Result < ( ) , PasskeyError > {
228230 if !result. user_verified ( ) {
229- return Err ( PasskeyError :: UserVerificationRequired ) ;
231+ return Err ( CredentialRejection :: UserVerificationRequired . into ( ) ) ;
230232 }
231233 tx. users ( )
232234 . find_active_by_id_for_update ( user_id)
@@ -246,13 +248,13 @@ impl PasskeyApplication {
246248 . and_then ( Value :: as_u64)
247249 . ok_or ( PasskeyError :: MissingCounter ) ?;
248250 if ( counter > 0 || result. counter ( ) > 0 ) && u64:: from ( result. counter ( ) ) <= counter {
249- return Err ( PasskeyError :: CounterRegression ) ;
251+ return Err ( CredentialRejection :: CounterRegression . into ( ) ) ;
250252 }
251253 let mut credential: Passkey =
252254 serde_json:: from_value ( stored. credential ) . map_err ( PasskeyError :: StoredCredential ) ?;
253255 credential
254256 . update_credential ( result)
255- . ok_or ( PasskeyError :: CredentialMismatch ) ?;
257+ . ok_or ( CredentialRejection :: CredentialMismatch ) ?;
256258 if !tx
257259 . passkeys ( )
258260 . update_after_authentication (
@@ -266,6 +268,50 @@ impl PasskeyApplication {
266268 Ok ( ( ) )
267269 }
268270
271+ /// Takes `WebAuthn`'s verdict on an assertion for `user_id` and, once the latest
272+ /// credential state also accepts it, opens the transaction that completes the
273+ /// authentication. A [`CredentialRejection`] rolls the ceremony back, then commits
274+ /// its audit entry on the pool so the record outlives the rejection.
275+ async fn accept_credential (
276+ & self ,
277+ user_id : UserId ,
278+ verified : Result < AuthenticationResult , WebauthnError > ,
279+ ) -> Result < DatabaseTransaction < ' _ > , PasskeyError > {
280+ let rejection = match verified {
281+ Ok ( result) => {
282+ let mut tx = self . database . begin ( ) . await ?;
283+ match Self :: update_credential ( & mut tx, user_id, & result) . await {
284+ Ok ( ( ) ) => return Ok ( tx) ,
285+ Err ( PasskeyError :: Credential ( rejection) ) => {
286+ tx. rollback ( ) . await ?;
287+ rejection
288+ }
289+ Err ( error) => return Err ( error) ,
290+ }
291+ }
292+ // webauthn-rs compares the counter with the ceremony's credential snapshot
293+ // only after the signature verified, so this one implicates the account.
294+ // Its other errors, user verification included, can precede the signature.
295+ Err ( WebauthnError :: CredentialPossibleCompromise ) => {
296+ CredentialRejection :: CounterRegression
297+ }
298+ Err ( error) => return Err ( PasskeyError :: Webauthn ( error) ) ,
299+ } ;
300+ // Logged before the audit insert so the signal survives a database failure.
301+ if matches ! ( rejection, CredentialRejection :: CounterRegression ) {
302+ tracing:: warn!(
303+ event = COUNTER_REGRESSION_EVENT ,
304+ %user_id,
305+ "passkey signature counter regression rejected"
306+ ) ;
307+ }
308+ self . database
309+ . audit_events ( )
310+ . record_rejection ( user_id, rejection. audit_action ( ) )
311+ . await ?;
312+ Err ( rejection. into ( ) )
313+ }
314+
269315 /// Locks account and session authorization in the same order as credential changes.
270316 async fn lock_management (
271317 tx : & mut DatabaseTransaction < ' _ > ,
@@ -439,15 +485,14 @@ impl PasskeyApplication {
439485 headers : HeaderMap ,
440486 Json ( input) : Json < AuthenticationFinish > ,
441487 ) -> Result < Response , ApiError > {
442- let ( user_id, result ) = match app
488+ let ( user_id, verified ) = match app
443489 . consume :: < AuthenticationCeremony > ( & headers, input. ceremony_id )
444490 . await ?
445491 {
446492 AuthenticationCeremony :: Login ( LoginCeremony { user_id, state } ) => (
447493 user_id,
448494 app. webauthn
449- . finish_passkey_authentication ( & input. credential , & state)
450- . map_err ( PasskeyError :: Webauthn ) ?,
495+ . finish_passkey_authentication ( & input. credential , & state) ,
451496 ) ,
452497 AuthenticationCeremony :: Discover ( DiscoverCeremony { state } ) => {
453498 let ( uuid, _) = app
@@ -463,14 +508,15 @@ impl PasskeyApplication {
463508 . collect ( ) ;
464509 (
465510 user_id,
466- app. webauthn
467- . finish_discoverable_authentication ( & input. credential , state, & keys)
468- . map_err ( PasskeyError :: Webauthn ) ?,
511+ app. webauthn . finish_discoverable_authentication (
512+ & input. credential ,
513+ state,
514+ & keys,
515+ ) ,
469516 )
470517 }
471518 } ;
472- let mut tx = app. database . begin ( ) . await ?;
473- Self :: update_credential ( & mut tx, user_id, & result) . await ?;
519+ let tx = app. accept_credential ( user_id, verified) . await ?;
474520 Ok ( Self :: complete_sign_in ( tx, user_id, AuditAction :: PasskeyLogin ) . await ?)
475521 }
476522
@@ -514,12 +560,10 @@ impl PasskeyApplication {
514560 if session. user_id != user_id || session. id != session_id {
515561 return Err ( PasskeyError :: SessionChanged . into ( ) ) ;
516562 }
517- let result = app
563+ let verified = app
518564 . webauthn
519- . finish_passkey_authentication ( & input. credential , & state)
520- . map_err ( PasskeyError :: Webauthn ) ?;
521- let mut tx = app. database . begin ( ) . await ?;
522- Self :: update_credential ( & mut tx, user_id, & result) . await ?;
565+ . finish_passkey_authentication ( & input. credential , & state) ;
566+ let mut tx = app. accept_credential ( user_id, verified) . await ?;
523567 let expires = tx. sessions ( ) . grant_management ( session_id) . await ?;
524568 tx. audit_events ( )
525569 . record ( user_id, AuditAction :: SettingsUnlock , None )
0 commit comments