From 29663bd659b63696e9385dd959dbdf25f68c2c90 Mon Sep 17 00:00:00 2001 From: dzejkop Date: Wed, 18 Nov 2026 14:32:26 +0000 Subject: [PATCH] fix(registration)!: reject oversized vaults before insertion Co-Authored-By: GPT-6.1 Sol --- .../src/registration/approver.rs | 88 +++++++++++++++++-- .../walletkit-core/src/registration/errors.rs | 4 + crates/walletkit-core/src/registration/mod.rs | 4 + .../walletkit-core/src/registration/types.rs | 3 - .../tests/registration_integration.rs | 1 - docs/accounts.md | 11 +++ 6 files changed, 99 insertions(+), 12 deletions(-) diff --git a/crates/walletkit-core/src/registration/approver.rs b/crates/walletkit-core/src/registration/approver.rs index 1fcaef2c0..bff4ec2cc 100644 --- a/crates/walletkit-core/src/registration/approver.rs +++ b/crates/walletkit-core/src/registration/approver.rs @@ -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 @@ -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, @@ -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, }), @@ -516,7 +520,6 @@ impl CheckedRegistration { reason: refusal.reason, }, }, - vault_omitted, delivered, }) } @@ -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, + ) { + 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 { diff --git a/crates/walletkit-core/src/registration/errors.rs b/crates/walletkit-core/src/registration/errors.rs index ab05d616f..f1db6ea5e 100644 --- a/crates/walletkit-core/src/registration/errors.rs +++ b/crates/walletkit-core/src/registration/errors.rs @@ -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, diff --git a/crates/walletkit-core/src/registration/mod.rs b/crates/walletkit-core/src/registration/mod.rs index 10af5209d..a65613ace 100644 --- a/crates/walletkit-core/src/registration/mod.rs +++ b/crates/walletkit-core/src/registration/mod.rs @@ -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 diff --git a/crates/walletkit-core/src/registration/types.rs b/crates/walletkit-core/src/registration/types.rs index b10a3ad8b..347e9c278 100644 --- a/crates/walletkit-core/src/registration/types.rs +++ b/crates/walletkit-core/src/registration/types.rs @@ -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, } diff --git a/crates/walletkit-core/tests/registration_integration.rs b/crates/walletkit-core/tests/registration_integration.rs index cd5ae104c..1892b8bf5 100644 --- a/crates/walletkit-core/tests/registration_integration.rs +++ b/crates/walletkit-core/tests/registration_integration.rs @@ -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 { diff --git a/docs/accounts.md b/docs/accounts.md index 19599c865..b13e6156f 100644 --- a/docs/accounts.md +++ b/docs/accounts.md @@ -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