tldr-cli 0.1.3

CLI binary for TLDR code analysis tool
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
//! Input validation and path safety utilities for Contracts & Flow commands.
//!
//! This module provides security-focused validation functions to mitigate:
//! - **TIGER-02**: Path traversal attacks via malicious file paths
//! - **TIGER-03**: Unbounded recursion in CFG/slice computation
//! - **TIGER-04**: Memory exhaustion from large SSA graphs
//! - **TIGER-08**: Stack overflow from deeply nested ASTs
//!
//! All file paths are canonicalized and checked against project boundaries.
//! Resource limits are enforced to prevent denial-of-service conditions.

use std::fs;
use std::path::{Path, PathBuf};

use super::error::{ContractsError, ContractsResult};

// =============================================================================
// Resource Limits (TIGER Mitigations)
// =============================================================================

/// Maximum file size for analysis (10 MB).
/// Files larger than this will be rejected (TIGER-04 partial mitigation).
pub const MAX_FILE_SIZE: u64 = 10 * 1024 * 1024;

/// Warning threshold for file size (1 MB).
/// Files larger than this emit a warning but are still processed.
pub const WARN_FILE_SIZE: u64 = 1024 * 1024;

/// Maximum CFG/slice recursion depth (TIGER-03 mitigation).
/// Prevents stack overflow from deeply recursive control flow analysis.
pub const MAX_CFG_DEPTH: usize = 1000;

/// Maximum SSA nodes to construct (TIGER-04 mitigation).
/// Prevents memory exhaustion from extremely large SSA graphs.
pub const MAX_SSA_NODES: usize = 100_000;

/// Maximum AST traversal depth (TIGER-08 mitigation).
/// Prevents stack overflow from deeply nested source code.
pub const MAX_AST_DEPTH: usize = 100;

/// Maximum function name length.
pub const MAX_FUNCTION_NAME_LEN: usize = 256;

/// Maximum number of conditions to report per function.
pub const MAX_CONDITIONS_PER_FUNCTION: usize = 100;

// =============================================================================
// Blocked System Directories
// =============================================================================

/// System directories that should never be analyzed (security measure).
/// Note: We specifically target sensitive system directories, not general
/// /var or /private paths which include temp files.
const BLOCKED_PREFIXES: &[&str] = &[
    "/etc/",
    "/etc/passwd",
    "/etc/shadow",
    "/root/",
    "/sys/",
    "/proc/",
    "/dev/",
    "/var/run/",
    "/var/log/",
    "/private/etc/",  // macOS system config
    "C:\\Windows\\",  // Windows
    "C:\\System32\\", // Windows
];

// =============================================================================
// Path Validation (TIGER-02 Mitigation)
// =============================================================================

/// Validate and canonicalize a file path.
///
/// This function:
/// 1. Checks that the path exists
/// 2. Canonicalizes the path (resolves symlinks, `.`, `..`)
/// 3. Rejects paths that escape the project root (if specified)
/// 4. Rejects system directories
/// 5. Validates UTF-8 encoding
///
/// # Arguments
///
/// * `path` - The path to validate
///
/// # Returns
///
/// The canonicalized path if valid, or an error.
///
/// # Errors
///
/// - `ContractsError::FileNotFound` if the file doesn't exist
/// - `ContractsError::PathTraversal` if path escapes project or is a system dir
///
/// # Example
///
/// ```ignore
/// let valid = validate_file_path(Path::new("src/main.rs"))?;
/// assert!(valid.is_absolute());
/// ```
pub fn validate_file_path(path: &Path) -> ContractsResult<PathBuf> {
    // Check file exists
    if !path.exists() {
        return Err(ContractsError::FileNotFound {
            path: path.to_path_buf(),
        });
    }

    // Canonicalize the path (resolves symlinks, .., .)
    let canonical = fs::canonicalize(path).map_err(|_| ContractsError::FileNotFound {
        path: path.to_path_buf(),
    })?;

    // Check for system directories
    let canonical_str = canonical.to_string_lossy();
    for blocked in BLOCKED_PREFIXES {
        // Check with trailing slash for directories, or exact match for files
        if canonical_str.starts_with(blocked) || canonical_str == blocked.trim_end_matches('/') {
            return Err(ContractsError::PathTraversal {
                path: path.to_path_buf(),
            });
        }
    }

    // Validate UTF-8 (path.to_str() returns None if not valid UTF-8)
    if canonical.to_str().is_none() {
        return Err(ContractsError::PathTraversal {
            path: path.to_path_buf(),
        });
    }

    Ok(canonical)
}

/// Validate a file path ensuring it stays within a project root.
///
/// This is stricter than `validate_file_path` - it ensures the resolved
/// path is a descendant of the project root directory.
///
/// # Arguments
///
/// * `path` - The path to validate
/// * `project_root` - The project root directory to stay within
///
/// # Returns
///
/// The canonicalized path if valid and within project root.
///
/// # Errors
///
/// - `ContractsError::FileNotFound` if the file doesn't exist
/// - `ContractsError::PathTraversal` if path escapes project root
pub fn validate_file_path_in_project(path: &Path, project_root: &Path) -> ContractsResult<PathBuf> {
    // First do basic validation
    let canonical = validate_file_path(path)?;

    // Canonicalize project root too
    let canonical_root =
        fs::canonicalize(project_root).map_err(|_| ContractsError::FileNotFound {
            path: project_root.to_path_buf(),
        })?;

    // Check that canonical path starts with canonical root
    if !canonical.starts_with(&canonical_root) {
        return Err(ContractsError::PathTraversal {
            path: path.to_path_buf(),
        });
    }

    Ok(canonical)
}

/// Check if a path contains path traversal patterns.
///
/// This is a quick check for suspicious patterns before canonicalization.
/// Returns true if the path looks suspicious.
pub fn has_path_traversal_pattern(path: &Path) -> bool {
    let path_str = path.to_string_lossy();

    // Check for explicit traversal patterns
    if path_str.contains("..") {
        return true;
    }

    // Check for null bytes (could be used to truncate paths)
    if path_str.contains('\0') {
        return true;
    }

    false
}

// =============================================================================
// Line Number Validation
// =============================================================================

/// Validate line number range.
///
/// Ensures:
/// - start <= end
/// - Both are within valid range (1 to max)
///
/// # Arguments
///
/// * `start` - Start line (1-indexed)
/// * `end` - End line (1-indexed)
/// * `max` - Maximum valid line number (typically file line count)
///
/// # Returns
///
/// Ok(()) if valid, error otherwise.
///
/// # Errors
///
/// - Returns error if start > end
/// - Returns error if either line exceeds max
/// - Returns error if either line is 0
pub fn validate_line_numbers(start: u32, end: u32, max: u32) -> ContractsResult<()> {
    // Lines are 1-indexed
    if start == 0 {
        return Err(ContractsError::LineOutsideFunction {
            line: start,
            function: "unknown".to_string(),
            start: 1,
            end: max,
        });
    }

    if end == 0 {
        return Err(ContractsError::LineOutsideFunction {
            line: end,
            function: "unknown".to_string(),
            start: 1,
            end: max,
        });
    }

    // Start must be <= end
    if start > end {
        return Err(ContractsError::LineOutsideFunction {
            line: start,
            function: "unknown".to_string(),
            start: 1,
            end,
        });
    }

    // Both must be within bounds
    if start > max {
        return Err(ContractsError::LineOutsideFunction {
            line: start,
            function: "unknown".to_string(),
            start: 1,
            end: max,
        });
    }

    if end > max {
        return Err(ContractsError::LineOutsideFunction {
            line: end,
            function: "unknown".to_string(),
            start: 1,
            end: max,
        });
    }

    Ok(())
}

// =============================================================================
// Function Name Validation
// =============================================================================

/// Validate a function name for safety.
///
/// Ensures the name:
/// - Is not empty
/// - Contains only valid identifier characters
/// - Doesn't exceed maximum length
/// - Doesn't contain suspicious characters
///
/// # Arguments
///
/// * `name` - The function name to validate
///
/// # Returns
///
/// Ok(()) if valid, error otherwise.
///
/// # Errors
///
/// - `ContractsError::InvalidFunctionName` for invalid names
pub fn validate_function_name(name: &str) -> ContractsResult<()> {
    // Check empty
    if name.is_empty() {
        return Err(ContractsError::InvalidFunctionName {
            reason: "function name cannot be empty".to_string(),
        });
    }

    // Check length
    if name.len() > MAX_FUNCTION_NAME_LEN {
        return Err(ContractsError::InvalidFunctionName {
            reason: format!(
                "function name too long ({} chars, max {})",
                name.len(),
                MAX_FUNCTION_NAME_LEN
            ),
        });
    }

    // Check for suspicious characters that could be used for injection
    // Valid identifiers: letters, digits, underscore (and some languages allow $)
    let suspicious_chars = [
        ';', '(', ')', '{', '}', '[', ']', '`', '"', '\'', '\\', '/', '\0',
    ];
    for c in name.chars() {
        if suspicious_chars.contains(&c) {
            return Err(ContractsError::InvalidFunctionName {
                reason: format!("function name contains invalid character: '{}'", c),
            });
        }
    }

    // First character should be letter or underscore (standard identifier rules)
    if let Some(first) = name.chars().next() {
        if !first.is_alphabetic() && first != '_' {
            return Err(ContractsError::InvalidFunctionName {
                reason: "function name must start with letter or underscore".to_string(),
            });
        }
    }

    Ok(())
}

// =============================================================================
// Safe File Reading
// =============================================================================

/// Safely read a file with size limits and UTF-8 validation.
///
/// This function:
/// 1. Validates the file path
/// 2. Checks file size against limits
/// 3. Reads the file content
/// 4. Validates UTF-8 encoding
///
/// # Arguments
///
/// * `path` - The path to the file to read
///
/// # Returns
///
/// The file contents as a String if successful.
///
/// # Errors
///
/// - `ContractsError::FileNotFound` if file doesn't exist
/// - `ContractsError::FileTooLarge` if file exceeds MAX_FILE_SIZE
/// - `ContractsError::Io` for other IO errors
pub fn read_file_safe(path: &Path) -> ContractsResult<String> {
    // Validate path first
    let canonical = validate_file_path(path)?;

    // Check file size
    let metadata = fs::metadata(&canonical)?;
    let size = metadata.len();

    if size > MAX_FILE_SIZE {
        return Err(ContractsError::FileTooLarge {
            path: path.to_path_buf(),
            bytes: size,
            max_bytes: MAX_FILE_SIZE,
        });
    }

    // Read the file
    let content = fs::read(&canonical)?;

    // Validate UTF-8
    String::from_utf8(content).map_err(|_| ContractsError::ParseError {
        file: path.to_path_buf(),
        message: "file is not valid UTF-8".to_string(),
    })
}

/// Read a file safely, emitting a warning for large files.
///
/// Like `read_file_safe`, but also logs a warning to stderr for files
/// larger than WARN_FILE_SIZE.
///
/// # Arguments
///
/// * `path` - The path to the file to read
/// * `warn_fn` - Optional callback for warnings (if None, prints to stderr)
///
/// # Returns
///
/// The file contents as a String if successful.
pub fn read_file_safe_with_warning<F>(path: &Path, warn_fn: Option<F>) -> ContractsResult<String>
where
    F: FnOnce(&str),
{
    // Validate path first
    let canonical = validate_file_path(path)?;

    // Check file size
    let metadata = fs::metadata(&canonical)?;
    let size = metadata.len();

    if size > MAX_FILE_SIZE {
        return Err(ContractsError::FileTooLarge {
            path: path.to_path_buf(),
            bytes: size,
            max_bytes: MAX_FILE_SIZE,
        });
    }

    // Warn for large files
    if size > WARN_FILE_SIZE {
        let warning = format!(
            "Warning: {} is large ({:.1} MB), analysis may be slow",
            path.display(),
            size as f64 / 1024.0 / 1024.0
        );
        if let Some(f) = warn_fn {
            f(&warning);
        } else {
            eprintln!("{}", warning);
        }
    }

    // Read the file
    let content = fs::read(&canonical)?;

    // Validate UTF-8
    String::from_utf8(content).map_err(|_| ContractsError::ParseError {
        file: path.to_path_buf(),
        message: "file is not valid UTF-8".to_string(),
    })
}

// =============================================================================
// Depth Checking Utilities
// =============================================================================

/// Check if a depth limit has been exceeded.
///
/// Used for tracking recursion depth in CFG/slice analysis.
pub fn check_depth_limit(current_depth: usize, max_depth: usize) -> ContractsResult<()> {
    if current_depth >= max_depth {
        Err(ContractsError::SliceDepthExceeded {
            max_depth: max_depth as u32,
        })
    } else {
        Ok(())
    }
}

/// Check if SSA node count exceeds limit.
pub fn check_ssa_node_limit(node_count: usize) -> ContractsResult<()> {
    if node_count > MAX_SSA_NODES {
        Err(ContractsError::SsaTooLarge {
            nodes: node_count as u32,
            max_nodes: MAX_SSA_NODES as u32,
        })
    } else {
        Ok(())
    }
}

/// Check if AST depth exceeds limit.
pub fn check_ast_depth(depth: usize, file: &Path) -> ContractsResult<()> {
    if depth > MAX_AST_DEPTH {
        Err(ContractsError::AstTooDeep {
            file: file.to_path_buf(),
            depth: depth as u32,
            max_depth: MAX_AST_DEPTH as u32,
        })
    } else {
        Ok(())
    }
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::{tempdir, NamedTempFile};

    // -------------------------------------------------------------------------
    // Path Validation Tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_validate_file_path_normal() {
        // Create a temp file
        let file = NamedTempFile::new().unwrap();
        let path = file.path();

        let result = validate_file_path(path);
        assert!(result.is_ok());

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

    #[test]
    fn test_validate_file_path_not_exists() {
        let result = validate_file_path(Path::new("/nonexistent/file.py"));
        assert!(result.is_err());

        match result.unwrap_err() {
            ContractsError::FileNotFound { path } => {
                assert!(path.to_string_lossy().contains("nonexistent"));
            }
            _ => panic!("Expected FileNotFound error"),
        }
    }

    #[test]
    fn test_validate_file_path_traversal_rejected() {
        // Create a temp directory structure
        let temp = tempdir().unwrap();
        let subdir = temp.path().join("subdir");
        fs::create_dir(&subdir).unwrap();

        // Create a file in the temp root
        let file_path = temp.path().join("secret.txt");
        fs::write(&file_path, "secret").unwrap();

        // Check that path with .. pattern is detected
        let suspicious = subdir.join("..").join("secret.txt");
        assert!(has_path_traversal_pattern(&suspicious));
    }

    #[test]
    fn test_validate_file_path_symlink_outside_project() {
        // Create temp directories
        let project = tempdir().unwrap();
        let outside = tempdir().unwrap();

        // Create a file outside project
        let outside_file = outside.path().join("secret.txt");
        fs::write(&outside_file, "secret").unwrap();

        // Create a symlink inside project pointing outside
        let symlink_path = project.path().join("link.txt");

        #[cfg(unix)]
        {
            std::os::unix::fs::symlink(&outside_file, &symlink_path).unwrap();

            // The symlink should resolve but fail the project check
            let result = validate_file_path_in_project(&symlink_path, project.path());
            assert!(result.is_err());

            match result.unwrap_err() {
                ContractsError::PathTraversal { .. } => {}
                e => panic!("Expected PathTraversal error, got {:?}", e),
            }
        }
    }

    #[test]
    fn test_validate_file_path_system_dir_rejected() {
        // Test that system directories are blocked
        // We can't actually create files there, so just verify the check logic

        let blocked = [
            "/etc/passwd",
            "/root/.bashrc",
            "/sys/kernel/config",
            "/proc/self/status",
        ];

        for path_str in blocked {
            let path = Path::new(path_str);
            // If the file exists on this system, it should be rejected
            if path.exists() {
                let result = validate_file_path(path);
                assert!(result.is_err(), "Should reject system path: {}", path_str);
            }
        }
    }

    // -------------------------------------------------------------------------
    // Line Number Validation Tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_validate_line_numbers_valid_range() {
        assert!(validate_line_numbers(1, 10, 100).is_ok());
        assert!(validate_line_numbers(1, 1, 100).is_ok()); // same line
        assert!(validate_line_numbers(50, 100, 100).is_ok()); // at max
    }

    #[test]
    fn test_validate_line_numbers_start_after_end() {
        let result = validate_line_numbers(10, 5, 100);
        assert!(result.is_err());

        match result.unwrap_err() {
            ContractsError::LineOutsideFunction { line, .. } => {
                assert_eq!(line, 10);
            }
            _ => panic!("Expected LineOutsideFunction error"),
        }
    }

    #[test]
    fn test_validate_line_numbers_exceeds_max() {
        let result = validate_line_numbers(1, 200, 100);
        assert!(result.is_err());

        match result.unwrap_err() {
            ContractsError::LineOutsideFunction { line, .. } => {
                assert_eq!(line, 200);
            }
            _ => panic!("Expected LineOutsideFunction error"),
        }
    }

    #[test]
    fn test_validate_line_numbers_zero() {
        assert!(validate_line_numbers(0, 10, 100).is_err());
        assert!(validate_line_numbers(1, 0, 100).is_err());
    }

    // -------------------------------------------------------------------------
    // Function Name Validation Tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_validate_function_name_valid() {
        assert!(validate_function_name("my_function").is_ok());
        assert!(validate_function_name("_private").is_ok());
        assert!(validate_function_name("CamelCase").is_ok());
        assert!(validate_function_name("func123").is_ok());
        assert!(validate_function_name("__dunder__").is_ok());
    }

    #[test]
    fn test_validate_function_name_empty() {
        let result = validate_function_name("");
        assert!(result.is_err());

        match result.unwrap_err() {
            ContractsError::InvalidFunctionName { reason } => {
                assert!(reason.contains("empty"));
            }
            _ => panic!("Expected InvalidFunctionName error"),
        }
    }

    #[test]
    fn test_validate_function_name_invalid_chars() {
        let invalid_names = [
            "func;drop",  // semicolon
            "func()",     // parentheses
            "func{}",     // braces
            "func`cmd`",  // backticks
            "func\"name", // quotes
            "func\\name", // backslash
            "func/name",  // forward slash
        ];

        for name in invalid_names {
            let result = validate_function_name(name);
            assert!(result.is_err(), "Should reject: {}", name);
        }
    }

    #[test]
    fn test_validate_function_name_starts_with_digit() {
        let result = validate_function_name("123func");
        assert!(result.is_err());

        match result.unwrap_err() {
            ContractsError::InvalidFunctionName { reason } => {
                assert!(reason.contains("start with"));
            }
            _ => panic!("Expected InvalidFunctionName error"),
        }
    }

    #[test]
    fn test_validate_function_name_too_long() {
        let long_name = "a".repeat(MAX_FUNCTION_NAME_LEN + 1);
        let result = validate_function_name(&long_name);
        assert!(result.is_err());

        match result.unwrap_err() {
            ContractsError::InvalidFunctionName { reason } => {
                assert!(reason.contains("too long"));
            }
            _ => panic!("Expected InvalidFunctionName error"),
        }
    }

    // -------------------------------------------------------------------------
    // Safe File Reading Tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_read_file_safe_normal() {
        let mut file = NamedTempFile::new().unwrap();
        writeln!(file, "def hello():\n    print('hello')").unwrap();

        let content = read_file_safe(file.path()).unwrap();
        assert!(content.contains("def hello"));
        assert!(content.contains("print"));
    }

    #[test]
    fn test_read_file_safe_not_exists() {
        let result = read_file_safe(Path::new("/nonexistent/file.py"));
        assert!(result.is_err());

        match result.unwrap_err() {
            ContractsError::FileNotFound { .. } => {}
            e => panic!("Expected FileNotFound error, got {:?}", e),
        }
    }

    #[test]
    fn test_read_file_safe_too_large() {
        // Create a file larger than MAX_FILE_SIZE
        let temp = tempdir().unwrap();
        let _large_file = temp.path().join("large.txt");

        // Write a file just over the limit (we can't actually create 10MB in tests easily,
        // so we'll test the logic with a mock)
        // For now, just verify the constant value.
        let max_file_size = std::hint::black_box(MAX_FILE_SIZE);
        assert_eq!(max_file_size, 10 * 1024 * 1024);
    }

    #[test]
    fn test_read_file_safe_not_utf8() {
        let temp = tempdir().unwrap();
        let binary_file = temp.path().join("binary.bin");

        // Write invalid UTF-8 bytes
        let invalid_utf8 = vec![0xFF, 0xFE, 0x00, 0x01];
        fs::write(&binary_file, invalid_utf8).unwrap();

        let result = read_file_safe(&binary_file);
        assert!(result.is_err());

        match result.unwrap_err() {
            ContractsError::ParseError { message, .. } => {
                assert!(message.contains("UTF-8"));
            }
            e => panic!("Expected ParseError, got {:?}", e),
        }
    }

    // -------------------------------------------------------------------------
    // Resource Limits Constants Tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_resource_limits_constants() {
        // Verify TIGER mitigation constants have sensible values
        assert_eq!(MAX_FILE_SIZE, 10 * 1024 * 1024); // 10 MB
        assert_eq!(MAX_CFG_DEPTH, 1000); // TIGER-03
        assert_eq!(MAX_SSA_NODES, 100_000); // TIGER-04
        assert_eq!(MAX_AST_DEPTH, 100); // TIGER-08
    }

    #[test]
    fn test_check_depth_limit() {
        assert!(check_depth_limit(0, 1000).is_ok());
        assert!(check_depth_limit(999, 1000).is_ok());
        assert!(check_depth_limit(1000, 1000).is_err());
        assert!(check_depth_limit(1001, 1000).is_err());
    }

    #[test]
    fn test_check_ssa_node_limit() {
        assert!(check_ssa_node_limit(0).is_ok());
        assert!(check_ssa_node_limit(MAX_SSA_NODES).is_ok());
        assert!(check_ssa_node_limit(MAX_SSA_NODES + 1).is_err());
    }

    #[test]
    fn test_check_ast_depth() {
        let file = Path::new("test.py");
        assert!(check_ast_depth(0, file).is_ok());
        assert!(check_ast_depth(MAX_AST_DEPTH, file).is_ok());
        assert!(check_ast_depth(MAX_AST_DEPTH + 1, file).is_err());
    }

    // -------------------------------------------------------------------------
    // Path Traversal Pattern Detection Tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_has_path_traversal_pattern() {
        // Suspicious patterns
        assert!(has_path_traversal_pattern(Path::new("../etc/passwd")));
        assert!(has_path_traversal_pattern(Path::new("foo/../bar")));
        assert!(has_path_traversal_pattern(Path::new(
            "..\\Windows\\System32"
        )));

        // Normal paths
        assert!(!has_path_traversal_pattern(Path::new("src/main.rs")));
        assert!(!has_path_traversal_pattern(Path::new("/home/user/project")));
        assert!(!has_path_traversal_pattern(Path::new(".")));
    }
}