From be111a671a2c4bc17700513d67321f1f4ba6c096 Mon Sep 17 00:00:00 2001 From: Finley Holt <150464521+FinleyHolt@users.noreply.github.com> Date: Sun, 11 Oct 2026 19:36:38 -0700 Subject: [PATCH] fix(cli): let gpauth read the pasted callback with --browser remote (#671) --- crates/gpapi/src/process/auth_launcher.rs | 25 ++++- crates/gpapi/src/process/command_runner.rs | 112 ++++++++++++++++----- crates/gpapi/src/process/desktop_child.rs | 4 +- 3 files changed, 113 insertions(+), 28 deletions(-) diff --git a/crates/gpapi/src/process/auth_launcher.rs b/crates/gpapi/src/process/auth_launcher.rs index e651db4..d4aa227 100644 --- a/crates/gpapi/src/process/auth_launcher.rs +++ b/crates/gpapi/src/process/auth_launcher.rs @@ -12,7 +12,10 @@ use tokio_util::sync::CancellationToken; use crate::auth::AuthWindowTheme; use crate::{auth::SamlAuthResult, credential::Credential, log_format::LogFormat, os_profile::OsProfile}; -use super::{command_runner::run_observed, command_traits::CommandExt}; +use super::{ + command_runner::{ChildInput, run_observed}, + command_traits::CommandExt, +}; use super::rendering_recovery::{RenderingCrash, StderrDiagnostic, eligible, is_crash_exit}; @@ -300,6 +303,7 @@ impl<'a> SamlAuthLauncher<'a> { if embedded && bundled && self.dmabuf_disabled { command.env("WEBKIT_DISABLE_DMABUF_RENDERER", "1"); } + let stdin = auth_stdin(self.browser); let cancellation = self.cancellation.clone(); let output = tokio::task::spawn_blocking(move || { let check = || { @@ -319,7 +323,7 @@ impl<'a> SamlAuthLauncher<'a> { Some(MAX_AUTH_RESULT_BYTES), &mut |bytes| diagnostic.push(bytes), &mut std::io::stderr(), - None, + stdin, )?; Ok::<_, std::io::Error>((output.output, diagnostic.matched())) }) @@ -339,6 +343,14 @@ impl<'a> SamlAuthLauncher<'a> { } } +/// `gpauth --browser remote` reads the pasted `globalprotectcallback:` data from the user's terminal. +fn auth_stdin(browser: Option<&str>) -> ChildInput<'static> { + match browser { + Some(browser) if browser.eq_ignore_ascii_case("remote") => ChildInput::Terminal, + _ => ChildInput::Null, + } +} + fn is_bundled_authenticator(program: &Path, bundled: &Path) -> bool { match (program.canonicalize(), bundled.canonicalize()) { (Ok(program), Ok(bundled)) => program == bundled, @@ -449,6 +461,15 @@ mod tests { assert_eq!(launcher.browser_listen, Some(listen_ip)); } + + #[test] + fn only_a_remote_browser_reads_the_terminal() { + assert!(matches!(auth_stdin(Some("remote")), ChildInput::Terminal)); + assert!(matches!(auth_stdin(Some("REMOTE")), ChildInput::Terminal)); + assert!(matches!(auth_stdin(Some("firefox")), ChildInput::Null)); + assert!(matches!(auth_stdin(None), ChildInput::Null)); + } + #[test] fn failed_authenticator_discards_partial_or_valid_stdout_before_decoding() { use std::os::unix::process::ExitStatusExt; diff --git a/crates/gpapi/src/process/command_runner.rs b/crates/gpapi/src/process/command_runner.rs index a221d1f..ae83e01 100644 --- a/crates/gpapi/src/process/command_runner.rs +++ b/crates/gpapi/src/process/command_runner.rs @@ -14,13 +14,21 @@ use nix::{ use super::collection::CollectionControl; +/// What the child reads on stdin. +pub(crate) enum ChildInput<'a> { + Null, + Frame(&'a [u8]), + /// The caller's own stdin, for a child that prompts the user. + Terminal, +} + /// Owns and reaps the command's process group. Its async caller must cancel and await this worker. pub(crate) fn run_controlled( command: Command, control: &dyn CollectionControl, output_limit: usize, ) -> io::Result { - run(command, control, Some(output_limit), None, None).map(|observed| observed.output) + run(command, control, Some(output_limit), None, ChildInput::Null).map(|observed| observed.output) } #[derive(Debug)] @@ -35,15 +43,9 @@ pub(crate) fn run_observed( output_limit: Option, observer: &mut dyn FnMut(&[u8]), stderr_sink: &mut dyn Write, - stdin_frame: Option<&[u8]>, + stdin: ChildInput<'_>, ) -> io::Result { - run( - command, - control, - output_limit, - Some((observer, stderr_sink)), - stdin_frame, - ) + run(command, control, output_limit, Some((observer, stderr_sink)), stdin) } fn nonblocking(fd: &impl std::os::fd::AsFd) -> io::Result<()> { @@ -61,26 +63,31 @@ fn run( control: &dyn CollectionControl, output_limit: Option, mut observation: Option<(&mut dyn FnMut(&[u8]), &mut dyn Write)>, - stdin_frame: Option<&[u8]>, + stdin: ChildInput<'_>, ) -> io::Result { control.check()?; - command - .stdout(if output_limit.is_some() { - Stdio::piped() - } else { - Stdio::inherit() - }) - .process_group(0); - command.stdin(if stdin_frame.is_some() { + command.stdout(if output_limit.is_some() { Stdio::piped() } else { - Stdio::null() + Stdio::inherit() }); + // A child in a process group of its own is a background job, so reading the terminal would stop it with SIGTTIN. + let own_group = !matches!(stdin, ChildInput::Terminal); + if own_group { + command.process_group(0); + } + let (stdio, stdin_frame) = match stdin { + ChildInput::Null => (Stdio::null(), None), + ChildInput::Frame(frame) => (Stdio::piped(), Some(frame)), + ChildInput::Terminal => (Stdio::inherit(), None), + }; + command.stdin(stdio); if observation.is_some() { command.stderr(Stdio::piped()); } let mut child = ControlledChild { process: command.spawn()?, + own_group, stopped: false, }; let mut stdout = child.process.stdout.take(); @@ -187,13 +194,18 @@ fn run( struct ControlledChild { process: Child, + own_group: bool, stopped: bool, } impl ControlledChild { fn stop_helpers(&mut self) { if !self.stopped { - let _ = killpg(Pid::from_raw(self.process.id() as i32), Signal::SIGKILL); + if self.own_group { + let _ = killpg(Pid::from_raw(self.process.id() as i32), Signal::SIGKILL); + } else { + let _ = self.process.kill(); + } self.stopped = true; } } @@ -223,7 +235,7 @@ mod tests { Some(1024), &mut |bytes| seen.extend_from_slice(bytes), &mut sink, - None, + ChildInput::Null, ) .unwrap(); assert!(!output.output.status.success()); @@ -244,7 +256,15 @@ mod tests { Ok(()) } }; - let error = run_observed(command, &control, Some(1024), &mut |_| {}, &mut io::sink(), None).unwrap_err(); + let error = run_observed( + command, + &control, + Some(1024), + &mut |_| {}, + &mut io::sink(), + ChildInput::Null, + ) + .unwrap_err(); assert_eq!(error.kind(), io::ErrorKind::Interrupted); assert!(start.elapsed() < Duration::from_secs(2)); } @@ -259,7 +279,7 @@ mod tests { None, &mut |_| {}, &mut io::sink(), - Some(&vec![0; 128 * 1024]), + ChildInput::Frame(&vec![0; 128 * 1024]), ) .unwrap(); assert_eq!(observed.output.status.code(), Some(2)); @@ -284,7 +304,51 @@ mod tests { None, &mut |_| {}, &mut io::sink(), - Some(&vec![0; 128 * 1024]), + ChildInput::Frame(&vec![0; 128 * 1024]), + ) + .unwrap_err(); + assert_eq!(error.kind(), io::ErrorKind::Interrupted); + assert!(start.elapsed() < Duration::from_secs(2)); + } + + #[cfg(target_os = "linux")] + #[test] + fn terminal_input_shares_the_callers_stdin_and_process_group() { + let mut command = Command::new("sh"); + command.args(["-c", "readlink /proc/$$/fd/0; cut -d' ' -f5 /proc/$$/stat"]); + let observed = run_observed( + command, + &|| Ok(()), + Some(1024), + &mut |_| {}, + &mut io::sink(), + ChildInput::Terminal, + ) + .unwrap(); + let stdout = String::from_utf8(observed.output.stdout).unwrap(); + let stdin = std::fs::read_link("/proc/self/fd/0").unwrap(); + assert_eq!(stdout, format!("{}\n{}\n", stdin.display(), nix::unistd::getpgrp())); + } + + #[test] + fn cancellation_kills_a_terminal_child() { + let mut command = Command::new("sleep"); + command.arg("30"); + let start = Instant::now(); + let control = || { + if start.elapsed() > Duration::from_millis(100) { + Err(io::Error::new(io::ErrorKind::Interrupted, "cancelled")) + } else { + Ok(()) + } + }; + let error = run_observed( + command, + &control, + None, + &mut |_| {}, + &mut io::sink(), + ChildInput::Terminal, ) .unwrap_err(); assert_eq!(error.kind(), io::ErrorKind::Interrupted); diff --git a/crates/gpapi/src/process/desktop_child.rs b/crates/gpapi/src/process/desktop_child.rs index 4787017..a5beb9b 100644 --- a/crates/gpapi/src/process/desktop_child.rs +++ b/crates/gpapi/src/process/desktop_child.rs @@ -1,5 +1,5 @@ use super::{ - command_runner::run_observed, + command_runner::{ChildInput, run_observed}, rendering_recovery::{RenderingCrash, StderrDiagnostic, eligible_with_inheritance, is_crash_exit}, }; use crate::service::transport::SessionCredential; @@ -55,7 +55,7 @@ pub(super) async fn launch_desktop_child( None, &mut |bytes| diagnostic.push(bytes), &mut sink, - Some(&frame), + ChildInput::Frame(&frame), )?; let output = observed.output; if is_crash_exit(output.status) && retry_eligible && diagnostic.matched() {