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
101 changes: 96 additions & 5 deletions pkgs/id/src/hosted_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
//! | `create_pairing_code` | pairing code (live); one-time maps | consumed check precedes live-map lookup (#4868); the #5644 bounded sweep runs before the collision loop, so expired codes no longer reserve their value; minting supersedes every earlier unexchanged code for the challenge (#4871); 10/account/hour | conforming |
//! | `exchange_pairing_code` | pairing code; challenge (read only) | removes the code before any check, so it is single-use even on failure; does not consume the challenge | conforming; see (a) |
//! | `request_delayed_rebind` | challenge; binding; rebind request | expiry: no state change (§3.4, #5048). Session mismatch and bad signature consume the challenge. `active` becomes `rebind_pending`; a pending request reserves the target key | conforming; `SameKey` and `NewKeyBoundElsewhere` after a valid proof leave the challenge consumed, as a successful proof already spent it |
//! | `cancel_rebind` | rebind request; binding | `pending` becomes `cancelled`; binding `rebind_pending` becomes `active` (re-attested) | known gap tracked by #5643: the request is marked `cancelled` before binding lookup and re-attestation, so `NoBinding` or `RecordEncoding` can fail after that mutation |
//! | `cancel_rebind` | rebind request; binding | `pending` becomes `cancelled`; binding `rebind_pending` becomes `active` (re-attested) | conforming (#5643): the restored binding and its attestation are prepared before either map is mutated, so `NoBinding` and `RecordEncoding` leave the request and binding unchanged |
//! | `rebind_with_old_key_authorization` | challenge; binding; rebind request | fast path, or early approval of a pending request (`pending` becomes `effected`). Expiry: no state change. Once proof verification starts, session mismatch or signature failure consumes the challenge; success swaps the key index and appends history | conforming; pre-proof request/binding errors do not consume the challenge; failures after a valid proof leave it consumed |
//! | `effect_due_rebind` | rebind request; binding | `pending` becomes `effected` once `effective_at` has passed; the target reservation is rechecked before any mutation; `NewKeyBoundElsewhere` leaves the request `pending` | conforming |
//! | `apply_rebind` (private) | binding; key index; history; rebind records | refuses a foreign key-index owner; appends history; releases the old key from the index; claims the new key | conforming |
Expand Down Expand Up @@ -821,6 +821,8 @@ pub struct HostedBindingService {
challenge_issued_at: HashMap<String, Vec<u64>>,
pairing_issued_at: HashMap<String, Vec<u64>>,
now_override: Option<u64>,
#[cfg(test)]
fail_attestation_for_test: bool,
}

impl HostedBindingService {
Expand Down Expand Up @@ -851,6 +853,8 @@ impl HostedBindingService {
challenge_issued_at: HashMap::new(),
pairing_issued_at: HashMap::new(),
now_override: None,
#[cfg(test)]
fail_attestation_for_test: false,
}
}

Expand Down Expand Up @@ -1446,7 +1450,10 @@ impl HostedBindingService {
///
/// [`RebindError::UnknownRebind`] / [`RebindError::RebindNotPending`]
/// for a dead request; [`RebindError::BadOldKeySignature`] when the
/// signature does not verify against the currently bound key.
/// signature does not verify against the currently bound key;
/// [`RebindError::NoBinding`] when the request's binding is absent; and
/// [`RebindError::RecordEncoding`] when the restored binding cannot be
/// attested. All errors leave the request and binding maps unchanged.
pub fn cancel_rebind(
&mut self,
rebind_id: &str,
Expand All @@ -1466,9 +1473,6 @@ impl HostedBindingService {
old_key_signature,
RebindError::BadOldKeySignature,
)?;
if let Some(stored) = self.rebind_requests.get_mut(rebind_id) {
stored.status = RebindRequestStatus::Cancelled;
}
let mut binding = self
.bindings
.get(&request.binding_id)
Expand All @@ -1480,6 +1484,14 @@ impl HostedBindingService {
.attest_bytes(&binding)
.ok_or(RebindError::RecordEncoding)?,
);

// All fallible work is complete. Only now commit the restored
// binding and the request's terminal status.
let stored = self
.rebind_requests
.get_mut(rebind_id)
.ok_or(RebindError::UnknownRebind)?;
stored.status = RebindRequestStatus::Cancelled;
self.bindings
.insert(binding.binding_id.clone(), binding.clone());
Ok(binding)
Expand Down Expand Up @@ -1872,6 +1884,10 @@ impl HostedBindingService {
}

fn attest_bytes(&self, record: &BindingRecord) -> Option<[u8; 64]> {
#[cfg(test)]
if self.fail_attestation_for_test {
return None;
}
let digest = record_digest(record)?;
Some(self.service_key.sign(&digest).to_bytes())
}
Expand Down Expand Up @@ -2772,6 +2788,81 @@ mod tests {
);
}

#[test]
fn delayed_rebind_cancel_no_binding_leaves_request_and_indexes_unchanged() {
let mut service = service();
let binding = bind(&mut service, SUB, 21);
let new_key = root_key(22);
let (challenge_id, proof) = signed_challenge(&mut service, SUB, &new_key);
let (request, _) = service
.request_delayed_rebind(SUB, &challenge_id, &proof)
.unwrap();

// Preserve the request while creating the post-verification lookup
// failure that the original implementation handled non-atomically.
service.bindings.remove(&binding.binding_id).unwrap();
let requests_before = service.rebind_requests.clone();
let bindings_before = service.bindings.clone();
let by_sub_before = service.binding_by_sub.clone();
let by_iroh_before = service.binding_by_iroh.clone();
let old_key = root_key(21);
let cancel = old_key.sign(&rebind_cancel_bytes(&request.rebind_id));

assert_eq!(
service.cancel_rebind(&request.rebind_id, &cancel.to_bytes()),
Err(RebindError::NoBinding)
);
assert_eq!(service.rebind_requests, requests_before);
assert_eq!(service.bindings, bindings_before);
assert_eq!(service.binding_by_sub, by_sub_before);
assert_eq!(service.binding_by_iroh, by_iroh_before);
assert!(!service.bindings.contains_key(&binding.binding_id));
assert_eq!(
service.rebind_request(&request.rebind_id).unwrap().status,
RebindRequestStatus::Pending
);
}

#[test]
fn delayed_rebind_cancel_record_encoding_leaves_request_and_binding_unchanged() {
let mut service = service();
let binding = bind(&mut service, SUB, 21);
let new_key = root_key(22);
let (challenge_id, proof) = signed_challenge(&mut service, SUB, &new_key);
let (request, _) = service
.request_delayed_rebind(SUB, &challenge_id, &proof)
.unwrap();

let requests_before = service.rebind_requests.clone();
let bindings_before = service.bindings.clone();
let by_sub_before = service.binding_by_sub.clone();
let by_iroh_before = service.binding_by_iroh.clone();
let old_key = root_key(21);
let cancel = old_key.sign(&rebind_cancel_bytes(&request.rebind_id));
service.fail_attestation_for_test = true;

assert_eq!(
service.cancel_rebind(&request.rebind_id, &cancel.to_bytes()),
Err(RebindError::RecordEncoding)
);
assert_eq!(service.rebind_requests, requests_before);
assert_eq!(service.bindings, bindings_before);
assert_eq!(service.binding_by_sub, by_sub_before);
assert_eq!(service.binding_by_iroh, by_iroh_before);
assert_eq!(
service.binding_for_sub(SUB).unwrap().binding_id,
binding.binding_id
);
assert_eq!(
service.rebind_request(&request.rebind_id).unwrap().status,
RebindRequestStatus::Pending
);
assert_eq!(
service.binding_for_sub(SUB).unwrap().status,
BindingStatus::RebindPending
);
}

#[test]
fn delayed_rebind_effects_at_effective_at_with_signed_publication() {
let mut service = service();
Expand Down
Loading