Skip to content

Commit afc69b2

Browse files
committed
fix(auth): handle sign-in no longer lists credential IDs that link a renamed account to its old handle
Handle sign-in answered with allowCredentials holding every active passkey's ID. The IDs survive a rename, so anyone who recorded them could find the account's new handle, and they also gave away how many passkeys it has and often their provider. Handle sign-in, and a related origin's sign-in for a hinted account, now start a discoverable ceremony and check that the authenticator names the account they were for. Registration requires a discoverable credential, so every passkey can still sign in. The choice is recorded in docs/dev/security/handles.md. Closes #297
1 parent b8f029e commit afc69b2

14 files changed

Lines changed: 219 additions & 127 deletions

File tree

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,16 @@ pub(super) struct RegistrationCeremony {
4343
pub(super) state: PasskeyRegistration,
4444
}
4545

46-
/// Authentication against an already identified account's credentials.
46+
/// Sign-in to an account named before the challenge, by its handle or a client's
47+
/// hint. It lists none of the account's credentials, whose IDs would link the
48+
/// account across renames (`docs/dev/security/handles.md`): the authenticator
49+
/// names the account, which must be the one named here.
4750
#[derive(Serialize, Deserialize)]
4851
pub(super) struct LoginCeremony {
4952
/// Account named when authentication started.
5053
pub(super) user_id: UserId,
51-
/// Server-side challenge and credential snapshot.
52-
pub(super) state: PasskeyAuthentication,
54+
/// Server-side challenge, listing no credentials.
55+
pub(super) state: DiscoverableAuthentication,
5356
}
5457

5558
/// Account-picker authentication whose account is learned from the signed response.

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ pub(crate) enum PasskeyError {
7171
/// Handle sign-in named a handle without an active account or passkey.
7272
#[error("no active account has this handle")]
7373
UnknownHandle,
74+
/// The passkey names another account than the one its sign-in was for.
75+
#[error("passkey belongs to another account than the one named")]
76+
OtherAccount,
7477
/// The account, or the credential it signed with, was disabled or removed.
7578
#[error("active account and credential required")]
7679
AccountInactive,
@@ -187,6 +190,9 @@ impl From<PasskeyError> for ApiError {
187190
error @ PasskeyError::InvalidUserHandle(_) => {
188191
error.rejected("This passkey does not belong to an Origin Account.")
189192
}
193+
error @ PasskeyError::OtherAccount => error.rejected(
194+
"This passkey signs in to another account. Choose one for the account you're signing in to.",
195+
),
190196
error @ PasskeyError::UserVerificationRequired => error
191197
.rejected("Your authenticator must verify you with a PIN, fingerprint or face."),
192198
error @ PasskeyError::CounterRegression => error.rejected(
@@ -222,6 +228,7 @@ impl PasskeyError {
222228
| Self::SignedForAnotherOrigin
223229
| Self::ClientData(_)
224230
| Self::InvalidUserHandle(_)
231+
| Self::OtherAccount
225232
| Self::UserVerificationRequired
226233
| Self::CredentialRevoked => Some(SignInFailure::InvalidCredential),
227234
Self::Handle(_)

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

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ impl PasskeyApplication {
241241
Ok(())
242242
}
243243

244-
/// Starts handle-based authentication or lets the authenticator identify the account.
244+
/// Starts sign-in to the account a handle names, or lets the authenticator
245+
/// identify the account. Either way the challenge lists no credentials.
245246
///
246247
/// Handles are public (`docs/dev/security/handles.md`), so a handle without an active
247248
/// account or passkey is refused at once, before any ceremony. Text that is not a
@@ -251,40 +252,36 @@ impl PasskeyApplication {
251252
headers: HeaderMap,
252253
Json(input): Json<LoginStart>,
253254
) -> Result<Response, ApiError> {
254-
let (ceremony, challenge) =
255-
if let Some(input) = input.handle.filter(|handle| !handle.is_empty()) {
256-
let keys = match Handle::parse(&input) {
257-
Ok(handle) => {
258-
app.database
259-
.passkeys()
260-
.list_active_by_handle(&handle)
261-
.await?
262-
}
263-
Err(_) => Vec::new(),
264-
};
265-
let user_id = keys
266-
.first()
267-
.map(|key| key.user_id)
268-
.ok_or(PasskeyError::UnknownHandle)?;
269-
let (challenge, state) = app
270-
.webauthn
271-
.start_passkey_authentication(&Self::decode(keys)?)
272-
.map_err(PasskeyError::from)?;
273-
(Ceremony::Login(LoginCeremony { user_id, state }), challenge)
274-
} else {
275-
let (challenge, state) = app.discoverable()?;
276-
(Ceremony::Discover(state), challenge)
255+
let (challenge, state) = app.discoverable()?;
256+
let ceremony = if let Some(input) = input.handle.filter(|handle| !handle.is_empty()) {
257+
let keys = match Handle::parse(&input) {
258+
Ok(handle) => {
259+
app.database
260+
.passkeys()
261+
.list_active_by_handle(&handle)
262+
.await?
263+
}
264+
Err(_) => Vec::new(),
277265
};
266+
let user_id = keys
267+
.first()
268+
.map(|key| key.user_id)
269+
.ok_or(PasskeyError::UnknownHandle)?;
270+
Ceremony::Login(LoginCeremony { user_id, state })
271+
} else {
272+
Ceremony::Discover(DiscoverCeremony { state })
273+
};
278274
Ok(app.start(&headers, ceremony, challenge).await?)
279275
}
280276

281-
/// Starts an account-picker sign-in, whose account the authenticator names.
277+
/// Starts a sign-in whose account the authenticator names, listing no
278+
/// credentials.
282279
pub(super) fn discoverable(
283280
&self,
284-
) -> Result<(RequestChallengeResponse, DiscoverCeremony), PasskeyError> {
281+
) -> Result<(RequestChallengeResponse, DiscoverableAuthentication), PasskeyError> {
285282
let (mut challenge, state) = self.webauthn.start_discoverable_authentication()?;
286283
challenge.mediation = None;
287-
Ok((challenge, DiscoverCeremony { state }))
284+
Ok((challenge, state))
288285
}
289286

290287
/// Verifies the assertion and latest credential state before signing the browser in.
@@ -320,41 +317,44 @@ impl PasskeyApplication {
320317

321318
/// Checks `credential` against a sign-in `ceremony` with `webauthn`, and
322319
/// returns the account it signs in to with `WebAuthn`'s verdict, which
323-
/// [`Self::accept_credential`] takes. A discoverable credential that names no
324-
/// account is rejected, and recorded, here.
320+
/// [`Self::accept_credential`] takes. A credential that names no account, or
321+
/// another account than the ceremony named, is rejected, and recorded, here.
325322
pub(super) async fn verify(
326323
&self,
327324
webauthn: &Webauthn,
328325
context: RequestContext,
329326
ceremony: AuthenticationCeremony,
330327
credential: &PublicKeyCredential,
331328
) -> Result<(UserId, Result<AuthenticationResult, WebauthnError>), PasskeyError> {
332-
match ceremony {
333-
AuthenticationCeremony::Login(LoginCeremony { user_id, state }) => Ok((
329+
let (named, state) = match ceremony {
330+
AuthenticationCeremony::Login(LoginCeremony { user_id, state }) => {
331+
(Some(user_id), state)
332+
}
333+
AuthenticationCeremony::Discover(DiscoverCeremony { state }) => (None, state),
334+
};
335+
match self.discover(credential, named).await {
336+
Ok((user_id, keys)) => Ok((
334337
user_id,
335-
webauthn.finish_passkey_authentication(credential, &state),
338+
webauthn.finish_discoverable_authentication(credential, state, &keys),
336339
)),
337-
AuthenticationCeremony::Discover(DiscoverCeremony { state }) => {
338-
match self.discover(credential).await {
339-
Ok((user_id, keys)) => Ok((
340-
user_id,
341-
webauthn.finish_discoverable_authentication(credential, state, &keys),
342-
)),
343-
Err(error) => Err(self.reject(context, Actor::Anonymous, None, error).await),
344-
}
345-
}
340+
Err(error) => Err(self.reject(context, Actor::Anonymous, named, error).await),
346341
}
347342
}
348343

349-
/// Reads the account a discoverable credential names, and that account's keys.
344+
/// Reads the account a discoverable credential names, which must be `named`
345+
/// when the ceremony named one, and that account's keys.
350346
async fn discover(
351347
&self,
352348
credential: &PublicKeyCredential,
349+
named: Option<UserId>,
353350
) -> Result<(UserId, Vec<DiscoverableKey>), PasskeyError> {
354351
let (uuid, _) = self
355352
.webauthn
356353
.identify_discoverable_authentication(credential)?;
357354
let user_id = UserId::from_uuid(uuid).map_err(PasskeyError::InvalidUserHandle)?;
355+
if named.is_some_and(|named| named != user_id) {
356+
return Err(PasskeyError::OtherAccount);
357+
}
358358
let keys = self
359359
.keys(user_id)
360360
.await?

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,10 +346,11 @@ impl PasskeyApplication {
346346
name,
347347
exclusions,
348348
)?;
349-
// Prefer account-picker credentials while keeping security keys that need a username.
349+
// Sign-in lists no credentials (`docs/dev/security/handles.md`), so only
350+
// a discoverable credential can sign in: refuse to create any other.
350351
if let Some(selection) = challenge.public_key.authenticator_selection.as_mut() {
351-
selection.resident_key = Some(ResidentKeyRequirement::Preferred);
352-
selection.require_resident_key = false;
352+
selection.resident_key = Some(ResidentKeyRequirement::Required);
353+
selection.require_resident_key = true;
353354
}
354355
Ok((challenge, state))
355356
}

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

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use super::{
88
PasskeyApplication, PasskeyError,
99
acceptance::SignIn,
10-
ceremonies::{AuthenticationCeremony, LoginCeremony, RelatedCeremony},
10+
ceremonies::{AuthenticationCeremony, DiscoverCeremony, LoginCeremony, RelatedCeremony},
1111
types::{AuthenticationFinish, RelatedOrigins, RelatedStart},
1212
};
1313
use crate::{
@@ -71,8 +71,9 @@ impl PasskeyApplication {
7171
))
7272
}
7373

74-
/// Claims the page's client's pushed request and starts its sign-in: with the
75-
/// passkeys of the account an `id_token_hint` names, or else an account picker.
74+
/// Claims the page's client's pushed request and starts its sign-in: to the
75+
/// account an `id_token_hint` names, or else with an account picker. Neither
76+
/// lists the account's credentials.
7677
/// The claim and the ceremony commit together, so a refused start leaves the
7778
/// request for the redirect flow.
7879
pub(super) async fn start_related_login(
@@ -92,25 +93,17 @@ impl PasskeyApplication {
9293
.oidc
9394
.claim_inline(&mut tx, &client_id, &input.request_uri)
9495
.await?;
95-
let (challenge, sign_in) = if let Some(user_id) = claim.hinted_user_id {
96+
let (challenge, state) = app.discoverable()?;
97+
let sign_in = if let Some(user_id) = claim.hinted_user_id {
9698
// Read inside `tx`: a second connection per start could exhaust the pool.
97-
let keys = Self::decode(tx.passkeys().list_active(user_id).await?)?;
98-
if keys.is_empty() {
99+
if tx.passkeys().list_active(user_id).await?.is_empty() {
99100
return Err(ApiError::interaction_required(
100101
"The account this application asked for signs in without a passkey.",
101102
));
102103
}
103-
let (challenge, state) = app
104-
.webauthn
105-
.start_passkey_authentication(&keys)
106-
.map_err(PasskeyError::from)?;
107-
(
108-
challenge,
109-
AuthenticationCeremony::Login(LoginCeremony { user_id, state }),
110-
)
104+
AuthenticationCeremony::Login(LoginCeremony { user_id, state })
111105
} else {
112-
let (challenge, state) = app.discoverable()?;
113-
(challenge, AuthenticationCeremony::Discover(state))
106+
AuthenticationCeremony::Discover(DiscoverCeremony { state })
114107
};
115108
let ceremony = RelatedCeremony {
116109
origin,

0 commit comments

Comments
 (0)