repolens 2.0.0

A CLI tool to audit and prepare repositories for open source or enterprise standards
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
//! Git hygiene rules
//!
//! This module provides rules for checking Git repository best practices, including:
//! - Large binary files detection (GIT001)
//! - .gitattributes presence (GIT002)
//! - Sensitive files tracked in repository (GIT003)

use crate::config::Config;
use crate::error::RepoLensError;
use crate::rules::engine::RuleCategory;
use crate::rules::results::{Finding, Severity};
use crate::scanner::Scanner;

/// Binary file extensions to check for large files
const BINARY_EXTENSIONS: &[&str] = &[
    "exe", "dll", "so", "dylib", "zip", "tar", "gz", "png", "jpg", "jpeg", "mp4", "pdf", "jar",
    "whl", "a", "lib", "bin", "o", "obj",
];

/// Sensitive file patterns to check
const SENSITIVE_PATTERNS: &[&str] = &[
    ".env",
    "*.key",
    "*.pem",
    "credentials*",
    "secrets*",
    "*_rsa",
    "*.p12",
];

/// Size threshold for large binary files (1 MB)
const LARGE_FILE_THRESHOLD: u64 = 1024 * 1024;

/// Rules for checking Git repository best practices
pub struct GitRules;

#[async_trait::async_trait]
impl RuleCategory for GitRules {
    fn name(&self) -> &'static str {
        "git"
    }

    async fn run(&self, scanner: &Scanner, config: &Config) -> Result<Vec<Finding>, RepoLensError> {
        let mut findings = Vec::new();

        if config.is_rule_enabled("git/large-binaries") {
            findings.extend(check_large_binaries(scanner).await?);
        }

        if config.is_rule_enabled("git/gitattributes") {
            findings.extend(check_gitattributes(scanner).await?);
        }

        if config.is_rule_enabled("git/sensitive-files") {
            findings.extend(check_sensitive_files(scanner).await?);
        }

        Ok(findings)
    }
}

/// GIT001: Check for large binary files (> 1MB)
async fn check_large_binaries(scanner: &Scanner) -> Result<Vec<Finding>, RepoLensError> {
    let mut findings = Vec::new();

    // Get files larger than threshold
    let large_files = scanner.files_larger_than(LARGE_FILE_THRESHOLD);

    for file_info in large_files {
        // Check if file has a binary extension
        let path_lower = file_info.path.to_lowercase();
        let is_binary = BINARY_EXTENSIONS
            .iter()
            .any(|ext| path_lower.ends_with(&format!(".{}", ext)));

        if is_binary {
            let size_mb = file_info.size as f64 / (1024.0 * 1024.0);
            findings.push(
                Finding::new(
                    "GIT001",
                    "git",
                    Severity::Warning,
                    format!(
                        "Large binary file '{}' ({:.2} MB) detected",
                        file_info.path, size_mb
                    ),
                )
                .with_location(&file_info.path)
                .with_description(
                    "Large binary files in Git repositories increase clone time, \
                     consume disk space, and cannot be efficiently diffed. \
                     Git is designed for text files and handles binaries poorly.",
                )
                .with_remediation(
                    "Consider using Git LFS (Large File Storage) for binary files, \
                     or store them in an external artifact repository. \
                     Add the file to .gitignore if it shouldn't be tracked.",
                ),
            );
        }
    }

    Ok(findings)
}

/// GIT002: Check for .gitattributes file presence
async fn check_gitattributes(scanner: &Scanner) -> Result<Vec<Finding>, RepoLensError> {
    let mut findings = Vec::new();

    if !scanner.file_exists(".gitattributes") {
        findings.push(
            Finding::new(
                "GIT002",
                "git",
                Severity::Info,
                ".gitattributes file is missing",
            )
            .with_description(
                "A .gitattributes file helps ensure consistent line endings, \
                 enables Git LFS tracking, and defines merge strategies for specific files. \
                 Without it, contributors may experience inconsistent behavior across platforms.",
            )
            .with_remediation(
                "Create a .gitattributes file to define text/binary handling, line endings, \
                 and Git LFS patterns. Example:\n\
                 * text=auto\n\
                 *.png binary\n\
                 *.jpg binary\n\
                 *.sh text eol=lf",
            ),
        );
    }

    Ok(findings)
}

/// GIT003: Check for sensitive files that might be tracked
async fn check_sensitive_files(scanner: &Scanner) -> Result<Vec<Finding>, RepoLensError> {
    let mut findings = Vec::new();

    // Read .gitignore content if it exists
    let gitignore_content = scanner.read_file(".gitignore").unwrap_or_default();

    // Check each sensitive pattern
    for pattern in SENSITIVE_PATTERNS {
        let matching_files = find_sensitive_files(scanner, pattern);

        for file_path in matching_files {
            // Check if the file is ignored in .gitignore
            if !is_pattern_in_gitignore(&gitignore_content, &file_path, pattern) {
                findings.push(
                    Finding::new(
                        "GIT003",
                        "git",
                        Severity::Warning,
                        format!(
                            "Sensitive file '{}' may be tracked in repository",
                            file_path
                        ),
                    )
                    .with_location(&file_path)
                    .with_description(
                        "This file matches a sensitive pattern and may contain secrets, \
                         credentials, or private keys. Tracking such files in Git exposes \
                         them in the repository history, even if later deleted.",
                    )
                    .with_remediation(format!(
                        "Add '{}' or '{}' to .gitignore. If the file was already committed, \
                         you may need to remove it from Git history using 'git filter-branch' \
                         or 'git filter-repo', and rotate any exposed credentials.",
                        file_path, pattern
                    )),
                );
            }
        }
    }

    Ok(findings)
}

/// Find files matching a sensitive pattern
fn find_sensitive_files(scanner: &Scanner, pattern: &str) -> Vec<String> {
    let mut matching_files = Vec::new();

    // Handle different pattern types
    if let Some(ext) = pattern.strip_prefix("*.") {
        // Extension pattern like "*.key"
        for file_info in scanner.files_with_extensions(&[ext]) {
            matching_files.push(file_info.path.clone());
        }
    } else if let Some(prefix) = pattern.strip_suffix('*') {
        // Prefix pattern like "credentials*"
        for file_info in scanner.files_matching_pattern(pattern) {
            // Verify it matches the prefix
            let file_name = file_info.path.rsplit('/').next().unwrap_or(&file_info.path);
            if file_name.starts_with(prefix) {
                matching_files.push(file_info.path.clone());
            }
        }
    } else if let Some(suffix) = pattern.strip_prefix('*') {
        // Suffix pattern like "*_rsa"
        for file_info in scanner.files_matching_pattern(pattern) {
            if file_info.path.ends_with(suffix) {
                matching_files.push(file_info.path.clone());
            }
        }
    } else {
        // Exact match like ".env"
        if scanner.file_exists(pattern) {
            matching_files.push(pattern.to_string());
        }
    }

    matching_files
}

/// Check if a file path or pattern is covered by .gitignore
fn is_pattern_in_gitignore(gitignore_content: &str, file_path: &str, pattern: &str) -> bool {
    let file_name = file_path.rsplit('/').next().unwrap_or(file_path);

    for line in gitignore_content.lines() {
        let line = line.trim();

        // Skip comments and empty lines
        if line.is_empty() || line.starts_with('#') {
            continue;
        }

        // Check if the line ignores this file
        if line == file_path || line == file_name {
            return true;
        }

        // Check if the pattern itself is in gitignore
        if line == pattern {
            return true;
        }

        // Handle glob patterns in gitignore
        if line.contains('*')
            && (gitignore_pattern_matches(line, file_name)
                || gitignore_pattern_matches(line, file_path))
        {
            return true;
        }

        // Handle directory patterns (trailing slash)
        if let Some(dir_pattern) = line.strip_suffix('/') {
            if file_path.starts_with(&format!("{}/", dir_pattern)) {
                return true;
            }
        }
    }

    false
}

/// Check if a gitignore glob pattern matches a path
fn gitignore_pattern_matches(pattern: &str, path: &str) -> bool {
    // Simple glob matching for common patterns
    if let Some(ext) = pattern.strip_prefix('*') {
        // Extension or suffix pattern (*.key or *_rsa)
        return path.ends_with(ext);
    }

    if let Some(prefix) = pattern.strip_suffix('*') {
        // Prefix pattern
        return path.starts_with(prefix);
    }

    // Exact match
    pattern == path
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::Config;
    use crate::scanner::Scanner;
    use std::fs;
    use tempfile::TempDir;

    // --- GIT001: Large binary files ---

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

        // Create a large binary file (> 1MB)
        let large_content = vec![0u8; 2 * 1024 * 1024]; // 2MB
        fs::write(root.join("large_file.zip"), large_content).unwrap();

        let scanner = Scanner::new(root.to_path_buf());
        let findings = check_large_binaries(&scanner).await.unwrap();

        assert!(findings.iter().any(|f| f.rule_id == "GIT001"));
        assert!(
            findings
                .iter()
                .any(|f| f.message.contains("large_file.zip"))
        );
    }

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

        // Create a small binary file (< 1MB)
        let small_content = vec![0u8; 100 * 1024]; // 100KB
        fs::write(root.join("small_file.zip"), small_content).unwrap();

        let scanner = Scanner::new(root.to_path_buf());
        let findings = check_large_binaries(&scanner).await.unwrap();

        assert!(findings.is_empty());
    }

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

        // Create a large text file (not binary extension)
        let large_content = "x".repeat(2 * 1024 * 1024); // 2MB text
        fs::write(root.join("large_file.txt"), large_content).unwrap();

        let scanner = Scanner::new(root.to_path_buf());
        let findings = check_large_binaries(&scanner).await.unwrap();

        assert!(findings.is_empty());
    }

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

        let large_content = vec![0u8; 2 * 1024 * 1024];

        // Create large files with various binary extensions
        fs::write(root.join("file.exe"), &large_content).unwrap();
        fs::write(root.join("file.dll"), &large_content).unwrap();
        fs::write(root.join("file.jar"), &large_content).unwrap();

        let scanner = Scanner::new(root.to_path_buf());
        let findings = check_large_binaries(&scanner).await.unwrap();

        assert_eq!(findings.len(), 3);
        assert!(findings.iter().all(|f| f.rule_id == "GIT001"));
    }

    // --- GIT002: .gitattributes missing ---

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

        fs::write(root.join("README.md"), "# Project").unwrap();

        let scanner = Scanner::new(root.to_path_buf());
        let findings = check_gitattributes(&scanner).await.unwrap();

        assert!(findings.iter().any(|f| f.rule_id == "GIT002"));
        assert!(findings.iter().any(|f| f.severity == Severity::Info));
    }

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

        fs::write(root.join(".gitattributes"), "* text=auto").unwrap();

        let scanner = Scanner::new(root.to_path_buf());
        let findings = check_gitattributes(&scanner).await.unwrap();

        assert!(findings.is_empty());
    }

    // --- GIT003: Sensitive files tracked ---

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

        fs::write(root.join(".env"), "SECRET_KEY=abc123").unwrap();

        let scanner = Scanner::new(root.to_path_buf());
        let findings = check_sensitive_files(&scanner).await.unwrap();

        assert!(findings.iter().any(|f| f.rule_id == "GIT003"));
        assert!(findings.iter().any(|f| f.message.contains(".env")));
    }

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

        fs::write(root.join(".env"), "SECRET_KEY=abc123").unwrap();
        fs::write(root.join(".gitignore"), ".env\n").unwrap();

        let scanner = Scanner::new(root.to_path_buf());
        let findings = check_sensitive_files(&scanner).await.unwrap();

        // Should not report .env since it's in .gitignore
        assert!(!findings.iter().any(|f| f.message.contains(".env")));
    }

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

        fs::write(root.join("private.key"), "-----BEGIN PRIVATE KEY-----").unwrap();

        let scanner = Scanner::new(root.to_path_buf());
        let findings = check_sensitive_files(&scanner).await.unwrap();

        assert!(findings.iter().any(|f| f.rule_id == "GIT003"));
        assert!(findings.iter().any(|f| f.message.contains("private.key")));
    }

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

        fs::write(root.join("cert.pem"), "-----BEGIN CERTIFICATE-----").unwrap();
        fs::write(root.join(".gitignore"), "*.pem\n").unwrap();

        let scanner = Scanner::new(root.to_path_buf());
        let findings = check_sensitive_files(&scanner).await.unwrap();

        // Should not report cert.pem since *.pem is in .gitignore
        assert!(!findings.iter().any(|f| f.message.contains("cert.pem")));
    }

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

        fs::write(root.join("credentials.json"), "{}").unwrap();

        let scanner = Scanner::new(root.to_path_buf());
        let findings = check_sensitive_files(&scanner).await.unwrap();

        assert!(findings.iter().any(|f| f.message.contains("credentials")));
    }

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

        fs::write(root.join("id_rsa"), "-----BEGIN RSA PRIVATE KEY-----").unwrap();

        let scanner = Scanner::new(root.to_path_buf());
        let findings = check_sensitive_files(&scanner).await.unwrap();

        assert!(findings.iter().any(|f| f.message.contains("id_rsa")));
    }

    // --- Integration tests ---

    #[tokio::test]
    async fn test_git_rules_category_name() {
        let rules = GitRules;
        assert_eq!(rules.name(), "git");
    }

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

        // Create files that trigger all rules
        let large_content = vec![0u8; 2 * 1024 * 1024];
        fs::write(root.join("archive.zip"), large_content).unwrap();
        fs::write(root.join(".env"), "SECRET=value").unwrap();
        // No .gitattributes

        let scanner = Scanner::new(root.to_path_buf());
        let config = Config::default();
        let rules = GitRules;

        let findings = rules.run(&scanner, &config).await.unwrap();

        let rule_ids: Vec<&str> = findings.iter().map(|f| f.rule_id.as_str()).collect();
        assert!(
            rule_ids.contains(&"GIT001"),
            "Should find GIT001 (large binary)"
        );
        assert!(
            rule_ids.contains(&"GIT002"),
            "Should find GIT002 (no .gitattributes)"
        );
        assert!(
            rule_ids.contains(&"GIT003"),
            "Should find GIT003 (sensitive file)"
        );
    }

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

        // Create a clean repo with proper configuration
        fs::write(root.join(".gitattributes"), "* text=auto").unwrap();
        fs::write(root.join(".gitignore"), ".env\n*.key\n*.pem\n").unwrap();
        fs::write(root.join("README.md"), "# Clean Project").unwrap();

        let scanner = Scanner::new(root.to_path_buf());
        let config = Config::default();
        let rules = GitRules;

        let findings = rules.run(&scanner, &config).await.unwrap();

        assert!(findings.is_empty(), "Clean repo should have no findings");
    }

    // --- Helper function tests ---

    #[test]
    fn test_gitignore_pattern_matches_extension() {
        assert!(gitignore_pattern_matches("*.key", "private.key"));
        assert!(gitignore_pattern_matches("*.pem", "cert.pem"));
        assert!(!gitignore_pattern_matches("*.key", "private.pem"));
    }

    #[test]
    fn test_gitignore_pattern_matches_prefix() {
        assert!(gitignore_pattern_matches(
            "credentials*",
            "credentials.json"
        ));
        assert!(gitignore_pattern_matches("secrets*", "secrets.yaml"));
        assert!(!gitignore_pattern_matches("credentials*", "my_credentials"));
    }

    #[test]
    fn test_gitignore_pattern_matches_suffix() {
        assert!(gitignore_pattern_matches("*_rsa", "id_rsa"));
        assert!(gitignore_pattern_matches("*_rsa", "deploy_rsa"));
        assert!(!gitignore_pattern_matches("*_rsa", "id_rsa.pub"));
    }

    #[test]
    fn test_is_pattern_in_gitignore() {
        let gitignore = ".env\n*.key\ncredentials*\n# comment\n";

        assert!(is_pattern_in_gitignore(gitignore, ".env", ".env"));
        assert!(is_pattern_in_gitignore(gitignore, "private.key", "*.key"));
        assert!(is_pattern_in_gitignore(
            gitignore,
            "credentials.json",
            "credentials*"
        ));
        assert!(!is_pattern_in_gitignore(
            gitignore,
            "config.yml",
            "config.yml"
        ));
    }
}