Skip to main content

agentmux_bashwrap/
askpass.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! askpass mode: `ssh` runs `$SSH_ASKPASS "<prompt>"` and reads the answer on
5//! stdout. When AgentMux runs `ssh` for an agent it points `SSH_ASKPASS` here
6//! and sets `AGENTMUX_ASKPASS_SECRET` on that `ssh` only; this asks srv
7//! (`POST /api/v1/askpass`), which shows the prompt to the user, and prints
8//! their answer. The secret, not anything here, decides whose prompt it is
9//! (srv's `backend::remote::askpass`).
10//!
11//! Recognised before clap runs: ssh passes the prompt as the only argument,
12//! with no subcommand.
13
14use std::time::Duration;
15
16const ENV_SECRET: &str = "AGENTMUX_ASKPASS_SECRET";
17
18/// Longer than srv waits for the user (two minutes), so srv's own answer, or
19/// its "not answered", is what arrives.
20const ANSWER_TIMEOUT: Duration = Duration::from_secs(150);
21
22/// bashwrap's own subcommands: never an askpass call, even if the variable
23/// was inherited by something run under that ssh.
24const SUBCOMMANDS: &[&str] = &[
25    "exec",
26    "hook",
27    "precompact",
28    "sessionstart",
29    "git-credential",
30    "github-token",
31    "github-owner-tag",
32    "github-text-guard",
33    "git-hook",
34    "git-env",
35    "help",
36    "--help",
37    "-h",
38    "--version",
39    "-V",
40];
41
42/// The prompt, when this run is ssh asking: the secret is set and the
43/// arguments are not a bashwrap subcommand.
44pub fn askpass_prompt(secret: Option<&str>, args: &[String]) -> Option<String> {
45    secret.filter(|s| !s.is_empty())?;
46    if args
47        .first()
48        .is_some_and(|a| SUBCOMMANDS.contains(&a.as_str()))
49    {
50        return None;
51    }
52    Some(args.join(" "))
53}
54
55/// Run askpass mode if this is an askpass call: `Some(exit code)` (0 with the
56/// answer on stdout, 1 when there is none), `None` to carry on as bashwrap.
57pub fn run_if_asked() -> Option<i32> {
58    let secret = std::env::var(ENV_SECRET).ok();
59    let args: Vec<String> = std::env::args().skip(1).collect();
60    let prompt = askpass_prompt(secret.as_deref(), &args)?;
61    let secret = secret.unwrap_or_default();
62    let Some(client) = crate::mps_client::WpsClient::from_env() else {
63        eprintln!("agentmux askpass: AGENTMUX_LOCAL_URL / AGENTMUX_AUTH_KEY not set");
64        return Some(1);
65    };
66    let rt = match tokio::runtime::Runtime::new() {
67        Ok(rt) => rt,
68        Err(e) => {
69            eprintln!("agentmux askpass: {e}");
70            return Some(1);
71        }
72    };
73    // ssh's own word for the prompt (OpenSSH 8.4+): `confirm`, `none`, or unset.
74    let hint = std::env::var("SSH_ASKPASS_PROMPT").unwrap_or_default();
75    let body = serde_json::json!({ "secret": secret, "prompt": prompt, "hint": hint });
76    let answer = rt.block_on(client.post_json("/api/v1/askpass", None, &body, ANSWER_TIMEOUT));
77    match answer
78        .ok()
79        .and_then(|v| v.get("answer").and_then(|a| a.as_str()).map(str::to_string))
80    {
81        Some(text) => {
82            // ssh reads one line; the answer is never logged or echoed.
83            println!("{text}");
84            Some(0)
85        }
86        None => Some(1),
87    }
88}
89
90#[cfg(test)]
91mod tests {
92    use super::*;
93
94    fn args(a: &[&str]) -> Vec<String> {
95        a.iter().map(|s| s.to_string()).collect()
96    }
97
98    #[test]
99    fn ssh_asking_is_askpass_and_everything_else_is_bashwrap() {
100        assert_eq!(
101            askpass_prompt(Some("amxa_x"), &args(&["asaf@area54's password: "])),
102            Some("asaf@area54's password: ".to_string())
103        );
104        assert_eq!(
105            askpass_prompt(None, &args(&["asaf@area54's password: "])),
106            None,
107            "no secret, no askpass"
108        );
109        assert_eq!(askpass_prompt(Some(""), &args(&["password: "])), None);
110        assert_eq!(
111            askpass_prompt(Some("amxa_x"), &args(&["exec", "--tool-id", "t"])),
112            None,
113            "an inherited secret never turns a subcommand into askpass"
114        );
115        assert_eq!(askpass_prompt(Some("amxa_x"), &args(&["hook"])), None);
116    }
117}