ggen-utils 26.5.5

Shared utilities for ggen
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
//! Comprehensive security tests for PathValidator
//!
//! Tests all edge cases and attack vectors:
//! - Path traversal variations
//! - Symlink attacks
//! - Null byte injection
//! - Unicode normalization
//! - Absolute path escapes
//! - Depth limit violations

use ggen_utils::path_validator::PathValidator;
use std::path::Path;
use tempfile::tempdir;

// ============================================================================
// Path Traversal Attack Tests
// ============================================================================

#[test]
fn test_basic_path_traversal() {
    // Arrange
    let workspace = tempdir().expect("Failed to create temp dir");
    let validator = PathValidator::new(workspace.path());

    // Act & Assert
    let attacks = vec![
        "../../../etc/passwd",
        "../../etc/passwd",
        "../etc/passwd",
        "subdir/../../etc/passwd",
        "./../../etc/passwd",
    ];

    for attack in attacks {
        let result = validator.validate(attack);
        assert!(result.is_err(), "Should block path traversal: {}", attack);
        assert!(
            result.unwrap_err().to_string().contains("traversal"),
            "Error should mention path traversal"
        );
    }
}

#[test]
fn test_encoded_path_traversal() {
    // Arrange
    let workspace = tempdir().expect("Failed to create temp dir");
    let validator = PathValidator::new(workspace.path());

    // Act & Assert - URL-encoded path traversal
    // Note: Rust's Path automatically decodes, so we test the decoded form
    let attacks = vec![
        "..%2F..%2F..%2Fetc%2Fpasswd", // URL encoded
        "..\\..\\..\\etc\\passwd",     // Windows-style (converted to /)
    ];

    for attack in attacks {
        let result = validator.validate(attack);
        // These may or may not be blocked depending on OS and Path behavior
        // The key is that if they're not blocked, they should still be within workspace
        if let Ok(safe_path) = result {
            assert!(
                safe_path.absolute().starts_with(workspace.path()),
                "Path should be within workspace"
            );
        }
    }
}

#[test]
fn test_double_encoded_traversal() {
    // Arrange
    let workspace = tempdir().expect("Failed to create temp dir");
    let validator = PathValidator::new(workspace.path());

    // Act & Assert
    let attacks = vec![
        "%252e%252e%252f%252e%252e%252fetc%252fpasswd", // Double URL encoded
    ];

    for attack in attacks {
        // These should be treated as literal filenames (safe but weird)
        let result = validator.validate(attack);
        // As long as it doesn't escape workspace, it's fine
        if let Ok(safe_path) = result {
            assert!(safe_path.absolute().starts_with(workspace.path()));
        }
    }
}

// ============================================================================
// Null Byte Injection Tests
// ============================================================================

#[test]
fn test_null_byte_injection() {
    // Arrange
    let workspace = tempdir().expect("Failed to create temp dir");
    let validator = PathValidator::new(workspace.path());

    // Act & Assert
    let attacks = vec![
        "file.txt\0.evil",
        "safe\0../../etc/passwd",
        "\0",
        "dir/\0/file.txt",
    ];

    for attack in attacks {
        let result = validator.validate(attack);
        assert!(result.is_err(), "Should block null byte: {}", attack);
        assert!(
            result.unwrap_err().to_string().contains("null byte"),
            "Error should mention null byte"
        );
    }
}

// ============================================================================
// Absolute Path Tests
// ============================================================================

#[test]
fn test_absolute_paths_blocked_by_default() {
    // Arrange
    let workspace = tempdir().expect("Failed to create temp dir");
    let validator = PathValidator::new(workspace.path());

    // Act & Assert
    let absolute_paths = vec!["/etc/passwd", "/tmp/evil", "/var/log/secrets"];

    for path in absolute_paths {
        let result = validator.validate(path);
        assert!(result.is_err(), "Should block absolute path: {}", path);
        assert!(
            result.unwrap_err().to_string().contains("Absolute"),
            "Error should mention absolute path"
        );
    }
}

#[test]
fn test_absolute_paths_within_workspace_allowed() {
    // Arrange
    let workspace = tempdir().expect("Failed to create temp dir");
    let test_file = workspace.path().join("test.txt");
    std::fs::write(&test_file, "content").expect("Failed to create test file");

    let validator = PathValidator::new(workspace.path()).with_absolute_paths(true);

    // Act
    let result = validator.validate(&test_file);

    // Assert
    assert!(
        result.is_ok(),
        "Should allow absolute path within workspace"
    );
}

#[test]
fn test_absolute_paths_outside_workspace_blocked() {
    // Arrange
    let workspace = tempdir().expect("Failed to create temp dir");
    let validator = PathValidator::new(workspace.path()).with_absolute_paths(true);

    // Act - try to access /etc/passwd
    let result = validator.validate("/etc/passwd");

    // Assert
    assert!(
        result.is_err(),
        "Should block absolute path outside workspace"
    );
    assert!(
        result.unwrap_err().to_string().contains("workspace"),
        "Error should mention workspace escape"
    );
}

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

#[test]
#[cfg(unix)]
fn test_symlink_pointing_outside_workspace_blocked() {
    use std::os::unix::fs::symlink;

    // Arrange
    let workspace = tempdir().expect("Failed to create temp dir");
    let validator = PathValidator::new(workspace.path());

    // Create symlink pointing outside workspace
    let link_path = workspace.path().join("evil_link");
    symlink("/etc/passwd", &link_path).expect("Failed to create symlink");

    // Act
    let result = validator.validate("evil_link");

    // Assert
    assert!(result.is_err(), "Should block symlink escape");
}

#[test]
#[cfg(unix)]
fn test_symlink_within_workspace_allowed() {
    use std::os::unix::fs::symlink;

    // Arrange
    let workspace = tempdir().expect("Failed to create temp dir");
    let validator = PathValidator::new(workspace.path());

    // Create target file
    let target = workspace.path().join("target.txt");
    std::fs::write(&target, "content").expect("Failed to create target");

    // Create symlink within workspace
    let link_path = workspace.path().join("link.txt");
    symlink(&target, &link_path).expect("Failed to create symlink");

    // Act
    let result = validator.validate("link.txt");

    // Assert
    assert!(result.is_ok(), "Should allow symlink within workspace");
}

#[test]
#[cfg(unix)]
fn test_symlink_chain_attack() {
    use std::os::unix::fs::symlink;

    // Arrange
    let workspace = tempdir().expect("Failed to create temp dir");
    let validator = PathValidator::new(workspace.path());

    // Create chain: link1 -> link2 -> /etc/passwd
    let link1 = workspace.path().join("link1");
    let link2 = workspace.path().join("link2");
    symlink("/etc/passwd", &link2).expect("Failed to create link2");
    symlink(&link2, &link1).expect("Failed to create link1");

    // Act
    let result = validator.validate("link1");

    // Assert - should block because link2 points outside
    assert!(result.is_err(), "Should block symlink chain escape");
}

// ============================================================================
// Extension Validation Tests
// ============================================================================

#[test]
fn test_extension_whitelist_enforced() {
    // Arrange
    let workspace = tempdir().expect("Failed to create temp dir");
    let validator =
        PathValidator::new(workspace.path()).with_allowed_extensions(vec!["tmpl", "tera", "ttl"]);

    // Act & Assert - allowed extensions
    let allowed = vec!["template.tmpl", "example.tera", "ontology.ttl"];
    for path in allowed {
        let result = validator.validate(path);
        assert!(result.is_ok(), "Should allow extension: {}", path);
    }

    // Act & Assert - blocked extensions
    let blocked = vec!["script.sh", "binary.exe", "config.yaml"];
    for path in blocked {
        let result = validator.validate(path);
        assert!(result.is_err(), "Should block extension: {}", path);
        assert!(
            result.unwrap_err().to_string().contains("extension"),
            "Error should mention extension"
        );
    }
}

#[test]
fn test_extension_case_sensitivity() {
    // Arrange
    let workspace = tempdir().expect("Failed to create temp dir");
    let validator = PathValidator::new(workspace.path()).with_allowed_extensions(vec!["tera"]);

    // Act - uppercase extension
    let result = validator.validate("template.TERA");

    // Assert - should be blocked (case-sensitive)
    assert!(
        result.is_err(),
        "Extension validation should be case-sensitive"
    );
}

#[test]
fn test_double_extension_handling() {
    // Arrange
    let workspace = tempdir().expect("Failed to create temp dir");
    let validator = PathValidator::new(workspace.path()).with_allowed_extensions(vec!["tera"]);

    // Act - file with double extension
    let result = validator.validate("archive.tar.tera");

    // Assert - should validate based on last extension
    assert!(result.is_ok(), "Should check only the last extension");
}

// ============================================================================
// Depth Limit Tests
// ============================================================================

#[test]
fn test_depth_limit_enforced() {
    // Arrange
    let workspace = tempdir().expect("Failed to create temp dir");
    let validator = PathValidator::new(workspace.path()).with_max_depth(3);

    // Act & Assert - within depth
    let shallow = "a/b/c.txt";
    assert!(
        validator.validate(shallow).is_ok(),
        "Should allow path within depth limit"
    );

    // Act & Assert - exceeds depth
    let deep = "a/b/c/d/e.txt";
    let result = validator.validate(deep);
    assert!(result.is_err(), "Should block path exceeding depth");
    assert!(
        result.unwrap_err().to_string().contains("depth"),
        "Error should mention depth"
    );
}

// ============================================================================
// Unicode and Encoding Tests
// ============================================================================

#[test]
fn test_unicode_path_allowed() {
    // Arrange
    let workspace = tempdir().expect("Failed to create temp dir");
    let validator = PathValidator::new(workspace.path());

    // Act - various Unicode paths
    let unicode_paths = vec![
        "文件.txt",     // Chinese
        "файл.txt",     // Russian
        "ファイル.txt", // Japanese
        "αρχείο.txt",   // Greek
        "📁/file.txt",  // Emoji
    ];

    for path in unicode_paths {
        let result = validator.validate(path);
        assert!(result.is_ok(), "Should allow Unicode path: {}", path);
    }
}

#[test]
fn test_mixed_unicode_and_ascii() {
    // Arrange
    let workspace = tempdir().expect("Failed to create temp dir");
    let validator = PathValidator::new(workspace.path());

    // Act
    let mixed = "templates/例え_example_文件.tera";
    let result = validator.validate(mixed);

    // Assert
    assert!(result.is_ok(), "Should allow mixed Unicode/ASCII");
}

// ============================================================================
// Edge Cases
// ============================================================================

#[test]
fn test_empty_path_blocked() {
    // Arrange
    let workspace = tempdir().expect("Failed to create temp dir");
    let validator = PathValidator::new(workspace.path());

    // Act
    let result = validator.validate("");

    // Assert
    assert!(result.is_err(), "Should block empty path");
    assert!(
        result.unwrap_err().to_string().contains("empty"),
        "Error should mention empty path"
    );
}

#[test]
fn test_current_directory_reference() {
    // Arrange
    let workspace = tempdir().expect("Failed to create temp dir");
    let validator = PathValidator::new(workspace.path());

    // Act
    let paths = vec!["./file.txt", "./dir/./file.txt", "././file.txt"];

    // Assert - these should be normalized and allowed
    for path in paths {
        let result = validator.validate(path);
        assert!(
            result.is_ok(),
            "Should allow current dir reference: {}",
            path
        );
    }
}

#[test]
fn test_trailing_slashes() {
    // Arrange
    let workspace = tempdir().expect("Failed to create temp dir");
    let validator = PathValidator::new(workspace.path());

    // Act
    let paths = vec!["dir/", "dir/file.txt/"];

    // Assert
    for path in paths {
        let result = validator.validate(path);
        // Should either succeed or fail gracefully
        // The key is no panic or undefined behavior
        let _ = result;
    }
}

#[test]
fn test_very_long_path() {
    // Arrange
    let workspace = tempdir().expect("Failed to create temp dir");
    let validator = PathValidator::new(workspace.path());

    // Act - create a very long path (but not exceeding OS limits)
    let long_component = "a".repeat(255); // Max filename on most systems
    let long_path = format!("{}/file.txt", long_component);
    let result = validator.validate(&long_path);

    // Assert - should handle gracefully
    let _ = result;
}

#[test]
fn test_special_characters_in_filename() {
    // Arrange
    let workspace = tempdir().expect("Failed to create temp dir");
    let validator = PathValidator::new(workspace.path());

    // Act - filenames with special chars (legal on most systems)
    let special_paths = vec![
        "file-name.txt",
        "file_name.txt",
        "file.name.txt",
        "file (1).txt",
        "file@2024.txt",
    ];

    for path in special_paths {
        let result = validator.validate(path);
        assert!(result.is_ok(), "Should allow special char path: {}", path);
    }
}

// ============================================================================
// Batch Validation Tests
// ============================================================================

#[test]
fn test_batch_validation_all_valid() {
    // Arrange
    let workspace = tempdir().expect("Failed to create temp dir");
    let validator = PathValidator::new(workspace.path());

    let paths = vec!["file1.txt", "file2.txt", "dir/file3.txt"];

    // Act
    let result = validator.validate_batch(&paths);

    // Assert
    assert!(result.is_ok());
    let safe_paths = result.expect("All should validate");
    assert_eq!(safe_paths.len(), 3);
}

#[test]
fn test_batch_validation_with_invalid() {
    // Arrange
    let workspace = tempdir().expect("Failed to create temp dir");
    let validator = PathValidator::new(workspace.path());

    let paths = vec!["file1.txt", "../../../etc/passwd", "file3.txt"];

    // Act
    let result = validator.validate_batch(&paths);

    // Assert - should fail on first invalid path
    assert!(result.is_err());
}

// ============================================================================
// SafePath API Tests
// ============================================================================

#[test]
fn test_safe_path_accessors() {
    // Arrange
    let workspace = tempdir().expect("Failed to create temp dir");
    let validator = PathValidator::new(workspace.path());

    // Act
    let safe_path = validator
        .validate("templates/example.tera")
        .expect("Should validate");

    // Assert
    assert_eq!(safe_path.extension(), Some("tera"));
    assert_eq!(safe_path.file_name(), Some("example.tera"));
    assert_eq!(safe_path.as_path(), Path::new("templates/example.tera"));
    assert!(safe_path.absolute().is_absolute());
}

#[test]
fn test_safe_path_as_ref() {
    // Arrange
    let workspace = tempdir().expect("Failed to create temp dir");
    let validator = PathValidator::new(workspace.path());

    // Act
    let safe_path = validator.validate("file.txt").expect("Should validate");

    // Assert - should work with AsRef<Path>
    fn take_path_ref<P: AsRef<Path>>(p: P) -> bool {
        p.as_ref().to_str().is_some()
    }

    assert!(take_path_ref(&safe_path));
}