Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.
Rust agentai-agentapplecomputer-usecomputer-use-agentcontainerizationcuadesktop-automationhacktoberfestlumemacosmanusoperatorswiftvirtualizationvirtualization-frameworkwindowswindows-sandbox
fix(relay): let the owner re-register a machine whose host is gone (#4553)
ddupont21b7bd6e0c58
diff --git a/libs/cua-spacesd/crates/cua-relay/src/api.rs b/libs/cua-spacesd/crates/cua-relay/src/api.rsindex 4a51f3f414..b7fe092cbf 100644--- a/libs/cua-spacesd/crates/cua-relay/src/api.rs+++ b/libs/cua-spacesd/crates/cua-relay/src/api.rs@@ -149,6 +149,14 @@ fn directory_error(e: DirectoryError) -> Response {error(status, e.to_string())}+/// A connected machine that heartbeated within this many seconds is live+/// (machines heartbeat every 15 s): re-registering it needs its machine token+/// or an enrolled device. Past it, the owner's account token is enough.+pub const LIVE_HEARTBEAT_SECS: u64 = 45;++/// `tracing` target of the re-register decision lines.+pub const REGISTER_LOG_TARGET: &str = "cua_relay::register";+/// Header a host re-running setup sends its current machine token in, so/// rotating an existing machine's token needs the machine (or an enrolled/// device), not just an account session.@@ -354,14 +362,41 @@ async fn register(let existed = existing.is_some();// Registering a new machine needs only the account (hosting never// enrolls the host as a client). Re-registering an existing one rotates- // its token and can change its allowlist, so it also needs the current- // machine token (the host re-running setup) or an enrolled device.+ // its token, which cuts the running host off, so while that host is+ // live it also needs the current machine token (the host re-running+ // setup) or an enrolled device: a stray account session cannot knock a+ // live host offline or take it over. A record whose host is gone (a+ // setup that registered and then died, a reinstall that lost the+ // machine token) has nothing to protect, and its owner's account token+ // re-registers it, the way it registered it in the first place.if let Some(record) = &existing {- let by_machine = bearer(&headers, MACHINE_AUTHORIZATION_HEADER)- .and_then(|t| relay.directory.machine_for_token(t))- .is_some_and(|m| m.id == record.id);- if !by_machine && record.owner.id == who.account {- if let Err(m) = &device {+ if record.owner.id == who.account {+ let by_machine = bearer(&headers, MACHINE_AUTHORIZATION_HEADER)+ .and_then(|t| relay.directory.machine_for_token(t))+ .is_some_and(|m| m.id == record.id);+ let live = relay.machine(&record.id).filter(|m| {+ matches!(&m.registrant, Registrant::Account(owner) if owner == &record.owner.id)+ });+ let heartbeat_age = live.as_ref().map(|m| m.heartbeat_age().as_secs());+ let online = heartbeat_age.is_some_and(|age| age <= LIVE_HEARTBEAT_SECS);+ let path = if by_machine {+ "machine_token"+ } else if device.is_ok() {+ "enrolled_device"+ } else if !online {+ "stale_record"+ } else {+ "refused_live_machine"+ };+ tracing::info!(+ target: REGISTER_LOG_TARGET,+ path,+ connected = live.is_some(),+ heartbeat_age_secs = heartbeat_age,+ "machine re-register"+ );+ if path == "refused_live_machine" {+ let m = device.as_ref().err().cloned().unwrap_or_default();return error(StatusCode::FORBIDDEN,format!("re-registering this machine needs its machine token or an enrolled device ({m})"),diff --git a/libs/cua-spacesd/crates/cua-relay/src/server.rs b/libs/cua-spacesd/crates/cua-relay/src/server.rsindex 2b97eb3121..8989133edd 100644--- a/libs/cua-spacesd/crates/cua-relay/src/server.rs+++ b/libs/cua-spacesd/crates/cua-relay/src/server.rs@@ -187,6 +187,11 @@ pub(crate) struct Machine {}impl Machine {+ /// Time since the machine's last heartbeat.+ pub(crate) fn heartbeat_age(&self) -> Duration {+ self.last_heartbeat.lock().expect("heartbeat").elapsed()+ }+/// Cuts every client stream and forgets presence.pub(crate) fn cut_clients(&self) {let old = std::mem::take(&mut *self.clients_cut.lock().expect("cut"));diff --git a/libs/cua-spacesd/crates/cua-relay/tests/device_enrollment.rs b/libs/cua-spacesd/crates/cua-relay/tests/device_enrollment.rsindex 9d94042dbb..3be5555e67 100644--- a/libs/cua-spacesd/crates/cua-relay/tests/device_enrollment.rs+++ b/libs/cua-spacesd/crates/cua-relay/tests/device_enrollment.rs@@ -379,15 +379,21 @@ async fn clients_need_an_enrolled_device_after_the_grace_period() {);let r = get(&base, "/m/machine-aaaa/v1/anything", &ada, None).await;assert_eq!(r.status(), StatusCode::FORBIDDEN);- // Re-registering (token rotation) needs the machine token or a device.+ // The machine is not connected (its setup died, or a reinstall lost+ // its token): the owner's account token re-registers it and rotates+ // its token (tests/reregister.rs covers a live machine).let r = http().post(format!("{base}/v1/machines")).bearer_auth(&ada)- .json(&json!({"id": "machine-aaaa", "allow": ["[email protected]"]}))+ .json(&json!({"id": "machine-aaaa"})).send().await.unwrap();- assert_eq!(r.status(), StatusCode::FORBIDDEN);+ assert!(r.status().is_success(), "{}", r.status());+ let v: Value = r.json().await.unwrap();+ let rotated = v["machine_token"].as_str().unwrap().to_owned();+ assert_ne!(rotated, machine_token);+ let machine_token = rotated;let r = http().post(format!("{base}/v1/machines")).bearer_auth(&ada)diff --git a/libs/cua-spacesd/crates/cua-relay/tests/reregister.rs b/libs/cua-spacesd/crates/cua-relay/tests/reregister.rsnew file mode 100644index 0000000000..064cb109ca--- /dev/null+++ b/libs/cua-spacesd/crates/cua-relay/tests/reregister.rs@@ -0,0 +1,209 @@+// SPDX-License-Identifier: FSL-1.1-MIT+// Copyright (c) 2026 Cua AI, Inc.++//! Re-registering an existing machine (host setup run again): the owner's+//! account token alone re-registers a machine whose host is gone, while a+//! live host needs its machine token or an enrolled device, and another+//! account never can. Each decision logs its path, and no secret.++use std::io::Write;+use std::sync::{Arc, Mutex, OnceLock};+use std::time::Duration;++use cua_relay::devices::DevicePolicy;+use cua_relay::oidc::testing::FakeIssuer;+use cua_relay::oidc::{OidcConfig, OidcValidator};+use cua_relay::server::{Relay, RelayConfig};+use reqwest::StatusCode;+use serde_json::{json, Value};+use tokio_util::sync::CancellationToken;++const ISSUER: &str = "https://auth.test/realms/cua";++#[derive(Clone, Default)]+struct Buffer(Arc<Mutex<Vec<u8>>>);++impl Write for Buffer {+ fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {+ self.0.lock().unwrap().extend_from_slice(buf);+ Ok(buf.len())+ }+ fn flush(&mut self) -> std::io::Result<()> {+ Ok(())+ }+}++fn logs() -> &'static Buffer {+ static LOGS: OnceLock<Buffer> = OnceLock::new();+ LOGS.get_or_init(|| {+ let buffer = Buffer::default();+ let writer = buffer.clone();+ tracing_subscriber::fmt()+ .with_ansi(false)+ .with_writer(move || writer.clone())+ .init();+ buffer+ })+}++/// The re-register lines logged since the last call.+fn reregister_lines() -> Vec<String> {+ let mut buf = logs().0.lock().unwrap();+ let text = String::from_utf8_lossy(&buf).into_owned();+ buf.clear();+ text.lines()+ .filter(|l| l.contains("cua_relay::register"))+ .map(str::to_owned)+ .collect()+}++async fn start(issuer: &FakeIssuer) -> (Relay, String) {+ let relay = Relay::new(RelayConfig {+ oidc: Some(Arc::new(OidcValidator::with_jwks(+ OidcConfig::new(ISSUER),+ issuer.jwks(),+ ))),+ // Enrollment enforced, grace over: an account token alone has no+ // enrolled device behind it.+ device_policy: DevicePolicy {+ grace_secs: 0,+ ..DevicePolicy::default()+ },+ ..RelayConfig::default()+ });+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();+ let base = format!("http://{}", listener.local_addr().unwrap());+ let serving = relay.clone();+ tokio::spawn(async move {+ let _ = serving.serve(listener, std::future::pending()).await;+ });+ (relay, base)+}++async fn register(+ base: &str,+ account: &str,+ machine_token: Option<&str>,+ id: &str,+) -> reqwest::Response {+ let mut r = reqwest::Client::new()+ .post(format!("{base}/v1/machines"))+ .bearer_auth(account)+ .json(&json!({"id": id, "name": id}));+ if let Some(t) = machine_token {+ r = r.header("x-cua-machine-authorization", format!("Bearer {t}"));+ }+ r.send().await.unwrap()+}++async fn token_of(r: reqwest::Response) -> String {+ assert!(r.status().is_success(), "{}", r.status());+ let v: Value = r.json().await.unwrap();+ v["machine_token"].as_str().unwrap().to_owned()+}++/// Connects `id` as a host with `token`; returns once the relay sees it.+async fn connect(relay: &Relay, base: &str, id: &str, token: &str) -> CancellationToken {+ let unused = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();+ let local = unused.local_addr().unwrap();+ drop(unused);+ let mut join = cua_relay::client::JoinConfig::new(+ base.replace("http://", "ws://"),+ token.to_owned(),+ id.to_owned(),+ local,+ );+ join.heartbeat = Duration::from_secs(1);+ let stop = CancellationToken::new();+ tokio::spawn(cua_relay::client::run(join, stop.clone()));+ for _ in 0..200 {+ if relay.machine_ids().contains(&id.to_owned()) {+ return stop;+ }+ tokio::time::sleep(Duration::from_millis(25)).await;+ }+ panic!("{id} did not connect");+}++#[tokio::test]+async fn reregistering_needs_proof_only_while_the_host_is_live() {+ logs();+ let issuer = FakeIssuer::new(ISSUER);+ let (relay, base) = start(&issuer).await;+ let ada = issuer.token("ada-sub-4c1f", Some("[email protected]"), "cua-relay", 300);+ let eve = issuer.token("eve-sub-9d2e", Some("[email protected]"), "cua-relay", 300);+ let id = format!("m{}", uuid::Uuid::new_v4().simple());+ let first = token_of(register(&base, &ada, None, &id).await).await;+ reregister_lines();++ // Stale record, same owner, host offline (setup died / reinstall lost+ // the token): the account token alone re-registers it and rotates the+ // token; the old one stops working.+ let second = token_of(register(&base, &ada, None, &id).await).await;+ assert_ne!(second, first);+ let lines = reregister_lines();+ assert_eq!(lines.len(), 1, "{lines:?}");+ assert!(lines[0].contains("path=\"stale_record\""), "{}", lines[0]);+ assert!(lines[0].contains("connected=false"), "{}", lines[0]);+ let r = register(&base, &ada, Some(&first), &id).await;+ assert_eq!(+ r.status(),+ StatusCode::OK,+ "an unknown machine token is no proof, but the host is still offline"+ );+ let second = token_of(r).await;+ let lines = reregister_lines();+ assert!(lines[0].contains("path=\"stale_record\""), "{}", lines[0]);++ // Another account: refused, live or not.+ let r = register(&base, &eve, None, &id).await;+ assert_eq!(r.status(), StatusCode::CONFLICT);+ assert!(+ reregister_lines().is_empty(),+ "not the owner: no re-register path"+ );++ // The host connects and heartbeats: the account token alone is refused+ // (no enrolled device), so a stray session cannot knock it offline.+ let stop = connect(&relay, &base, &id, &second).await;+ let r = register(&base, &ada, None, &id).await;+ assert_eq!(r.status(), StatusCode::FORBIDDEN);+ let body: Value = r.json().await.unwrap();+ assert!(+ body["error"]+ .as_str()+ .unwrap()+ .contains("needs its machine token or an enrolled device"),+ "{body}"+ );+ let lines = reregister_lines();+ assert_eq!(lines.len(), 1, "{lines:?}");+ assert!(+ lines[0].contains("path=\"refused_live_machine\""),+ "{}",+ lines[0]+ );+ assert!(lines[0].contains("connected=true"), "{}", lines[0]);+ assert!(relay.machine_ids().contains(&id), "still connected");+ // Another account cannot either.+ assert_eq!(+ register(&base, &eve, None, &id).await.status(),+ StatusCode::CONFLICT+ );++ // The host itself, with its machine token: allowed while live.+ let third = token_of(register(&base, &ada, Some(&second), &id).await).await;+ assert_ne!(third, second);+ let lines = reregister_lines();+ assert!(lines[0].contains("path=\"machine_token\""), "{}", lines[0]);+ stop.cancel();++ // No secret, id or identity in any re-register line.+ let all = lines.join("\n");+ for secret in [&ada, &eve, &first, &second, &third, &id] {+ assert!(!all.contains(secret.as_str()), "leaked {secret}");+ }+ for s in ["[email protected]", "ada-sub-4c1f", "eve-sub-9d2e"] {+ assert!(!all.contains(s), "leaked {s}");+ }+}