magellan 3.3.3

Deterministic codebase mapping tool for local development
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
//! Integration tests for path traversal validation.
//!
//! These tests verify that path validation prevents directory traversal
//! attacks across all supported platforms (Linux, macOS, Windows).

use magellan::validation::{
    has_suspicious_traversal, is_safe_symlink, validate_path_within_root, PathValidationError,
};
use std::fs;
use std::path::Path;
use tempfile::TempDir;

/// Helper to create a file and return its path
fn create_test_file(dir: &Path, name: &str) -> std::path::PathBuf {
    let path = dir.join(name);
    fs::write(&path, b"test content").unwrap();
    path
}

/// Helper to create a symlink
#[cfg(any(unix, windows))]
fn create_symlink(from: &Path, to: &Path) {
    #[cfg(unix)]
    std::os::unix::fs::symlink(to, from).unwrap();

    #[cfg(windows)]
    {
        if to.is_dir() {
            std::os::windows::fs::symlink_dir(to, from).unwrap();
        } else {
            std::os::windows::fs::symlink_file(to, from).unwrap();
        }
    }
}

// =========================================================================
// Parent Directory Traversal Tests
// =========================================================================

#[test]
fn test_single_parent_traversal_rejected() {
    let temp_dir = TempDir::new().unwrap();
    let root = temp_dir.path();

    // "../something" pattern - flagged as suspicious (single parent, shallow depth)
    let traversal = root.join("../etc");
    let result = validate_path_within_root(&traversal, root);
    assert!(result.is_err());

    // Either SuspiciousTraversal or CannotCanonicalize is acceptable
    // (path doesn't exist, so canonicalization fails)
    match result {
        Err(PathValidationError::SuspiciousTraversal(_)) => {}
        Err(PathValidationError::CannotCanonicalize(_)) => {}
        e => panic!(
            "Expected SuspiciousTraversal or CannotCanonicalize, got {:?}",
            e
        ),
    }
}

#[test]
fn test_double_parent_traversal_rejected() {
    let temp_dir = TempDir::new().unwrap();
    let root = temp_dir.path();

    // "../../etc" is NOT flagged by suspicious_traversal (2 parents)
    // But should be caught by canonicalization if it escapes root
    let traversal = root.join("../../etc");
    let result = validate_path_within_root(&traversal, root);

    // Either flagged as suspicious or fails canonicalization
    // (both are acceptable outcomes)
    match result {
        Err(PathValidationError::SuspiciousTraversal(_)) => {}
        Err(PathValidationError::OutsideRoot(_, _)) => {}
        Err(PathValidationError::CannotCanonicalize(_)) => {}
        Ok(_) => panic!("Path should have been rejected"),
        Err(e) => panic!("Unexpected error: {:?}", e),
    }
}

#[test]
fn test_multiple_parent_traversal_rejected() {
    let temp_dir = TempDir::new().unwrap();
    let root = temp_dir.path();

    let traversal = root.join("../../../etc/passwd");
    let result = validate_path_within_root(&traversal, root);
    assert!(result.is_err());

    // Should be flagged as suspicious (>=3 parents)
    match &result {
        Err(PathValidationError::SuspiciousTraversal(_)) => {}
        e => panic!(
            "Expected SuspiciousTraversal error for >=3 parents, got {:?}",
            e
        ),
    }
}

#[test]
fn test_legitimate_nested_parent_accepted() {
    let temp_dir = TempDir::new().unwrap();
    let root = temp_dir.path();

    // Create a nested directory structure
    let deep = root.join("a").join("b").join("c");
    fs::create_dir_all(&deep).unwrap();
    let file = root.join("target").join("file.rs");
    fs::create_dir_all(root.join("target")).unwrap();
    fs::write(&file, b"fn test() {}").unwrap();

    // From deep directory, "../../target/file.rs" is valid
    let resolved = deep.join("../../target/file.rs");
    let result = validate_path_within_root(&resolved, root);

    // This should either work (canonicalized correctly) or fail if path doesn't exist
    // but NOT fail due to security concerns
    match result {
        Ok(canonical) => {
            // Valid path within root
            assert!(canonical.starts_with(root));
        }
        Err(PathValidationError::CannotCanonicalize(_)) => {
            // File doesn't exist yet, that's ok
        }
        Err(e) => panic!("Unexpected error: {:?}", e),
    }
}

#[test]
fn test_legitimate_two_parent_deep_path_accepted() {
    let temp_dir = TempDir::new().unwrap();
    let root = temp_dir.path();

    // Create a nested directory structure
    let subdir = root.join("project").join("src").join("module");
    fs::create_dir_all(&subdir).unwrap();

    let target = root.join("project").join("lib.rs");
    fs::write(&target, b"fn lib() {}").unwrap();

    // From module, "../../../lib.rs" escapes project but stays in root
    // Actually wait, module -> src -> project -> root
    // So "../../../lib" would try to go outside root
    // Let's test "../../lib" which goes from module to project
    let resolved = subdir.join("../../lib.rs");
    let result = validate_path_within_root(&resolved, root);

    // Should work if file exists
    match result {
        Ok(_) => {}                                           // Good
        Err(PathValidationError::CannotCanonicalize(_)) => {} // File missing, ok
        Err(e) => panic!("Unexpected error: {:?}", e),
    }
}

// =========================================================================
// Cross-Platform Path Separator Tests
// =========================================================================

#[test]
fn test_forward_slash_paths() {
    let temp_dir = TempDir::new().unwrap();
    let root = temp_dir.path();

    let file = create_test_file(root, "test.rs");
    let result = validate_path_within_root(&file, root);
    assert!(result.is_ok());
}

#[test]
#[cfg(windows)]
fn test_backslash_paths_windows() {
    let temp_dir = TempDir::new().unwrap();
    let root = temp_dir.path();

    // Create file using backslashes
    let file_path = root.join("subdir").join("test.rs");
    fs::create_dir_all(root.join("subdir")).unwrap();
    fs::write(&file_path, b"fn test() {}").unwrap();

    // Windows paths with backslashes should work
    let path_str = file_path.to_string_lossy();
    assert!(path_str.contains('\\'));

    let result = validate_path_within_root(&file_path, root);
    assert!(result.is_ok());
}

#[test]
#[cfg(windows)]
fn test_windows_backslash_traversal_rejected() {
    let temp_dir = TempDir::new().unwrap();
    let root = temp_dir.path();

    // Windows-style traversal with backslashes
    // Need to create this path as PathBuf
    let mut traversal = root.clone();
    traversal.push("..");
    traversal.push("..");
    traversal.push("..");
    traversal.push("windows");
    traversal.push("system32");

    let result = validate_path_within_root(&traversal, root);
    assert!(result.is_err());
}

#[test]
#[cfg(windows)]
fn test_windows_unc_path_rejected() {
    let temp_dir = TempDir::new().unwrap();
    let root = temp_dir.path();

    // UNC path (\\server\share\...) or extended-length path (\\?\C:\...)
    // These are absolute paths outside the root
    let unc_path = Path::new(r"\\?\C:\Windows\System32");

    let result = validate_path_within_root(unc_path, root);
    assert!(result.is_err());
}

#[test]
#[cfg(unix)]
fn test_unix_absolute_path_rejected() {
    let temp_dir = TempDir::new().unwrap();
    let root = temp_dir.path();

    // Unix absolute path
    let abs_path = Path::new("/etc/passwd");

    let result = validate_path_within_root(abs_path, root);
    assert!(result.is_err());
}

// =========================================================================
// Case Sensitivity Tests (macOS is case-insensitive, Linux is case-sensitive)
// =========================================================================

#[test]
fn test_case_sensitive_path_validation() {
    let temp_dir = TempDir::new().unwrap();
    let root = temp_dir.path();

    let file = create_test_file(root, "TestFile.rs");

    // Exact match should work
    let result = validate_path_within_root(&file, root);
    assert!(result.is_ok());

    // On case-insensitive systems (macOS, Windows), different case should still work
    // On case-sensitive systems (Linux), the path won't exist if case differs
    #[cfg(any(target_os = "macos", windows))]
    {
        let different_case = root.join("testfile.rs");
        // This may or may not exist depending on filesystem
        let _ = validate_path_within_root(&different_case, root);
    }
}

// =========================================================================
// Symlink Tests
// =========================================================================

#[test]
#[cfg(any(unix, windows))]
fn test_safe_symlink_inside_root_accepted() {
    let temp_dir = TempDir::new().unwrap();
    let root = temp_dir.path();

    // Create target file
    let target = create_test_file(root, "target.rs");

    // Create symlink
    let symlink = root.join("link.rs");
    create_symlink(&symlink, &target);

    let result = is_safe_symlink(&symlink, root);
    assert!(result.is_ok() || matches!(result, Err(PathValidationError::CannotCanonicalize(_))));
}

#[test]
#[cfg(any(unix, windows))]
fn test_symlink_outside_root_rejected() {
    let temp_dir = TempDir::new().unwrap();
    let root = temp_dir.path();

    // Create target file outside root
    let outside_dir = TempDir::new().unwrap();
    let target = create_test_file(outside_dir.path(), "outside.rs");

    // Create symlink inside root pointing outside
    let symlink = root.join("link.rs");
    create_symlink(&symlink, &target);

    let result = is_safe_symlink(&symlink, root);
    assert!(result.is_err());

    match result.unwrap_err() {
        PathValidationError::SymlinkEscape(_, _) => {}
        PathValidationError::OutsideRoot(_, _) => {}
        _ => panic!("Expected SymlinkEscape or OutsideRoot error"),
    }
}

#[test]
#[cfg(any(unix, windows))]
fn test_symlink_chain_outside_root_rejected() {
    let temp_dir = TempDir::new().unwrap();
    let root = temp_dir.path();

    // Create chain: root/link1 -> root/link2 -> outside/target
    let outside_dir = TempDir::new().unwrap();
    let target = create_test_file(outside_dir.path(), "target.rs");

    let link2 = root.join("link2.rs");
    create_symlink(&link2, &target);

    let link1 = root.join("link1.rs");
    create_symlink(&link1, &link2);

    let result = is_safe_symlink(&link1, root);
    assert!(result.is_err());
}

#[test]
#[cfg(any(unix, windows))]
fn test_broken_symlink_handled() {
    let temp_dir = TempDir::new().unwrap();
    let root = temp_dir.path();

    // Create symlink to non-existent file
    let symlink = root.join("broken.rs");
    create_symlink(&symlink, Path::new("nonexistent.rs"));

    let result = is_safe_symlink(&symlink, root);
    // Broken symlink cannot be canonicalized
    assert!(matches!(
        result,
        Err(PathValidationError::CannotCanonicalize(_))
    ));
}

// =========================================================================
// Mixed Traversal Pattern Tests
// =========================================================================

#[test]
fn test_mixed_dotdot_pattern_rejected() {
    assert!(has_suspicious_traversal("./subdir/../../etc"));
}

#[test]
fn test_mixed_slash_pattern_rejected() {
    #[cfg(windows)]
    assert!(has_suspicious_traversal(".\\subdir\\..\\..\\etc"));
}

#[test]
fn test_normal_paths_not_flagged() {
    assert!(!has_suspicious_traversal("src/main.rs"));
    assert!(!has_suspicious_traversal("./src/lib.rs"));
    assert!(!has_suspicious_traversal("../../normal/path")); // 2 parents is ok
    assert!(!has_suspicious_traversal("../parent/deep/nested/path")); // 1 parent but deep
}

// =========================================================================
// Edge Case Tests
// =========================================================================

#[test]
fn test_empty_path_components() {
    let temp_dir = TempDir::new().unwrap();
    let root = temp_dir.path();

    // Path with empty components (// on Unix)
    let _ = create_test_file(root, "test.rs");

    #[cfg(unix)]
    {
        let double_slash = Path::new(root.to_str().unwrap()).join("test.rs");
        let result = validate_path_within_root(&double_slash, root);
        // Should either work or fail gracefully
        assert!(result.is_ok() || result.is_err());
    }
}

#[test]
fn test_relative_from_root() {
    let temp_dir = TempDir::new().unwrap();
    let root = temp_dir.path();

    let _ = create_test_file(root, "test.rs");

    // Relative path from root should work
    let relative = Path::new("test.rs");
    let full = root.join(relative);
    let result = validate_path_within_root(&full, root);
    assert!(result.is_ok());
}

#[test]
fn test_dot_in_path() {
    let temp_dir = TempDir::new().unwrap();
    let root = temp_dir.path();

    let subdir = root.join("sub.dir");
    fs::create_dir(&subdir).unwrap();
    let file = subdir.join("test.rs");
    fs::write(&file, b"fn test() {}").unwrap();

    let result = validate_path_within_root(&file, root);
    assert!(result.is_ok());
}

#[test]
fn test_deep_nesting_accepted() {
    let temp_dir = TempDir::new().unwrap();
    let root = temp_dir.path();

    // Create a deeply nested directory structure
    let deep = root.join("a").join("b").join("c").join("d").join("e");
    fs::create_dir_all(&deep).unwrap();
    let file = deep.join("deep.rs");
    fs::write(&file, b"fn deep() {}").unwrap();

    let result = validate_path_within_root(&file, root);
    assert!(result.is_ok());
    assert!(result.unwrap().starts_with(root));
}

#[test]
fn test_nonexistent_file_in_root() {
    let temp_dir = TempDir::new().unwrap();
    let root = temp_dir.path();

    // File doesn't exist
    let nonexistent = root.join("nonexistent.rs");

    let result = validate_path_within_root(&nonexistent, root);
    // Should fail to canonicalize
    assert!(matches!(
        result,
        Err(PathValidationError::CannotCanonicalize(_))
    ));
}

// =========================================================================
// Platform-Specific Behavior Documentation
// =========================================================================

#[test]
fn test_path_separator_normalization() {
    // Test that path handling is consistent across platforms
    let temp_dir = TempDir::new().unwrap();
    let root = temp_dir.path();

    let subdir = root.join("src").join("main");
    fs::create_dir_all(&subdir).unwrap();
    let file = subdir.join("lib.rs");
    fs::write(&file, b"fn lib() {}").unwrap();

    // Regardless of platform, canonicalization should produce valid path
    let result = validate_path_within_root(&file, root);
    assert!(result.is_ok());

    let canonical = result.unwrap();
    assert!(canonical.starts_with(root));
}

#[test]
fn test_traversal_detection_is_platform_agnostic() {
    // Test that suspicious traversal detection works regardless of platform
    // This function doesn't do filesystem access, just string analysis

    // Unix-style
    assert!(has_suspicious_traversal("../../../etc/passwd"));
    assert!(has_suspicious_traversal("../etc"));

    // Windows-style (backslash patterns get normalized)
    assert!(has_suspicious_traversal("..\\..\\..\\windows\\system32"));

    // Mixed patterns
    assert!(has_suspicious_traversal("./subdir/../../etc"));

    // Normal paths should NOT be flagged
    assert!(!has_suspicious_traversal("src/main.rs"));
    assert!(!has_suspicious_traversal("./src/lib.rs"));
}

#[test]
fn test_validate_path_rejects_absolute_outside() {
    let temp_dir = TempDir::new().unwrap();
    let root = temp_dir.path();

    // Try to access an absolute path outside root
    #[cfg(unix)]
    let abs_path = Path::new("/etc/passwd");

    #[cfg(windows)]
    let abs_path = Path::new("C:\\Windows\\System32\\config\\SAM");

    let result = validate_path_within_root(abs_path, root);
    assert!(result.is_err());
}

#[test]
fn test_symlink_relative_inside_root() {
    let temp_dir = TempDir::new().unwrap();
    let root = temp_dir.path();

    #[cfg(any(unix, windows))]
    {
        // Create target directory
        let target_dir = root.join("target");
        fs::create_dir(&target_dir).unwrap();
        let target_file = target_dir.join("file.rs");
        fs::write(&target_file, b"fn file() {}").unwrap();

        // Create a directory
        let link_dir = root.join("links");
        fs::create_dir(&link_dir).unwrap();

        // Create relative symlink from link_dir/../link -> target_dir/file
        let symlink = link_dir.join("link.rs");
        create_symlink(&symlink, Path::new("../target/file.rs"));

        let result = is_safe_symlink(&symlink, root);
        assert!(result.is_ok());
    }
}