ssh-cli 0.5.2

Native Rust CLI that gives LLMs (Claude Code, Cursor, Windsurf) the ability to operate remote servers via SSH over stdin/stdout
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
// SPDX-License-Identifier: MIT OR Apache-2.0
// G-SECDEV-05: pure module — no `unsafe` permitted (crate root allows only OS FFI / test env).
#![forbid(unsafe_code)]
//! File path validation and normalization.
//!
//! Cross-platform guards against path traversal, Windows reserved device
//! names, forbidden characters, Unicode NFC drift (macOS NFD vs Linux NFC),
//! and legacy Windows `MAX_PATH` (260) limits without the `\\?\` prefix.

use crate::errors::{SshCliError, SshCliResult};
use std::path::Path;
use unicode_normalization::UnicodeNormalization;

#[inline]
fn path_err(msg: impl Into<String>) -> SshCliError {
    SshCliError::InvalidArgument(msg.into())
}

/// Names reserved by the Windows file system (case-insensitive).
const WINDOWS_RESERVED_NAMES: &[&str] = &[
    "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8",
    "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",
];

/// Characters forbidden in file names (Windows-illegal or shell-hostile on Unix).
const FORBIDDEN_CHARS: &[char] = &['/', '\\', ':', '*', '?', '"', '<', '>', '|', '\0'];

/// Legacy Windows `MAX_PATH` including the trailing NUL (Win32 default without long-path prefix).
pub const WINDOWS_MAX_PATH: usize = 260;

/// Maximum length of a single path component on Windows (excluding separators).
pub const WINDOWS_MAX_COMPONENT: usize = 255;

const _: () = assert!(!WINDOWS_RESERVED_NAMES.is_empty());
const _: () = assert!(!FORBIDDEN_CHARS.is_empty());
const _: () = assert!(WINDOWS_MAX_PATH > WINDOWS_MAX_COMPONENT);

/// Validates a file name (no path separators).
///
/// Rejects:
/// - Empty strings
/// - Names with `..` components (path traversal)
/// - Forbidden characters
/// - Windows reserved names (case-insensitive)
/// - Names ending with a dot or space (problematic on Windows)
///
/// # Examples
///
/// ```
/// use ssh_cli::paths::validate_name;
///
/// assert!(validate_name("meu-servidor").is_ok());
/// assert!(validate_name("../etc/passwd").is_err());
/// assert!(validate_name("CON").is_err());
/// ```
///
/// # Errors
/// Returns [`SshCliError::InvalidArgument`] if the name is empty, contains
/// traversal/forbidden characters/whitespace, or is Windows-reserved.
pub fn validate_name(name: &str) -> SshCliResult<()> {
    if name.is_empty() {
        return Err(path_err("file name cannot be empty"));
    }

    if name.contains("..") {
        return Err(path_err(format!(
            "file name contains path traversal component: '{name}'"
        )));
    }

    for c in FORBIDDEN_CHARS {
        if name.contains(*c) {
            return Err(path_err(format!(
                "file name contains forbidden character '{}': '{name}'",
                c.escape_default()
            )));
        }
    }

    let name_upper = name.to_uppercase();
    // Also checks without extension (e.g. "NUL.txt" is forbidden on Windows)
    let root = name_upper.split('.').next().unwrap_or(&name_upper);
    if WINDOWS_RESERVED_NAMES.contains(&root) {
        return Err(path_err(format!(
            "file name uses a Windows reserved name: '{name}'"
        )));
    }

    if name.ends_with('.') || name.ends_with(' ') {
        return Err(path_err(format!(
            "file name cannot end with a dot or space: '{name}'"
        )));
    }

    // GAP-AUD-VAL-001: reject any internal whitespace (spaces/tabs) so VPS registry
    // keys stay shell/TOML/agent-safe single tokens.
    if name.chars().any(|c| c.is_whitespace()) {
        return Err(path_err(format!(
            "file name cannot contain whitespace: '{name}'"
        )));
    }

    Ok(())
}

/// Normalizes a file name to Unicode NFC form.
///
/// NFC normalization is required for consistent comparisons across OSes
/// (macOS often stores NFD; Linux typically uses NFC).
///
/// # Examples
///
/// ```
/// use ssh_cli::paths::normalize_nfc;
///
/// let nfc = normalize_nfc("cafe");
/// assert_eq!(nfc, "cafe");
/// assert_eq!(normalize_nfc(&nfc), nfc); // idempotent
/// ```
#[must_use]
pub fn normalize_nfc(name: &str) -> String {
    name.nfc().collect()
}

/// Validates and normalizes a file name in one operation.
///
/// Returns the NFC-normalized name if all validations pass.
///
/// # Examples
///
/// ```
/// use ssh_cli::paths::validate_and_normalize;
///
/// let name = validate_and_normalize("lab-01").unwrap();
/// assert_eq!(name.as_str(), "lab-01");
/// assert!(validate_and_normalize("../etc").is_err());
/// ```
///
/// # Errors
/// Returns [`SshCliError::Domain`] / [`SshCliError::InvalidArgument`] if
/// [`validate_name`] / [`crate::domain::VpsName::try_new`] fails.
pub fn validate_and_normalize(name: &str) -> SshCliResult<crate::domain::VpsName> {
    // G-TYPE-08: return refined type (proof not discarded as bare String).
    // DomainError maps via From → SshCliError::Domain (G-ERR-02).
    Ok(crate::domain::VpsName::try_new(name)?)
}

/// Validates that a path has no traversal components.
///
/// Checks all path segments separated by `/` or `\`.
///
/// # Examples
///
/// ```
/// use ssh_cli::paths::validate_no_traversal;
///
/// assert!(validate_no_traversal("/tmp/file.bin").is_ok());
/// assert!(validate_no_traversal("a/../../etc/passwd").is_err());
/// assert!(validate_no_traversal("").is_err());
/// ```
///
/// # Errors
/// Empty path or any `..` segment → [`SshCliError::InvalidArgument`].
pub fn validate_no_traversal(path: &str) -> SshCliResult<()> {
    if path.is_empty() {
        return Err(path_err("path cannot be empty"));
    }

    let segments = path.split(['/', '\\']);
    for segment in segments {
        if segment == ".." {
            return Err(path_err(format!(
                "path contains path traversal component: '{path}'"
            )));
        }
    }

    Ok(())
}

/// Default cap for primary-key files (hex is 64 chars; allow whitespace/BOM).
pub const MAX_SECRETS_KEY_FILE_BYTES: u64 = 4_096;

/// Cap for `config.toml` (local agent registry — not a multi-tenant store).
pub const MAX_CONFIG_TOML_BYTES: u64 = 4 * 1024 * 1024;

/// Cap for TOFU `known_hosts` text file.
pub const MAX_KNOWN_HOSTS_BYTES: u64 = 1_024 * 1024;

const _: () = assert!(MAX_SECRETS_KEY_FILE_BYTES >= 64);
const _: () = assert!(MAX_CONFIG_TOML_BYTES >= 4_096);
const _: () = assert!(MAX_KNOWN_HOSTS_BYTES >= 256);

/// Resolves the XDG config directory for [`crate::constants::APP_NAME`].
///
/// Uses `directories::ProjectDirs` (Linux: `$XDG_CONFIG_HOME/ssh-cli` or
/// `~/.config/ssh-cli`). Does **not** honor `SSH_CLI_HOME` or `--config-dir`
/// — callers layer those overrides themselves.
///
/// # Errors
/// Returns [`SshCliError::XdgDirectory`] when the home/config root cannot be
/// determined (rare headless environments) — G-ERR-03.
pub fn xdg_config_dir() -> SshCliResult<std::path::PathBuf> {
    directories::ProjectDirs::from(
        crate::constants::PROJECT_QUALIFIER,
        crate::constants::PROJECT_ORGANIZATION,
        crate::constants::APP_NAME,
    )
    .map(|d| d.config_dir().to_path_buf())
    .ok_or(SshCliError::XdgDirectory)
}

/// Returns `true` when `path` uses the Windows extended-length prefix (`\\?\` or `//?/`).
#[must_use]
pub fn has_windows_long_path_prefix(path: &Path) -> bool {
    let s = path.as_os_str().to_string_lossy();
    s.starts_with(r"\\?\") || s.starts_with("//?/")
}

/// Validates a local filesystem path against Windows legacy length limits.
///
/// On **all** platforms this checks component length (≤255) so config written
/// on Linux remains openable if copied to Windows. On **Windows** (or when
/// `force_windows_rules` is true in tests), rejects total path length ≥
/// [`WINDOWS_MAX_PATH`] unless the path already uses the `\\?\` long-path
/// prefix.
///
/// Remote SCP paths are **not** validated here (remote FS is Unix-like).
///
/// # Errors
/// Returns [`SshCliError::InvalidArgument`] when a component or the full path
/// exceeds platform limits.
pub fn validate_local_path_length(path: &Path) -> SshCliResult<()> {
    validate_local_path_length_inner(path, cfg!(windows))
}

fn validate_local_path_length_inner(path: &Path, enforce_windows_total: bool) -> SshCliResult<()> {
    // Split on both separators so Windows-style paths are validated even when
    // this code runs on Unix hosts (CI, cross-compile checks, agent sandboxes).
    let raw = path.as_os_str().to_string_lossy();
    let stripped = raw
        .strip_prefix(r"\\?\")
        .or_else(|| raw.strip_prefix("//?/"))
        .unwrap_or(raw.as_ref());
    for segment in stripped.split(['/', '\\']) {
        if segment.is_empty() || segment == "." || segment == ".." {
            continue;
        }
        // Drive letter ("C:") is not subject to the 255-byte file-name limit.
        if segment.len() == 2 && segment.as_bytes()[1] == b':' {
            continue;
        }
        if segment.len() > WINDOWS_MAX_COMPONENT {
            return Err(path_err(format!(
                "path component exceeds {WINDOWS_MAX_COMPONENT} bytes (Windows limit): '{segment}'"
            )));
        }
    }

    if enforce_windows_total && !has_windows_long_path_prefix(path) {
        // Lossy UTF-8 length as a conservative Win32 MAX_PATH estimate.
        let encoded_len = raw.len();
        // Win32 MAX_PATH counts the trailing NUL; reject at 259 visible chars.
        if encoded_len >= WINDOWS_MAX_PATH - 1 {
            return Err(path_err(format!(
                "local path length {encoded_len} approaches Windows MAX_PATH ({WINDOWS_MAX_PATH}); \
                 use a shorter path or the \\\\?\\ extended-length prefix"
            )));
        }
    }

    Ok(())
}

/// Reads a UTF-8 text file with a hard byte cap (memory / OOM hygiene).
///
/// Checks `metadata().len()` first, then reads with `Take(max+1)` so a TOCTOU
/// grow cannot allocate unbounded heap. Rejects files larger than `max_bytes`.
///
/// # Errors
/// Returns [`std::io::Error`] on I/O failure or when the file exceeds `max_bytes`.
pub fn read_text_capped(path: &std::path::Path, max_bytes: u64) -> std::io::Result<String> {
    use std::io::Read;

    let meta = std::fs::metadata(path)?;
    if meta.len() > max_bytes {
        return Err(std::io::Error::new(
            std::io::ErrorKind::InvalidData,
            format!(
                "file {} exceeds max size of {max_bytes} bytes",
                path.display()
            ),
        ));
    }

    let file = std::fs::File::open(path)?;
    let mut limited = file.take(max_bytes.saturating_add(1));
    let mut buf = String::new();
    limited.read_to_string(&mut buf)?;
    if (buf.len() as u64) > max_bytes {
        return Err(std::io::Error::new(
            std::io::ErrorKind::InvalidData,
            format!(
                "file {} exceeds max size of {max_bytes} bytes",
                path.display()
            ),
        ));
    }
    Ok(buf)
}

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

    #[test]
    fn common_valid_name_passes() {
        assert!(validate_name("meu-servidor").is_ok());
        assert!(validate_name("vps_01").is_ok());
        assert!(validate_name("servidor.produção").is_ok());
    }

    #[test]
    fn empty_name_rejected() {
        assert!(validate_name("").is_err());
    }

    #[test]
    fn path_traversal_rejected() {
        assert!(validate_name("..").is_err());
        assert!(validate_name("../etc/passwd").is_err());
        assert!(validate_name("foo/../bar").is_err());
    }

    #[test]
    fn forbidden_chars_rejected() {
        assert!(validate_name("foo/bar").is_err());
        assert!(validate_name("foo\\bar").is_err());
        assert!(validate_name("foo:bar").is_err());
        assert!(validate_name("foo*bar").is_err());
        assert!(validate_name("foo?bar").is_err());
    }

    #[test]
    fn windows_reserved_names_rejected() {
        assert!(validate_name("CON").is_err());
        assert!(validate_name("con").is_err());
        assert!(validate_name("NUL.txt").is_err());
        assert!(validate_name("COM1").is_err());
        assert!(validate_name("LPT9").is_err());
    }

    #[test]
    fn name_ending_with_dot_rejected() {
        assert!(validate_name("file.").is_err());
    }

    #[test]
    fn name_with_internal_space_rejected() {
        assert!(validate_name("a b").is_err());
        assert!(validate_name("a\tb").is_err());
    }

    #[test]
    fn name_ending_with_space_rejected() {
        assert!(validate_name("file ").is_err());
    }

    #[test]
    fn normalize_nfc_returns_string() {
        let result = normalize_nfc("servidor");
        assert_eq!(result, "servidor");
    }

    #[test]
    fn validate_and_normalize_returns_valid_string() {
        let result = validate_and_normalize("meu-servidor").unwrap();
        assert_eq!(result.as_str(), "meu-servidor");
    }

    #[test]
    fn validate_no_traversal_accepts_normal_path() {
        assert!(validate_no_traversal("/home/usuario/file.txt").is_ok());
        assert!(validate_no_traversal("relative/path/file.txt").is_ok());
    }

    #[test]
    fn validate_no_traversal_rejects_traversal() {
        assert!(validate_no_traversal("/home/../etc/passwd").is_err());
        assert!(validate_no_traversal("../secreto").is_err());
    }

    #[test]
    fn validate_no_traversal_rejects_empty() {
        assert!(validate_no_traversal("").is_err());
    }

    #[test]
    fn name_with_brazilian_accents_valid() {
        assert!(validate_name("produção").is_ok());
        assert!(validate_name("ação-configuração").is_ok());
    }

    #[test]
    fn name_with_cjk_unicode_valid() {
        assert!(validate_name("server-\u{4e16}\u{754c}").is_ok());
    }

    #[test]
    fn name_with_emoji_valid() {
        assert!(validate_name("server-\u{1f680}").is_ok());
    }

    #[test]
    fn windows_reserved_mixed_case_rejected() {
        assert!(validate_name("cOn").is_err());
        assert!(validate_name("Nul").is_err());
        assert!(validate_name("lPt1").is_err());
    }

    #[test]
    fn normalize_nfc_converts_nfd_to_nfc() {
        let nfd = "e\u{0301}"; // e + combining acute
        let nfc = "\u{00e9}"; // é precomposed
        assert_eq!(normalize_nfc(nfd), nfc);
    }

    #[test]
    fn normalize_nfc_preserves_nfc() {
        let nfc = "\u{00e9}";
        assert_eq!(normalize_nfc(nfc), nfc);
    }

    #[test]
    fn normalize_nfc_idempotent() {
        let input = "cafe\u{0301}";
        let once = normalize_nfc(input);
        let twice = normalize_nfc(&once);
        assert_eq!(once, twice);
    }

    #[test]
    fn validate_and_normalize_converts_nfd() {
        let result = validate_and_normalize("cafe\u{0301}").unwrap();
        assert_eq!(result.as_str(), "caf\u{00e9}");
    }

    #[test]
    fn validate_no_traversal_rejects_backslash() {
        assert!(validate_no_traversal("foo\\..\\bar").is_err());
    }

    #[test]
    fn validate_no_traversal_accepts_dot_alone() {
        assert!(validate_no_traversal("./file").is_ok());
    }

    #[test]
    fn long_path_prefix_detected() {
        assert!(has_windows_long_path_prefix(Path::new(r"\\?\C:\very\long")));
        assert!(has_windows_long_path_prefix(Path::new("//?/C:/very/long")));
        assert!(!has_windows_long_path_prefix(Path::new(r"C:\short")));
    }

    #[test]
    fn component_over_255_rejected() {
        let long = "a".repeat(WINDOWS_MAX_COMPONENT + 1);
        let p = Path::new(&long);
        let err = validate_local_path_length_inner(p, false).unwrap_err();
        assert!(err.to_string().contains("255"));
    }

    #[test]
    fn windows_total_path_limit_enforced() {
        // Build a path whose lossy length is >= 259 without long-path prefix.
        let mut s = String::from("C:");
        while s.len() < WINDOWS_MAX_PATH - 1 {
            s.push_str("\\seg");
        }
        let p = Path::new(&s);
        assert!(validate_local_path_length_inner(p, true).is_err());
        let extended = format!(r"\\?\{s}");
        assert!(validate_local_path_length_inner(Path::new(&extended), true).is_ok());
    }

    #[test]
    fn short_local_path_ok() {
        assert!(validate_local_path_length(Path::new("/home/user/.config/ssh-cli/config.toml")).is_ok());
    }

    #[test]
    fn read_text_capped_accepts_small_file() {
        let dir = tempfile::tempdir().unwrap();
        let p = dir.path().join("k.txt");
        std::fs::write(&p, "abc").unwrap();
        let s = read_text_capped(&p, 64).unwrap();
        assert_eq!(s, "abc");
    }

    #[test]
    fn read_text_capped_rejects_oversize() {
        let dir = tempfile::tempdir().unwrap();
        let p = dir.path().join("big.txt");
        std::fs::write(&p, "0123456789").unwrap();
        let err = read_text_capped(&p, 4).unwrap_err();
        assert_eq!(err.kind(), std::io::ErrorKind::InvalidData);
    }

}