purple-ssh 3.29.0

Free, open-source terminal SSH manager and SSH config editor in Rust for macOS and Linux that keeps ~/.ssh/config in sync with 18 cloud providers, monitors live SSH tunnels and manages Docker and Podman containers fleet-wide. Spin up a VM, it shows up. Destroy it, it dims. Plus scp, Vault SSH certs and an MCP server for AI agents. MIT licensed.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
//! Key handling for `Screen::PasswordPrompt` plus the retry machinery the
//! password prompt and the host-key trust dialog share.
//!
//! A background ssh cannot reach the terminal, so the answer it needs is
//! collected here and the operation is run again with that answer in hand.

use std::sync::mpsc;

use crossterm::event::{KeyCode, KeyEvent};

use crate::app::{App, PASSWORD_MAX_CHARS, PasswordPromptField, PendingRetry, Screen};
use crate::event::AppEvent;

pub(super) fn handle_key(app: &mut App, key: KeyEvent, events_tx: &mpsc::Sender<AppEvent>) {
    if app.password_prompt.is_none() {
        // State pruned out from under the screen; do not strand the user.
        let back = return_screen(app);
        app.set_screen(back);
        return;
    }
    match key.code {
        KeyCode::Esc => cancel(app),
        KeyCode::Tab | KeyCode::Down | KeyCode::BackTab | KeyCode::Up => {
            if let Some(state) = app.password_prompt.as_mut() {
                state.focus = state.focus.next();
            }
        }
        KeyCode::Enter => submit(app, events_tx),
        KeyCode::Backspace => {
            if let Some(state) = app.password_prompt.as_mut()
                && state.focus == PasswordPromptField::Password
            {
                state.input.pop();
            }
        }
        // SPACE GUARD MUST PRECEDE the generic Char(c) arm.
        // Rust matches arms top-to-bottom; below the generic insert-char arm
        // this would never fire and the toggle would be unreachable. On the
        // password field Space falls through and inserts a literal space,
        // because a password may contain one.
        KeyCode::Char(' ') => {
            if let Some(state) = app.password_prompt.as_mut() {
                if state.focus == PasswordPromptField::Remember {
                    state.remember = !state.remember;
                } else {
                    insert_char(state, ' ');
                }
            }
        }
        KeyCode::Char(c) => {
            if let Some(state) = app.password_prompt.as_mut()
                && state.focus == PasswordPromptField::Password
            {
                insert_char(state, c);
            }
        }
        _ => {}
    }
}

/// Append one character to the password buffer. Control characters are
/// dropped so a paste carrying a newline or an escape cannot smuggle extra
/// input past the single-line field, and the buffer is capped.
fn insert_char(state: &mut crate::app::PasswordPromptState, c: char) {
    if c.is_control() || state.input.chars().count() >= PASSWORD_MAX_CHARS {
        return;
    }
    state.input.push(c);
}

/// Esc: drop the prompt and the operation behind it, then offer the next
/// host waiting for an answer.
fn cancel(app: &mut App) {
    let Some(state) = app.password_prompt.take() else {
        return;
    };
    log::debug!("[purple] password prompt: dismissed alias={}", state.alias);
    let back = return_screen(app);
    app.set_screen(back);
    app.notify_warning(crate::messages::askpass::prompt_cancelled(&state.alias));
    super::event_loop::key_push::drain_next_key_push_prompt(app);
}

/// Enter: store the password where the user asked, then run the operation
/// again. An empty buffer is a no-op, like the container exec prompt.
fn submit(app: &mut App, events_tx: &mpsc::Sender<AppEvent>) {
    let Some(state) = app.password_prompt.as_ref() else {
        return;
    };
    if state.input.is_empty() {
        return;
    }
    let Some(state) = app.password_prompt.take() else {
        return;
    };
    let crate::app::PasswordPromptState {
        alias,
        source_is_keychain,
        input,
        remember,
        retry,
        ..
    } = state;

    // Keep the password for this session unless it is provably reachable
    // through the keychain on the next run.
    let stored = remember && store_in_keychain(app, &alias, &input, source_is_keychain);
    if stored {
        app.session_passwords.remove(&alias);
    } else {
        app.session_passwords.insert(alias.clone(), input);
    }
    log::debug!(
        "[purple] password prompt: submitted alias={} remember={} keychain={}",
        alias,
        remember,
        stored
    );

    let back = return_screen(app);
    app.set_screen(back);
    execute_retry(app, retry, false, events_tx);
    // Hand the next waiting host its dialog. A key-push retry has just
    // started a run, so the drain refuses and that run's finalize takes
    // over. Any other retry leaves the queue free to continue here.
    super::event_loop::key_push::drain_next_key_push_prompt(app);
}

/// Store the password in the OS keychain and point the host at it. Returns
/// true only when a later run will find the password by itself: the store
/// succeeded and the alias already resolves to `keychain`, or the directive
/// was written. Every other outcome falls back to the session password and
/// says why.
fn store_in_keychain(app: &mut App, alias: &str, password: &str, already_keychain: bool) -> bool {
    if let Err(e) = crate::askpass::store_in_keychain(app.env(), alias, password) {
        log::warn!("[external] password prompt: keychain store failed alias={alias}: {e}");
        app.notify_error(crate::messages::askpass::keychain_store_failed_session(&e));
        return false;
    }
    if already_keychain {
        log::debug!(
            "[purple] password prompt: stored alias={alias} in keychain, source already keychain"
        );
        return true;
    }
    point_host_at_keychain(app, alias)
}

/// Write `# purple:askpass keychain` on the host so a later run finds the
/// password by itself. Returns false when the directive did not land, which
/// keeps the password for this session and says why. Split from the keychain
/// call so the config outcomes are testable on their own.
fn point_host_at_keychain(app: &mut App, alias: &str) -> bool {
    // The config watcher is suppressed while a dialog is open, so the
    // in-memory model can be older than the file. Writing it back would
    // silently undo whatever was edited meanwhile, and this write only adds
    // a convenience directive. Leave the file alone and say so.
    if app.external_config_changed() {
        log::warn!(
            "[config] password prompt: askpass write skipped for alias={alias}, config changed on disk"
        );
        app.notify_warning(
            crate::messages::askpass::keychain_source_skipped_external_change(alias),
        );
        return false;
    }
    let backup = app.hosts_state.ssh_config().clone();
    if !app
        .hosts_state
        .ssh_config_mut()
        .set_host_askpass(alias, "keychain")
    {
        log::warn!("[config] password prompt: askpass directive not written for alias={alias}");
        app.notify_warning(crate::messages::askpass::keychain_source_not_written(alias));
        return false;
    }
    if let Err(e) = app.hosts_state.ssh_config().write() {
        app.hosts_state.set_ssh_config(backup);
        log::warn!("[config] password prompt: config write failed alias={alias}: {e}");
        // The keychain entry stays. `security add-generic-password -U` and
        // `secret-tool store` both overwrite, so purple cannot tell whether
        // it created the entry or replaced one the user put there, and
        // deleting it could throw away a working credential.
        app.notify_error(crate::messages::askpass::keychain_stored_without_source(&e));
        return false;
    }
    log::debug!("[purple] password prompt: wrote askpass=keychain for alias={alias}");
    app.update_last_modified();
    app.reload_hosts();
    true
}

/// The screen a dialog returns to: the file browser while its overlay is
/// still open, else the host list.
pub(crate) fn return_screen(app: &App) -> Screen {
    match app.file_browser_session.as_ref() {
        Some(fb) => Screen::FileBrowser {
            alias: fb.alias.clone(),
        },
        None => Screen::HostList,
    }
}

/// Run the operation a dialog was holding. `trust_new_host_key` is true
/// only right after the user accepted the trust dialog, so ssh records the
/// host key on that one run.
pub(crate) fn execute_retry(
    app: &mut App,
    retry: PendingRetry,
    trust_new_host_key: bool,
    events_tx: &mpsc::Sender<AppEvent>,
) {
    match retry {
        PendingRetry::KeyPush { key_path, alias } => {
            // Look the key up by path: a successful push rebuilds the list,
            // so the position it had when the dialog opened may now be
            // another key.
            let Some(key_index) = app
                .keys
                .list()
                .iter()
                .position(|k| k.display_path == key_path)
            else {
                log::debug!("[purple] retry: key {key_path} is gone, dropping alias={alias}");
                return;
            };
            log::debug!("[purple] retry: key push alias={alias} trust={trust_new_host_key}");
            super::confirm::start_key_push(
                app,
                key_index,
                vec![alias],
                trust_new_host_key,
                events_tx,
            );
        }
        PendingRetry::FileBrowserListing { alias, path } => {
            // The overlay may have been closed while the dialog was open.
            let Some(fb) = app.file_browser_session.as_mut() else {
                return;
            };
            if fb.alias != alias {
                return;
            }
            fb.remote_loading = true;
            fb.remote_error = None;
            fb.remote_entries.clear();
            fb.remote_selected.clear();
            fb.remote_list_state = ratatui::widgets::ListState::default();
            let show_hidden = fb.show_hidden;
            let sort = fb.sort;
            // Resolve the source again rather than reusing the snapshot the
            // session took when it opened. Answering the prompt with
            // "remember" on writes `# purple:askpass keychain` on the host,
            // and both this retry and every later listing must see it.
            let askpass = app.askpass_source_for(&alias);
            if let Some(fb) = app.file_browser_session.as_mut() {
                fb.askpass = askpass.clone();
            }
            log::debug!("[purple] retry: remote listing alias={alias} trust={trust_new_host_key}");
            let mut ctx = app.ssh_context_for(alias, askpass);
            ctx.trust_new_host_key = trust_new_host_key;
            crate::file_browser::spawn_remote_open(
                ctx,
                path,
                show_hidden,
                sort,
                super::file_browser::fb_send(events_tx.clone()),
            );
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::app::{PasswordPromptState, Screen};
    use crate::ssh_config::model::SshConfigFile;
    use crossterm::event::KeyModifiers;

    fn app_with(config: &str) -> App {
        let scratch = tempfile::tempdir().expect("tempdir").keep();
        let cfg = SshConfigFile {
            elements: SshConfigFile::parse_content(config),
            path: scratch.join("config"),
            crlf: false,
            bom: false,
        };
        App::new(cfg)
    }

    fn open_prompt(app: &mut App, alias: &str) {
        app.password_prompt = Some(PasswordPromptState::new(
            alias.to_string(),
            false,
            PendingRetry::KeyPush {
                key_path: "~/.ssh/id_test".into(),
                alias: alias.to_string(),
            },
        ));
        app.screen = Screen::PasswordPrompt;
    }

    fn k(code: KeyCode) -> KeyEvent {
        KeyEvent::new(code, KeyModifiers::NONE)
    }

    fn press(app: &mut App, code: KeyCode) {
        let (tx, _rx) = mpsc::channel();
        handle_key(app, k(code), &tx);
    }

    #[test]
    fn typing_fills_the_password_field() {
        let mut app = app_with("Host h\n  HostName 1.1.1.1\n");
        open_prompt(&mut app, "h");
        for c in "pw".chars() {
            press(&mut app, KeyCode::Char(c));
        }
        assert_eq!(app.password_prompt.as_ref().unwrap().input, "pw");
    }

    #[test]
    fn space_inserts_a_literal_space_in_the_password() {
        // A password may contain a space; only the toggle field consumes it.
        let mut app = app_with("Host h\n  HostName 1.1.1.1\n");
        open_prompt(&mut app, "h");
        press(&mut app, KeyCode::Char('a'));
        press(&mut app, KeyCode::Char(' '));
        press(&mut app, KeyCode::Char('b'));
        assert_eq!(app.password_prompt.as_ref().unwrap().input, "a b");
    }

    #[test]
    fn space_on_the_toggle_field_flips_remember() {
        let mut app = app_with("Host h\n  HostName 1.1.1.1\n");
        open_prompt(&mut app, "h");
        press(&mut app, KeyCode::Tab);
        assert_eq!(
            app.password_prompt.as_ref().unwrap().focus,
            PasswordPromptField::Remember
        );
        press(&mut app, KeyCode::Char(' '));
        assert!(!app.password_prompt.as_ref().unwrap().remember);
        press(&mut app, KeyCode::Char(' '));
        assert!(app.password_prompt.as_ref().unwrap().remember);
        // The toggle field never collects characters.
        assert!(app.password_prompt.as_ref().unwrap().input.is_empty());
    }

    #[test]
    fn focus_cycles_with_tab_and_arrows() {
        let mut app = app_with("Host h\n  HostName 1.1.1.1\n");
        open_prompt(&mut app, "h");
        for code in [KeyCode::Tab, KeyCode::Down, KeyCode::BackTab, KeyCode::Up] {
            let before = app.password_prompt.as_ref().unwrap().focus;
            press(&mut app, code);
            assert_ne!(
                app.password_prompt.as_ref().unwrap().focus,
                before,
                "{code:?}"
            );
        }
    }

    #[test]
    fn backspace_deletes_from_the_password_only() {
        let mut app = app_with("Host h\n  HostName 1.1.1.1\n");
        open_prompt(&mut app, "h");
        press(&mut app, KeyCode::Char('a'));
        press(&mut app, KeyCode::Char('b'));
        press(&mut app, KeyCode::Backspace);
        assert_eq!(app.password_prompt.as_ref().unwrap().input, "a");
        press(&mut app, KeyCode::Tab);
        press(&mut app, KeyCode::Backspace);
        assert_eq!(app.password_prompt.as_ref().unwrap().input, "a");
    }

    #[test]
    fn control_characters_are_dropped() {
        // A paste carrying a newline must not smuggle extra input through.
        let mut app = app_with("Host h\n  HostName 1.1.1.1\n");
        open_prompt(&mut app, "h");
        press(&mut app, KeyCode::Char('a'));
        press(&mut app, KeyCode::Char('\n'));
        press(&mut app, KeyCode::Char('\t'));
        press(&mut app, KeyCode::Char('b'));
        assert_eq!(app.password_prompt.as_ref().unwrap().input, "ab");
    }

    #[test]
    fn the_buffer_is_capped() {
        let mut app = app_with("Host h\n  HostName 1.1.1.1\n");
        open_prompt(&mut app, "h");
        for _ in 0..(PASSWORD_MAX_CHARS + 50) {
            press(&mut app, KeyCode::Char('x'));
        }
        assert_eq!(
            app.password_prompt.as_ref().unwrap().input.chars().count(),
            PASSWORD_MAX_CHARS
        );
    }

    #[test]
    fn an_empty_submit_is_a_no_op() {
        let mut app = app_with("Host h\n  HostName 1.1.1.1\n");
        open_prompt(&mut app, "h");
        press(&mut app, KeyCode::Enter);
        assert!(app.password_prompt.is_some(), "prompt stays open");
        assert_eq!(app.screen, Screen::PasswordPrompt);
    }

    #[test]
    fn esc_drops_the_prompt_and_warns() {
        let mut app = app_with("Host h\n  HostName 1.1.1.1\n");
        open_prompt(&mut app, "h");
        press(&mut app, KeyCode::Char('x'));
        press(&mut app, KeyCode::Esc);
        assert!(app.password_prompt.is_none());
        assert_eq!(app.screen, Screen::HostList);
        let toast = app.status_center.toast().expect("toast");
        assert!(toast.text.contains('h'), "got: {}", toast.text);
    }

    /// An App whose config file exists on disk, so the mtime guard and the
    /// writer both have something real to work with.
    fn app_with_file(config: &str) -> App {
        let scratch = tempfile::tempdir().expect("tempdir").keep();
        let path = scratch.join("config");
        std::fs::write(&path, config).expect("write config");
        let cfg = SshConfigFile {
            elements: SshConfigFile::parse_content(config),
            path,
            crlf: false,
            bom: false,
        };
        let mut app = App::new(cfg);
        app.update_last_modified();
        app.reload_hosts();
        app
    }

    #[test]
    fn pointing_a_host_at_the_keychain_writes_the_directive() {
        let _guard = crate::demo_flag::GLOBAL_TEST_LOCK
            .lock()
            .unwrap_or_else(|p| p.into_inner());
        let mut app = app_with_file("Host h\n  HostName 1.1.1.1\n");
        let path = app.hosts_state.ssh_config().path.clone();
        assert!(point_host_at_keychain(&mut app, "h"));
        let on_disk = std::fs::read_to_string(&path).expect("read back");
        assert!(
            on_disk.contains("# purple:askpass keychain"),
            "got: {on_disk}"
        );
        assert_eq!(
            app.askpass_source_for("h").as_deref(),
            Some("keychain"),
            "the reload picks the directive up"
        );
    }

    #[test]
    fn a_config_edited_while_the_prompt_was_open_is_left_alone() {
        let _guard = crate::demo_flag::GLOBAL_TEST_LOCK
            .lock()
            .unwrap_or_else(|p| p.into_inner());
        // The watcher is suppressed behind a dialog, so the in-memory model
        // can be older than the file. Writing it back would undo the edit.
        let mut app = app_with_file("Host h\n  HostName 1.1.1.1\n");
        let path = app.hosts_state.ssh_config().path.clone();
        // Someone else edits the file. Move the mtime past the snapshot so
        // the guard sees the change on filesystems with coarse timestamps.
        std::fs::write(&path, "Host h\n  HostName 2.2.2.2\n  Port 2222\n").expect("edit");
        let later = std::time::SystemTime::now() + std::time::Duration::from_secs(2);
        std::fs::File::options()
            .write(true)
            .open(&path)
            .and_then(|f| f.set_modified(later))
            .expect("move the mtime past the snapshot");
        assert!(
            !point_host_at_keychain(&mut app, "h"),
            "the write is skipped"
        );
        let on_disk = std::fs::read_to_string(&path).expect("read back");
        assert!(
            on_disk.contains("Port 2222"),
            "the edit survives: {on_disk}"
        );
        assert!(!on_disk.contains("purple:askpass"), "got: {on_disk}");
    }

    #[test]
    fn a_host_purple_does_not_own_keeps_the_password_for_the_session() {
        let _guard = crate::demo_flag::GLOBAL_TEST_LOCK
            .lock()
            .unwrap_or_else(|p| p.into_inner());
        // No block for this alias in the file purple writes, so there is
        // nowhere to put the directive and the session copy carries on.
        let mut app = app_with_file("Host other\n  HostName 1.1.1.1\n");
        let path = app.hosts_state.ssh_config().path.clone();
        assert!(!point_host_at_keychain(&mut app, "h"));
        let on_disk = std::fs::read_to_string(&path).expect("read back");
        assert!(!on_disk.contains("purple:askpass"), "got: {on_disk}");
    }

    #[test]
    fn submit_with_remember_off_keeps_the_password_for_the_session() {
        let mut app = app_with("Host h\n  HostName 1.1.1.1\n");
        open_prompt(&mut app, "h");
        press(&mut app, KeyCode::Tab);
        press(&mut app, KeyCode::Char(' ')); // remember off
        press(&mut app, KeyCode::BackTab);
        for c in "hunter2".chars() {
            press(&mut app, KeyCode::Char(c));
        }
        press(&mut app, KeyCode::Enter);
        assert!(app.password_prompt.is_none());
        assert_eq!(
            app.session_passwords.get("h").map(String::as_str),
            Some("hunter2")
        );
        // Nothing was written to the config.
        assert!(
            !app.hosts_state
                .ssh_config()
                .host_entries()
                .iter()
                .any(|e| e.askpass.is_some())
        );
    }

    #[test]
    fn a_missing_state_returns_to_the_host_list_instead_of_stranding() {
        let mut app = app_with("Host h\n  HostName 1.1.1.1\n");
        app.screen = Screen::PasswordPrompt;
        press(&mut app, KeyCode::Enter);
        assert_eq!(app.screen, Screen::HostList);
    }

    #[test]
    fn return_screen_prefers_an_open_file_browser() {
        let mut app = app_with("Host h\n  HostName 1.1.1.1\n");
        assert_eq!(return_screen(&app), Screen::HostList);
        app.file_browser_session = Some(crate::file_browser::FileBrowserSession {
            alias: "h".into(),
            askpass: None,
            active_pane: crate::file_browser::BrowserPane::Local,
            local_path: std::path::PathBuf::from("/tmp"),
            local_entries: Vec::new(),
            local_list_state: ratatui::widgets::ListState::default(),
            local_selected: std::collections::HashSet::new(),
            local_error: None,
            remote_path: "/home".into(),
            remote_entries: Vec::new(),
            remote_list_state: ratatui::widgets::ListState::default(),
            remote_selected: std::collections::HashSet::new(),
            remote_error: None,
            remote_loading: false,
            show_hidden: false,
            sort: crate::file_browser::BrowserSort::Name,
            confirm_copy: None,
            transferring: None,
            transfer_error: None,
            connection_recorded: false,
        });
        assert_eq!(
            return_screen(&app),
            Screen::FileBrowser { alias: "h".into() }
        );
    }
}