|
| 1 | +//! #517 on Windows: the XAA login opens its URL through `cmd.exe /C start`, |
| 2 | +//! which must get the whole URL. Before, the URL reached `cmd.exe` unquoted, |
| 3 | +//! and `cmd.exe` ended the command at the URL's first `&`. |
| 4 | +//! |
| 5 | +//! `cmd.exe` also replaces `%NAME%` with the variable's value, which can be |
| 6 | +//! a `"` that ends the quoting around the URL. |
| 7 | +//! |
| 8 | +//! A URL scheme registered for the current user stands in for the browser: |
| 9 | +//! Windows runs this test binary with the URL it was asked to open. Whether a |
| 10 | +//! real browser then shows the page can only be seen on a desktop. |
| 11 | +#![cfg(windows)] |
| 12 | + |
| 13 | +use claurst_mcp::oauth::{initiate_xaa_login, pkce_challenge}; |
| 14 | +use std::collections::BTreeMap; |
| 15 | +use std::path::{Path, PathBuf}; |
| 16 | +use std::process::Command; |
| 17 | +use std::time::{Duration, Instant}; |
| 18 | + |
| 19 | +const CAPTURE: &str = "capture"; |
| 20 | + |
| 21 | +// The registry helpers are repeated in claurst-tui's `windows_open_tests`: |
| 22 | +// test crates cannot share a module. |
| 23 | + |
| 24 | +/// A registry key under `HKCU\Software\Classes`, removed on drop. |
| 25 | +struct Registered(String); |
| 26 | + |
| 27 | +impl Drop for Registered { |
| 28 | + fn drop(&mut self) { |
| 29 | + let _ = Command::new("reg") |
| 30 | + .args(["delete", self.0.as_str(), "/f"]) |
| 31 | + .output(); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +fn reg_add(key: &str, value: &[&str], data: &str) { |
| 36 | + let output = Command::new("reg") |
| 37 | + .arg("add") |
| 38 | + .arg(key) |
| 39 | + .args(value) |
| 40 | + .args(["/d", data, "/f"]) |
| 41 | + .output() |
| 42 | + .unwrap(); |
| 43 | + assert!(output.status.success(), "reg add {key}: {output:?}"); |
| 44 | +} |
| 45 | + |
| 46 | +/// Make `scheme:` URLs open with this test binary, which writes the URL it |
| 47 | +/// was given to `capture`. |
| 48 | +fn register_scheme(scheme: &str, capture: &Path) -> Registered { |
| 49 | + let registered = Registered(format!(r"HKCU\Software\Classes\{scheme}")); |
| 50 | + reg_add(®istered.0, &["/ve"], "URL:coven-code #517 test"); |
| 51 | + reg_add(®istered.0, &["/v", "URL Protocol"], ""); |
| 52 | + let handler = format!( |
| 53 | + r#""{}" --exact url_open_handler --ignored --nocapture "{}" "%1""#, |
| 54 | + std::env::current_exe().unwrap().display(), |
| 55 | + capture.display(), |
| 56 | + ); |
| 57 | + reg_add( |
| 58 | + &format!(r"{}\shell\open\command", registered.0), |
| 59 | + &["/ve"], |
| 60 | + &handler, |
| 61 | + ); |
| 62 | + registered |
| 63 | +} |
| 64 | + |
| 65 | +fn wait_for(capture: &Path) -> String { |
| 66 | + let deadline = Instant::now() + Duration::from_secs(60); |
| 67 | + loop { |
| 68 | + if let Ok(text) = std::fs::read_to_string(capture) { |
| 69 | + return text; |
| 70 | + } |
| 71 | + assert!( |
| 72 | + Instant::now() < deadline, |
| 73 | + "nothing was opened within 60 s: {} was not written", |
| 74 | + capture.display() |
| 75 | + ); |
| 76 | + std::thread::sleep(Duration::from_millis(100)); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +#[test] |
| 81 | +fn xaa_login_opens_the_whole_url() { |
| 82 | + let directory = tempfile::tempdir().unwrap(); |
| 83 | + let capture = directory.path().join(format!("url.{CAPTURE}")); |
| 84 | + let scheme = format!("coven-test-517-{}", std::process::id()); |
| 85 | + let _registered = register_scheme(&scheme, &capture); |
| 86 | + // The login appends four parameters with `&`. `%PATH%` is set, and |
| 87 | + // `cmd.exe` would replace it with its value; with delayed expansion on, |
| 88 | + // it would also replace `!PATH!` and drop the `^`. |
| 89 | + let endpoint = format!("{scheme}://authorize?audience=mcp%2Btools¬e=%PATH%!PATH!^x"); |
| 90 | + |
| 91 | + // The launch waits for `cmd.exe`; bound it, should Windows ask anything. |
| 92 | + let (sent, launched) = std::sync::mpsc::channel(); |
| 93 | + std::thread::spawn(move || { |
| 94 | + let _ = sent.send(initiate_xaa_login("fixture", &endpoint)); |
| 95 | + }); |
| 96 | + let state = launched |
| 97 | + .recv_timeout(Duration::from_secs(60)) |
| 98 | + .expect("the login did not return within 60 s") |
| 99 | + .unwrap(); |
| 100 | + |
| 101 | + let opened = wait_for(&capture); |
| 102 | + let url = url::Url::parse(&opened).unwrap(); |
| 103 | + assert_eq!(url.scheme(), scheme, "{opened}"); |
| 104 | + let query: BTreeMap<_, _> = url.query_pairs().into_owned().collect(); |
| 105 | + let expected = BTreeMap::from([ |
| 106 | + ("audience".to_string(), "mcp+tools".to_string()), |
| 107 | + ("note".to_string(), "%PATH%!PATH!^x".to_string()), |
| 108 | + ("response_type".to_string(), "code".to_string()), |
| 109 | + ( |
| 110 | + "code_challenge".to_string(), |
| 111 | + pkce_challenge(&state.verifier), |
| 112 | + ), |
| 113 | + ("code_challenge_method".to_string(), "S256".to_string()), |
| 114 | + ( |
| 115 | + "redirect_uri".to_string(), |
| 116 | + format!("http://127.0.0.1:{}/callback", state.redirect_port), |
| 117 | + ), |
| 118 | + ]); |
| 119 | + assert_eq!(query, expected, "Windows was asked to open {opened}"); |
| 120 | +} |
| 121 | + |
| 122 | +/// Run by Windows as the test scheme's handler, with the capture file and |
| 123 | +/// the URL as its last two arguments. It does nothing unless the first of |
| 124 | +/// those is a capture file. |
| 125 | +#[test] |
| 126 | +#[ignore = "run by Windows as the handler of the test URL scheme"] |
| 127 | +fn url_open_handler() { |
| 128 | + let mut arguments = std::env::args_os().rev(); |
| 129 | + let (Some(opened), Some(capture)) = (arguments.next(), arguments.next()) else { |
| 130 | + return; |
| 131 | + }; |
| 132 | + let capture = PathBuf::from(capture); |
| 133 | + if capture |
| 134 | + .extension() |
| 135 | + .is_none_or(|extension| extension != CAPTURE) |
| 136 | + { |
| 137 | + return; |
| 138 | + } |
| 139 | + let partial = capture.with_extension("partial"); |
| 140 | + std::fs::write(&partial, opened.to_string_lossy().as_bytes()).unwrap(); |
| 141 | + std::fs::rename(partial, capture).unwrap(); |
| 142 | +} |
0 commit comments