Skip to content
Merged
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
55 changes: 53 additions & 2 deletions pkgs/id/src/hosted_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
///
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down
Loading