strict-path 0.1.2

Handle paths from external or unknown sources securely. Defends against 19+ real-world CVEs including symlinks, Windows 8.3 short names, and encoding tricks and exploits.
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
//! Security tests for Linux /proc/PID/root magic symlink handling
//!
//! ## Vulnerability Background (soft-canonicalize issue #44)
//!
//! `/proc/PID/root` and `/proc/PID/cwd` are "magic symlinks" in Linux that provide
//! access to a process's root filesystem and current working directory within its
//! mount namespace (e.g., a container).
//!
//! ### The Problem
//!
//! `std::fs::canonicalize` (and by extension, older versions of `soft_canonicalize`)
//! calls `readlink()` on these magic symlinks, which returns `/`. This completely
//! loses the namespace context:
//!
//! ```text
//! // DANGEROUS OLD BEHAVIOR:
//! soft_canonicalize("/proc/1234/root") → "/"
//! soft_canonicalize("/proc/1234/root/etc/passwd") → "/etc/passwd"
//! ```
//!
//! ### Security Impact
//!
//! When using `PathBoundary::try_new("/proc/PID/root")` to create a container boundary,
//! the boundary silently becomes `/` (host root!), making security checks useless:
//!
//! ```text
//! // ATTACK SCENARIO:
//! let boundary = PathBoundary::try_new("/proc/12345/root")?;
//! // boundary becomes "/" (WRONG!)
//!
//! boundary.strict_join("../../../etc/shadow")?;
//! // This now checks against "/" not the container root!
//! // SECURITY BYPASS!
//! ```
//!
//! ### The Fix (proc-canonicalize integration)
//!
//! `soft-canonicalize` v0.5.0 integrates `proc-canonicalize` which preserves the
//! namespace prefix instead of resolving it:
//!
//! ```text
//! // CORRECT NEW BEHAVIOR:
//! soft_canonicalize("/proc/1234/root") → "/proc/1234/root"
//! soft_canonicalize("/proc/1234/root/etc/passwd") → "/proc/1234/root/etc/passwd"
//! ```
//!
//! ## Test Categories
//!
//! These tests validate that `strict-path` correctly handles Linux namespace boundaries:
//!
//! 1. **Black-box tests**: Test from attacker's perspective without knowledge of internals
//! 2. **White-box tests**: Test internal behavior and edge cases
//! 3. **CVE resistance tests**: Test against known container escape patterns
//! 4. **Container boundary tests**: Test PathBoundary/VirtualRoot with /proc paths
//!
//! Regression, edge-case, multi-container, and documentation tests live in
//! `proc_magic_symlink_regression.rs`.

// All tests in this module are Linux-only since /proc/PID/root is a Linux-specific construct
// (gated via #[cfg(target_os = "linux")] in mod.rs)

use crate::{PathBoundary, StrictPathError};
use std::fs;
use std::path::{Path, PathBuf};

// =============================================================================
// HELPER FUNCTIONS
// =============================================================================

/// Get a valid /proc/PID/root path for testing.
/// Uses /proc/self/root which always exists and refers to the current process's root.
pub(super) fn get_proc_self_root() -> PathBuf {
    PathBuf::from("/proc/self/root")
}

/// Get the current process's PID
pub(super) fn get_self_pid() -> u32 {
    std::process::id()
}

// =============================================================================
// BLACK-BOX SECURITY TESTS
// Tests from an attacker's perspective without knowledge of internal implementation
// =============================================================================

/// Black-box test: Verify /proc/self/root is preserved as a path prefix
/// This is the fundamental test that the magic symlink is NOT resolved to "/"
#[test]
fn blackbox_proc_self_root_prefix_preserved() {
    let proc_self_root = get_proc_self_root();

    // Create a boundary at /proc/self/root
    // The key security property: this should NOT become "/"
    match PathBoundary::<()>::try_new(&proc_self_root) {
        Ok(container_dir) => {
            // The boundary's interop path must preserve the /proc/self/root prefix
            let boundary_path = container_dir.interop_path();
            let boundary_str = boundary_path.to_string_lossy();

            assert!(
                boundary_str.starts_with("/proc/self/root"),
                "SECURITY FAILURE: /proc/self/root was resolved to {} instead of preserving the namespace prefix",
                boundary_str
            );

            // Additional sanity check: it should NOT be just "/"
            assert_ne!(
                boundary_str, "/",
                "SECURITY FAILURE: /proc/self/root was incorrectly resolved to /"
            );
        }
        Err(e) => {
            // On systems where /proc/self/root isn't accessible, skip gracefully
            eprintln!("Skipping test: /proc/self/root not accessible: {e:?}");
        }
    }
}

/// Black-box test: Attacker attempts to escape container boundary using traversal
#[test]
fn blackbox_container_escape_via_traversal_rejected() {
    let proc_self_root = get_proc_self_root();

    if let Ok(container_dir) = PathBoundary::<()>::try_new(&proc_self_root) {
        // Attack pattern: escape using parent directory traversal
        let attack_patterns = [
            "../etc/shadow",
            "../../etc/passwd",
            "../../../var/log/auth.log",
            "../../../../root/.ssh/id_rsa",
            "../../../../../etc/sudoers",
        ];

        for attack_input in attack_patterns {
            let result = container_dir.strict_join(attack_input);
            match result {
                Err(StrictPathError::PathEscapesBoundary { .. }) => {
                    // Expected: traversal attack correctly rejected
                }
                Err(StrictPathError::PathResolutionError { .. }) => {
                    // Also acceptable: path doesn't exist
                }
                Ok(path) => {
                    // Verify the path is still within the boundary (clamped)
                    let path_str = path.strictpath_to_string_lossy();
                    assert!(
                        path_str.starts_with("/proc/self/root"),
                        "SECURITY FAILURE: Attack pattern '{}' escaped to: {}",
                        attack_input,
                        path_str
                    );
                }
                Err(e) => {
                    panic!(
                        "Unexpected error for attack pattern '{}': {:?}",
                        attack_input, e
                    );
                }
            }
        }
    }
}

/// Black-box test: Attacker attempts to access host /etc/passwd via /proc path
#[test]
fn blackbox_host_etc_passwd_access_blocked() {
    let proc_self_root = get_proc_self_root();

    if let Ok(container_dir) = PathBoundary::<()>::try_new(&proc_self_root) {
        // The boundary is at /proc/self/root
        // Accessing "etc/passwd" should give /proc/self/root/etc/passwd
        // NOT /etc/passwd on the host
        match container_dir.strict_join("etc/passwd") {
            Ok(passwd_path) => {
                let path_str = passwd_path.strictpath_to_string_lossy();

                // CRITICAL: The path MUST be within /proc/self/root
                assert!(
                    path_str.starts_with("/proc/self/root"),
                    "SECURITY FAILURE: etc/passwd resolved to host path: {}",
                    path_str
                );

                // CRITICAL: It must NOT be the raw /etc/passwd
                assert_ne!(
                    path_str, "/etc/passwd",
                    "SECURITY FAILURE: Boundary bypass allowed access to /etc/passwd"
                );
            }
            Err(e) => {
                // Path doesn't exist or resolution failed - this is acceptable
                eprintln!("Expected: etc/passwd access resulted in: {:?}", e);
            }
        }
    }
}

/// Black-box test: Attacker creates boundary then attempts absolute path escape
#[test]
fn blackbox_absolute_path_escape_rejected() {
    let proc_self_root = get_proc_self_root();

    if let Ok(container_dir) = PathBoundary::<()>::try_new(&proc_self_root) {
        // Attempt to use absolute paths to escape
        let absolute_escape_attempts = ["/etc/shadow", "/root/.bashrc", "/var/log/syslog", "/home"];

        for attack_input in absolute_escape_attempts {
            let result = container_dir.strict_join(attack_input);
            match result {
                Err(StrictPathError::PathEscapesBoundary { .. }) => {
                    // Expected: absolute path escape rejected
                }
                Err(StrictPathError::PathResolutionError { .. }) => {
                    // Also acceptable
                }
                Ok(path) => {
                    // If accepted, must still be within the namespace boundary
                    let path_str = path.strictpath_to_string_lossy();
                    assert!(
                        path_str.starts_with("/proc/self/root"),
                        "SECURITY FAILURE: Absolute path '{}' escaped to: {}",
                        attack_input,
                        path_str
                    );
                }
                Err(e) => {
                    panic!("Unexpected error for '{}': {:?}", attack_input, e);
                }
            }
        }
    }
}

// =============================================================================
// WHITE-BOX SECURITY TESTS
// Tests with knowledge of internal implementation details
// =============================================================================

/// White-box test: Verify /proc/PID/root patterns are all preserved
#[test]
fn whitebox_all_proc_root_variants_preserved() {
    // All magic symlink patterns that should be preserved
    let magic_patterns = [
        "/proc/self/root",
        "/proc/thread-self/root",
        // Note: We can't test /proc/1/root without root privileges
    ];

    // Also test with actual PID
    let pid = get_self_pid();
    let pid_pattern = format!("/proc/{}/root", pid);

    let mut all_patterns: Vec<&str> = magic_patterns.to_vec();
    let pid_pattern_ref: &str = &pid_pattern;
    all_patterns.push(pid_pattern_ref);

    for pattern in all_patterns {
        if !Path::new(pattern).exists() {
            continue;
        }

        match PathBoundary::<()>::try_new(pattern) {
            Ok(container_dir) => {
                let boundary_path = container_dir.interop_path();
                let boundary_str = boundary_path.to_string_lossy();

                // Extract the expected prefix (everything up to and including "root")
                // Pattern: /proc/{something}/root
                assert!(
                    boundary_str.starts_with("/proc/"),
                    "Boundary for '{}' doesn't start with /proc/: {}",
                    pattern,
                    boundary_str
                );
                assert!(
                    boundary_str.contains("/root"),
                    "Boundary for '{}' doesn't contain /root: {}",
                    pattern,
                    boundary_str
                );
                assert_ne!(
                    boundary_str, "/",
                    "SECURITY FAILURE: '{}' was resolved to /",
                    pattern
                );
            }
            Err(e) => {
                eprintln!("Pattern '{}' not accessible: {:?}", pattern, e);
            }
        }
    }
}

/// White-box test: Verify /proc/PID/cwd patterns are also preserved
#[test]
fn whitebox_proc_cwd_patterns_preserved() {
    let cwd_patterns = ["/proc/self/cwd", "/proc/thread-self/cwd"];

    let pid = get_self_pid();
    let pid_cwd = format!("/proc/{}/cwd", pid);

    for pattern in cwd_patterns
        .iter()
        .chain(std::iter::once(&pid_cwd.as_str()))
    {
        let pattern = *pattern;
        if !Path::new(pattern).exists() {
            continue;
        }

        match PathBoundary::<()>::try_new(pattern) {
            Ok(container_dir) => {
                let boundary_path = container_dir.interop_path();
                let boundary_str = boundary_path.to_string_lossy();

                // The path should preserve the /proc/.../ prefix structure
                // Note: /proc/self/cwd may resolve to the actual CWD path, but the
                // proc-canonicalize fix should preserve the magic symlink prefix
                // when the path IS the magic symlink itself
                assert!(
                    boundary_str.starts_with("/proc/") || boundary_str == pattern,
                    "Boundary for '{}' unexpectedly resolved: {}",
                    pattern,
                    boundary_str
                );
            }
            Err(e) => {
                eprintln!("Pattern '{}' not accessible: {:?}", pattern, e);
            }
        }
    }
}

/// White-box test: Verify symlink resolution inside /proc namespace is handled correctly
#[test]
fn whitebox_symlink_inside_proc_namespace() {
    // Create a temp directory to simulate container filesystem
    let temp = tempfile::tempdir().unwrap();
    let container_root = temp.path();

    // Create a symlink inside the "container" that points to a valid internal path
    let etc_dir = container_root.join("etc");
    let var_dir = container_root.join("var");
    fs::create_dir_all(&etc_dir).unwrap();
    fs::create_dir_all(&var_dir).unwrap();

    // Create /etc/passwd inside container
    fs::write(etc_dir.join("passwd"), "root:x:0:0::/root:/bin/bash").unwrap();

    // Create a symlink /var/log -> /etc (valid inside container)
    let log_link = var_dir.join("log");
    std::os::unix::fs::symlink(&etc_dir, &log_link).unwrap();

    // Now create a boundary and test symlink following
    let container_dir: PathBoundary = PathBoundary::try_new(container_root).unwrap();

    // Following the symlink should stay within the boundary
    match container_dir.strict_join("var/log/passwd") {
        Ok(path) => {
            assert!(
                path.strictpath_starts_with(container_dir.interop_path()),
                "Symlink escape: {} is outside boundary",
                path.strictpath_to_string_lossy()
            );
        }
        Err(e) => {
            // Symlink resolution may fail if target doesn't exist correctly
            eprintln!("Symlink test: {:?}", e);
        }
    }
}

/// White-box test: Verify that deeply nested /proc paths don't lose context
#[test]
fn whitebox_nested_proc_paths_preserve_context() {
    let proc_self_root = get_proc_self_root();

    if let Ok(container_dir) = PathBoundary::<()>::try_new(&proc_self_root) {
        // Test deeply nested paths within the namespace
        let nested_paths = [
            "usr/local/bin",
            "home/user/.config",
            "var/lib/dpkg/status",
            "etc/apt/sources.list.d",
        ];

        for nested_input in nested_paths {
            match container_dir.strict_join(nested_input) {
                Ok(path) => {
                    let path_str = path.strictpath_to_string_lossy();
                    assert!(
                        path_str.starts_with("/proc/self/root"),
                        "Nested path '{}' lost namespace context: {}",
                        nested_input,
                        path_str
                    );
                }
                Err(_) => {
                    // Path doesn't exist - acceptable
                }
            }
        }
    }
}

// =============================================================================
// CVE RESISTANCE TESTS
// Tests against known container escape CVE patterns
// =============================================================================

/// CVE resistance test: Symlink-based container escape patterns
/// Similar to CVE-2019-5736 (runc container escape via /proc/self/exe)
#[test]
fn cve_resistance_runc_style_proc_self_escape() {
    let proc_self_root = get_proc_self_root();

    if let Ok(container_dir) = PathBoundary::<()>::try_new(&proc_self_root) {
        // Attack patterns inspired by runc CVE-2019-5736
        let attack_patterns = [
            "../proc/self/exe",       // Escape via /proc/self/exe
            "../../proc/self/fd/0",   // Escape via file descriptors
            "../../../proc/1/root",   // Escape to init's root
            "../../../../proc/1/cwd", // Escape to init's cwd
        ];

        for attack_input in attack_patterns {
            let result = container_dir.strict_join(attack_input);
            match result {
                Err(StrictPathError::PathEscapesBoundary { .. }) => {
                    // Expected: escape attempt rejected
                }
                Err(StrictPathError::PathResolutionError { .. }) => {
                    // Also acceptable: path doesn't exist
                }
                Ok(path) => {
                    // If somehow accepted, verify containment
                    let path_str = path.strictpath_to_string_lossy();
                    assert!(
                        path_str.starts_with("/proc/self/root"),
                        "CVE-2019-5736 style escape succeeded: '{}' -> '{}'",
                        attack_input,
                        path_str
                    );
                }
                Err(e) => {
                    // Other errors are acceptable
                    eprintln!("Pattern '{}': {:?}", attack_input, e);
                }
            }
        }
    }
}

/// CVE resistance test: Docker/Podman escape via /proc paths
/// Related to container escape vulnerabilities in container runtimes
#[test]
fn cve_resistance_container_runtime_escape_patterns() {
    let proc_self_root = get_proc_self_root();

    if let Ok(container_dir) = PathBoundary::<()>::try_new(&proc_self_root) {
        // Patterns used in various container escape attempts
        let escape_patterns = [
            // Break out of overlay filesystem
            "../../../host/etc/passwd",
            // Access docker socket
            "../../../var/run/docker.sock",
            // Access kubelet credentials
            "../../../var/lib/kubelet/pods",
            // Access host's /proc
            "../proc",
            "../../../proc/1/ns/mnt",
        ];

        for attack_input in escape_patterns {
            let result = container_dir.strict_join(attack_input);
            match result {
                Err(StrictPathError::PathEscapesBoundary { .. }) => {
                    // Expected: container escape rejected
                }
                Err(_) => {
                    // Other errors acceptable
                }
                Ok(path) => {
                    // Verify containment if accepted
                    let path_str = path.strictpath_to_string_lossy();
                    assert!(
                        path_str.starts_with("/proc/self/root"),
                        "Container escape pattern '{}' succeeded: {}",
                        attack_input,
                        path_str
                    );
                }
            }
        }
    }
}

/// CVE resistance test: Namespace confusion attacks
/// Attacker tries to confuse the namespace boundary detection
#[test]
fn cve_resistance_namespace_confusion_attacks() {
    let proc_self_root = get_proc_self_root();

    if let Ok(container_dir) = PathBoundary::<()>::try_new(&proc_self_root) {
        // Tricky patterns that might confuse namespace detection
        let confusion_patterns = [
            "proc/self/root",         // Without leading slash
            "./proc/self/root",       // Relative form
            "proc/../etc/passwd",     // Inject proc in path
            "./../../../proc/1/root", // Mixed relative + absolute
        ];

        for attack_input in confusion_patterns {
            let result = container_dir.strict_join(attack_input);
            match result {
                Ok(path) => {
                    let path_str = path.strictpath_to_string_lossy();
                    // Must stay within namespace boundary
                    assert!(
                        path_str.starts_with("/proc/self/root"),
                        "Namespace confusion attack '{}' escaped: {}",
                        attack_input,
                        path_str
                    );
                }
                Err(_) => {
                    // Rejection is acceptable
                }
            }
        }
    }
}

// =============================================================================
// PATHBOUNDARY CONTAINER BOUNDARY TESTS
// =============================================================================

/// PathBoundary test: Creating boundary at /proc/self/root maintains isolation
#[test]
fn pathboundary_proc_root_maintains_isolation() {
    let proc_self_root = get_proc_self_root();

    if let Ok(container_dir) = PathBoundary::<()>::try_new(&proc_self_root) {
        // The boundary should be exactly at /proc/self/root
        let boundary_path_str = container_dir.interop_path().to_string_lossy().to_string();

        // Not just "/", must preserve namespace
        assert!(
            boundary_path_str.starts_with("/proc/self/root"),
            "PathBoundary lost namespace context: {}",
            boundary_path_str
        );

        // Any path operation must stay within this boundary
        if let Ok(etc_path) = container_dir.strict_join("etc") {
            assert!(
                etc_path.strictpath_starts_with(container_dir.interop_path()),
                "Path escaped PathBoundary"
            );
        }
    }
}

/// PathBoundary test: Strict join with traversal is rejected
#[test]
fn pathboundary_strict_join_rejects_traversal() {
    let proc_self_root = get_proc_self_root();

    if let Ok(container_dir) = PathBoundary::<()>::try_new(&proc_self_root) {
        // Traversal must be rejected
        let result = container_dir.strict_join("../../../etc/passwd");

        match result {
            Err(StrictPathError::PathEscapesBoundary { .. }) => {
                // Correct behavior
            }
            Ok(path) => {
                // If somehow accepted (e.g., path doesn't exist), verify containment
                let path_str = path.strictpath_to_string_lossy();
                assert!(
                    path_str.starts_with("/proc/self/root"),
                    "Traversal escaped boundary: {}",
                    path_str
                );
            }
            Err(_) => {
                // Other errors acceptable
            }
        }
    }
}