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): make Windows browser prepare language independent (#3135)

Hal9bf47f65dda1
diff --git a/libs/cua-driver/rust/crates/platform-windows/src/browser_consent_ui.rs b/libs/cua-driver/rust/crates/platform-windows/src/browser_consent_ui.rs
index a12808e80f..80163868fe 100644
--- a/libs/cua-driver/rust/crates/platform-windows/src/browser_consent_ui.rs
+++ b/libs/cua-driver/rust/crates/platform-windows/src/browser_consent_ui.rs
@@ -14,23 +14,17 @@ use windows::Win32::UI::WindowsAndMessaging::GetWindowThreadProcessId;
use crate::uia::UiaNode;
-fn refusal(code: BrowserRefusalCode, message: impl Into<String>) -> BrowserRefusal {
- BrowserRefusal::new(code, message)
+const CHROMIUM_DIALOG_BUTTON_CLASS: &str = "MdTextButton";
+
+#[derive(Clone, Debug, PartialEq, Eq)]
+struct ConsentButtonCandidate {
+ element_ptr: usize,
+ rect: (i32, i32, i32, i32),
+ has_keyboard_focus: bool,
}
-fn normalized_text(node: &UiaNode) -> String {
- [
- node.name.as_deref(),
- node.value.as_deref(),
- node.automation_id.as_deref(),
- node.help_text.as_deref(),
- ]
- .into_iter()
- .flatten()
- .collect::<Vec<_>>()
- .join(" ")
- .trim()
- .to_ascii_lowercase()
+fn refusal(code: BrowserRefusalCode, message: impl Into<String>) -> BrowserRefusal {
+ BrowserRefusal::new(code, message)
}
fn release_nodes(nodes: &[UiaNode]) {
@@ -59,50 +53,290 @@ fn is_in_web_content(nodes: &[UiaNode], node: &UiaNode) -> bool {
true
}
-fn trusted_prompt_nodes(nodes: &[UiaNode]) -> impl Iterator<Item = &UiaNode> {
- nodes.iter().filter(|node| {
- !node.in_web_content
- && !node.control_type.eq_ignore_ascii_case("Document")
- && !is_in_web_content(nodes, node)
+fn is_trusted_prompt_node(nodes: &[UiaNode], node: &UiaNode) -> bool {
+ !node.in_web_content
+ && !node.control_type.eq_ignore_ascii_case("Document")
+ && !is_in_web_content(nodes, node)
+}
+
+fn node_name(node: &UiaNode) -> Option<&str> {
+ node.name
+ .as_deref()
+ .map(str::trim)
+ .filter(|name| !name.is_empty())
+}
+
+fn subtree_end(nodes: &[UiaNode], root_index: usize) -> usize {
+ let root_depth = nodes[root_index].depth;
+ nodes
+ .iter()
+ .enumerate()
+ .skip(root_index + 1)
+ .find(|(_, node)| node.depth <= root_depth)
+ .map_or(nodes.len(), |(index, _)| index)
+}
+
+fn has_matching_pane_ancestor(nodes: &[UiaNode], window_index: usize, name: &str) -> bool {
+ let mut descendant_depth = nodes[window_index].depth;
+ for ancestor in nodes[..window_index].iter().rev() {
+ if ancestor.depth >= descendant_depth {
+ continue;
+ }
+ descendant_depth = ancestor.depth;
+ if is_trusted_prompt_node(nodes, ancestor)
+ && ancestor.control_type == "Pane"
+ && node_name(ancestor) == Some(name)
+ {
+ return true;
+ }
+ }
+ false
+}
+
+fn has_pane_rooted_title_binding(nodes: &[UiaNode], pane_index: usize, name: &str) -> bool {
+ let pane = &nodes[pane_index];
+ let end = subtree_end(nodes, pane_index);
+ let descendants = &nodes[(pane_index + 1)..end];
+ let has_direct_title = descendants.iter().any(|node| {
+ is_trusted_prompt_node(nodes, node)
+ && node.depth == pane.depth + 1
+ && node.control_type == "Text"
+ && node_name(node) == Some(name)
+ });
+ let has_nested_title = descendants.iter().any(|node| {
+ is_trusted_prompt_node(nodes, node)
+ && node.depth > pane.depth + 1
+ && node.control_type == "Text"
+ && node_name(node) == Some(name)
+ });
+ let has_nested_window = descendants
+ .iter()
+ .any(|node| is_trusted_prompt_node(nodes, node) && node.control_type == "Window");
+ has_direct_title && has_nested_title && !has_nested_window
+}
+
+fn native_prompt_surfaces(nodes: &[UiaNode]) -> Vec<(usize, usize)> {
+ nodes
+ .iter()
+ .enumerate()
+ .filter_map(|(root_index, root)| {
+ if !is_trusted_prompt_node(nodes, root) || root.depth == 0 {
+ return None;
+ }
+ let name = node_name(root)?;
+ let end = subtree_end(nodes, root_index);
+ match root.control_type.as_str() {
+ "Window" => {
+ if !has_matching_pane_ancestor(nodes, root_index, name) {
+ return None;
+ }
+ nodes[(root_index + 1)..end]
+ .iter()
+ .any(|node| {
+ is_trusted_prompt_node(nodes, node)
+ && node.control_type == "Text"
+ && node_name(node) == Some(name)
+ })
+ .then_some((root_index, end))
+ }
+ "Pane" if root.depth > 1 => has_pane_rooted_title_binding(nodes, root_index, name)
+ .then_some((root_index, end)),
+ _ => None,
+ }
+ })
+ .collect()
+}
+
+fn native_prompt_surface_present(nodes: &[UiaNode]) -> bool {
+ !native_prompt_surfaces(nodes).is_empty()
+}
+
+fn native_button_properties(element_ptr: usize) -> Result<(String, bool), BrowserRefusal> {
+ let element = unsafe { IUIAutomationElement::from_raw(element_ptr as *mut _) };
+ let properties = unsafe {
+ element.CurrentClassName().and_then(|class_name| {
+ element
+ .CurrentHasKeyboardFocus()
+ .map(|focused| (class_name.to_string(), focused.as_bool()))
+ })
+ };
+ std::mem::forget(element);
+ properties.map_err(|error| {
+ refusal(
+ BrowserRefusalCode::BrowserWrongTargetRefused,
+ format!("could not prove a native Chromium consent button: {error}"),
+ )
})
}
-fn remote_debugging_prompt_present(nodes: &[UiaNode]) -> bool {
- let has_title =
- trusted_prompt_nodes(nodes).any(|node| normalized_text(node) == "allow remote debugging?");
- let body = trusted_prompt_nodes(nodes)
- .map(normalized_text)
- .collect::<Vec<_>>()
- .join(" ");
- has_title
- && body.contains("external app wants full control")
- && body.contains("saved data, cookies and site data")
- && body.contains("navigate to any url")
+fn edge_gap(first: (i32, i32, i32, i32), second: (i32, i32, i32, i32)) -> Option<i64> {
+ let (first_left, first_top, first_right, first_bottom) = first;
+ let (second_left, second_top, second_right, second_bottom) = second;
+ let same_row = first_top == second_top && first_bottom == second_bottom;
+ if !same_row {
+ return None;
+ }
+ if first_right <= second_left {
+ Some(i64::from(second_left) - i64::from(first_right))
+ } else if second_right <= first_left {
+ Some(i64::from(first_left) - i64::from(second_right))
+ } else {
+ None
+ }
}
-fn exact_allow_button(nodes: &[UiaNode]) -> Result<Option<usize>, BrowserRefusal> {
- if !remote_debugging_prompt_present(nodes) {
+fn select_language_independent_allow(
+ candidates: &[ConsentButtonCandidate],
+) -> Result<usize, BrowserRefusal> {
+ // Chromium builds this modal from one separated extra action plus the
+ // standard OK/Cancel pair. Accessible names are localized, and the whole
+ // footer mirrors for RTL locales, but adjacency and separation are stable.
+ // Cancel focus is additional contradiction evidence when the browser is
+ // active; an inactive browser legitimately reports no focused button.
+ if candidates.len() != 3 {
+ return Err(refusal(
+ BrowserRefusalCode::BrowserWrongTargetRefused,
+ "the native Chromium consent prompt did not expose exactly three distinct dialog buttons",
+ ));
+ }
+ let focused_indices = candidates
+ .iter()
+ .enumerate()
+ .filter(|(_, candidate)| candidate.has_keyboard_focus)
+ .map(|(index, _)| index)
+ .collect::<Vec<_>>();
+ if focused_indices.len() > 1 {
+ return Err(refusal(
+ BrowserRefusalCode::BrowserWrongTargetRefused,
+ "the native Chromium consent prompt exposed multiple focused dialog buttons",
+ ));
+ }
+
+ let mut pairwise_gaps = Vec::new();
+ for first in 0..candidates.len() {
+ for second in (first + 1)..candidates.len() {
+ if let Some(gap) = edge_gap(candidates[first].rect, candidates[second].rect) {
+ pairwise_gaps.push((gap, first, second));
+ }
+ }
+ }
+ pairwise_gaps.sort_by_key(|(gap, _, _)| *gap);
+ let [(standard_gap, first_standard, second_standard), (extra_gap, _, _), _] =
+ pairwise_gaps.as_slice()
+ else {
+ return Err(refusal(
+ BrowserRefusalCode::BrowserWrongTargetRefused,
+ "the native Chromium consent buttons did not form one exact non-overlapping dialog row",
+ ));
+ };
+ let first_width = i64::from(candidates[*first_standard].rect.2)
+ - i64::from(candidates[*first_standard].rect.0);
+ let second_width = i64::from(candidates[*second_standard].rect.2)
+ - i64::from(candidates[*second_standard].rect.0);
+ if *standard_gap >= *extra_gap
+ || *standard_gap > first_width.max(second_width)
+ || *extra_gap < standard_gap.saturating_mul(2)
+ {
+ return Err(refusal(
+ BrowserRefusalCode::BrowserWrongTargetRefused,
+ "the native Chromium consent prompt had no uniquely separated standard button pair",
+ ));
+ }
+
+ let extra_index = (0..candidates.len())
+ .find(|index| index != first_standard && index != second_standard)
+ .expect("three candidates and a two-button pair");
+ let first_to_extra = edge_gap(
+ candidates[*first_standard].rect,
+ candidates[extra_index].rect,
+ );
+ let second_to_extra = edge_gap(
+ candidates[*second_standard].rect,
+ candidates[extra_index].rect,
+ );
+ let allow_index = match (first_to_extra, second_to_extra) {
+ (Some(first_gap), Some(second_gap)) if first_gap < second_gap => *first_standard,
+ (Some(first_gap), Some(second_gap)) if second_gap < first_gap => *second_standard,
+ _ => {
+ return Err(refusal(
+ BrowserRefusalCode::BrowserWrongTargetRefused,
+ "the native Chromium consent prompt had no unique standard button adjacent to the extra action",
+ ));
+ }
+ };
+ let cancel_index = if allow_index == *first_standard {
+ *second_standard
+ } else {
+ *first_standard
+ };
+ if focused_indices
+ .first()
+ .is_some_and(|focused| *focused != cancel_index)
+ {
+ return Err(refusal(
+ BrowserRefusalCode::BrowserWrongTargetRefused,
+ "the native Chromium consent prompt focus contradicted the structural cancel button",
+ ));
+ }
+ Ok(candidates[allow_index].element_ptr)
+}
+
+fn exact_allow_button_with<F>(
+ nodes: &[UiaNode],
+ mut properties: F,
+) -> Result<Option<usize>, BrowserRefusal>
+where
+ F: FnMut(usize) -> Result<(String, bool), BrowserRefusal>,
+{
+ let surfaces = native_prompt_surfaces(nodes);
+ if surfaces.is_empty() {
return Ok(None);
}
- let matches = trusted_prompt_nodes(nodes)
- .filter(|node| {
- node.control_type == "Button"
- && normalized_text(node) == "allow"
+ let mut matches = Vec::new();
+ for (start, end) in surfaces {
+ let mut candidates = Vec::new();
+ for node in nodes[(start + 1)..end].iter().filter(|node| {
+ is_trusted_prompt_node(nodes, node)
+ && node.control_type == "Button"
&& node.actions.iter().any(|action| action == "invoke")
&& node.element_ptr != 0
- })
- .map(|node| node.element_ptr)
- .collect::<Vec<_>>();
+ && node.rect.is_some()
+ }) {
+ let (class_name, has_keyboard_focus) = properties(node.element_ptr)?;
+ if class_name != CHROMIUM_DIALOG_BUTTON_CLASS {
+ continue;
+ }
+ let rect = node.rect.expect("filtered above");
+ if rect.0 >= rect.2 || rect.1 >= rect.3 {
+ continue;
+ }
+ if candidates
+ .iter()
+ .any(|candidate: &ConsentButtonCandidate| candidate.rect == rect)
+ {
+ continue;
+ }
+ candidates.push(ConsentButtonCandidate {
+ element_ptr: node.element_ptr,
+ rect,
+ has_keyboard_focus,
+ });
+ }
+ matches.push(select_language_independent_allow(&candidates)?);
+ }
match matches.as_slice() {
- [] => Ok(None),
[element] => Ok(Some(*element)),
_ => Err(refusal(
BrowserRefusalCode::BrowserWrongTargetRefused,
- "multiple exact Allow actions matched the browser consent prompt",
+ "multiple bound native Chromium consent prompts exposed structural allow actions",
)),
}
}
+fn exact_allow_button(nodes: &[UiaNode]) -> Result<Option<usize>, BrowserRefusal> {
+ exact_allow_button_with(nodes, native_button_properties)
+}
+
unsafe fn invoke(element_ptr: usize) -> Result<(), BrowserRefusal> {
let element = IUIAutomationElement::from_raw(element_ptr as *mut _);
let result = element
@@ -153,7 +387,7 @@ pub async fn handle(
format!("could not inspect the browser consent UI: {error}"),
)
})?;
- let prompt_present = remote_debugging_prompt_present(&tree.nodes);
+ let prompt_present = native_prompt_surface_present(&tree.nodes);
saw_prompt |= prompt_present;
match exact_allow_button(&tree.nodes) {
Ok(Some(element)) => {
@@ -193,7 +427,7 @@ mod tests {
fn node(control_type: &str, name: &str, actions: &[&str]) -> UiaNode {
UiaNode {
- element_index: (!actions.is_empty()).then_some(0),
+ element_index: None,
control_type: control_type.to_owned(),
name: Some(name.to_owned()),
value: None,
@@ -202,7 +436,7 @@ mod tests {
actions: actions.iter().map(|value| (*value).to_owned()).collect(),
enabled: None,
selected: None,
- element_ptr: 7,
+ element_ptr: 0,
center_x: 0,
center_y: 0,
rect: None,
@@ -213,47 +447,293 @@ mod tests {
}
}
- fn prompt() -> Vec<UiaNode> {
+ fn dialog_node(control_type: &str, name: &str, depth: usize) -> UiaNode {
+ let mut node = node(control_type, name, &[]);
+ node.depth = depth;
+ node
+ }
+
+ fn button(name: &str, element_ptr: usize, rect: (i32, i32, i32, i32)) -> UiaNode {
+ let mut node = node("Button", name, &["invoke"]);
+ node.element_index = Some(element_ptr);
+ node.element_ptr = element_ptr;
+ node.rect = Some(rect);
+ node.depth = 8;
+ node
+ }
+
+ fn prompt(title: &str, labels: [&str; 3]) -> Vec<UiaNode> {
vec![
- node("Text", "Allow remote debugging?", &[]),
- node(
- "Text",
- "An external app wants full control. This includes access to your saved data, cookies and site data, and the ability to navigate to any URL.",
- &[],
- ),
- node("Button", "Cancel", &["invoke"]),
- node("Button", "Allow", &["invoke"]),
+ dialog_node("Pane", title, 2),
+ dialog_node("Window", title, 3),
+ dialog_node("Text", title, 7),
+ button(labels[0], 11, (-6066, 343, -5886, 399)),
+ button(labels[1], 12, (-5657, 343, -5560, 399)),
+ button(labels[2], 13, (-5549, 343, -5452, 399)),
+ ]
+ }
+
+ fn pane_rooted_prompt(title: &str, labels: [&str; 3]) -> Vec<UiaNode> {
+ vec![
+ dialog_node("Pane", title, 2),
+ dialog_node("Text", title, 3),
+ dialog_node("Text", title, 7),
+ dialog_node("Text", "opaque explanatory surface", 10),
+ button(labels[0], 11, (282, 293, 419, 325)),
+ button(labels[1], 12, (574, 293, 629, 325)),
+ button(labels[2], 13, (637, 293, 698, 325)),
]
}
+ fn properties(element_ptr: usize) -> Result<(String, bool), BrowserRefusal> {
+ Ok((CHROMIUM_DIALOG_BUTTON_CLASS.to_owned(), element_ptr == 13))
+ }
+
#[test]
- fn matcher_requires_exact_security_prompt_and_unique_allow_action() {
- assert_eq!(exact_allow_button(&prompt()).unwrap(), Some(7));
+ fn matcher_is_language_independent_across_localized_native_dialogs() {
+ for (title, labels) in [
+ (
+ "リモート デバッグを許可しますか?",
+ ["設定でオフ", "許可", "キャンセル"],
+ ),
+ (
+ "Remote-Debugging zulassen?",
+ ["In Einstellungen deaktivieren", "Zulassen", "Abbrechen"],
+ ),
+ (
+ "Autoriser le débogage à distance ?",
+ ["Désactiver", "Autoriser", "Annuler"],
+ ),
+ (
+ "هل تريد السماح بتصحيح الأخطاء عن بُعد؟",
+ ["تعطيل", "سماح", "إلغاء"],
+ ),
+ ] {
+ assert_eq!(
+ exact_allow_button_with(&prompt(title, labels), properties).unwrap(),
+ Some(12)
+ );
+ }
+ }
+
+ #[test]
+ fn matcher_supports_pane_rooted_native_edge_prompt() {
+ assert_eq!(
+ exact_allow_button_with(
+ &pane_rooted_prompt(
+ "¿Permitir la depuración remota?",
+ ["Desactivar", "Permitir", "Cancelar"],
+ ),
+ properties,
+ )
+ .unwrap(),
+ Some(12)
+ );
+ }
+
+ #[test]
+ fn pane_rooted_prompt_requires_both_title_bindings_and_no_nested_window() {
+ let mut missing_direct = pane_rooted_prompt("opaque title", ["A", "B", "C"]);
+ missing_direct[1].name = Some("different direct title".to_owned());
+ assert_eq!(
+ exact_allow_button_with(&missing_direct, properties).unwrap(),
+ None
+ );
+
+ let mut missing_nested = pane_rooted_prompt("opaque title", ["A", "B", "C"]);
+ missing_nested[2].name = Some("different nested title".to_owned());
+ assert_eq!(
+ exact_allow_button_with(&missing_nested, properties).unwrap(),
+ None
+ );
+
+ let mut nested_window = pane_rooted_prompt("opaque title", ["A", "B", "C"]);
+ nested_window.insert(2, dialog_node("Window", "unrelated native window", 3));
assert_eq!(
- exact_allow_button(&[node("Button", "Allow", &["invoke"])]).unwrap(),
+ exact_allow_button_with(&nested_window, properties).unwrap(),
+ None
+ );
+
+ let mut browser_root = pane_rooted_prompt("opaque title", ["A", "B", "C"]);
+ browser_root[0].depth = 1;
+ assert_eq!(
+ exact_allow_button_with(&browser_root, properties).unwrap(),
None
);
}
#[test]
- fn matcher_refuses_ambiguous_allow_actions() {
- let mut nodes = prompt();
- nodes.push(node("Button", "Allow", &["invoke"]));
+ fn matcher_refuses_multiple_pane_rooted_native_prompts() {
+ let mut nodes = pane_rooted_prompt("first opaque title", ["A", "B", "C"]);
+ nodes.extend(pane_rooted_prompt("second opaque title", ["D", "E", "F"]));
+
assert_eq!(
- exact_allow_button(&nodes).unwrap_err().code,
+ exact_allow_button_with(&nodes, properties)
+ .unwrap_err()
+ .code,
+ BrowserRefusalCode::BrowserWrongTargetRefused
+ );
+ }
+
+ #[test]
+ fn matcher_treats_nonempty_unicode_names_as_opaque_data() {
+ for (title, labels) in [
+ ("e\u{301}", ["é", "E\u{301}", "ë"]),
+ ("हिन्दी", ["ไทย", "עברית", "فارسی"]),
+ ("日本語", ["한국어", "简体中文", "繁體中文"]),
+ ("\u{2067}العربية\u{2069}", ["\u{2066}A\u{2069}", "👩🏽‍💻", "𐐷"]),
+ ("Հայերեն", ["ქართული", "አማርኛ", "বাংলা"]),
+ ("A\u{200d}B", ["無", "⠿", "✅"]),
+ ] {
+ assert_eq!(
+ exact_allow_button_with(&prompt(title, labels), properties).unwrap(),
+ Some(12)
+ );
+ }
+ }
+
+ #[test]
+ fn matcher_refuses_when_accessible_names_cannot_bind_the_native_surface() {
+ let mut nodes = prompt("placeholder", ["A", "B", "C"]);
+ for node in &mut nodes {
+ node.name = None;
+ }
+
+ assert_eq!(exact_allow_button_with(&nodes, properties).unwrap(), None);
+ }
+
+ #[test]
+ fn matcher_deduplicates_repeated_uia_walk_rows_by_exact_geometry() {
+ let mut nodes = prompt("任何语言", ["A", "B", "C"]);
+ let mut duplicate = nodes[4].clone();
+ duplicate.element_ptr = 99;
+ nodes.push(duplicate);
+ assert_eq!(
+ exact_allow_button_with(&nodes, properties).unwrap(),
+ Some(12)
+ );
+ }
+
+ #[test]
+ fn matcher_is_direction_independent_for_rtl_dialog_layouts() {
+ let mut nodes = prompt(
+ "هل تريد السماح بتصحيح الأخطاء عن بُعد؟",
+ ["تعطيل", "سماح", "إلغاء"],
+ );
+ nodes[3].rect = Some((600, 343, 780, 399));
+ nodes[4].rect = Some((299, 343, 396, 399));
+ nodes[5].rect = Some((191, 343, 288, 399));
+ assert_eq!(
+ exact_allow_button_with(&nodes, properties).unwrap(),
+ Some(12)
+ );
+ }
+
+ #[test]
+ fn matcher_does_not_require_focus_when_the_browser_is_inactive() {
+ let nodes = prompt(
+ "¿Permitir la depuración remota?",
+ ["Desactivar", "Permitir", "Cancelar"],
+ );
+ assert_eq!(
+ exact_allow_button_with(&nodes, |_| {
+ Ok((CHROMIUM_DIALOG_BUTTON_CLASS.to_owned(), false))
+ })
+ .unwrap(),
+ Some(12)
+ );
+ }
+
+ #[test]
+ fn matcher_refuses_ambiguous_focus_or_button_geometry() {
+ let nodes = prompt("Qualsiasi lingua", ["A", "B", "C"]);
+ assert_eq!(
+ exact_allow_button_with(&nodes, |element_ptr| {
+ Ok((CHROMIUM_DIALOG_BUTTON_CLASS.to_owned(), element_ptr >= 12))
+ })
+ .unwrap_err()
+ .code,
+ BrowserRefusalCode::BrowserWrongTargetRefused
+ );
+
+ let mut equidistant = nodes;
+ equidistant[3].rect = Some((-5700, 343, -5560, 399));
+ assert_eq!(
+ exact_allow_button_with(&equidistant, properties)
+ .unwrap_err()
+ .code,
BrowserRefusalCode::BrowserWrongTargetRefused
);
}
#[test]
fn matcher_ignores_a_spoofed_prompt_inside_web_content() {
- let mut nodes = prompt();
- nodes.insert(0, node("Document", "Example page", &[]));
- nodes[0].element_index = Some(42);
- for child in &mut nodes[1..] {
- child.parent_element_index = Some(42);
+ let mut nodes = prompt("Permitir depuração remota?", ["A", "B", "C"]);
+ for child in &mut nodes {
child.in_web_content = true;
}
- assert_eq!(exact_allow_button(&nodes).unwrap(), None);
+ assert_eq!(exact_allow_button_with(&nodes, properties).unwrap(), None);
+ }
+
+ #[test]
+ fn matcher_refuses_chromium_buttons_outside_the_bound_prompt_subtree() {
+ let title = "opaque consent surface";
+ let nodes = vec![
+ dialog_node("Pane", title, 2),
+ dialog_node("Window", title, 3),
+ dialog_node("Text", title, 7),
+ dialog_node("Pane", "unrelated native sibling", 2),
+ button("A", 11, (-6066, 343, -5886, 399)),
+ button("B", 12, (-5657, 343, -5560, 399)),
+ button("C", 13, (-5549, 343, -5452, 399)),
+ ];
+
+ assert_eq!(
+ exact_allow_button_with(&nodes, properties)
+ .unwrap_err()
+ .code,
+ BrowserRefusalCode::BrowserWrongTargetRefused
+ );
+ }
+
+ #[test]
+ fn matcher_requires_the_matching_pane_to_be_a_window_ancestor() {
+ let title = "opaque consent surface";
+ let nodes = vec![
+ dialog_node("Pane", title, 2),
+ dialog_node("Pane", "different native container", 2),
+ dialog_node("Window", title, 3),
+ dialog_node("Text", title, 7),
+ button("A", 11, (-6066, 343, -5886, 399)),
+ button("B", 12, (-5657, 343, -5560, 399)),
+ button("C", 13, (-5549, 343, -5452, 399)),
+ ];
+
+ assert_eq!(exact_allow_button_with(&nodes, properties).unwrap(), None);
+ }
+
+ #[test]
+ fn matcher_requires_the_native_prompt_surface_and_chromium_button_class() {
+ let mut nodes = prompt("원격 디버깅을 허용하시겠습니까?", ["A", "B", "C"]);
+ nodes[0].name = Some("different native pane".to_owned());
+ assert_eq!(exact_allow_button_with(&nodes, properties).unwrap(), None);
+
+ let nodes = prompt("원격 디버깅을 허용하시겠습니까?", ["A", "B", "C"]);
+ assert_eq!(
+ exact_allow_button_with(&nodes, |element_ptr| {
+ Ok((
+ if element_ptr == 12 {
+ "RendererButton"
+ } else {
+ CHROMIUM_DIALOG_BUTTON_CLASS
+ }
+ .to_owned(),
+ element_ptr == 13,
+ ))
+ })
+ .unwrap_err()
+ .code,
+ BrowserRefusalCode::BrowserWrongTargetRefused
+ );
}
}
diff --git a/libs/cua-driver/rust/crates/platform-windows/src/browser_platform.rs b/libs/cua-driver/rust/crates/platform-windows/src/browser_platform.rs
index 7dfc66881e..7f8fd9c120 100644
--- a/libs/cua-driver/rust/crates/platform-windows/src/browser_platform.rs
+++ b/libs/cua-driver/rust/crates/platform-windows/src/browser_platform.rs
@@ -986,6 +986,34 @@ async fn browser_websocket_url(port: u16) -> Option<String> {
.flatten()
}
+fn select_provisional_setup_port(
+ ports: &[u16],
+ listeners_before: &[u16],
+ setup_was_already_enabled: bool,
+) -> Result<Option<u16>, BrowserRefusal> {
+ let correlated = ports
+ .iter()
+ .copied()
+ .filter(|port| !listeners_before.contains(port))
+ .collect::<Vec<_>>();
+ match correlated.as_slice() {
+ [port] => Ok(Some(*port)),
+ [] if setup_was_already_enabled => match ports {
+ [] => Ok(None),
+ [port] => Ok(Some(*port)),
+ _ => Err(refusal(
+ BrowserRefusalCode::BrowserBindingAmbiguous,
+ "the pre-enabled browser setup exposed multiple existing exact-pid loopback listeners",
+ )),
+ },
+ [] => Ok(None),
+ _ => Err(refusal(
+ BrowserRefusalCode::BrowserBindingAmbiguous,
+ "the approved setup action exposed multiple newly correlated exact-pid listeners",
+ )),
+ }
+}
+
const ENDPOINT_DISCOVERY_ATTEMPTS: usize = 4;
const ENDPOINT_DISCOVERY_RETRY_DELAY: Duration = Duration::from_millis(100);
@@ -1662,6 +1690,7 @@ impl BrowserPlatform for WindowsBrowserPlatform {
})??;
let opened_setup_page = handle.opened_setup_page;
let enabled_remote_debugging = handle.enabled_remote_debugging;
+ let setup_was_already_enabled = !enabled_remote_debugging;
let focused_setup_address_field = handle.focused_setup_address_field;
let foregrounded_window = handle.foregrounded_window;
let injected_global_input = handle.injected_global_input;
@@ -1695,25 +1724,22 @@ impl BrowserPlatform for WindowsBrowserPlatform {
}
}
if endpoints.is_empty() {
- let correlated = ports
- .iter()
- .copied()
- .filter(|port| !listeners_before.contains(port))
- .collect::<Vec<_>>();
- if let [port] = correlated.as_slice() {
- endpoints.push((
- *port,
+ match select_provisional_setup_port(
+ &ports,
+ &listeners_before,
+ setup_was_already_enabled,
+ ) {
+ Ok(Some(port)) => endpoints.push((
+ port,
format!("ws://127.0.0.1:{port}/devtools/browser"),
- "new exact browser-pid listener correlated with approved setup",
- ));
- } else if correlated.len() > 1 {
- break Err(refusal(
- BrowserRefusalCode::BrowserBindingAmbiguous,
- format!(
- "{} exposed multiple newly correlated exact-pid listeners",
- descriptor.product_name
- ),
- ));
+ if listeners_before.contains(&port) {
+ "unique existing exact browser-pid listener bound to a pre-enabled exact setup page"
+ } else {
+ "new exact browser-pid listener correlated with approved setup"
+ },
+ )),
+ Ok(None) => {}
+ Err(error) => break Err(error),
}
}
match endpoints.as_slice() {
@@ -2384,6 +2410,18 @@ mod tests {
assert_eq!(literal_loopback_websocket_port("ws://127.0.0.1:9222"), None);
}
+ #[test]
+ fn pre_enabled_setup_reuses_one_exact_pid_port_before_consent() {
+ assert_eq!(
+ select_provisional_setup_port(&[9222], &[9222], true).unwrap(),
+ Some(9222)
+ );
+ assert_eq!(
+ select_provisional_setup_port(&[9222], &[9222], false).unwrap(),
+ None
+ );
+ }
+
#[test]
fn windows_command_line_parser_preserves_quoted_profile_paths() {
let args = parse_windows_command_line(
@@ -2423,6 +2461,16 @@ mod tests {
);
}
+ #[test]
+ fn pre_enabled_setup_refuses_multiple_existing_exact_pid_ports() {
+ assert_eq!(
+ select_provisional_setup_port(&[9222, 9333], &[9222, 9333], true)
+ .unwrap_err()
+ .code,
+ BrowserRefusalCode::BrowserBindingAmbiguous
+ );
+ }
+
#[test]
fn legacy_setup_endpoint_must_remain_exact_during_active_port_preference_window() {
let start = std::time::Instant::now();
diff --git a/libs/cua-driver/rust/crates/platform-windows/src/browser_setup_ui.rs b/libs/cua-driver/rust/crates/platform-windows/src/browser_setup_ui.rs
index f7213bb60d..8e80f51aea 100644
--- a/libs/cua-driver/rust/crates/platform-windows/src/browser_setup_ui.rs
+++ b/libs/cua-driver/rust/crates/platform-windows/src/browser_setup_ui.rs
@@ -2,7 +2,7 @@
use std::time::{Duration, Instant};
use std::{
- collections::HashMap,
+ collections::{HashMap, HashSet},
sync::{Mutex, OnceLock},
};
@@ -19,74 +19,197 @@ use windows::Win32::UI::Accessibility::{
use crate::uia::UiaNode;
+// Native Chromium chrome is localized, so its accessible names are diagnostic
+// text rather than a stable automation contract. Bootstrap against the exact
+// approved HWND, native-vs-renderer boundary, control type, supported action,
+// uniqueness, and exact post-action state instead of maintaining language
+// allowlists or accepting fuzzy labels. The internal setup page follows the
+// same rule: its native URL and web-control topology are contracts; its
+// localized document, heading, and checkbox names are not.
+
fn refusal(code: BrowserRefusalCode, message: impl Into<String>) -> BrowserRefusal {
BrowserRefusal::new(code, message)
}
-fn field_equals(node: &UiaNode, expected: &str) -> bool {
- [
- node.name.as_deref(),
- node.value.as_deref(),
- node.automation_id.as_deref(),
- node.help_text.as_deref(),
- ]
- .into_iter()
- .flatten()
- .any(|value| value.trim().eq_ignore_ascii_case(expected))
-}
-
fn release_nodes(nodes: &[UiaNode]) {
for node in nodes.iter().filter(|node| node.element_ptr != 0) {
unsafe { drop(IUIAutomationElement::from_raw(node.element_ptr as *mut _)) };
}
}
-fn unique_actionable(
+fn unique_web_actionable(
nodes: &[UiaNode],
control_type: &str,
- label: &str,
action: &str,
) -> Result<Option<usize>, BrowserRefusal> {
let matches = nodes
.iter()
.filter(|node| {
- node.control_type == control_type
- && field_equals(node, label)
+ node.in_web_content
+ && node.control_type == control_type
&& node.actions.iter().any(|value| value == action)
+ && node.enabled != Some(false)
+ && node.element_ptr != 0
})
.map(|node| node.element_ptr)
- .collect::<Vec<_>>();
- match matches.as_slice() {
- [] => Ok(None),
- [element] => Ok(Some(*element)),
+ .collect::<HashSet<_>>();
+ match matches.len() {
+ 0 => Ok(None),
+ 1 => Ok(matches.into_iter().next()),
_ => Err(refusal(
BrowserRefusalCode::BrowserWrongTargetRefused,
- format!("multiple exact {control_type} controls matched {label:?}"),
+ format!(
+ "multiple web {control_type} controls expose the exact {action} action on the setup page"
+ ),
)),
}
}
-fn setup_page_proven(nodes: &[UiaNode], descriptor: &BrowserSetupDescriptor) -> bool {
- let exact_url = nodes.iter().any(|node| {
- node.control_type == "Edit"
- && field_equals(node, "Address and search bar")
+fn unique_native_actionable(
+ nodes: &[UiaNode],
+ control_type: &str,
+ action: &str,
+) -> Result<Option<usize>, BrowserRefusal> {
+ unique_native_actionable_with_focus(nodes, control_type, action, element_has_keyboard_focus)
+}
+
+fn element_has_keyboard_focus(element_ptr: usize) -> bool {
+ if element_ptr == 0 {
+ return false;
+ }
+ let element = unsafe { IUIAutomationElement::from_raw(element_ptr as *mut _) };
+ let focused = unsafe { element.CurrentHasKeyboardFocus() }
+ .ok()
+ .is_some_and(|value| value.as_bool());
+ std::mem::forget(element);
+ focused
+}
+
+fn unique_native_actionable_with_focus(
+ nodes: &[UiaNode],
+ control_type: &str,
+ action: &str,
+ mut has_keyboard_focus: impl FnMut(usize) -> bool,
+) -> Result<Option<usize>, BrowserRefusal> {
+ let matches = nodes
+ .iter()
+ .filter(|node| {
+ !node.in_web_content
+ && node.control_type == control_type
+ && node.actions.iter().any(|value| value == action)
+ && node.enabled != Some(false)
+ && node.element_ptr != 0
+ })
+ .map(|node| node.element_ptr)
+ .collect::<HashSet<_>>();
+ match matches.len() {
+ 0 => Ok(None),
+ 1 => Ok(matches.into_iter().next()),
+ _ => {
+ let focused = matches
+ .into_iter()
+ .filter(|element| has_keyboard_focus(*element))
+ .collect::<Vec<_>>();
+ match focused.as_slice() {
+ [element] => Ok(Some(*element)),
+ _ => Err(refusal(
+ BrowserRefusalCode::BrowserWrongTargetRefused,
+ format!(
+ "multiple native {control_type} controls expose the exact {action} action, \
+ and keyboard focus did not identify exactly one"
+ ),
+ )),
+ }
+ }
+ }
+}
+
+fn native_tab_count(nodes: &[UiaNode]) -> usize {
+ nodes
+ .iter()
+ .filter(|node| !node.in_web_content && node.control_type == "TabItem")
+ .filter_map(|node| node.rect)
+ .filter(|(left, top, right, bottom)| left < right && top < bottom)
+ .collect::<std::collections::HashSet<_>>()
+ .len()
+}
+
+fn exact_native_new_tab_button(nodes: &[UiaNode]) -> Result<Option<usize>, BrowserRefusal> {
+ let Some(last_tab_index) = nodes.iter().rposition(|node| {
+ !node.in_web_content
+ && node.control_type == "TabItem"
&& node
- .value
- .as_deref()
- .is_some_and(|value| value.trim().eq_ignore_ascii_case(descriptor.setup_url))
- });
- let exact_heading = nodes.iter().any(|node| {
- matches!(node.control_type.as_str(), "Header" | "Text")
- && field_equals(node, descriptor.page_heading)
- });
- let exact_page = nodes.iter().any(|node| {
- node.control_type == "Document"
- && descriptor
- .page_titles
- .iter()
- .any(|title| field_equals(node, title))
- });
- exact_url && exact_page && exact_heading
+ .rect
+ .is_some_and(|(left, top, right, bottom)| left < right && top < bottom)
+ }) else {
+ return Ok(None);
+ };
+ let last_tab = &nodes[last_tab_index];
+ let successor_index =
+ (last_tab_index + 1..nodes.len()).find(|index| nodes[*index].depth <= last_tab.depth);
+ let Some(successor) = successor_index.map(|index| &nodes[index]) else {
+ return Ok(None);
+ };
+ let vertically_overlaps_tab_row = match (last_tab.rect, successor.rect) {
+ (Some((_, tab_top, _, tab_bottom)), Some((_, button_top, _, button_bottom))) => {
+ tab_top < button_bottom && button_top < tab_bottom
+ }
+ _ => false,
+ };
+ if successor.in_web_content
+ || successor.control_type != "Button"
+ || successor.enabled == Some(false)
+ || successor.element_ptr == 0
+ || !successor.actions.iter().any(|action| action == "invoke")
+ || !vertically_overlaps_tab_row
+ {
+ return Err(refusal(
+ BrowserRefusalCode::BrowserWrongTargetRefused,
+ "the native tab strip did not expose one exact structural new-tab action",
+ ));
+ }
+ Ok(Some(successor.element_ptr))
+}
+
+fn stable_native_tab_count(hwnd: u64, initial_count: usize) -> Result<usize, BrowserRefusal> {
+ let deadline = Instant::now() + Duration::from_secs(3);
+ let mut previous = initial_count;
+ loop {
+ std::thread::sleep(Duration::from_millis(100));
+ let tree = crate::uia::walk_tree(hwnd, None);
+ let current = native_tab_count(&tree.nodes);
+ release_nodes(&tree.nodes);
+ if current > 0 && current == previous {
+ return Ok(current);
+ }
+ previous = current;
+ if Instant::now() >= deadline {
+ return Err(refusal(
+ BrowserRefusalCode::BrowserWrongTargetRefused,
+ "the approved Chromium window did not expose a stable native tab topology",
+ ));
+ }
+ }
+}
+
+fn setup_page_proven(nodes: &[UiaNode], descriptor: &BrowserSetupDescriptor) -> bool {
+ let exact_url_count = nodes
+ .iter()
+ .filter(|node| {
+ !node.in_web_content
+ && node.control_type == "Edit"
+ && node.actions.iter().any(|value| value == "set_value")
+ && node
+ .value
+ .as_deref()
+ .is_some_and(|value| value.trim().eq_ignore_ascii_case(descriptor.setup_url))
+ })
+ .count();
+ let document_count = nodes
+ .iter()
+ .filter(|node| node.control_type == "Document" && !node.in_web_content)
+ .count();
+ exact_url_count == 1 && document_count == 1
}
fn exact_setup_checkbox(
@@ -96,43 +219,43 @@ fn exact_setup_checkbox(
if !setup_page_proven(nodes, descriptor) {
return Ok(None);
}
- unique_actionable(nodes, "CheckBox", descriptor.checkbox_label, "toggle")
+ unique_web_actionable(nodes, "CheckBox", "toggle")
}
-unsafe fn invoke(element_ptr: usize) -> Result<(), BrowserRefusal> {
+unsafe fn set_value(element_ptr: usize, value: &str) -> Result<(), BrowserRefusal> {
let element = IUIAutomationElement::from_raw(element_ptr as *mut _);
let result = element
- .GetCurrentPattern(UIA_InvokePatternId)
- .and_then(|pattern| pattern.cast::<IUIAutomationInvokePattern>())
- .and_then(|pattern| pattern.Invoke());
+ .GetCurrentPattern(UIA_ValuePatternId)
+ .and_then(|pattern| pattern.cast::<IUIAutomationValuePattern>())
+ .and_then(|pattern| pattern.SetValue(&BSTR::from(value)));
std::mem::forget(element);
result.map_err(|error| {
refusal(
BrowserRefusalCode::BrowserWrongTargetRefused,
- format!("the exact UIA Invoke action failed: {error}"),
+ format!("the exact UIA Value action failed: {error}"),
)
})
}
-unsafe fn set_value(element_ptr: usize, value: &str) -> Result<(), BrowserRefusal> {
+fn force_setup_foreground(target: windows::Win32::Foundation::HWND) -> (bool, bool) {
+ unsafe { crate::input::force_foreground_assisted(target) }
+}
+
+unsafe fn invoke(element_ptr: usize, description: &str) -> Result<(), BrowserRefusal> {
let element = IUIAutomationElement::from_raw(element_ptr as *mut _);
let result = element
- .GetCurrentPattern(UIA_ValuePatternId)
- .and_then(|pattern| pattern.cast::<IUIAutomationValuePattern>())
- .and_then(|pattern| pattern.SetValue(&BSTR::from(value)));
+ .GetCurrentPattern(UIA_InvokePatternId)
+ .and_then(|pattern| pattern.cast::<IUIAutomationInvokePattern>())
+ .and_then(|pattern| pattern.Invoke());
std::mem::forget(element);
result.map_err(|error| {
refusal(
BrowserRefusalCode::BrowserWrongTargetRefused,
- format!("the exact UIA Value action failed: {error}"),
+ format!("the exact {description} UIA Invoke action failed: {error}"),
)
})
}
-fn force_setup_foreground(target: windows::Win32::Foundation::HWND) -> (bool, bool) {
- unsafe { crate::input::force_foreground_assisted(target) }
-}
-
fn confirm_setup_navigation(
hwnd: u64,
element_ptr: usize,
@@ -400,51 +523,49 @@ pub fn enable(
enable_attempted: false,
},
Ok(None) => {
- let tab_count_before = initial
- .nodes
- .iter()
- .filter(|node| node.control_type == "TabItem")
- .count();
- let new_tab = match unique_actionable(&initial.nodes, "Button", "New Tab", "invoke") {
- Ok(Some(element)) => element,
- Ok(None) => {
- release_nodes(&initial.nodes);
- return Err(refusal(
- BrowserRefusalCode::BrowserWrongTargetRefused,
- format!(
- "the approved {} window has no exact New Tab button",
- descriptor.product_name
- ),
- ));
- }
- Err(error) => {
- release_nodes(&initial.nodes);
- return Err(error);
- }
- };
- let invoked = unsafe { invoke(new_tab) };
+ let initial_tab_count = native_tab_count(&initial.nodes);
release_nodes(&initial.nodes);
- invoked?;
+ let tab_count_before = stable_native_tab_count(hwnd, initial_tab_count)?;
let mut handle = SetupUiHandle {
hwnd,
descriptor,
- opened_setup_page: true,
+ opened_setup_page: false,
enabled_remote_debugging: false,
focused_setup_address_field: false,
foregrounded_window: false,
injected_global_input: false,
enable_attempted: false,
};
+ let tab_tree = crate::uia::walk_tree(hwnd, None);
+ let new_tab_button = match exact_native_new_tab_button(&tab_tree.nodes) {
+ Ok(Some(element)) => element,
+ Ok(None) => {
+ release_nodes(&tab_tree.nodes);
+ return Err(handle.abort(refusal(
+ BrowserRefusalCode::BrowserWrongTargetRefused,
+ format!(
+ "the exact {} window has no structural native new-tab action",
+ descriptor.product_name
+ ),
+ )));
+ }
+ Err(error) => {
+ release_nodes(&tab_tree.nodes);
+ return Err(handle.abort(error));
+ }
+ };
+ if let Err(error) = unsafe { invoke(new_tab_button, "native new-tab button") } {
+ release_nodes(&tab_tree.nodes);
+ return Err(handle.abort(error));
+ }
+ release_nodes(&tab_tree.nodes);
+ handle.opened_setup_page = true;
let deadline = Instant::now() + Duration::from_secs(3);
let mut created = loop {
let tree = crate::uia::walk_tree(hwnd, None);
- let tab_count_after = tree
- .nodes
- .iter()
- .filter(|node| node.control_type == "TabItem")
- .count();
+ let tab_count_after = native_tab_count(&tree.nodes);
if tab_count_after == tab_count_before + 1 {
break tree;
}
@@ -460,23 +581,18 @@ pub fn enable(
}
std::thread::sleep(Duration::from_millis(100));
};
- let omnibox = unique_actionable(
- &created.nodes,
- "Edit",
- "Address and search bar",
- "set_value",
- )?
- .ok_or_else(|| {
- refusal(
- BrowserRefusalCode::BrowserWrongTargetRefused,
- format!(
- "the approved {} window has no exact address-and-search field",
- descriptor.product_name
- ),
- )
- });
- let omnibox = match omnibox {
- Ok(element) => element,
+ let omnibox = match unique_native_actionable(&created.nodes, "Edit", "set_value") {
+ Ok(Some(element)) => element,
+ Ok(None) => {
+ release_nodes(&created.nodes);
+ return Err(handle.abort(refusal(
+ BrowserRefusalCode::BrowserWrongTargetRefused,
+ format!(
+ "the approved {} window has no unique native editable address field",
+ descriptor.product_name
+ ),
+ )));
+ }
Err(error) => {
release_nodes(&created.nodes);
return Err(handle.abort(error));
@@ -488,24 +604,30 @@ pub fn enable(
}
release_nodes(&created.nodes);
created = crate::uia::walk_tree(hwnd, None);
- let refreshed_omnibox = created
- .nodes
- .iter()
- .find(|node| {
- node.control_type == "Edit"
- && field_equals(node, "Address and search bar")
- && node.value.as_deref().is_some_and(|value| {
- value.trim().eq_ignore_ascii_case(descriptor.setup_url)
- })
- })
- .map(|node| node.element_ptr);
- let Some(refreshed_omnibox) = refreshed_omnibox else {
- release_nodes(&created.nodes);
- return Err(handle.abort(refusal(
- BrowserRefusalCode::BrowserWrongTargetRefused,
- "the exact address field did not retain the setup URL",
- )));
- };
+ let refreshed_omnibox =
+ match unique_native_actionable(&created.nodes, "Edit", "set_value") {
+ Ok(Some(element))
+ if created.nodes.iter().any(|node| {
+ node.element_ptr == element
+ && node.value.as_deref().is_some_and(|value| {
+ value.trim().eq_ignore_ascii_case(descriptor.setup_url)
+ })
+ }) =>
+ {
+ element
+ }
+ Ok(_) => {
+ release_nodes(&created.nodes);
+ return Err(handle.abort(refusal(
+ BrowserRefusalCode::BrowserWrongTargetRefused,
+ "the unique native address field did not retain the exact setup URL",
+ )));
+ }
+ Err(error) => {
+ release_nodes(&created.nodes);
+ return Err(handle.abort(error));
+ }
+ };
if let Err(error) = confirm_setup_navigation(
hwnd,
refreshed_omnibox,
@@ -606,7 +728,9 @@ mod tests {
}
#[test]
- fn checkbox_requires_exact_url_heading_and_unique_toggle() {
+ fn checkbox_requires_exact_internal_url_and_unique_web_toggle() {
+ let mut checkbox = node("CheckBox", descriptor().checkbox_label, None, &["toggle"]);
+ checkbox.in_web_content = true;
let nodes = vec![
node(
"Edit",
@@ -616,10 +740,21 @@ mod tests {
),
node("Document", descriptor().page_titles[0], None, &[]),
node("Header", descriptor().page_heading, None, &[]),
- node("CheckBox", descriptor().checkbox_label, None, &["toggle"]),
+ checkbox,
];
assert_eq!(exact_setup_checkbox(&nodes, descriptor()).unwrap(), Some(7));
+ let mut localized = nodes.clone();
+ localized[0].name = Some("アドレス検索バー".to_owned());
+ localized[1].name = Some("远程调试页面".to_owned());
+ localized[2].name = Some("Удалённая отладка".to_owned());
+ localized[3].name = Some("السماح بتصحيح الأخطاء لهذا المتصفح".to_owned());
+ localized[3].in_web_content = true;
+ assert_eq!(
+ exact_setup_checkbox(&localized, descriptor()).unwrap(),
+ Some(7)
+ );
+
let mut wrong_url = nodes.clone();
wrong_url[0].value = Some("https://example.test/".to_owned());
assert_eq!(
@@ -627,4 +762,224 @@ mod tests {
None
);
}
+
+ #[test]
+ fn setup_page_names_are_opaque_across_unicode_scripts_and_normalization() {
+ let samples = [
+ ("e\u{301}", "é", "✅"),
+ ("हिन्दी", "ไทย", "עברית"),
+ ("日本語", "한국어", "简体中文"),
+ ("\u{2067}العربية\u{2069}", "فارسی", "اردو"),
+ ("Հայերեն", "ქართული", "አማርኛ"),
+ ("👩🏽‍💻", "A\u{200d}B", "𐐷"),
+ ("", "", ""),
+ ];
+ for (document_name, heading_name, checkbox_name) in samples {
+ let mut checkbox = node("CheckBox", checkbox_name, None, &["toggle"]);
+ checkbox.in_web_content = true;
+ let nodes = vec![
+ node(
+ "Edit",
+ "opaque native address field",
+ Some(descriptor().setup_url),
+ &["set_value"],
+ ),
+ node("Document", document_name, None, &[]),
+ node("Header", heading_name, None, &[]),
+ checkbox,
+ ];
+ assert_eq!(exact_setup_checkbox(&nodes, descriptor()).unwrap(), Some(7));
+ }
+ }
+
+ #[test]
+ fn setup_page_does_not_require_accessible_names() {
+ let mut checkbox = node("CheckBox", "placeholder", None, &["toggle"]);
+ checkbox.name = None;
+ checkbox.in_web_content = true;
+ let mut address = node(
+ "Edit",
+ "placeholder",
+ Some(descriptor().setup_url),
+ &["set_value"],
+ );
+ address.name = None;
+ let mut document = node("Document", "placeholder", None, &[]);
+ document.name = None;
+ let nodes = vec![address, document, checkbox];
+
+ assert_eq!(exact_setup_checkbox(&nodes, descriptor()).unwrap(), Some(7));
+ }
+
+ #[test]
+ fn setup_page_refuses_ambiguous_web_toggles_without_reading_their_names() {
+ let mut first = node("CheckBox", "A", None, &["toggle"]);
+ first.element_ptr = 41;
+ first.in_web_content = true;
+ let mut second = node("CheckBox", "B", None, &["toggle"]);
+ second.element_ptr = 42;
+ second.in_web_content = true;
+ let nodes = vec![
+ node(
+ "Edit",
+ "address",
+ Some(descriptor().setup_url),
+ &["set_value"],
+ ),
+ node("Document", "opaque", None, &[]),
+ first,
+ second,
+ ];
+
+ assert_eq!(
+ exact_setup_checkbox(&nodes, descriptor()).unwrap_err().code,
+ BrowserRefusalCode::BrowserWrongTargetRefused
+ );
+ }
+
+ #[test]
+ fn native_address_control_is_language_independent_without_trusting_web_content() {
+ let mut native_address = node(
+ "Edit",
+ "アドレス検索バー",
+ Some("chrome://inspect/#remote-debugging"),
+ &["set_value"],
+ );
+ native_address.element_ptr = 11;
+ let mut renderer_spoof = node(
+ "Edit",
+ "アドレス検索バー",
+ Some("chrome://inspect/#remote-debugging"),
+ &["set_value"],
+ );
+ renderer_spoof.element_ptr = 12;
+ renderer_spoof.in_web_content = true;
+
+ assert_eq!(
+ unique_native_actionable(&[native_address, renderer_spoof], "Edit", "set_value")
+ .unwrap(),
+ Some(11)
+ );
+ }
+
+ #[test]
+ fn native_browser_controls_still_refuse_ambiguous_structural_matches() {
+ let mut first = node("Edit", "Adress- und Suchleiste", None, &["set_value"]);
+ first.element_ptr = 31;
+ let mut second = node(
+ "Edit",
+ "Barre d'adresse et de recherche",
+ None,
+ &["set_value"],
+ );
+ second.element_ptr = 32;
+
+ let error =
+ unique_native_actionable_with_focus(&[first, second], "Edit", "set_value", |_| false)
+ .unwrap_err();
+ assert_eq!(error.code, BrowserRefusalCode::BrowserWrongTargetRefused);
+ }
+
+ #[test]
+ fn native_address_control_uses_unique_keyboard_focus_when_edge_exposes_multiple_edits() {
+ let mut first = node("Edit", "opaque first", None, &["set_value"]);
+ first.element_ptr = 31;
+ let mut second = node("Edit", "opaque second", None, &["set_value"]);
+ second.element_ptr = 32;
+
+ assert_eq!(
+ unique_native_actionable_with_focus(&[first, second], "Edit", "set_value", |element| {
+ element == 32
+ },)
+ .unwrap(),
+ Some(32)
+ );
+ }
+
+ #[test]
+ fn native_address_control_refuses_multiple_focused_edits() {
+ let mut first = node("Edit", "opaque first", None, &["set_value"]);
+ first.element_ptr = 31;
+ let mut second = node("Edit", "opaque second", None, &["set_value"]);
+ second.element_ptr = 32;
+
+ assert_eq!(
+ unique_native_actionable_with_focus(&[first, second], "Edit", "set_value", |_| true,)
+ .unwrap_err()
+ .code,
+ BrowserRefusalCode::BrowserWrongTargetRefused
+ );
+ }
+
+ #[test]
+ fn setup_page_proof_rejects_duplicate_native_exact_urls() {
+ let mut nodes = vec![
+ node(
+ "Edit",
+ "Barra de direcciones y de búsqueda",
+ Some(descriptor().setup_url),
+ &["set_value"],
+ ),
+ node("Document", descriptor().page_titles[0], None, &[]),
+ node("Header", descriptor().page_heading, None, &[]),
+ ];
+ let mut duplicate = nodes[0].clone();
+ duplicate.element_ptr = 99;
+ nodes.push(duplicate);
+
+ assert!(!setup_page_proven(&nodes, descriptor()));
+ }
+
+ #[test]
+ fn native_tab_count_is_structural_and_deduplicates_repeated_uia_rows() {
+ let mut first = node("TabItem", "新标签页", None, &[]);
+ first.rect = Some((10, 10, 110, 40));
+ let duplicate = first.clone();
+ let mut second = node("TabItem", "Neue Registerkarte", None, &[]);
+ second.rect = Some((120, 10, 220, 40));
+ let mut renderer_spoof = node("TabItem", "Tab", None, &[]);
+ renderer_spoof.rect = Some((230, 10, 330, 40));
+ renderer_spoof.in_web_content = true;
+ let mut invalid = node("TabItem", "Onglet", None, &[]);
+ invalid.rect = Some((0, 0, 0, 0));
+
+ assert_eq!(
+ native_tab_count(&[first, duplicate, second, renderer_spoof, invalid]),
+ 2
+ );
+ }
+
+ #[test]
+ fn native_new_tab_button_is_the_strict_successor_of_the_tab_strip() {
+ let mut first = node("TabItem", "opaque-1", None, &["select"]);
+ first.element_index = Some(10);
+ first.element_ptr = 10;
+ first.depth = 8;
+ first.rect = Some((10, 10, 110, 40));
+ let mut first_close = node("Button", "opaque-close-1", None, &["invoke"]);
+ first_close.element_index = Some(11);
+ first_close.element_ptr = 11;
+ first_close.depth = 9;
+ first_close.parent_element_index = Some(10);
+ let mut second = node("TabItem", "opaque-2", None, &["select"]);
+ second.element_index = Some(20);
+ second.element_ptr = 20;
+ second.depth = 8;
+ second.rect = Some((120, 10, 220, 40));
+ let mut second_close = node("Button", "opaque-close-2", None, &["invoke"]);
+ second_close.element_index = Some(21);
+ second_close.element_ptr = 21;
+ second_close.depth = 9;
+ second_close.parent_element_index = Some(20);
+ let mut new_tab = node("Button", "opaque-new-tab", None, &["invoke"]);
+ new_tab.element_ptr = 30;
+ new_tab.depth = 6;
+ new_tab.rect = Some((220, 10, 260, 40));
+
+ assert_eq!(
+ exact_native_new_tab_button(&[first, first_close, second, second_close, new_tab,])
+ .unwrap(),
+ Some(30)
+ );
+ }
}