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 background Cmd+V when the browser focus is its omnibox (#5124)

Francesco Bonaccic18bb1c058ad
diff --git a/libs/cua-driver/rust/crates/platform-macos/src/tools/hotkey.rs b/libs/cua-driver/rust/crates/platform-macos/src/tools/hotkey.rs
index 438d8d21a9..d197f10666 100644
--- a/libs/cua-driver/rust/crates/platform-macos/src/tools/hotkey.rs
+++ b/libs/cua-driver/rust/crates/platform-macos/src/tools/hotkey.rs
@@ -95,6 +95,17 @@ fn def() -> &'static ToolDef {
})
}
+/// Cmd+V and its paste variants (Cmd+Shift+V, Cmd+Opt+Shift+V).
+fn is_paste_combo(modifiers: &[String], key: &str) -> bool {
+ key.eq_ignore_ascii_case("v")
+ && modifiers.iter().any(|m| {
+ matches!(
+ m.to_ascii_lowercase().as_str(),
+ "cmd" | "command" | "meta" | "super"
+ )
+ })
+}
+
/// Modifier key names — split the keys array into modifiers + base key.
fn is_modifier(k: &str) -> bool {
matches!(
@@ -327,6 +338,35 @@ impl Tool for HotkeyTool {
}
}
+ // ── Paste must not land in browser chrome ──
+ // A background Cmd+V goes to the window's first responder. In a
+ // browser that can be the omnibox rather than the page field the
+ // caller meant, which pastes (possibly secret) clipboard content into
+ // the address bar. With no element/pixel target, refuse when the
+ // window's focused field is positively browser chrome.
+ if !fg && px.is_none() && element_index.is_none() && is_paste_combo(&modifiers, &key) {
+ if let Some(wid) = window_id {
+ let chrome = tokio::task::spawn_blocking(move || {
+ super::type_text::is_browser_pid(pid)
+ && super::type_text::window_focus_is_browser_chrome_field(pid, wid)
+ })
+ .await
+ .unwrap_or(false);
+ if chrome {
+ return ToolResult::error(
+ "hotkey: the browser's focused field is its own address/search bar, \
+ not a page field, so the paste was not sent. Focus the page field \
+ first (type_text with x,y, or a snapshot element_token), or use \
+ browser_type for page text.",
+ )
+ .with_structured(serde_json::json!({
+ "code": "paste_target_is_browser_chrome",
+ "delivered": false
+ }));
+ }
+ }
+ }
+
// ── Exact-target background gate (macOS background input v1) ──
// A window-addressed background combo is process-scoped transport: it
// must prove exact delivery to the requested window (fresh AXWindows
@@ -603,6 +643,15 @@ mod tests {
assert_eq!(structured["refusal"]["code"], "stale_element_token");
}
+ #[test]
+ fn paste_combo_detection() {
+ let m = |v: &[&str]| v.iter().map(|s| s.to_string()).collect::<Vec<_>>();
+ assert!(is_paste_combo(&m(&["cmd"]), "v"));
+ assert!(is_paste_combo(&m(&["Command", "shift"]), "V"));
+ assert!(!is_paste_combo(&m(&["ctrl"]), "v"));
+ assert!(!is_paste_combo(&m(&["cmd"]), "c"));
+ }
+
#[test]
fn hotkey_contract_accepts_snapshot_bound_ax_targets() {
let properties = def().input_schema["properties"]
diff --git a/libs/cua-driver/rust/crates/platform-macos/src/tools/type_text.rs b/libs/cua-driver/rust/crates/platform-macos/src/tools/type_text.rs
index b8016a8db5..0c5d3c8e20 100644
--- a/libs/cua-driver/rust/crates/platform-macos/src/tools/type_text.rs
+++ b/libs/cua-driver/rust/crates/platform-macos/src/tools/type_text.rs
@@ -1096,6 +1096,31 @@ fn classify_target_web_area(
}
}
+/// True when the window's own focused element is an editable field that sits
+/// OUTSIDE the page's `AXWebArea` — the browser's native chrome (omnibox,
+/// find bar). A paste or typed chord aimed at "the page" would land there.
+/// Conservative: unknown focus, a non-editable role, or any AX failure is
+/// `false`, so an unprobeable browser is never refused.
+pub(super) fn window_focus_is_browser_chrome_field(pid: i32, window_id: u32) -> bool {
+ unsafe {
+ let Some(el) = crate::ax::exact_target::focused_element_in_window(pid, window_id) else {
+ return false;
+ };
+ let role = copy_string_attr(el, "AXRole");
+ CFRelease(el as _);
+ if !matches!(
+ role.as_deref(),
+ Some("AXTextField") | Some("AXComboBox") | Some("AXSearchField")
+ ) {
+ return false;
+ }
+ }
+ matches!(
+ classify_target_web_area(pid, None, Some(window_id)),
+ WebAreaClassification::NonWebContent
+ )
+}
+
/// Whether `pid` is a web browser whose omnibox and pages the foreground
/// typing rung must activate fully (see the browser branch of the rung).
pub(super) fn is_browser_pid(pid: i32) -> bool {