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(cua-compat): name root to a guest agent that switches users itself (#6459)
injaneityc2b7a906cac6
diff --git a/libs/cua/crates/cua-compat/src/daytona/toolbox.rs b/libs/cua/crates/cua-compat/src/daytona/toolbox.rsindex bbc545d618..20d1f5cb89 100644--- a/libs/cua/crates/cua-compat/src/daytona/toolbox.rs+++ b/libs/cua/crates/cua-compat/src/daytona/toolbox.rs@@ -1849,7 +1849,7 @@ pub(super) async fn guest_bytes(/// Whether file operations for `user` must go through `sudo`.fn switched(driver: &str, user: &str) -> bool {- driver != "root" && user != driver+ !crate::guest::without_sudo(driver) && user != driver}async fn create_folder(@@ -1881,7 +1881,7 @@ async fn create_folder(Err(e) => e,};}- let from = if driver == "root" && user != "root" {+ let from = if crate::guest::without_sudo(&driver) && user != "root" {crate::guestfs::first_missing(&c, &path).await} else {Nonediff --git a/libs/cua/crates/cua-compat/src/guest.rs b/libs/cua/crates/cua-compat/src/guest.rsindex 9a8443bce1..76bd50320c 100644--- a/libs/cua/crates/cua-compat/src/guest.rs+++ b/libs/cua/crates/cua-compat/src/guest.rs@@ -20,14 +20,15 @@ impl Drivers {}// An agent that switches users itself (`process.run_as` mode `root`// or `capabilities`, which also covers gVisor where `sudo` cannot- // work) is as good as root: no `sudo` anywhere.+ // work) needs no `sudo`, but it may not be root itself: every+ // switch, to root too, names the user (see [`as_user`]).if native_run_as(c).await {self.0.lock().await.insert(id.to_string(), "root".into());return "root".into();}let user = match c.run(Command::new("id").arg("-un")).await {Ok(o) if o.success() => String::from_utf8_lossy(&o.stdout).trim().to_string(),- _ => "root".to_string(),+ _ => UNNAMED.to_string(),};self.0.lock().await.insert(id.to_string(), user.clone());user@@ -38,6 +39,16 @@ impl Drivers {}}+/// What [`Drivers::of`] reports for an agent that cannot say who it runs as+/// (no `id`, as on Windows). It cannot switch users either: root work runs+/// as the agent, unnamed, and nothing goes through `sudo`.+pub const UNNAMED: &str = "";++/// Whether a guest agent running as `driver` acts for users without `sudo`.+pub fn without_sudo(driver: &str) -> bool {+ driver == "root" || driver == UNNAMED+}+/// Whether the guest agent switches users itself (feature `process.run_as`,/// attribute `mode` = `root` | `capabilities`); older agents have no such/// feature and get the `sudo` path.@@ -67,12 +78,20 @@ pub fn as_user(command: String,args: Vec<String>,) -> (String, Vec<String>, String) {- if user.is_empty() || user == driver {+ if user.is_empty() || (driver == UNNAMED && user == "root") {return (command, args, String::new());}+ if driver == UNNAMED {+ return (command, args, user.to_string());+ }+ // "root" is an agent that switches users itself, which in the Linux+ // images is `cua` holding CAP_SETUID: an unnamed user would be `cua`.if driver == "root" {return (command, args, user.to_string());}+ if user == driver {+ return (command, args, String::new());+ }let mut a = vec!["-n".to_string(),"-H".to_string(),@@ -96,7 +115,7 @@ pub fn as_user_in(args: Vec<String>,cwd: String,) -> (String, Vec<String>, String, String) {- if user.is_empty() || user == driver || driver == "root" {+ if user.is_empty() || user == driver || without_sudo(driver) {let (c, a, u) = as_user(driver, user, command, args);return (c, a, u, cwd);}@@ -255,6 +274,15 @@ mod tests {assert_eq!(u, "");let (c, _, u) = as_user("root", "user", "/bin/bash".into(), vec![]);assert_eq!((c.as_str(), u.as_str()), ("/bin/bash", "user"));+ // A driver that switches by capability is not root itself: root is+ // named, or the command runs as the agent's own user.+ let (c, _, u) = as_user("root", "root", "/bin/sh".into(), vec![]);+ assert_eq!((c.as_str(), u.as_str()), ("/bin/sh", "root"));+ // An agent that cannot say who it is cannot switch: root runs as it.+ let (c, _, u) = as_user(UNNAMED, "root", "/bin/sh".into(), vec![]);+ assert_eq!((c.as_str(), u.as_str()), ("/bin/sh", ""));+ let (c, _, u) = as_user(UNNAMED, "user", "/bin/sh".into(), vec![]);+ assert_eq!((c.as_str(), u.as_str()), ("/bin/sh", "user"));let (c, _, u) = as_user("cua", "cua", "/bin/bash".into(), vec![]);assert_eq!((c.as_str(), u.as_str()), ("/bin/bash", ""));// The directory is entered after the switch.diff --git a/libs/cua/crates/cua-compat/src/guestfs.rs b/libs/cua/crates/cua-compat/src/guestfs.rsindex 3deccfae6d..cff9c89b64 100644--- a/libs/cua/crates/cua-compat/src/guestfs.rs+++ b/libs/cua/crates/cua-compat/src/guestfs.rs@@ -140,8 +140,8 @@ impl<'a> Writer<'a> {user: &str,driver: &str,) -> Result<Writer<'a>, FsError> {- let staged = driver != "root";- let created_from = if driver == "root" && !user.is_empty() && user != "root" {+ let staged = !crate::guest::without_sudo(driver);+ let created_from = if !staged && !user.is_empty() && user != "root" {first_missing(c, path).await} else {None@@ -158,6 +158,12 @@ impl<'a> Writer<'a> {create_parents: true,expected_size: 0,expected_sha256: String::new(),+ // An agent that switches users itself may not be root: name it.+ run_as: if driver == "root" {+ "root".into()+ } else {+ String::new()+ },..Default::default()};let sink = if c.transport() == Transport::Native {diff --git a/libs/cua/crates/cua-compat/tests/gateway.rs b/libs/cua/crates/cua-compat/tests/gateway.rsindex 070ef28696..9c5f73a75a 100644--- a/libs/cua/crates/cua-compat/tests/gateway.rs+++ b/libs/cua/crates/cua-compat/tests/gateway.rs@@ -12,6 +12,7 @@ struct Gw {gw: Gateway,http: reqwest::Client,base: String,+ mock: Arc<MockBackend>,_dir: tempfile::TempDir,}@@ -26,12 +27,13 @@ async fn start(ports: &[(u16, String)]) -> Gw {o.host_port = 0;o.dir = Some(dir.path().to_path_buf());o.create_default_user = false;- let gw = Gateway::start(o, Some(backend)).await.unwrap();+ let gw = Gateway::start(o, Some(backend.clone())).await.unwrap();let base = format!("http://{}", gw.api_addr);Gw {gw,http: reqwest::Client::new(),base,+ mock: backend,_dir: dir,}}@@ -229,6 +231,39 @@ async fn process_start_streams_connect_envelopes() {assert_eq!(last.0 & 0x02, 0x02, "ends with an end-of-stream envelope");}+#[tokio::test]+async fn a_root_command_names_root_to_an_agent_that_switches_users() {+ // The mock agent is not root and switches users by capability (as the+ // Linux image's `cua` agent does): a command sent without a user runs+ // as the agent, so `root` must be named.+ use base64::Engine;+ let g = start(&[]).await;+ let sb = g.sandbox(json!({"templateID": "base"})).await;+ let body = envelope(&json!({+ "process": {"cmd": "/bin/bash", "args": ["-l", "-c", "echo hi"], "envs": {}},+ "stdin": false+ }));+ let basic = base64::engine::general_purpose::STANDARD.encode("root:");+ let r = g+ .envd(&sb, "process.Process/Start")+ .header("content-type", "application/connect+json")+ .header("authorization", format!("Basic {basic}"))+ .body(body)+ .send()+ .await+ .unwrap();+ assert_eq!(r.status(), 200);+ let _ = r.bytes().await.unwrap();+ let name = g.mock.created.lock().await[0].name.clone();+ let started = g.mock.state(&name).await.unwrap().started_processes();+ let run = started+ .iter()+ .filter_map(|p| p.config.as_ref())+ .find(|c| c.args.iter().any(|a| a == "echo hi"))+ .expect("the command reached the agent");+ assert_eq!(run.user, "root");+}+#[tokio::test]async fn unary_errors_are_connect_json() {let g = start(&[]).await;@@ -279,6 +314,10 @@ async fn files_round_trip_and_signed_urls() {w,json!([{"name": "x.txt", "type": "file", "path": "/tmp/x.txt"}]));+ // Written as root, not as the agent's own user.+ let name = g.mock.created.lock().await[0].name.clone();+ let owner = g.mock.state(&name).await.unwrap().file_owner("/tmp/x.txt");+ assert_eq!(owner.as_deref(), Some("root"));// A signed URL carries neither the sandbox id nor the token.let token = sb["envdAccessToken"].as_str().unwrap();let sig = format!(