Fix cancellation of macOS user-verification prompts (#53790)

## Why

Keychain signing reconstructs its own authentication context, so invalidating the caller's context does not reliably cancel the context that owns the biometric prompt.

## What changed

Evaluate the stored key's access-control policy explicitly with `LAContext` before signing. On cancellation, invalidate that context and wait for its authorization callback before releasing the lifecycle lock, rejecting any late success. Reload the key with the authorized context and disable further interaction so signing cannot open another prompt.

## Testing

Update unit tests to cover waiting for cancellation completion and rejecting completed authorization after cancellation. Add an ignored macOS hardware test and setup instructions covering persisted Secure Enclave key reuse, signature verification, cancellation, retry signing, and cleanup.

GitOrigin-RevId: 4ea9756430c5649654e95e047d1666617503072c
This commit is contained in:
ben zhang
2026-10-13 22:40:57 +00:00
committed by copyberry
parent 6786dccbe7
commit 815ef9bbc4
10 changed files with 381 additions and 114 deletions
@@ -1,4 +1,4 @@
//! Exercises prompt teardown and late-result rejection without requiring biometric hardware.
//! Exercises cancellation completion and late-result rejection without biometric hardware.
use super::*;
use crate::UserVerificationCancellationReason;
@@ -7,34 +7,26 @@ use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
#[test]
fn cancellation_tears_down_the_prompt_on_the_context_owner_thread() {
fn cancellation_waits_for_completion_on_the_context_owner_thread() {
let guard = UserVerificationRequestGuard::default();
let queued = guard.clone();
let (started_tx, started_rx) = mpsc::channel();
let (dismissed_tx, dismissed_rx) = mpsc::channel();
let (exited_tx, exited_rx) = mpsc::channel();
let (done_tx, done_rx) = mpsc::sync_channel(0);
let (cancelled_tx, cancelled_rx) = mpsc::channel();
let owner = std::thread::spawn(move || {
let owner_thread = std::thread::current().id();
run_with_cancellation(
&queued,
move || {
started_tx.send(()).expect("authentication started");
dismissed_rx
.recv_timeout(Duration::from_secs(/*secs*/ 5))
.expect("prompt dismissed before the deadline");
exited_tx.send(()).expect("signer exited");
Ok("late signature")
},
|| {
assert_eq!(std::thread::current().id(), owner_thread);
dismissed_tx.send(()).expect("dismiss authentication");
},
)
wait_with_cancellation(&queued, done_rx, || {
assert_eq!(std::thread::current().id(), owner_thread);
cancelled_tx.send(()).expect("dismiss authentication");
})
});
started_rx
.recv_timeout(Duration::from_secs(/*secs*/ 5))
.expect("signer is waiting for authentication");
guard.cancel();
cancelled_rx
.recv_timeout(Duration::from_secs(/*secs*/ 5))
.expect("authentication invalidated");
assert!(!owner.is_finished());
done_tx
.send(Ok("late authorization"))
.expect("complete authentication");
assert_eq!(
owner.join().expect("owner returned"),
Err(UserVerificationError::Cancelled {
@@ -42,42 +34,37 @@ fn cancellation_tears_down_the_prompt_on_the_context_owner_thread() {
message: "the verification operation is no longer active".to_string(),
})
);
exited_rx.try_recv().expect("signer exited before return");
}
#[test]
fn completed_operation_does_not_cancel_its_context() {
let cancelled = AtomicBool::new(/*v*/ false);
let (done_tx, done_rx) = mpsc::channel();
done_tx
.send(Ok("authorization"))
.expect("complete authentication");
assert_eq!(
run_with_cancellation(
&UserVerificationRequestGuard::default(),
|| Ok("signature"),
|| cancelled.store(/*val*/ true, Ordering::Release),
),
Ok("signature")
wait_with_cancellation(&UserVerificationRequestGuard::default(), done_rx, || {
cancelled.store(/*val*/ true, Ordering::Release)
},),
Ok("authorization")
);
assert!(!cancelled.load(Ordering::Acquire));
}
#[test]
fn already_cancelled_operation_never_starts_a_signer() {
fn cancellation_rejects_already_completed_authorization() {
let guard = UserVerificationRequestGuard::default();
let (done_tx, done_rx) = mpsc::channel();
done_tx.send(Ok(())).expect("complete authentication");
guard.cancel();
let started = AtomicBool::new(/*v*/ false);
let result = run_with_cancellation(
&guard,
|| {
started.store(/*val*/ true, Ordering::Release);
Ok(())
},
|| panic!("no context needs cancellation"),
);
assert_eq!(
result,
wait_with_cancellation(&guard, done_rx, || panic!(
"authentication already completed"
)),
Err(UserVerificationError::Cancelled {
reason: UserVerificationCancellationReason::Interrupted,
message: "the verification operation is no longer active".to_string(),
})
);
assert!(!started.load(Ordering::Acquire));
}