strict-path 0.2.1

Secure path handling for untrusted input. Prevents directory traversal, symlink escapes, and 19+ real-world CVE attack patterns.
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
//! Comprehensive security tests for /proc filesystem handling
//!
//! This module provides exhaustive coverage for /proc magic symlink behaviors,
//! ensuring that `strict-path` (via `soft-canonicalize`) correctly handles
//! all variations of /proc paths to prevent container escapes.
//!
//! Covers soft-canonicalize issue #44: Indirect symlinks to /proc/PID/root

#[cfg(target_os = "linux")]
use crate::PathBoundary;
#[cfg(all(target_os = "linux", feature = "virtual-path"))]
use crate::VirtualRoot;
#[cfg(target_os = "linux")]
use std::path::PathBuf;

// =============================================================================
// ISSUE #44 SPECIFIC TESTS
// These tests directly verify the fix for soft-canonicalize issue #44
// =============================================================================

/// Issue #44 core scenario: Indirect symlink to /proc/self/root
/// This is THE critical test that proves issue #44 is fixed.
#[cfg(target_os = "linux")]
#[test]
fn issue_44_indirect_symlink_to_proc_root() {
    let temp = tempfile::tempdir().unwrap();
    let link_path = temp.path().join("container_root");
    let target = PathBuf::from("/proc/self/root");

    std::os::unix::fs::symlink(&target, &link_path).unwrap();

    // THE BUG: Previously this would resolve to "/" instead of "/proc/self/root"
    match PathBoundary::<()>::try_new(&link_path) {
        Ok(proc_dir) => {
            let boundary_str = proc_dir.interop_path().to_string_lossy();

            // CRITICAL ASSERTION 1: Must NOT be "/"
            assert_ne!(
                boundary_str, "/",
                "ISSUE #44 NOT FIXED: Indirect symlink to /proc/self/root resolved to /"
            );

            // CRITICAL ASSERTION 2: Must preserve /proc/self/root prefix
            assert!(
                boundary_str.starts_with("/proc/self/root"),
                "ISSUE #44 NOT FIXED: Expected /proc/self/root prefix, got: {boundary_str}"
            );

            println!("Issue #44 FIXED: Resolved to {boundary_str}");
        }
        Err(e) => {
            panic!("Issue #44 test failed unexpectedly: {:?}", e);
        }
    }
}

/// Issue #44 with suffix: Access file through indirect symlink
#[cfg(target_os = "linux")]
#[test]
fn issue_44_indirect_symlink_with_suffix() {
    let temp = tempfile::tempdir().unwrap();
    let link_path = temp.path().join("container");
    let target = PathBuf::from("/proc/self/root");

    std::os::unix::fs::symlink(&target, &link_path).unwrap();

    // Create boundary through the symlink, then join a path
    if let Ok(proc_dir) = PathBoundary::<()>::try_new(&link_path) {
        match proc_dir.strict_join("etc/passwd") {
            Ok(path) => {
                let path_str = path.strictpath_display().to_string();

                // Must be /proc/self/root/etc/passwd, NOT /etc/passwd
                assert!(
                    path_str.starts_with("/proc/self/root"),
                    "Path through indirect symlink escaped: {}",
                    path_str
                );
                assert_ne!(
                    path_str, "/etc/passwd",
                    "SECURITY BUG: Accessed host /etc/passwd through indirect symlink"
                );
            }
            Err(_) => {
                // Path doesn't exist is acceptable
            }
        }
    }
}

/// Issue #44 with VirtualRoot: Indirect symlink creates proper virtual boundary
#[cfg(all(target_os = "linux", feature = "virtual-path"))]
#[test]
fn issue_44_virtualroot_indirect_symlink() {
    let temp = tempfile::tempdir().unwrap();
    let link_path = temp.path().join("vroot_link");
    let target = PathBuf::from("/proc/self/root");

    std::os::unix::fs::symlink(&target, &link_path).unwrap();

    match VirtualRoot::<()>::try_new(&link_path) {
        Ok(vroot) => {
            let vroot_str = vroot.interop_path().to_string_lossy();

            // Must NOT be "/"
            assert_ne!(
                vroot_str, "/",
                "ISSUE #44 NOT FIXED: VirtualRoot through indirect symlink resolved to /"
            );

            // Must preserve /proc prefix
            assert!(
                vroot_str.starts_with("/proc/self/root"),
                "ISSUE #44 NOT FIXED: VirtualRoot lost prefix: {}",
                vroot_str
            );

            // Test virtual_join through this root
            if let Ok(vpath) = vroot.virtual_join("etc/passwd") {
                let system_path = vpath.as_unvirtual().strictpath_display().to_string();
                assert!(
                    system_path.starts_with("/proc/self/root"),
                    "VirtualPath escaped via indirect symlink: {}",
                    system_path
                );
            }
        }
        Err(e) => {
            panic!("VirtualRoot indirect symlink test failed: {:?}", e);
        }
    }
}

// =============================================================================
// CHAINED SYMLINK TESTS
// =============================================================================

/// Test chained symlinks pointing to /proc/self/root
/// link1 -> link2 -> /proc/self/root
#[cfg(target_os = "linux")]
#[test]
fn test_chained_symlinks_to_proc_root() {
    let temp = tempfile::tempdir().unwrap();
    let link2 = temp.path().join("link2");
    let link1 = temp.path().join("link1");
    let target = PathBuf::from("/proc/self/root");

    // Create chain: link1 -> link2 -> /proc/self/root
    std::os::unix::fs::symlink(&target, &link2).unwrap();
    std::os::unix::fs::symlink(&link2, &link1).unwrap();

    match PathBoundary::<()>::try_new(&link1) {
        Ok(proc_dir) => {
            let boundary_str = proc_dir.interop_path().to_string_lossy();
            println!("Resolved chained boundary: {}", boundary_str);

            assert_ne!(boundary_str, "/", "Chained symlink resolved to /");
            assert!(
                boundary_str.starts_with("/proc/self/root"),
                "Chained symlink lost prefix: {}",
                boundary_str
            );
        }
        Err(e) => eprintln!("Chained symlink test failed: {:?}", e),
    }
}

/// Test triple-chained symlinks
#[cfg(target_os = "linux")]
#[test]
fn test_triple_chained_symlinks_to_proc() {
    let temp = tempfile::tempdir().unwrap();
    let link3 = temp.path().join("link3");
    let link2 = temp.path().join("link2");
    let link1 = temp.path().join("link1");
    let target = PathBuf::from("/proc/self/root");

    // Create chain: link1 -> link2 -> link3 -> /proc/self/root
    std::os::unix::fs::symlink(&target, &link3).unwrap();
    std::os::unix::fs::symlink(&link3, &link2).unwrap();
    std::os::unix::fs::symlink(&link2, &link1).unwrap();

    match PathBoundary::<()>::try_new(&link1) {
        Ok(proc_dir) => {
            let boundary_str = proc_dir.interop_path().to_string_lossy();
            assert_ne!(boundary_str, "/", "Triple-chained symlink resolved to /");
            assert!(
                boundary_str.starts_with("/proc/self/root"),
                "Triple-chained symlink lost prefix: {}",
                boundary_str
            );
        }
        Err(e) => eprintln!("Triple-chained symlink test: {:?}", e),
    }
}

// =============================================================================
// /proc/self/cwd TESTS
// =============================================================================

/// Test /proc/self/cwd magic symlink
#[cfg(target_os = "linux")]
#[test]
fn test_proc_self_cwd_preservation() {
    let target = PathBuf::from("/proc/self/cwd");

    // Direct access
    if let Ok(proc_dir) = PathBoundary::<()>::try_new(&target) {
        let boundary_str = proc_dir.interop_path().to_string_lossy();
        assert_ne!(boundary_str, "/", "/proc/self/cwd resolved to /");
        assert!(
            boundary_str.starts_with("/proc/self/cwd"),
            "/proc/self/cwd lost prefix: {}",
            boundary_str
        );
    }

    // Indirect access via symlink
    let temp = tempfile::tempdir().unwrap();
    let link = temp.path().join("link_to_cwd");
    std::os::unix::fs::symlink(&target, &link).unwrap();

    if let Ok(proc_dir) = PathBoundary::<()>::try_new(&link) {
        let boundary_str = proc_dir.interop_path().to_string_lossy();
        assert_ne!(boundary_str, "/", "Symlink to /proc/self/cwd resolved to /");
        assert!(
            boundary_str.starts_with("/proc/self/cwd"),
            "Symlink to /proc/self/cwd lost prefix: {}",
            boundary_str
        );
    }
}

// =============================================================================
// /proc/thread-self/root TESTS
// =============================================================================

/// Test /proc/thread-self/root magic symlink
#[cfg(target_os = "linux")]
#[test]
fn test_proc_thread_self_root_preservation() {
    let target = PathBuf::from("/proc/thread-self/root");

    // Direct access
    if let Ok(proc_dir) = PathBoundary::<()>::try_new(&target) {
        let boundary_str = proc_dir.interop_path().to_string_lossy();
        assert_ne!(boundary_str, "/", "/proc/thread-self/root resolved to /");
        assert!(
            boundary_str.starts_with("/proc/thread-self/root"),
            "/proc/thread-self/root lost prefix: {}",
            boundary_str
        );
    }
}

/// Test indirect symlink to /proc/thread-self/root
#[cfg(target_os = "linux")]
#[test]
fn test_indirect_symlink_to_proc_thread_self_root() {
    let temp = tempfile::tempdir().unwrap();
    let link = temp.path().join("thread_root_link");
    let target = PathBuf::from("/proc/thread-self/root");

    std::os::unix::fs::symlink(&target, &link).unwrap();

    if let Ok(proc_dir) = PathBoundary::<()>::try_new(&link) {
        let boundary_str = proc_dir.interop_path().to_string_lossy();
        assert_ne!(
            boundary_str, "/",
            "Indirect symlink to /proc/thread-self/root resolved to /"
        );
        assert!(
            boundary_str.starts_with("/proc/thread-self/root"),
            "Indirect symlink lost /proc/thread-self/root prefix: {}",
            boundary_str
        );
    }
}

// =============================================================================
// /proc/{PID}/root TESTS (with actual PID)
// =============================================================================

/// Test /proc/{PID}/root with actual process ID
#[cfg(target_os = "linux")]
#[test]
fn test_proc_pid_root_with_actual_pid() {
    let pid = std::process::id();
    let target = PathBuf::from(format!("/proc/{}/root", pid));

    if !target.exists() {
        eprintln!("Skipping: /proc/{}/root does not exist", pid);
        return;
    }

    if let Ok(proc_dir) = PathBoundary::<()>::try_new(&target) {
        let boundary_str = proc_dir.interop_path().to_string_lossy();
        assert_ne!(boundary_str, "/", "/proc/{}/root resolved to /", pid);
        assert!(
            boundary_str.contains("/proc/") && boundary_str.contains("/root"),
            "/proc/{}/root lost prefix: {}",
            pid,
            boundary_str
        );
    }
}

/// Test indirect symlink to /proc/{PID}/root
#[cfg(target_os = "linux")]
#[test]
fn test_indirect_symlink_to_proc_pid_root() {
    let pid = std::process::id();
    let target = PathBuf::from(format!("/proc/{}/root", pid));

    if !target.exists() {
        eprintln!("Skipping: /proc/{}/root does not exist", pid);
        return;
    }

    let temp = tempfile::tempdir().unwrap();
    let link = temp.path().join("pid_root_link");
    std::os::unix::fs::symlink(&target, &link).unwrap();

    if let Ok(proc_dir) = PathBoundary::<()>::try_new(&link) {
        let boundary_str = proc_dir.interop_path().to_string_lossy();
        assert_ne!(
            boundary_str, "/",
            "Indirect symlink to /proc/{}/root resolved to /",
            pid
        );
    }
}

// =============================================================================
// DEEP LINK TESTS
// =============================================================================

/// Test symlink pointing deep into /proc
/// link -> /proc/self/root/etc
#[cfg(target_os = "linux")]
#[test]
fn test_symlink_deep_into_proc() {
    let temp = tempfile::tempdir().unwrap();
    let link = temp.path().join("link_deep");
    // Note: /proc/self/root/etc usually exists
    let target = PathBuf::from("/proc/self/root/etc");

    if !target.exists() {
        eprintln!("Skipping deep link test: /proc/self/root/etc does not exist");
        return;
    }

    std::os::unix::fs::symlink(&target, &link).unwrap();

    if let Ok(proc_dir) = PathBoundary::<()>::try_new(&link) {
        let boundary_str = proc_dir.interop_path().to_string_lossy();
        assert_ne!(boundary_str, "/etc", "Deep link resolved to host /etc");
        assert!(
            boundary_str.starts_with("/proc/self/root"),
            "Deep link lost /proc prefix: {}",
            boundary_str
        );
    }
}

// =============================================================================
// SYMLINK LOOP TESTS
// =============================================================================

/// Test symlink loop involving /proc
/// link1 -> link2 -> link1
/// This should fail gracefully, not hang
#[cfg(target_os = "linux")]
#[test]
fn test_symlink_loop_handling() {
    let temp = tempfile::tempdir().unwrap();
    let link1 = temp.path().join("loop1");
    let link2 = temp.path().join("loop2");

    std::os::unix::fs::symlink(&link2, &link1).unwrap();
    std::os::unix::fs::symlink(&link1, &link2).unwrap();

    // Should return an error (Too many levels of symbolic links), not hang
    let result = PathBoundary::<()>::try_new(&link1);
    assert!(result.is_err(), "Symlink loop should fail");
}

// =============================================================================
// MIXED CHAIN TESTS
// =============================================================================

/// Test mixed chain: link -> dir -> link -> /proc/self/root
#[cfg(target_os = "linux")]
#[test]
fn test_mixed_chain_to_proc() {
    let temp = tempfile::tempdir().unwrap();
    let dir = temp.path().join("subdir");
    std::fs::create_dir(&dir).unwrap();

    let inner_link = dir.join("inner");
    let target = PathBuf::from("/proc/self/root");
    std::os::unix::fs::symlink(&target, &inner_link).unwrap();

    let outer_link = temp.path().join("outer");
    std::os::unix::fs::symlink(&inner_link, &outer_link).unwrap();

    if let Ok(proc_dir) = PathBoundary::<()>::try_new(&outer_link) {
        let boundary_str = proc_dir.interop_path().to_string_lossy();
        assert_ne!(boundary_str, "/", "Mixed chain resolved to /");
        assert!(
            boundary_str.starts_with("/proc/self/root"),
            "Mixed chain lost prefix: {}",
            boundary_str
        );
    }
}

// =============================================================================
// SECURITY SCENARIOS FROM ISSUE #44
// =============================================================================

/// Security scenario: Container boundary validation bypass
/// This is the attack scenario described in issue #44
#[cfg(target_os = "linux")]
#[test]
fn security_container_boundary_bypass_prevented() {
    let temp = tempfile::tempdir().unwrap();
    let container_root_link = temp.path().join("container_root");
    let target = PathBuf::from("/proc/self/root");

    std::os::unix::fs::symlink(&target, &container_root_link).unwrap();

    // Simulate the attack from issue #44:
    // 1. Create boundary from symlink
    // 2. Attempt to access file through boundary
    // 3. Verify it stays within the namespace

    match PathBoundary::<()>::try_new(&container_root_link) {
        Ok(proc_dir) => {
            // The boundary should be /proc/self/root, NOT /
            let boundary_str = proc_dir.interop_path().to_string_lossy();

            if boundary_str == "/" {
                panic!(
                    "SECURITY VULNERABILITY: Container boundary resolved to /, \
                     this would allow accessing any file on the host!"
                );
            }

            // Try to access /etc/shadow through the boundary
            match proc_dir.strict_join("etc/shadow") {
                Ok(path) => {
                    let path_str = path.strictpath_display().to_string();
                    // MUST be /proc/self/root/etc/shadow, NOT /etc/shadow
                    assert!(
                        path_str.starts_with("/proc/self/root"),
                        "SECURITY BUG: Accessed host path: {}",
                        path_str
                    );
                }
                Err(_) => {
                    // Path doesn't exist or access denied - this is fine
                }
            }

            // Try traversal attack
            match proc_dir.strict_join("../../../etc/shadow") {
                Ok(path) => {
                    let path_str = path.strictpath_display().to_string();
                    // Even if somehow accepted, must stay in namespace
                    assert!(
                        path_str.starts_with("/proc/self/root"),
                        "SECURITY BUG: Traversal escaped to: {}",
                        path_str
                    );
                }
                Err(_) => {
                    // Expected: traversal rejected
                }
            }
        }
        Err(e) => {
            panic!("Unexpected boundary creation failure: {:?}", e);
        }
    }
}

/// Security: Verify host root is NEVER accessible through indirect /proc symlinks
#[cfg(target_os = "linux")]
#[test]
fn security_host_root_never_accessible() {
    let temp = tempfile::tempdir().unwrap();

    // Create multiple indirection patterns
    let patterns = [
        ("/proc/self/root", "direct_proc"),
        ("/proc/self/root", "indirect_single"),
    ];

    for (target, name) in patterns {
        let link = temp.path().join(name);
        std::os::unix::fs::symlink(target, &link).unwrap();

        if let Ok(proc_dir) = PathBoundary::<()>::try_new(&link) {
            let boundary_str = proc_dir.interop_path().to_string_lossy();

            assert_ne!(
                boundary_str, "/",
                "Pattern '{}' (-> {}) resolved to host root /",
                name, target
            );
        }
    }
}