Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(host): sign-in as another account enrolls this computer with a new key (CUA-2293) (#5955)

cua-agents[bot]62d1dc3779c6
diff --git a/libs/cua/crates/cua-host/src/device.rs b/libs/cua/crates/cua-host/src/device.rs
index f506149452..119f70e9cd 100644
--- a/libs/cua/crates/cua-host/src/device.rs
+++ b/libs/cua/crates/cua-host/src/device.rs
@@ -481,6 +481,15 @@ impl DeviceAuth {
}
let key = self.key_or_create()?;
let enrollment = match self.register_key(&token, &key, name).await {
+ // A key the relay holds for another account (a different
+ // sign-in on this computer) can never enroll here either.
+ Err(Error::Conflict(m)) if m.contains("another account") => {
+ let (key, pkcs8) = DeviceKey::generate()?;
+ self.slot.save(&pkcs8)?;
+ // The new key has no record to keep a name from.
+ let name = if name.is_empty() { &self.name } else { name };
+ self.register_key(&token, &key, name).await?
+ }
Err(Error::PermissionDenied(m))
if m.contains("revoked") || m.contains("replaced by") =>
{
diff --git a/libs/cua/crates/cua-host/src/tests.rs b/libs/cua/crates/cua-host/src/tests.rs
index ed17bd161f..6c13f6bc71 100644
--- a/libs/cua/crates/cua-host/src/tests.rs
+++ b/libs/cua/crates/cua-host/src/tests.rs
@@ -1298,6 +1298,40 @@ async fn a_fresh_sign_in_enrolls_and_re_keys_this_machine() {
assert_eq!(approved.state, DeviceState::Enrolled);
}
+/// Signing in as another account on a computer whose device key belongs to
+/// the previous account enrolls with a new key instead of failing.
+#[tokio::test]
+async fn sign_in_as_another_account_replaces_the_key_of_the_previous_one() {
+ let relay = FakeRelay::start().await;
+ relay.add_account("acct-a", "user-a", Some("[email protected]"));
+ relay.add_account("acct-b", "user-b", Some("[email protected]"));
+ relay.require_devices(true);
+ relay.fresh_sign_in("user-a");
+ relay.fresh_sign_in("user-b");
+ let slot = Arc::new(MemoryKeySlot::default());
+ let on_this_mac = |token: &str| {
+ DeviceAuth::new(
+ &relay.url,
+ Arc::new(StaticToken(token.into())),
+ slot.clone(),
+ "Mac",
+ )
+ .unwrap()
+ .with_machine_id(Some("mac-1".into()))
+ };
+ let as_a = on_this_mac("acct-a");
+ as_a.enroll_after_sign_in().await.unwrap();
+ let old = as_a.device_id().unwrap().unwrap();
+
+ let as_b = on_this_mac("acct-b");
+ let r = as_b.enroll_after_sign_in().await.unwrap();
+ assert_eq!(r.device.state, DeviceState::Enrolled);
+ assert_ne!(r.device.id, old);
+ assert_eq!(r.device.name, "Mac");
+ assert_eq!(as_b.device_id().unwrap().unwrap(), r.device.id);
+ assert!(as_b.session().await.is_ok());
+}
+
/// Two builds on one machine that each made a key end up with one: the key
/// the relay enrolled wins, the other's record is retired.
#[tokio::test]