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): refuse ambiguous macOS browser AppleScript (#4051)

Kevin Rajanfb33104d2e74
diff --git a/libs/cua-driver/rust/crates/platform-macos/src/browser/browser_js.rs b/libs/cua-driver/rust/crates/platform-macos/src/browser/browser_js.rs
index 460095eb87..5d1e2c2adf 100644
--- a/libs/cua-driver/rust/crates/platform-macos/src/browser/browser_js.rs
+++ b/libs/cua-driver/rust/crates/platform-macos/src/browser/browser_js.rs
@@ -11,6 +11,7 @@ const CHROME_APP_BUNDLE_PREFIX: &str = "com.google.Chrome.app.";
#[derive(Clone, Debug)]
struct NativeWindowTarget {
+ pid: i32,
title: String,
bounds: WindowBounds,
same_bounds_ordinal: usize,
@@ -37,6 +38,7 @@ impl BrowserJs {
pub async fn execute(
javascript: &str,
bundle_id: &str,
+ expected_pid: i32,
window_id: u32,
) -> anyhow::Result<String> {
let app_name = app_name_for_bundle(bundle_id)
@@ -49,6 +51,14 @@ impl BrowserJs {
tokio::task::spawn_blocking(move || native_window_target(wid)).await?
}?;
+ if target.pid != expected_pid {
+ anyhow::bail!(
+ "browser_window_owner_pid_mismatch: window_id {window_id} is owned by pid {}, not requested pid {expected_pid}",
+ target.pid
+ );
+ }
+ ensure_applescript_process_identity(bundle_id, expected_pid)?;
+
let escaped_js = escape_js_for_applescript(javascript);
let script = if bundle_id == "com.apple.Safari" {
@@ -141,12 +151,60 @@ fn native_window_target(window_id: u32) -> anyhow::Result<NativeWindowTarget> {
.unwrap_or(0);
Ok(NativeWindowTarget {
+ pid: target.pid,
title: target.title.clone(),
bounds: target.bounds.clone(),
same_bounds_ordinal,
})
}
+fn ensure_applescript_process_identity(bundle_id: &str, expected_pid: i32) -> anyhow::Result<()> {
+ let matching_pids = crate::apps::list_running_apps()
+ .into_iter()
+ .filter(|app| app.bundle_id.as_deref() == Some(bundle_id))
+ .map(|app| app.pid)
+ .filter(|pid| process_is_alive(*pid))
+ .collect::<Vec<_>>();
+ ensure_applescript_process_identity_in(bundle_id, expected_pid, &matching_pids)
+}
+
+fn process_is_alive(pid: i32) -> bool {
+ if pid <= 0 {
+ return false;
+ }
+ // Signal zero checks process existence without delivering a signal.
+ // EPERM still proves that the pid exists; only ESRCH proves it is gone.
+ if unsafe { libc::kill(pid, 0) } == 0 {
+ return true;
+ }
+ std::io::Error::last_os_error().raw_os_error() != Some(libc::ESRCH)
+}
+
+fn ensure_applescript_process_identity_in(
+ bundle_id: &str,
+ expected_pid: i32,
+ matching_pids: &[i32],
+) -> anyhow::Result<()> {
+ let unique = matching_pids
+ .iter()
+ .copied()
+ .filter(|pid| *pid > 0)
+ .collect::<std::collections::BTreeSet<_>>();
+
+ if !unique.contains(&expected_pid) {
+ anyhow::bail!(
+ "browser_process_not_running: requested pid {expected_pid} is not a live regular application for bundle {bundle_id}"
+ );
+ }
+ if unique.len() != 1 {
+ anyhow::bail!(
+ "browser_applescript_process_ambiguous: bundle {bundle_id} has {} live regular application instances; AppleScript cannot prove requested pid {expected_pid}. Use an exact CDP target or close the other instance.",
+ unique.len()
+ );
+ }
+ Ok(())
+}
+
fn chromium_window_script(
app_name: &str,
target: &NativeWindowTarget,
@@ -347,6 +405,7 @@ mod tests {
fn target(title: &str, same_bounds_ordinal: usize) -> NativeWindowTarget {
NativeWindowTarget {
+ pid: 4242,
title: title.to_owned(),
bounds: WindowBounds {
x: 22.0,
@@ -358,6 +417,34 @@ mod tests {
}
}
+ #[test]
+ fn applescript_process_identity_accepts_only_the_requested_single_instance() {
+ assert!(ensure_applescript_process_identity_in("com.google.Chrome", 4242, &[4242]).is_ok());
+ }
+
+ #[test]
+ fn process_liveness_filters_departed_running_application_rows() {
+ assert!(process_is_alive(std::process::id() as i32));
+ assert!(!process_is_alive(i32::MAX));
+ }
+
+ #[test]
+ fn applescript_process_identity_refuses_same_bundle_ambiguity() {
+ let error =
+ ensure_applescript_process_identity_in("com.google.Chrome", 4242, &[4242, 9001])
+ .unwrap_err();
+ assert!(error
+ .to_string()
+ .contains("browser_applescript_process_ambiguous"));
+ }
+
+ #[test]
+ fn applescript_process_identity_refuses_missing_requested_instance() {
+ let error =
+ ensure_applescript_process_identity_in("com.google.Chrome", 4242, &[9001]).unwrap_err();
+ assert!(error.to_string().contains("browser_process_not_running"));
+ }
+
#[test]
fn chromium_script_matches_window_bounds_and_same_bounds_ordinal() {
let script = chromium_window_script(
diff --git a/libs/cua-driver/rust/crates/platform-macos/src/tools/page.rs b/libs/cua-driver/rust/crates/platform-macos/src/tools/page.rs
index 050ac124c3..b1aa23e9de 100644
--- a/libs/cua-driver/rust/crates/platform-macos/src/tools/page.rs
+++ b/libs/cua-driver/rust/crates/platform-macos/src/tools/page.rs
@@ -271,7 +271,7 @@ async fn execute_js(js: &str, bundle_id: &str, pid: i32, window_id: u64) -> anyh
if BrowserJs::supports(bundle_id) {
let window_id = u32::try_from(window_id)
.map_err(|_| anyhow::anyhow!("macOS window_id {window_id} is out of u32 range"))?;
- return BrowserJs::execute(js, bundle_id, window_id).await;
+ return BrowserJs::execute(js, bundle_id, pid, window_id).await;
}
let is_electron = tokio::task::spawn_blocking(move || ElectronJs::is_electron(pid)).await?;
if is_electron {