bashkit 0.18.0

Awesomely fast virtual sandbox with bash and file system
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
//! Default SSH handler using russh.
//!
//! Provides a real SSH transport backed by the `russh` crate.
//! Used automatically when no custom [`SshHandler`] is set.

use std::sync::Arc;
use std::time::Duration;

use async_trait::async_trait;
use base64::Engine;

use super::config::TrustedHostKey;
use super::handler::{SshHandler, SshOutput, SshTarget};

/// Shell-escape a string for safe interpolation into a remote command.
/// Wraps in single quotes and escapes embedded single quotes.
fn shell_escape(s: &str) -> String {
    format!("'{}'", s.replace('\'', "'\\''"))
}

/// SSH client handler with host key verification.
///
/// THREAT[TM-SSH-006]: When strict host key checking is enabled (default),
/// connections are rejected unless the server key matches a trusted key.
struct ClientHandler {
    /// Target host for this connection (used to look up trusted keys).
    host: String,
    /// Whether to reject unknown host keys.
    strict: bool,
    /// Trusted host keys to verify against.
    trusted_keys: Vec<TrustedHostKey>,
}

impl russh::client::Handler for ClientHandler {
    type Error = russh::Error;

    async fn check_server_key(
        &mut self,
        server_public_key: &russh::keys::PublicKeyOrCertificate,
    ) -> Result<bool, Self::Error> {
        if !self.strict {
            // THREAT[TM-SSH-006]: Warn when accepting unverified host keys.
            eprintln!(
                "WARNING: ssh: accepting unverified host key for '{}' \
                 (strict_host_key_checking is disabled — vulnerable to MITM)",
                self.host
            );
            return Ok(true);
        }

        // THREAT[TM-SSH-006]: russh 0.63 widened this callback to also deliver
        // CA-signed host *certificates*, not just raw host keys. We have no CA
        // trust store, so a certificate can never be verified here. Reject it
        // rather than fall back to matching the key embedded in the
        // certificate: that would extend trust on the strength of a signature
        // chain we never validated, and would silently ignore the validity
        // window, principals and critical options the certificate carries.
        // Configuring the host key directly stays the supported path.
        let server_public_key = match server_public_key {
            russh::keys::PublicKeyOrCertificate::PublicKey { key, .. } => key,
            russh::keys::PublicKeyOrCertificate::Certificate(_) => {
                eprintln!(
                    "WARNING: ssh: rejecting host certificate for '{}' \
                     (certificate host keys are not supported; \
                     configure the host's public key as a trusted key instead)",
                    self.host
                );
                return Ok(false);
            }
        };

        // Serialize the server key for comparison.
        let server_key_str = server_public_key.to_string();

        for trusted in &self.trusted_keys {
            if trusted.host != self.host && trusted.host != "*" {
                continue;
            }
            // Compare the key type+data portion.
            if keys_match(&server_key_str, &trusted.public_key) {
                return Ok(true);
            }
        }

        eprintln!(
            "WARNING: ssh: rejecting unknown host key for '{}' \
             (no matching trusted key configured)",
            self.host
        );
        Ok(false)
    }
}

/// Compare two SSH public key strings, ignoring trailing comments.
/// Accepts formats like "ssh-ed25519 AAAA..." or "ssh-ed25519 AAAA... comment".
fn keys_match(server_key: &str, trusted_key: &str) -> bool {
    fn normalize(s: &str) -> (&str, &str) {
        let parts: Vec<&str> = s.trim().splitn(3, ' ').collect();
        if parts.len() >= 2 {
            (parts[0], parts[1])
        } else {
            (s.trim(), "")
        }
    }
    let (s_type, s_data) = normalize(server_key);
    let (t_type, t_data) = normalize(trusted_key);
    s_type == t_type && s_data == t_data
}

/// Default SSH transport using russh.
///
/// Supports password and private key authentication.
/// SCP/SFTP are implemented via remote commands (`cat`, `base64`).
pub struct RusshHandler {
    timeout: Duration,
    /// THREAT[TM-SSH-004]: Streaming size limit to prevent OOM from malicious servers.
    max_response_bytes: usize,
    /// THREAT[TM-SSH-006]: Whether to verify host keys.
    strict_host_key_checking: bool,
    /// Trusted host keys for verification.
    trusted_host_keys: Vec<TrustedHostKey>,
}

impl RusshHandler {
    pub fn new(
        timeout: Duration,
        max_response_bytes: usize,
        strict_host_key_checking: bool,
        trusted_host_keys: Vec<TrustedHostKey>,
    ) -> Self {
        Self {
            timeout,
            max_response_bytes,
            strict_host_key_checking,
            trusted_host_keys,
        }
    }

    /// Connect and authenticate to a remote host.
    async fn connect(
        &self,
        target: &SshTarget,
    ) -> std::result::Result<russh::client::Handle<ClientHandler>, String> {
        let config = russh::client::Config {
            inactivity_timeout: Some(self.timeout),
            ..<_>::default()
        };

        let handler = ClientHandler {
            host: target.host.clone(),
            strict: self.strict_host_key_checking,
            trusted_keys: self.trusted_host_keys.clone(),
        };

        let addr = (target.host.as_str(), target.port);
        let mut session = russh::client::connect(Arc::new(config), addr, handler)
            .await
            .map_err(|e| format!("connection failed: {e}"))?;

        // Authenticate: try "none" first so the server can succeed without
        // ever seeing the configured password/key (TM-SSH secrets-exposure,
        // issue #1574). Only fall back to credentials if the server rejects
        // none-auth.
        let none_auth = session
            .authenticate_none(&target.user)
            .await
            .map_err(|e| format!("auth failed: {e}"))?;
        if none_auth.success() {
            return Ok(session);
        }

        if let Some(ref key_pem) = target.private_key {
            let key_pair = russh::keys::PrivateKey::from_openssh(key_pem.as_bytes())
                .map_err(|e| format!("invalid private key: {e}"))?;
            let auth = session
                .authenticate_publickey(
                    &target.user,
                    russh::keys::PrivateKeyWithHashAlg::new(
                        Arc::new(key_pair),
                        session
                            .best_supported_rsa_hash()
                            .await
                            .ok()
                            .flatten()
                            .flatten(),
                    ),
                )
                .await
                .map_err(|e| format!("publickey auth failed: {e}"))?;
            if !auth.success() {
                return Err("publickey authentication rejected".to_string());
            }
        } else if let Some(ref password) = target.password {
            let auth = session
                .authenticate_password(&target.user, password)
                .await
                .map_err(|e| format!("password auth failed: {e}"))?;
            if !auth.success() {
                return Err("password authentication rejected".to_string());
            }
        } else {
            return Err("ssh: authentication failed (server requires credentials)".to_string());
        }

        Ok(session)
    }
}

#[async_trait]
impl SshHandler for RusshHandler {
    async fn exec(
        &self,
        target: &SshTarget,
        command: &str,
    ) -> std::result::Result<SshOutput, String> {
        let session = self.connect(target).await?;

        let mut channel = session
            .channel_open_session()
            .await
            .map_err(|e| format!("channel open failed: {e}"))?;

        channel
            .exec(true, command)
            .await
            .map_err(|e| format!("exec failed: {e}"))?;

        let mut stdout = Vec::new();
        let mut stderr = Vec::new();
        let mut exit_code: Option<u32> = None;

        loop {
            let Some(msg) = channel.wait().await else {
                break;
            };
            match msg {
                russh::ChannelMsg::Data { ref data } => {
                    stdout.extend_from_slice(data);
                }
                russh::ChannelMsg::ExtendedData { ref data, ext: 1 } => {
                    // stderr
                    stderr.extend_from_slice(data);
                }
                russh::ChannelMsg::ExtendedData { .. } => {}
                russh::ChannelMsg::ExitStatus { exit_status } => {
                    exit_code = Some(exit_status);
                }
                _ => {}
            }
            // THREAT[TM-SSH-004]: Enforce streaming size limit to prevent OOM
            if stdout.len() + stderr.len() > self.max_response_bytes {
                let _ = channel.close().await;
                let _ = session
                    .disconnect(russh::Disconnect::ByApplication, "", "")
                    .await;
                return Err(format!(
                    "ssh: response too large (streaming limit exceeded, max {} bytes)",
                    self.max_response_bytes
                ));
            }
        }

        let _ = session
            .disconnect(russh::Disconnect::ByApplication, "", "")
            .await;

        Ok(SshOutput {
            stdout: String::from_utf8_lossy(&stdout).into_owned(),
            stderr: String::from_utf8_lossy(&stderr).into_owned(),
            exit_code: exit_code.unwrap_or(0) as i32,
        })
    }

    async fn shell(&self, target: &SshTarget) -> std::result::Result<SshOutput, String> {
        let session = self.connect(target).await?;

        let mut channel = session
            .channel_open_session()
            .await
            .map_err(|e| format!("channel open failed: {e}"))?;

        // Request a PTY so the remote TUI sends output
        channel
            .request_pty(false, "xterm", 80, 24, 0, 0, &[])
            .await
            .map_err(|e| format!("pty request failed: {e}"))?;

        channel
            .request_shell(true)
            .await
            .map_err(|e| format!("shell request failed: {e}"))?;

        let mut stdout = Vec::new();
        let mut stderr = Vec::new();
        let mut exit_code: Option<u32> = None;

        loop {
            let Some(msg) = channel.wait().await else {
                break;
            };
            match msg {
                russh::ChannelMsg::Data { ref data } => {
                    stdout.extend_from_slice(data);
                }
                russh::ChannelMsg::ExtendedData { ref data, ext: 1 } => {
                    stderr.extend_from_slice(data);
                }
                russh::ChannelMsg::ExtendedData { .. } => {}
                russh::ChannelMsg::ExitStatus { exit_status } => {
                    exit_code = Some(exit_status);
                }
                _ => {}
            }
            // THREAT[TM-SSH-004]: Enforce streaming size limit to prevent OOM
            if stdout.len() + stderr.len() > self.max_response_bytes {
                let _ = channel.close().await;
                let _ = session
                    .disconnect(russh::Disconnect::ByApplication, "", "")
                    .await;
                return Err(format!(
                    "ssh: response too large (streaming limit exceeded, max {} bytes)",
                    self.max_response_bytes
                ));
            }
        }

        let _ = session
            .disconnect(russh::Disconnect::ByApplication, "", "")
            .await;

        Ok(SshOutput {
            stdout: String::from_utf8_lossy(&stdout).into_owned(),
            stderr: String::from_utf8_lossy(&stderr).into_owned(),
            exit_code: exit_code.unwrap_or(0) as i32,
        })
    }

    async fn upload(
        &self,
        target: &SshTarget,
        remote_path: &str,
        content: &[u8],
        mode: u32,
    ) -> std::result::Result<(), String> {
        // THREAT[TM-SSH-008]: Shell-escape remote path to prevent injection
        let b64 = base64::engine::general_purpose::STANDARD.encode(content);
        let escaped_path = shell_escape(remote_path);
        let cmd = format!(
            "echo '{}' | base64 -d > {} && chmod {:o} {}",
            b64, escaped_path, mode, escaped_path
        );
        let result = self.exec(target, &cmd).await?;
        if result.exit_code != 0 {
            return Err(format!(
                "upload failed (exit {}): {}",
                result.exit_code, result.stderr
            ));
        }
        Ok(())
    }

    async fn download(
        &self,
        target: &SshTarget,
        remote_path: &str,
    ) -> std::result::Result<Vec<u8>, String> {
        // THREAT[TM-SSH-008]: Shell-escape remote path to prevent injection
        let cmd = format!("base64 < {}", shell_escape(remote_path));
        let result = self.exec(target, &cmd).await?;
        if result.exit_code != 0 {
            return Err(format!(
                "download failed (exit {}): {}",
                result.exit_code, result.stderr
            ));
        }
        let decoded = base64::engine::general_purpose::STANDARD
            .decode(result.stdout.trim())
            .map_err(|e| format!("base64 decode failed: {e}"))?;
        Ok(decoded)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_russh_handler_stores_max_response_bytes() {
        let handler = RusshHandler::new(Duration::from_secs(30), 1024, true, vec![]);
        assert_eq!(handler.max_response_bytes, 1024);
    }

    #[test]
    fn test_russh_handler_default_max_response_bytes() {
        use super::super::config::DEFAULT_MAX_RESPONSE_BYTES;
        let handler = RusshHandler::new(
            Duration::from_secs(30),
            DEFAULT_MAX_RESPONSE_BYTES,
            true,
            vec![],
        );
        assert_eq!(handler.max_response_bytes, 10_000_000);
    }

    #[test]
    fn test_shell_escape() {
        assert_eq!(shell_escape("hello"), "'hello'");
        assert_eq!(shell_escape("it's"), "'it'\\''s'");
        assert_eq!(shell_escape(""), "''");
    }

    /// Verify the streaming limit is wired through from SshConfig to RusshHandler.
    /// The actual streaming enforcement is tested via the mock handler in client.rs tests;
    /// here we verify construction and field propagation.
    #[test]
    fn test_streaming_limit_propagation() {
        use super::super::client::SshClient;
        use super::super::config::SshConfig;

        let config = SshConfig::new().max_response_bytes(512);
        let client = SshClient::new(config);
        assert_eq!(client.config().max_response_bytes, 512);
    }

    /// Regression: issue #1574. `authenticate_none` must be attempted before
    /// any credential so a server that accepts none-auth never sees the
    /// configured default password or key. We assert this by inspecting
    /// `connect()` in the source — a unit-level test of the ordering would
    /// require a real SSH server.
    #[test]
    fn test_auth_order_none_first() {
        let src = include_str!("russh_handler.rs");
        // Locate the `connect` function body.
        let connect_start = src
            .find("async fn connect(")
            .expect("connect fn must exist");
        let body = &src[connect_start..];
        // Bound the search to the function: stop at the next `}\n}\n` (end of impl).
        let none_pos = body
            .find(".authenticate_none(")
            .expect("authenticate_none must be called in connect");
        let key_pos = body
            .find(".authenticate_publickey(")
            .expect("authenticate_publickey must be called in connect");
        let pass_pos = body
            .find(".authenticate_password(")
            .expect("authenticate_password must be called in connect");
        assert!(
            none_pos < key_pos,
            "authenticate_none must precede authenticate_publickey (#1574)"
        );
        assert!(
            none_pos < pass_pos,
            "authenticate_none must precede authenticate_password (#1574)"
        );
    }

    #[test]
    fn test_strict_host_key_checking_propagation() {
        use super::super::client::SshClient;
        use super::super::config::SshConfig;

        let config = SshConfig::new().strict_host_key_checking(true);
        let client = SshClient::new(config);
        assert!(client.config().strict_host_key_checking);

        let config = SshConfig::new().strict_host_key_checking(false);
        let client = SshClient::new(config);
        assert!(!client.config().strict_host_key_checking);
    }

    #[test]
    fn test_keys_match_same_key() {
        let key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKtQ";
        assert!(keys_match(key, key));
    }

    #[test]
    fn test_keys_match_ignores_comment() {
        let server = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKtQ";
        let trusted = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKtQ user@host";
        assert!(keys_match(server, trusted));
    }

    #[test]
    fn test_keys_match_different_key() {
        let server = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKtQ";
        let trusted = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDiff";
        assert!(!keys_match(server, trusted));
    }

    #[test]
    fn test_keys_match_different_type() {
        let server = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKtQ";
        let trusted = "ssh-rsa AAAAC3NzaC1lZDI1NTE5AAAAIKtQ";
        assert!(!keys_match(server, trusted));
    }

    /// An ed25519 host certificate and the public key it certifies. Lifted from
    /// the `ssh-key` crate's test vectors so the blob is a real, parseable
    /// OpenSSH certificate rather than a hand-rolled one.
    const TEST_CERT: &str = "ssh-ed25519-cert-v01@openssh.com AAAAIHNzaC1lZDI1NTE5LWNlcnQtdjAxQG9wZW5zc2guY29tAAAAIAYkJPGaYen7NK8MwZwWmNAyRaFNsc86AU9NObU2cM2uAAAAILM+rvN+ot98qgEN796jTiQfZfG1KaT0PtFDJ/XFSqtiAAAAAAAAAAAAAAACAAAAB2VkMjU1MTkAAAAUAAAAEGhvc3QuZXhhbXBsZS5jb20AAAAAYkx3NwAAAAB8DuY3AAAAAAAAAAAAAAAAAAAAMwAAAAtzc2gtZWQyNTUxOQAAACCzPq7zfqLffKoBDe/eo04kH2XxtSmk9D7RQyf1xUqrYgAAAFMAAAALc3NoLWVkMjU1MTkAAABApVXBNiYPlPoa1BYH5G4NP9XtjTMZlm7HO5GdbLSvvAw5Vdob7Ka+23hB7isJKHYtzFGGSKXAqxp/Zi8REbCaAw== user@example.com";
    const TEST_CERT_INNER_KEY: &str =
        "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILM+rvN+ot98qgEN796jTiQfZfG1KaT0PtFDJ/XFSqti";
    /// A second, different key. Only ever used as a *configured trusted key*,
    /// which `keys_match` compares as text, so it is never parsed.
    const OTHER_KEY: &str =
        "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIF3W9vLTBqRDvJmUZeTxJ7pQPqiVLXAiDVTHnDgQqDLm";

    fn handler_trusting(keys: &[&str], strict: bool) -> ClientHandler {
        ClientHandler {
            host: "host.example.com".to_string(),
            strict,
            trusted_keys: keys
                .iter()
                .map(|k| TrustedHostKey {
                    host: "host.example.com".to_string(),
                    public_key: (*k).to_string(),
                })
                .collect(),
        }
    }

    fn as_public_key(openssh: &str) -> russh::keys::PublicKeyOrCertificate {
        russh::keys::PublicKeyOrCertificate::PublicKey {
            key: russh::keys::PublicKey::from_openssh(openssh).expect("valid test key"),
            hash_alg: None,
        }
    }

    fn as_certificate() -> russh::keys::PublicKeyOrCertificate {
        russh::keys::PublicKeyOrCertificate::Certificate(
            russh::keys::Certificate::from_openssh(TEST_CERT).expect("valid test certificate"),
        )
    }

    /// THREAT[TM-SSH-006]: a plain host key matching a trusted entry is accepted.
    #[tokio::test]
    async fn test_strict_accepts_matching_public_key() {
        use russh::client::Handler;
        let mut h = handler_trusting(&[TEST_CERT_INNER_KEY], true);
        assert!(
            h.check_server_key(&as_public_key(TEST_CERT_INNER_KEY))
                .await
                .unwrap()
        );
    }

    /// THREAT[TM-SSH-006]: a plain host key with no trusted entry is rejected.
    #[tokio::test]
    async fn test_strict_rejects_unmatched_public_key() {
        use russh::client::Handler;
        let mut h = handler_trusting(&[OTHER_KEY], true);
        assert!(
            !h.check_server_key(&as_public_key(TEST_CERT_INNER_KEY))
                .await
                .unwrap()
        );
    }

    /// THREAT[TM-SSH-006]: russh 0.63 can deliver a CA-signed host certificate
    /// here. We have no CA trust store, so it must be rejected in strict mode —
    /// *even when the key the certificate wraps is itself trusted*. Matching the
    /// embedded key would grant trust on the strength of a signature chain that
    /// was never validated, ignoring the certificate's validity window,
    /// principals and critical options.
    #[tokio::test]
    async fn test_strict_rejects_certificate_even_when_inner_key_is_trusted() {
        use russh::client::Handler;
        let mut h = handler_trusting(&[TEST_CERT_INNER_KEY], true);
        assert!(!h.check_server_key(&as_certificate()).await.unwrap());
    }

    /// A wildcard trusted entry must not open the certificate path either.
    #[tokio::test]
    async fn test_strict_rejects_certificate_with_wildcard_host() {
        use russh::client::Handler;
        let mut h = ClientHandler {
            host: "host.example.com".to_string(),
            strict: true,
            trusted_keys: vec![TrustedHostKey {
                host: "*".to_string(),
                public_key: TEST_CERT_INNER_KEY.to_string(),
            }],
        };
        assert!(!h.check_server_key(&as_certificate()).await.unwrap());
    }

    /// Non-strict mode is documented as accepting anything, certificates included.
    #[tokio::test]
    async fn test_non_strict_accepts_certificate() {
        use russh::client::Handler;
        let mut h = handler_trusting(&[], false);
        assert!(h.check_server_key(&as_certificate()).await.unwrap());
    }

    /// THREAT[TM-SSH-006]: Default strict mode rejects connections with unknown keys.
    #[tokio::test]
    async fn test_strict_mode_rejects_unknown_key() {
        let config = super::super::config::SshConfig::new()
            .allow_all()
            .strict_host_key_checking(true);
        let client = super::super::client::SshClient::new(config);
        let target = super::super::handler::SshTarget {
            host: "localhost".to_string(),
            port: 22,
            user: "test".to_string(),
            private_key: None,
            password: None,
        };
        // Connection will fail — either because no server is listening,
        // or because the host key is unknown. Either way, strict mode
        // ensures we don't silently accept keys.
        let result = client.exec(&target, "echo hi").await;
        assert!(result.is_err());
    }
}