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-cli): open the sign-in URL on Windows without cmd.exe (CUA-1701) (#5260)

Francesco Bonaccic6fed8595754
diff --git a/libs/cua/crates/cua-cli/Cargo.toml b/libs/cua/crates/cua-cli/Cargo.toml
index 0f1bd2948c..62308d1a96 100644
--- a/libs/cua/crates/cua-cli/Cargo.toml
+++ b/libs/cua/crates/cua-cli/Cargo.toml
@@ -77,7 +77,7 @@ libc = "0.2"
[target.'cfg(windows)'.dependencies]
-windows-sys = { version = "0.61", features = ["Win32_Foundation", "Win32_System_Console"] }
+windows-sys = { version = "0.61", features = ["Win32_Foundation", "Win32_System_Console", "Win32_UI_Shell", "Win32_UI_WindowsAndMessaging"] }
[dev-dependencies]
# `stream-probe` input batches checked against the protocol's own type.
diff --git a/libs/cua/crates/cua-cli/src/util.rs b/libs/cua/crates/cua-cli/src/util.rs
index 5b51298a6c..6554bfef95 100644
--- a/libs/cua/crates/cua-cli/src/util.rs
+++ b/libs/cua/crates/cua-cli/src/util.rs
@@ -142,26 +142,58 @@ pub fn open_browser(url: &str) -> bool {
if std::env::var_os("CUA_NO_BROWSER").is_some() || !interactive() {
return false;
}
- let mut cmd = if cfg!(target_os = "macos") {
- let mut c = std::process::Command::new("open");
- c.arg(url);
- c
- } else if cfg!(windows) {
- let mut c = std::process::Command::new("cmd");
- c.args(["/C", "start", "", url]);
- c
- } else {
- let mut c = std::process::Command::new("xdg-open");
- c.arg(url);
- c
+ launch_url(url)
+}
+
+/// Hands `url` to the OS's default handler.
+///
+/// Windows uses `ShellExecuteW`, never `cmd /C start`: cmd.exe reads an
+/// unquoted `&` as a command separator, so a sign-in URL arrived as
+/// `...?response_type=code` with every other query parameter lost (CUA-1701).
+#[cfg(windows)]
+fn launch_url(url: &str) -> bool {
+ use windows_sys::Win32::UI::{Shell::ShellExecuteW, WindowsAndMessaging::SW_SHOWNORMAL};
+ let wide = |s: &str| s.encode_utf16().chain(Some(0)).collect::<Vec<u16>>();
+ let (verb, target) = (wide("open"), wide(url));
+ // SAFETY: both strings are NUL-terminated and outlive the call.
+ let r = unsafe {
+ ShellExecuteW(
+ std::ptr::null_mut(),
+ verb.as_ptr(),
+ target.as_ptr(),
+ std::ptr::null(),
+ std::ptr::null(),
+ SW_SHOWNORMAL,
+ )
};
- cmd.stdin(std::process::Stdio::null())
+ // Values above 32 mean success.
+ r as isize > 32
+}
+
+#[cfg(not(windows))]
+fn launch_url(url: &str) -> bool {
+ let (program, args) = launch_command(url);
+ std::process::Command::new(program)
+ .args(args)
+ .stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.spawn()
.is_ok()
}
+/// The command that opens `url` on macOS and Linux. The URL is one argv
+/// entry, passed through exactly; no shell sees it.
+#[cfg(not(windows))]
+fn launch_command(url: &str) -> (&'static str, Vec<String>) {
+ let program = if cfg!(target_os = "macos") {
+ "open"
+ } else {
+ "xdg-open"
+ };
+ (program, vec![url.to_string()])
+}
+
/// A reqwest client with sane timeouts.
pub fn http() -> reqwest::Client {
cua_spacesd_client::transport::ensure_crypto_provider();
@@ -484,3 +516,16 @@ mod create_step_tests {
assert_eq!(vm.step(Phase::Booting), Some("Booting"));
}
}
+
+#[cfg(all(test, not(windows)))]
+mod browser_tests {
+ /// A real sign-in URL: several `&`-joined parameters (CUA-1701).
+ const SIGN_IN: &str = "https://auth.cua.ai/realms/cyclops-cs/protocol/openid-connect/auth?response_type=code&client_id=cua-cli&redirect_uri=http%3A%2F%2F127.0.0.1%3A54968%2Fcallback&scope=openid&state=s&code_challenge=c&code_challenge_method=S256";
+
+ #[test]
+ fn the_url_reaches_the_opener_whole() {
+ let (program, args) = super::launch_command(SIGN_IN);
+ assert_ne!(program, "cmd");
+ assert_eq!(args, vec![SIGN_IN.to_string()]);
+ }
+}