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(cua-driver): isolate Computer History under CUA_DRIVER_RS_HOME (#5165)

injaneityfbab7c506cf9
diff --git a/.github/workflows/ci-rust-windows.yml b/.github/workflows/ci-rust-windows.yml
index af44ae7e6d..94c174b7b3 100644
--- a/.github/workflows/ci-rust-windows.yml
+++ b/.github/workflows/ci-rust-windows.yml
@@ -96,6 +96,13 @@ jobs:
- name: Run complete desktop-independent core tests
working-directory: libs/cua-driver/rust
run: cargo test -p cua-driver-core --lib --test recording_shutdown_errors --locked
+ - name: Run driver state isolation regressions
+ working-directory: libs/cua-driver/rust
+ env:
+ CUA_TEST_REQUIRE_DRIVER_BIN: "1"
+ run: |
+ cargo test -p cua-driver --test testkit_host_state_isolation_test --locked
+ if ($LASTEXITCODE -ne 0) { throw "driver state isolation tests failed with exit $LASTEXITCODE" }
- name: Run extension manager security regressions
working-directory: libs/cua-driver/rust
run: |
@@ -179,6 +186,8 @@ jobs:
if ($LASTEXITCODE -ne 0) { throw "SDK ABI tests failed with exit $LASTEXITCODE" }
cargo test -p cua-driver-sdk remote_ --lib --locked
if ($LASTEXITCODE -ne 0) { throw "remote SDK tests failed with exit $LASTEXITCODE" }
+ cargo test -p cua-driver-sdk driver_state_root_is_inherited_and_overridable --lib --locked
+ if ($LASTEXITCODE -ne 0) { throw "SDK driver state root tests failed with exit $LASTEXITCODE" }
cargo test -p cua-driver --bin cua-driver named_pipe_authentication_tests:: --locked
if ($LASTEXITCODE -ne 0) { throw "named-pipe authentication tests failed with exit $LASTEXITCODE" }
# The production binary embeds uiAccess=true and Windows correctly
diff --git a/libs/cua-driver/rust/README.md b/libs/cua-driver/rust/README.md
index c9581829b7..71828ca8ce 100644
--- a/libs/cua-driver/rust/README.md
+++ b/libs/cua-driver/rust/README.md
@@ -71,6 +71,17 @@ cargo test -p cua-driver-e2e --test harness_wpf_test -- --ignored --nocapture
See `crates/cua-driver-e2e/tests/README.md` for the desktop E2E suites and
`crates/cua-driver/tests/README.md` for the hermetic driver tests.
+Testkit-owned daemons use a temporary `CUA_DRIVER_RS_HOME` on every platform.
+A nonempty override also places Computer History data and its `admission.json`
+under `<CUA_DRIVER_RS_HOME>/computer-history`. Without an override (or with an
+empty value), History keeps its platform-specific default location. Existing
+data is not migrated, and installed-product admission checks still apply.
+On Windows this isolates driver state without redirecting `LOCALAPPDATA`,
+`APPDATA`, or `USERPROFILE`, which Windows app discovery needs. Explicit caller
+environment values take precedence; `SHARE_HOST_STATE` opts a testkit spawn out
+of isolation. Use separate roots for independent driver instances. This isolates
+files, not the operating system's credential store.
+
## Generated Outputs
- `target/`: Cargo output, ignored.
diff --git a/libs/cua-driver/rust/crates/cua-driver-sdk/src/embedded.rs b/libs/cua-driver/rust/crates/cua-driver-sdk/src/embedded.rs
index fa743005ae..373d63cc04 100644
--- a/libs/cua-driver/rust/crates/cua-driver-sdk/src/embedded.rs
+++ b/libs/cua-driver/rust/crates/cua-driver-sdk/src/embedded.rs
@@ -868,6 +868,7 @@ pub(crate) fn allowed_environment_name(name: &str) -> bool {
| "DBUS_SESSION_BUS_ADDRESS"
| "XAUTHORITY"
| "CUA_LOG"
+ | "CUA_DRIVER_RS_HOME"
| "CUA_DRIVER_RS_TELEMETRY_ENABLED"
| "CUA_TELEMETRY_ENABLED"
| "CUA_DRIVER_RS_UPDATE_CHECK"
@@ -1248,6 +1249,29 @@ mod tests {
assert!(allowed_environment_name("CUA_DRIVER_WINDOW_CHANGE_POLL_MS"));
}
+ #[test]
+ fn driver_state_root_is_inherited_and_overridable() {
+ let inherited = [("CUA_DRIVER_RS_HOME".into(), "host-state".into())];
+ for (overrides, expected) in [
+ (Vec::new(), "host-state"),
+ (
+ vec![EmbeddedEnvironmentVariable {
+ name: "CUA_DRIVER_RS_HOME".into(),
+ value: "isolated-state".into(),
+ }],
+ "isolated-state",
+ ),
+ ] {
+ let mut host_options = options(EmbeddedPermissionMode::Standard);
+ host_options.environment = overrides;
+ let validated = validate_options(host_options).expect("state root is supported");
+ let values = merge_safe_environment(inherited.clone(), &validated.environment);
+ assert_eq!(values.len(), 1);
+ assert_eq!(values[0].name, "CUA_DRIVER_RS_HOME");
+ assert_eq!(values[0].value, expected);
+ }
+ }
+
#[test]
fn telemetry_preferences_are_inherited_and_overridable() {
assert!(allowed_environment_name("CUA_DRIVER_RS_TELEMETRY_ENABLED"));
diff --git a/libs/cua-driver/rust/crates/cua-driver-testkit/src/host_state.rs b/libs/cua-driver/rust/crates/cua-driver-testkit/src/host_state.rs
index f1a9f2819f..d84a2eeb59 100644
--- a/libs/cua-driver/rust/crates/cua-driver-testkit/src/host_state.rs
+++ b/libs/cua-driver/rust/crates/cua-driver-testkit/src/host_state.rs
@@ -17,11 +17,7 @@
//! `shell:AppsFolder` enumeration depends on `LOCALAPPDATA` (a redirected
//! value makes Microsoft Edge unresolvable, `0x80070002`), and the shell
//! expands per-user known folders through `%USERPROFILE%`. The driver's
-//! `USERPROFILE`-derived state goes through its own `CUA_DRIVER_RS_HOME`
-//! override instead. Known limitation: the Windows Computer History root
-//! (`%LOCALAPPDATA%\cua-driver\computer-history`) has no separate driver
-//! override, so a Windows host with History admitted still leaks into test
-//! daemons.
+//! `USERPROFILE`-derived state goes through its own `CUA_DRIVER_RS_HOME` override instead.
//! - Linux pins `XDG_DATA_HOME` to the host data directory so user `.desktop`
//! entries remain discoverable after `HOME` moves.
//!
@@ -52,6 +48,7 @@ const STATE_VARIABLES: &[(&str, &str)] = &[("HOME", ""), ("CUA_DRIVER_RS_HOME",
#[cfg(not(target_os = "windows"))]
const STATE_VARIABLES: &[(&str, &str)] = &[
("HOME", ""),
+ ("CUA_DRIVER_RS_HOME", ".cua-driver"),
("XDG_CONFIG_HOME", ".config"),
("XDG_STATE_HOME", ".local/state"),
];
@@ -204,10 +201,18 @@ mod tests {
#[test]
fn caller_values_override_isolated_defaults() {
- let root = IsolatedStateRoot::for_env(&[("HOME", "/caller/home")]).expect("root");
+ let env = [
+ ("HOME", "/caller/home"),
+ ("CUA_DRIVER_RS_HOME", "/caller/driver"),
+ ];
+ let root = IsolatedStateRoot::for_env(&env).expect("root");
let mut command = Command::new("unused");
- apply_env(&mut command, Some(&root), &[("HOME", "/caller/home")]);
+ apply_env(&mut command, Some(&root), &env);
assert_eq!(value_of(&command, "HOME").as_deref(), Some("/caller/home"));
+ assert_eq!(
+ value_of(&command, "CUA_DRIVER_RS_HOME").as_deref(),
+ Some("/caller/driver")
+ );
}
#[test]
diff --git a/libs/cua-driver/rust/crates/cua-driver/src/history_runtime.rs b/libs/cua-driver/rust/crates/cua-driver/src/history_runtime.rs
index 54e807b1e2..709546823f 100644
--- a/libs/cua-driver/rust/crates/cua-driver/src/history_runtime.rs
+++ b/libs/cua-driver/rust/crates/cua-driver/src/history_runtime.rs
@@ -422,6 +422,13 @@ pub fn register_host_tools(registry: &mut ToolRegistry) {
}
pub fn history_root() -> PathBuf {
+ if let Some(home) = std::env::var_os("CUA_DRIVER_RS_HOME").filter(|value| !value.is_empty()) {
+ return PathBuf::from(home).join("computer-history");
+ }
+ default_history_root()
+}
+
+fn default_history_root() -> PathBuf {
#[cfg(target_os = "macos")]
{
let home = std::env::var_os("HOME")
@@ -501,7 +508,7 @@ mod tests {
#[test]
fn default_root_is_namespace_specific() {
- let root = history_root();
+ let root = default_history_root();
assert!(root.ends_with(format!(
"{}/computer-history",
crate::bundle::state_namespace()
diff --git a/libs/cua-driver/rust/crates/cua-driver/tests/embedded_host_sdk_mcp_test.rs b/libs/cua-driver/rust/crates/cua-driver/tests/embedded_host_sdk_mcp_test.rs
index 45d26fc728..2a43bcd903 100644
--- a/libs/cua-driver/rust/crates/cua-driver/tests/embedded_host_sdk_mcp_test.rs
+++ b/libs/cua-driver/rust/crates/cua-driver/tests/embedded_host_sdk_mcp_test.rs
@@ -46,7 +46,7 @@ fn isolated_driver_host(
.into_iter()
// The SDK forwards only its safe allowlist; without XDG overrides
// the driver derives its XDG state from the isolated HOME.
- .filter(|(name, _)| matches!(*name, "HOME" | "APPDATA" | "LOCALAPPDATA"))
+ .filter(|(name, _)| matches!(*name, "HOME" | "CUA_DRIVER_RS_HOME"))
.map(|(name, value)| EmbeddedEnvironmentVariable {
name: name.into(),
value: value.to_string_lossy().into_owned(),
diff --git a/libs/cua-driver/rust/crates/cua-driver/tests/testkit_host_state_isolation_test.rs b/libs/cua-driver/rust/crates/cua-driver/tests/testkit_host_state_isolation_test.rs
index da34d41ad5..b6a36053fc 100644
--- a/libs/cua-driver/rust/crates/cua-driver/tests/testkit_host_state_isolation_test.rs
+++ b/libs/cua-driver/rust/crates/cua-driver/tests/testkit_host_state_isolation_test.rs
@@ -10,32 +10,34 @@
//! This binary contains exactly one test so it can safely point its own
//! `HOME` (and the platform state variables) at a temporary directory before
//! any daemon starts. The real user's files are never touched.
-//!
-//! Windows is excluded: its History root derives from `LOCALAPPDATA`, which the
-//! testkit must leave on the host because `shell:AppsFolder` app resolution
-//! depends on it (see `cua-driver-testkit/src/host_state.rs`).
-#![cfg(not(target_os = "windows"))]
use std::path::{Path, PathBuf};
+use std::process::Command;
-use cua_driver_testkit::{driver_binary, CliDriver, McpDriver, RawDriver, SHARE_HOST_STATE};
+use cua_driver_testkit::{
+ driver_binary, ensure_driver_binary, CliDriver, McpDriver, RawDriver, SHARE_HOST_STATE,
+};
/// Every per-user history root the driver may resolve for `home`.
fn history_roots(home: &Path) -> Vec<PathBuf> {
- let base = if cfg!(target_os = "macos") {
+ let base = if cfg!(target_os = "windows") {
+ home.join("AppData").join("Local")
+ } else if cfg!(target_os = "macos") {
home.join("Library").join("Application Support")
} else {
home.join(".local").join("state")
};
- ["cua-driver", "cua-driver-local"]
+ let mut roots: Vec<_> = ["cua-driver", "cua-driver-local"]
.into_iter()
.map(|namespace| base.join(namespace).join("computer-history"))
- .collect()
+ .collect();
+ roots.push(home.join(".cua-driver").join("computer-history"));
+ roots
}
fn seed_admitted_host_home() -> tempfile::TempDir {
let home = tempfile::Builder::new()
- .prefix("cua-host-home-")
+ .prefix("cua host home-")
.tempdir()
.expect("temporary host home");
for root in history_roots(home.path()) {
@@ -49,20 +51,58 @@ fn seed_admitted_host_home() -> tempfile::TempDir {
// Only this single-test binary's process environment changes; it runs
// before any thread or child exists.
std::env::set_var("HOME", home.path());
- if !cfg!(target_os = "macos") {
+ std::env::set_var("CUA_DRIVER_RS_HOME", home.path().join(".cua-driver"));
+ if cfg!(target_os = "windows") {
+ std::env::set_var("USERPROFILE", home.path());
+ std::env::set_var("APPDATA", home.path().join("AppData").join("Roaming"));
+ std::env::set_var("LOCALAPPDATA", home.path().join("AppData").join("Local"));
+ } else if !cfg!(target_os = "macos") {
std::env::set_var("XDG_STATE_HOME", home.path().join(".local").join("state"));
}
home
}
+fn assert_admission_hint(driver_home: Option<&Path>, admitted: bool) {
+ #[cfg(windows)]
+ let socket = format!(r"\\.\pipe\cua-history-missing-{}", uuid::Uuid::new_v4());
+ #[cfg(unix)]
+ let socket = format!("/tmp/cua-history-missing-{}.sock", uuid::Uuid::new_v4());
+
+ let mut command = Command::new(driver_binary());
+ command
+ .args(["history", "status", "--socket", &socket])
+ .env("CUA_DRIVER_RS_TELEMETRY_ENABLED", "false");
+ match driver_home {
+ Some(path) => command.env("CUA_DRIVER_RS_HOME", path),
+ None => command.env_remove("CUA_DRIVER_RS_HOME"),
+ };
+ let output = command.output().expect("history status command");
+ let stderr = String::from_utf8_lossy(&output.stderr);
+ assert_eq!(output.status.code(), Some(1), "{stderr}");
+ assert!(
+ stderr.contains("Cua Driver daemon is not running."),
+ "{stderr}"
+ );
+ assert_eq!(
+ stderr.contains(" --experimental-history"),
+ admitted,
+ "{stderr}"
+ );
+}
+
#[test]
fn testkit_daemons_ignore_host_history_admission() {
- if !driver_binary().exists() {
- eprintln!("[skip] driver binary not built at {:?}", driver_binary());
+ if !ensure_driver_binary(&driver_binary()) {
return;
}
let host_home = seed_admitted_host_home();
+ assert_admission_hint(None, true);
+ assert_admission_hint(Some(Path::new("")), true);
+ let empty_home = tempfile::tempdir().expect("empty driver home");
+ assert_admission_hint(Some(empty_home.path()), false);
+ assert_admission_hint(Some(&host_home.path().join(".cua-driver")), true);
+
// Control: sharing the seeded host state reproduces the original failure,
// proving the seed is the state that used to leak into test daemons.
assert!(
@@ -90,6 +130,9 @@ fn testkit_daemons_ignore_host_history_admission() {
// The seeded host preference is still intact: isolation is read-side only.
for root in history_roots(host_home.path()) {
- assert!(root.join("admission.json").is_file());
+ assert_eq!(
+ std::fs::read(root.join("admission.json")).unwrap(),
+ br#"{"history_preview_admitted":true}"#
+ );
}
}