diff --git a/pkgs/id/src/hosted_binding.rs b/pkgs/id/src/hosted_binding.rs index ae39801b53..bf38cc244b 100644 --- a/pkgs/id/src/hosted_binding.rs +++ b/pkgs/id/src/hosted_binding.rs @@ -424,7 +424,9 @@ pub struct Challenge { } /// Short-lived pairing code for the own-node variant (ยง3.3b): single-use, -/// expires with the challenge, rate-limited per account. +/// expires with the challenge, rate-limited per account. At most one live +/// code exists per challenge: minting a new code supersedes every earlier +/// unexchanged code for the same challenge (#4871). #[derive(Clone, Debug, PartialEq, Eq)] struct PairingCode { /// The pending ceremony's challenge. @@ -1082,7 +1084,9 @@ impl HostedBindingService { /// Variant (b), own-node, browser side: mint a short-lived pairing code /// tied to the pending ceremony. The browser displays it; the user /// enters it on their node. Single-use, expires with the challenge, - /// rate-limited per account. + /// rate-limited per account. Minting supersedes every previously + /// minted, unexchanged code for the same challenge (#4871), so asking + /// for a fresh code revokes the old one. /// /// # Errors /// @@ -1128,6 +1132,15 @@ impl HostedBindingService { break candidate; } }; + // Minting supersedes every earlier unexchanged code for this + // challenge (#4871): at most one live pairing code per ceremony, + // so "that code went to the wrong place, give me a new one" + // actually revokes the old one. The supersede runs after the new + // code is generated, while the old codes are still in the map, so + // the collision loop above can never re-issue a just-revoked + // string as its replacement. + self.pairing_codes + .retain(|_, existing| existing.challenge_id != challenge.challenge_id); self.pairing_codes.insert( code.clone(), PairingCode { @@ -1926,6 +1939,44 @@ mod tests { ); } + #[test] + fn minting_a_pairing_code_supersedes_earlier_codes_for_the_challenge() { + let mut service = service(); + signup(&mut service, SUB); + let iroh_id = root_key(17).verifying_key().to_bytes(); + let challenge = service.request_challenge(SUB, &iroh_id).unwrap(); + let other = service.request_challenge(SUB, &iroh_id).unwrap(); + + let code_a = service + .create_pairing_code(SUB, &challenge.challenge_id) + .unwrap(); + let other_code = service + .create_pairing_code(SUB, &other.challenge_id) + .unwrap(); + let code_b = service + .create_pairing_code(SUB, &challenge.challenge_id) + .unwrap(); + + // The superseded code no longer exchanges. The replacement is + // generated while the old code is still in the map, so the two + // strings are always distinct. + assert_ne!(code_a, code_b); + assert_eq!( + service.exchange_pairing_code(&code_a), + Err(CeremonyError::UnknownChallenge) + ); + // The surviving code exchanges and returns the challenge, once. + let exchanged = service.exchange_pairing_code(&code_b).unwrap(); + assert_eq!(exchanged.challenge_id, challenge.challenge_id); + assert_eq!( + service.exchange_pairing_code(&code_b), + Err(CeremonyError::UnknownChallenge) + ); + // Supersede is per-challenge: the other ceremony's code is untouched. + let other_exchanged = service.exchange_pairing_code(&other_code).unwrap(); + assert_eq!(other_exchanged.challenge_id, other.challenge_id); + } + #[test] fn wrong_key_consumes_and_retry_needs_a_fresh_challenge() { let mut service = service();