Skip to content
Closed
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
88 changes: 80 additions & 8 deletions crates/walletkit-core/src/registration/approver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,8 @@ impl CheckedRegistration {
/// Call it only after the user consented and the platform's user verification succeeded,
/// also for an authenticator that is already registered (WIP-109 §3.7.3). `vault` is the
/// `CredentialStore::export_vault_for_backup` output, only if the user chose to share it; a
/// vault over [`MAX_VAULT_SIZE`] is left out. `authenticators` names the account's other
/// authenticators.
/// vault over [`MAX_VAULT_SIZE`] is rejected before any insertion. `authenticators` names
/// the account's other authenticators.
///
/// Unless the authenticator is already registered, this submits `InsertAuthenticator` and
/// tracks it until it is final on-chain or definitively failed. An insertion is never
Expand All @@ -455,7 +455,8 @@ impl CheckedRegistration {
///
/// Returns [`ApproverError::ApproverMismatch`] if `approver` is not the authenticator that
/// [`IncomingRegistration::check`] checked, or [`ApproverError::AlreadyHandled`] if the
/// registration was already decided.
/// registration was already decided. [`ApproverError::VaultTooLarge`] leaves the
/// session open: the host may retry with a supported vault or explicitly abort.
pub async fn approve(
&self,
approver: &Authenticator,
Expand All @@ -472,17 +473,20 @@ impl CheckedRegistration {
return Err(ApproverError::ApproverMismatch);
}

let channel = take(&self.channel)?;
let vault_omitted = vault
if vault
.as_ref()
.is_some_and(|data| data.len() > MAX_VAULT_SIZE);
.is_some_and(|data| data.len() > MAX_VAULT_SIZE)
{
return Err(ApproverError::VaultTooLarge);
}
let channel = take(&self.channel)?;
let mut result = RegistrationResult {
leaf_index: self.approver_leaf_index,
// Every valid slot encodes to the same single CBOR byte, so checking the size with
// this placeholder holds for the slot that registration assigns.
pubkey_id: 0,
authenticators,
vault: vault.filter(|_| !vault_omitted).map(|data| Vault {
vault: vault.map(|data| Vault {
format: PLAINTEXT_VAULT_FORMAT.to_string(),
data,
}),
Expand Down Expand Up @@ -516,7 +520,6 @@ impl CheckedRegistration {
reason: refusal.reason,
},
},
vault_omitted,
delivered,
})
}
Expand Down Expand Up @@ -1245,6 +1248,75 @@ mod tests {
assert!(checked.reject().await.unwrap());
}

#[test_case(false ; "new insertion")]
#[test_case(true ; "already registered")]
#[tokio::test]
async fn oversized_vault_is_rejected_without_consuming_the_session(
already_registered: bool,
) {
let (pending, code, _) = make_pending_registration().await;
let incoming = pending.authenticate(&code).await.unwrap();
let approver = make_test_authenticator([21; 32], 1).await;
let mut checked =
Arc::try_unwrap(incoming.check(&approver).await.unwrap()).unwrap();
assert!(matches!(checked.plan, RegistrationPlan::Insert { .. }));
if already_registered {
checked.plan = RegistrationPlan::AlreadyRegistered { pubkey_id: 1 };
}

let err = checked
.approve(&approver, Some(vec![0x42; MAX_VAULT_SIZE + 1]), Vec::new())
.await
.unwrap_err();
assert!(matches!(err, ApproverError::VaultTooLarge));
assert_eq!(
err.to_string(),
"the vault exceeds the registration size limit"
);
assert!(checked.abort().await.unwrap());
assert!(matches!(
checked.abort().await,
Err(ApproverError::AlreadyHandled)
));
}

#[test_case(None ; "without vault")]
#[test_case(Some(MAX_VAULT_SIZE) ; "exact size limit")]
#[tokio::test]
async fn supported_vault_sizes_can_be_approved_after_an_oversize_error(
size: Option<usize>,
) {
let (pending, code, _) = make_pending_registration().await;
let incoming = pending.authenticate(&code).await.unwrap();
let approver = make_test_authenticator([21; 32], 1).await;
let mut checked =
Arc::try_unwrap(incoming.check(&approver).await.unwrap()).unwrap();
checked.plan = RegistrationPlan::AlreadyRegistered { pubkey_id: 1 };
assert!(matches!(
checked
.approve(&approver, Some(vec![0x42; MAX_VAULT_SIZE + 1]), Vec::new())
.await,
Err(ApproverError::VaultTooLarge)
));

let outcome = checked
.approve(&approver, size.map(|size| vec![0x42; size]), Vec::new())
.await
.unwrap();
assert_eq!(
outcome.result,
ApprovalResult::Registered {
leaf_index: 1,
pubkey_id: 1
}
);
assert!(outcome.delivered);
assert!(matches!(
checked.abort().await,
Err(ApproverError::AlreadyHandled)
));
}

fn make_account(
slots: &[(world_id_core::EdDSAPublicKey, Address)],
) -> AccountAuthenticators {
Expand Down
4 changes: 4 additions & 0 deletions crates/walletkit-core/src/registration/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ pub enum ApproverError {
/// [`IncomingRegistration::check`](super::IncomingRegistration::check) checked.
#[error("the approver does not match the checked account")]
ApproverMismatch,
/// The supplied vault exceeds [`MAX_VAULT_SIZE`](super::MAX_VAULT_SIZE).
/// Nothing was submitted or sent; the host may retry or abort the checked session.
#[error("the vault exceeds the registration size limit")]
VaultTooLarge,
/// This step was already taken, so the registration has moved on or ended.
#[error("this registration step was already taken")]
AlreadyHandled,
Expand Down
4 changes: 4 additions & 0 deletions crates/walletkit-core/src/registration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
//! Every outcome is answered to the Requesting Authenticator, which verifies a successful result
//! with [`RegistrationRequester::verify`].
//!
//! [`CheckedRegistration::approve`] returns [`ApproverError::VaultTooLarge`] before
//! submission when a supplied vault exceeds [`MAX_VAULT_SIZE`]. The session stays open
//! for an explicit retry or abort; the vault is never silently omitted.
//!
//! The wire format and cryptography come from [`world_id_core::registration`].
//!
//! # Blocking key derivation
Expand Down
3 changes: 0 additions & 3 deletions crates/walletkit-core/src/registration/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,6 @@ pub enum RegistrationPlan {
pub struct ApprovalOutcome {
/// Whether the authenticator was registered.
pub result: ApprovalResult,
/// Whether the vault was left out because it exceeds [`MAX_VAULT_SIZE`](super::MAX_VAULT_SIZE).
/// The host must tell the user that the vault was not shared.
pub vault_omitted: bool,
/// Whether the response was delivered.
pub delivered: bool,
}
Expand Down
1 change: 0 additions & 1 deletion crates/walletkit-core/tests/registration_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ async fn a_proving_authenticator_registers_through_an_admin_authenticator() {
let approval = approval.unwrap();
let registered = registered.unwrap();
assert!(approval.delivered);
assert!(!approval.vault_omitted);
assert_eq!(
approval.result,
ApprovalResult::Registered {
Expand Down
11 changes: 11 additions & 0 deletions docs/accounts.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ database key in the device keystore; for the types involved, see the
In the browser, the app supplies the key itself; see
[Protect the database key](web/package.md#protect-the-database-key).

## Share a vault with a new authenticator

For WIP-109 registration, pass `CredentialStore::export_vault_for_backup` output to
`CheckedRegistration::approve` only after the user agrees to share it. A supplied
vault larger than `registration::MAX_VAULT_SIZE` returns `ApproverError::VaultTooLarge`
before `InsertAuthenticator` is submitted. The vault is never silently omitted.
The checked session stays open so the app can handle the error and retry, or call
`abort` to notify the requesting device. Passing `None` explicitly registers
without a vault. `ApprovalOutcome` reports the registration result and response
delivery; it has no `vault_omitted` field.

## From registration to stored credential

The following diagram shows the calls between the app, WalletKit, and the World ID
Expand Down
Loading