feat(plugins): add interactive prompts and richer connection forms

This commit is contained in:
Jinpy authored and GitHub committed 2026-09-17 19:29:28 +08:00
1 parent fac270fc8a
commit e4bda693fb
42 files changed
+3922 -200

No files matched your search

+4
View File
@@ -437,6 +437,10 @@ async fn ensure_worker_consent(data_dir: &Path, identity: &str, dest: &str, dige
previous_fingerprint: None,
prompt: Some(dest.to_string()),
echo: false,
source: None,
title: None,
default_value: None,
options: Vec::new(),
};
let Some(rx) = ssh_prompt::request_ssh_prompt(request) else {
return Err("Uploading a SQLite worker requires explicit consent in the Desktop UI".to_string());
+143 -2
View File
@@ -8,6 +8,12 @@
//! suspend on a `oneshot` while the Tauri layer forwards the request to the UI
//! and the UI answers through a command.
//!
//! The same gateway carries generic [`SshPromptKind::UserInput`] questions that
//! are not part of a host-owned SSH transport, e.g. a plugin backend asking for
//! a bastion MFA code through the `host/requestUserInput` Host API method. The
//! channel is intentionally transport-agnostic: the host only relays a question
//! and the typed answer, and it never answers on the user's behalf.
//!
//! The gateway is installed once at app startup by the Tauri layer
//! (`install_ssh_prompt_gateway`). In headless / test contexts where no
//! gateway is installed, `request_ssh_prompt` returns `None` and callers MUST
@@ -30,6 +36,20 @@ pub enum SshPromptKind {
SecretInput,
/// Confirm uploading the SQLite worker binary onto the file host.
WorkerUploadConsent,
/// A caller that is not a host-owned transport (today: a plugin backend
/// through the `host/requestUserInput` Host API method) needs a value typed
/// by the user. Unlike [`SshPromptKind::SecretInput`] the host never
/// synthesizes or auto-answers it: without a user there is no answer.
UserInput,
}
/// A fixed answer a [`SshPromptKind::UserInput`] request offers.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SshPromptOption {
/// Value returned to the caller when the user picks this option.
pub value: String,
/// Text the dialog shows.
pub label: String,
}
/// A request for user input, sent from the backend to the UI.
@@ -56,19 +76,52 @@ pub struct SshPromptRequest {
/// Passwords and verification codes normally set this to false.
#[serde(default)]
pub echo: bool,
/// UserInput: who is asking (e.g. the plugin display name), shown so the
/// user can tell a bastion login prompt from a host-owned one.
#[serde(default)]
pub source: Option<String>,
/// UserInput: heading above `prompt`, already localized by the caller.
#[serde(default)]
pub title: Option<String>,
/// UserInput: preset answer offered in the input.
#[serde(default)]
pub default_value: Option<String>,
/// UserInput: fixed answers; empty means free-form input.
#[serde(default)]
pub options: Vec<SshPromptOption>,
}
/// The user's answer, sent from the UI back to the backend.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum SshPromptAnswer {
/// Accept and (for HostKeyVerify) optionally persist the key.
Accept { remember: bool },
/// Reject.
Reject,
/// A typed secret (SecretInput).
/// A typed value (SecretInput / UserInput).
Secret(String),
}
impl Default for SshPromptRequest {
fn default() -> Self {
Self {
id: String::new(),
kind: SshPromptKind::HostKeyVerify,
host: String::new(),
port: 0,
key_type: None,
fingerprint: None,
previous_fingerprint: None,
prompt: None,
echo: false,
source: None,
title: None,
default_value: None,
options: Vec::new(),
}
}
}
/// Internal envelope: the request plus the channel to deliver the answer on.
pub struct SshPromptEnvelope {
pub request: SshPromptRequest,
@@ -88,6 +141,18 @@ pub fn clear_ssh_prompt_gateway() {
*PROMPT_GATEWAY.lock().unwrap() = None;
}
/// Serializes every test that mutates the process-global prompt gateway.
///
/// One gateway serves the host's SSH transports, the SQLite worker consent
/// prompt and plugin Host API questions, so tests in any module must hold this
/// lock while a gateway is installed — otherwise one test's gateway (or the
/// fail-closed checks that rely on *no* gateway) clobbers another's.
#[cfg(test)]
pub fn prompt_gateway_test_lock() -> &'static tokio::sync::Mutex<()> {
static LOCK: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());
&LOCK
}
/// Request input from the UI. Returns the receiver the caller should await, or
/// `None` if no gateway is installed (caller must fail closed).
pub fn request_ssh_prompt(request: SshPromptRequest) -> Option<oneshot::Receiver<SshPromptAnswer>> {
@@ -117,6 +182,10 @@ pub fn host_key_verify_request(
previous_fingerprint: None,
prompt: None,
echo: false,
source: None,
title: None,
default_value: None,
options: Vec::new(),
}
}
@@ -138,6 +207,10 @@ pub fn host_key_changed_request(
previous_fingerprint,
prompt: None,
echo: false,
source: None,
title: None,
default_value: None,
options: Vec::new(),
}
}
@@ -153,6 +226,74 @@ pub fn secret_input_request(host: &str, port: u16, prompt: String, echo: bool) -
previous_fingerprint: None,
prompt: Some(prompt),
echo,
source: None,
title: None,
default_value: None,
options: Vec::new(),
}
}
/// A generic question from a caller the host does not own (today: a plugin
/// backend). Built through [`UserInputRequest::into_request`], which allocates
/// the prompt id the UI answers with.
#[derive(Debug, Clone)]
pub struct UserInputRequest {
pub prompt: String,
pub echo: bool,
pub title: Option<String>,
pub source: Option<String>,
pub default_value: Option<String>,
pub options: Vec<SshPromptOption>,
}
impl UserInputRequest {
pub fn new(prompt: impl Into<String>) -> Self {
Self { prompt: prompt.into(), echo: false, title: None, source: None, default_value: None, options: Vec::new() }
}
pub fn with_title(mut self, title: Option<String>) -> Self {
self.title = title;
self
}
pub fn with_source(mut self, source: Option<String>) -> Self {
self.source = source;
self
}
pub fn with_default(mut self, default_value: Option<String>) -> Self {
self.default_value = default_value;
self
}
pub fn with_options(mut self, options: Vec<SshPromptOption>) -> Self {
self.options = options;
self
}
pub fn with_echo(mut self, echo: bool) -> Self {
self.echo = echo;
self
}
/// Turn the question into a prompt request. `host`/`port` are display-only
/// context a caller outside the host's own transports usually leaves empty.
pub fn into_request(self, host: &str, port: u16) -> SshPromptRequest {
SshPromptRequest {
id: Uuid::new_v4().to_string(),
kind: SshPromptKind::UserInput,
host: host.to_string(),
port,
key_type: None,
fingerprint: None,
previous_fingerprint: None,
prompt: Some(self.prompt),
echo: self.echo,
source: self.source,
title: self.title,
default_value: self.default_value,
options: self.options,
}
}
}
+26 -24
View File
@@ -1877,11 +1877,13 @@ pub(crate) fn effective_hop_timeout(hop: &SshTunnelConfig) -> u64 {
}
}
/// Serializes every test that mutates the process-global SSH prompt gateway so
/// they cannot clobber each other's gateway (or the MITM fail-closed test,
/// which relies on *no* gateway being installed). Test-only.
/// Serializes every test that mutates the process-global prompt gateway. The
/// lock lives in `db::ssh_prompt` because the gateway is shared with the plugin
/// Host API, so prompt tests in other modules must take the same lock. Test-only.
#[cfg(test)]
static PROMPT_TEST_LOCK: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());
fn prompt_test_lock() -> &'static tokio::sync::Mutex<()> {
crate::db::ssh_prompt::prompt_gateway_test_lock()
}
#[cfg(test)]
fn plan_chain(
@@ -1918,10 +1920,10 @@ fn plan_chain(
#[cfg(test)]
mod tests {
use super::prompt_test_lock;
#[cfg(unix)]
use super::resolve_ssh_agent_socket_path;
use super::SshClient;
use super::PROMPT_TEST_LOCK;
use super::{
bind_tunnel_listener, connect_and_authenticate, describe_terminal_auth_failure, effective_hop_timeout,
netcat_proxy_command, openssh_padding_len, plan_chain, read_ssh_string,
@@ -2404,7 +2406,7 @@ mod tests {
#[tokio::test]
async fn unknown_host_prompt_accept_learns_key() {
let _guard = PROMPT_TEST_LOCK.lock().await;
let _guard = prompt_test_lock().lock().await;
let dir = tempdir().unwrap();
let path = dir.path().join("known_hosts");
let verifier = HostKeyVerifier::new(path.clone());
@@ -2428,7 +2430,7 @@ mod tests {
#[tokio::test]
async fn unknown_host_prompt_accept_without_remember_is_session_only() {
let _guard = PROMPT_TEST_LOCK.lock().await;
let _guard = prompt_test_lock().lock().await;
let dir = tempdir().unwrap();
let path = dir.path().join("known_hosts");
let verifier = HostKeyVerifier::new(path.clone());
@@ -2454,7 +2456,7 @@ mod tests {
#[tokio::test]
async fn unknown_host_prompt_reject_aborts_handshake() {
let _guard = PROMPT_TEST_LOCK.lock().await;
let _guard = prompt_test_lock().lock().await;
let dir = tempdir().unwrap();
let path = dir.path().join("known_hosts");
let verifier = HostKeyVerifier::new(path.clone());
@@ -2480,7 +2482,7 @@ mod tests {
#[tokio::test]
async fn unknown_host_without_gateway_fails_closed() {
let _guard = PROMPT_TEST_LOCK.lock().await;
let _guard = prompt_test_lock().lock().await;
// Ensure no gateway is installed so request_ssh_prompt returns None.
ssh_prompt::clear_ssh_prompt_gateway();
let dir = tempdir().unwrap();
@@ -2501,7 +2503,7 @@ mod tests {
#[tokio::test]
async fn trusted_host_skips_prompt() {
let _guard = PROMPT_TEST_LOCK.lock().await;
let _guard = prompt_test_lock().lock().await;
let dir = tempdir().unwrap();
let path = dir.path().join("known_hosts");
let verifier = HostKeyVerifier::new(path);
@@ -2531,7 +2533,7 @@ mod tests {
#[tokio::test]
async fn changed_host_prompt_update_replaces_key() {
let _guard = PROMPT_TEST_LOCK.lock().await;
let _guard = prompt_test_lock().lock().await;
let dir = tempdir().unwrap();
let path = dir.path().join("known_hosts");
seed_changed_host_key(&path);
@@ -2559,7 +2561,7 @@ mod tests {
#[tokio::test]
async fn changed_host_prompt_continue_is_session_only() {
let _guard = PROMPT_TEST_LOCK.lock().await;
let _guard = prompt_test_lock().lock().await;
let dir = tempdir().unwrap();
let path = dir.path().join("known_hosts");
seed_changed_host_key(&path);
@@ -2587,7 +2589,7 @@ mod tests {
#[tokio::test]
async fn changed_host_prompt_reject_aborts_handshake() {
let _guard = PROMPT_TEST_LOCK.lock().await;
let _guard = prompt_test_lock().lock().await;
let dir = tempdir().unwrap();
let path = dir.path().join("known_hosts");
seed_changed_host_key(&path);
@@ -2735,7 +2737,7 @@ uveF/dLmnVN1IriEyEvHAAAACGRieC10ZXN0AQIDBAU=
#[tokio::test]
async fn password_auth_continues_with_keyboard_interactive_totp() {
let _guard = PROMPT_TEST_LOCK.lock().await;
let _guard = prompt_test_lock().lock().await;
ssh_prompt::clear_ssh_prompt_gateway();
let (port, server_task) = start_password_then_totp_server().await;
let dir = tempdir().unwrap();
@@ -2781,7 +2783,7 @@ uveF/dLmnVN1IriEyEvHAAAACGRieC10ZXN0AQIDBAU=
#[tokio::test]
async fn public_key_auth_continues_with_keyboard_interactive_totp() {
let _guard = PROMPT_TEST_LOCK.lock().await;
let _guard = prompt_test_lock().lock().await;
ssh_prompt::clear_ssh_prompt_gateway();
let (port, server_task) = start_password_then_totp_server().await;
let dir = tempdir().unwrap();
@@ -2901,7 +2903,7 @@ uveF/dLmnVN1IriEyEvHAAAACGRieC10ZXN0AQIDBAU=
#[tokio::test]
async fn socks5_proxy_forwards_requested_target_over_ssh() {
let _guard = PROMPT_TEST_LOCK.lock().await;
let _guard = prompt_test_lock().lock().await;
ssh_prompt::clear_ssh_prompt_gateway();
let server_key = decode_secret_key(TEST_SERVER_KEY_PEM, None).expect("decode test server key");
let server_config = server::Config { keys: vec![server_key], ..Default::default() };
@@ -3052,7 +3054,7 @@ uveF/dLmnVN1IriEyEvHAAAACGRieC10ZXN0AQIDBAU=
#[tokio::test]
async fn tunnel_falls_back_to_netcat_when_direct_tcpip_is_prohibited() {
let _guard = PROMPT_TEST_LOCK.lock().await;
let _guard = prompt_test_lock().lock().await;
ssh_prompt::clear_ssh_prompt_gateway();
let server_key = decode_secret_key(TEST_SERVER_KEY_PEM, None).expect("decode test server key");
let server_config = server::Config { keys: vec![server_key], ..Default::default() };
@@ -3107,7 +3109,7 @@ uveF/dLmnVN1IriEyEvHAAAACGRieC10ZXN0AQIDBAU=
#[tokio::test]
async fn prohibited_direct_tcpip_does_not_run_netcat_by_default() {
let _guard = PROMPT_TEST_LOCK.lock().await;
let _guard = prompt_test_lock().lock().await;
ssh_prompt::clear_ssh_prompt_gateway();
let server_key = decode_secret_key(TEST_SERVER_KEY_PEM, None).expect("decode test server key");
let server_config = server::Config { keys: vec![server_key], ..Default::default() };
@@ -3157,7 +3159,7 @@ uveF/dLmnVN1IriEyEvHAAAACGRieC10ZXN0AQIDBAU=
#[tokio::test]
async fn forwarded_connection_checks_logical_host_identity() {
let _guard = PROMPT_TEST_LOCK.lock().await;
let _guard = prompt_test_lock().lock().await;
ssh_prompt::clear_ssh_prompt_gateway();
let (connect_port, server_task) = start_accept_none_server().await;
let dir = tempdir().unwrap();
@@ -3205,7 +3207,7 @@ uveF/dLmnVN1IriEyEvHAAAACGRieC10ZXN0AQIDBAU=
/// wait must not be charged against `connect_timeout_secs`.
#[tokio::test]
async fn slow_host_key_acceptance_does_not_time_out_the_connection() {
let _guard = PROMPT_TEST_LOCK.lock().await;
let _guard = prompt_test_lock().lock().await;
ssh_prompt::clear_ssh_prompt_gateway();
let (connect_port, server_task) = start_accept_none_server().await;
let dir = tempdir().unwrap();
@@ -3248,7 +3250,7 @@ uveF/dLmnVN1IriEyEvHAAAACGRieC10ZXN0AQIDBAU=
#[tokio::test]
async fn concurrent_tunnel_starts_share_one_handshake() {
let _guard = PROMPT_TEST_LOCK.lock().await;
let _guard = prompt_test_lock().lock().await;
ssh_prompt::clear_ssh_prompt_gateway();
let (connect_port, server_task) = start_accept_none_server().await;
let dir = tempdir().unwrap();
@@ -3342,7 +3344,7 @@ uveF/dLmnVN1IriEyEvHAAAACGRieC10ZXN0AQIDBAU=
#[tokio::test]
async fn unknown_host_without_persist_permission_does_not_leak_password() {
let _guard = PROMPT_TEST_LOCK.lock().await;
let _guard = prompt_test_lock().lock().await;
// This test proves the client never sends a password to an untrusted
// host. It must run with NO prompt gateway installed so the handshake
// fails closed (no UI to confirm the key -> reject before auth).
@@ -3479,7 +3481,7 @@ uveF/dLmnVN1IriEyEvHAAAACGRieC10ZXN0AQIDBAU=
#[tokio::test]
async fn reconnect_auth_failure_is_reported_instead_of_a_dead_local_port() {
let _guard = PROMPT_TEST_LOCK.lock().await;
let _guard = prompt_test_lock().lock().await;
ssh_prompt::clear_ssh_prompt_gateway();
let publickey_attempts = Arc::new(AtomicUsize::new(0));
@@ -3573,7 +3575,7 @@ uveF/dLmnVN1IriEyEvHAAAACGRieC10ZXN0AQIDBAU=
#[tokio::test]
async fn rejected_public_key_reports_what_the_server_refused() {
let _guard = PROMPT_TEST_LOCK.lock().await;
let _guard = prompt_test_lock().lock().await;
ssh_prompt::clear_ssh_prompt_gateway();
// Every publickey attempt is refused, so this exercises the initial