Skip to content

Commit ef4e389

Browse files
PiquelChipsclaude
andcommitted
fix(auth): audit a counter regression even if its rollback fails
A failed rollback of the authentication transaction is now logged instead of skipping the audit row. Every WebAuthn error goes through the single From conversion, and the docs say which rejections any browser can cause. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 0c1421a commit ef4e389

2 files changed

Lines changed: 21 additions & 13 deletions

File tree

‎apps/auth-service/src/passkey/error.rs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,10 @@ impl From<PasskeyError> for ApiError {
194194

195195
impl PasskeyError {
196196
/// Rejects a passkey ceremony with `message`. The reason is logged at debug level:
197-
/// any browser can trigger these up to the rate limit, so a higher level would let
198-
/// it flood the logs. A [`CounterRegression`](Self::CounterRegression) is also
199-
/// audited and logged as a warning. The Display holds no challenge or credential.
197+
/// any browser can trigger most of these up to the rate limit, so a higher level
198+
/// would let it flood the logs. Only a credential holder can cause a
199+
/// [`CounterRegression`](Self::CounterRegression), so it is also audited and logged
200+
/// as a warning. The Display holds no challenge or credential.
200201
fn rejected(&self, message: impl Into<Cow<'static, str>>) -> ApiError {
201202
tracing::debug!(error = %piquel_common::ErrorChain(self), "WebAuthn request rejected");
202203
ApiError::bad_request(message)

‎apps/auth-service/src/passkey/mod.rs‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,8 @@ impl PasskeyApplication {
279279
/// Takes `WebAuthn`'s verdict on an assertion for `user_id` and, once the latest
280280
/// credential state also accepts it, opens the transaction that completes the
281281
/// authentication. The ceremony was already consumed. A
282-
/// [`PasskeyError::CounterRegression`] rolls this transaction back, then commits
283-
/// its audit entry on the pool so the record outlives the rejection.
282+
/// [`PasskeyError::CounterRegression`] rolls back any transaction it opened, then
283+
/// commits its audit entry on the pool so the record outlives the rejection.
284284
async fn accept_credential(
285285
&self,
286286
user_id: UserId,
@@ -305,8 +305,15 @@ impl PasskeyApplication {
305305
%user_id,
306306
"passkey signature counter regression rejected"
307307
);
308-
if let Some(tx) = tx {
309-
tx.rollback().await?;
308+
// A failed rollback is only logged: the audit entry matters more, and a broken
309+
// connection releases its locks anyway.
310+
if let Some(tx) = tx
311+
&& let Err(rollback) = tx.rollback().await
312+
{
313+
tracing::error!(
314+
error = %piquel_common::ErrorChain(&rollback),
315+
"passkey authentication rollback failed"
316+
);
310317
}
311318
self.database
312319
.audit_events()
@@ -406,7 +413,7 @@ impl PasskeyApplication {
406413
let key = app
407414
.webauthn
408415
.finish_passkey_registration(&input.credential, &state)
409-
.map_err(PasskeyError::Webauthn)?;
416+
.map_err(PasskeyError::from)?;
410417
let mut tx = app.database.begin().await?;
411418
if app.invite_required
412419
&& !tx
@@ -470,7 +477,7 @@ impl PasskeyApplication {
470477
let (mut challenge, state) = app
471478
.webauthn
472479
.start_passkey_authentication(&Self::decode(keys)?)
473-
.map_err(PasskeyError::Webauthn)?;
480+
.map_err(PasskeyError::from)?;
474481
let ceremony = if let Some(user_id) = user_id {
475482
Ceremony::Login(LoginCeremony { user_id, state })
476483
} else {
@@ -485,7 +492,7 @@ impl PasskeyApplication {
485492
let (mut challenge, state) = app
486493
.webauthn
487494
.start_discoverable_authentication()
488-
.map_err(PasskeyError::Webauthn)?;
495+
.map_err(PasskeyError::from)?;
489496
challenge.mediation = None;
490497
(Ceremony::Discover(DiscoverCeremony { state }), challenge)
491498
};
@@ -512,7 +519,7 @@ impl PasskeyApplication {
512519
let (uuid, _) = app
513520
.webauthn
514521
.identify_discoverable_authentication(&input.credential)
515-
.map_err(PasskeyError::Webauthn)?;
522+
.map_err(PasskeyError::from)?;
516523
let user_id = UserId::from_uuid(uuid).map_err(PasskeyError::InvalidUserHandle)?;
517524
let keys: Vec<DiscoverableKey> = app
518525
.keys(user_id)
@@ -543,7 +550,7 @@ impl PasskeyApplication {
543550
let (challenge, state) = app
544551
.webauthn
545552
.start_passkey_authentication(&app.keys(session.user_id).await?)
546-
.map_err(PasskeyError::Webauthn)?;
553+
.map_err(PasskeyError::from)?;
547554
Ok(app
548555
.start(
549556
&headers,
@@ -654,7 +661,7 @@ impl PasskeyApplication {
654661
let key = app
655662
.webauthn
656663
.finish_passkey_registration(&input.credential, &state)
657-
.map_err(PasskeyError::Webauthn)?;
664+
.map_err(PasskeyError::from)?;
658665
let mut tx = app.database.begin().await?;
659666
Self::lock_management(&mut tx, &session).await?;
660667
Self::insert_passkey(&mut tx, user_id, key, friendly_name).await?;

0 commit comments

Comments
 (0)