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
//! Secrets detection rules
//!
//! This module provides rules for detecting exposed secrets and credentials
//! in repository files. It checks for:
//! - Hardcoded secrets in source files (API keys, tokens, passwords)
//! - Sensitive files (private keys, certificates, credentials)
//! - Environment files (.env) that should not be committed

use rayon::prelude::*;

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

/// Rules for detecting secrets and credentials
pub struct SecretsRules;

#[async_trait::async_trait]
impl RuleCategory for SecretsRules {
    /// Get the category name
    fn name(&self) -> &'static str {
        "secrets"
    }

    /// Run all secrets detection rules
    ///
    /// # Arguments
    ///
    /// * `scanner` - The scanner to access repository files
    /// * `config` - The configuration with enabled rules
    ///
    /// # Returns
    ///
    /// A vector of findings for detected secrets
    ///
    /// # Errors
    ///
    /// Returns an error if the scan fails
    async fn run(&self, scanner: &Scanner, config: &Config) -> Result<Vec<Finding>, RepoLensError> {
        let mut findings = Vec::new();

        // Check for hardcoded secrets in source files
        if config.is_rule_enabled("secrets/hardcoded") {
            findings.extend(check_hardcoded_secrets(scanner, config).await?);
        }

        // Check for sensitive files
        if config.is_rule_enabled("secrets/files") {
            findings.extend(check_sensitive_files(scanner, config).await?);
        }

        // Check for .env files
        if config.is_rule_enabled("secrets/env") {
            findings.extend(check_env_files(scanner, config).await?);
        }

        Ok(findings)
    }
}

/// Check for hardcoded secrets in source files
///
/// Scans files with common source code extensions for patterns that indicate
/// hardcoded secrets like API keys, tokens, and passwords.
///
/// # Arguments
///
/// * `scanner` - The scanner to access repository files
/// * `config` - The configuration with ignore patterns
///
/// # Returns
///
/// A vector of findings for detected secrets
async fn check_hardcoded_secrets(
    scanner: &Scanner,
    config: &Config,
) -> Result<Vec<Finding>, RepoLensError> {
    let mut findings = Vec::new();

    // File extensions to scan
    let extensions = [
        "js", "ts", "jsx", "tsx", "py", "rb", "php", "java", "go", "rs", "cpp", "c", "yml", "yaml",
        "json", "toml", "env", "config", "conf", "sql", "sh", "bash",
    ];

    let files: Vec<_> = scanner
        .files_with_extensions(&extensions)
        .into_iter()
        .filter(|file| !config.should_ignore_file(&file.path))
        .map(|file| file.path.clone())
        .collect();

    // Process files in parallel
    let file_findings: Vec<Vec<Finding>> = files
        .par_iter()
        .filter_map(|file_path| {
            let content = match scanner.read_file(file_path) {
                Ok(c) => c,
                Err(e) => {
                    tracing::warn!("Failed to read file {}: {}", file_path, e);
                    return None;
                }
            };

            match check_file_for_secrets(file_path, &content, config) {
                Ok(f) => Some(f),
                Err(e) => {
                    tracing::warn!("Error checking file {}: {}", file_path, e);
                    None
                }
            }
        })
        .collect();

    // Flatten results
    for file_finding in file_findings {
        findings.extend(file_finding);
    }

    Ok(findings)
}

/// Check a single file for secrets
///
/// # Arguments
///
/// * `file_path` - Path to the file
/// * `content` - File content
/// * `config` - Configuration with ignore patterns
///
/// # Returns
///
/// A vector of findings for secrets found in this file
fn check_file_for_secrets(
    file_path: &str,
    content: &str,
    config: &Config,
) -> Result<Vec<Finding>, RepoLensError> {
    let mut findings = Vec::new();

    for pattern in SECRET_PATTERNS.iter() {
        if let Some(captures) = pattern.regex.captures(content) {
            if config.should_ignore_pattern(file_path) {
                continue;
            }

            let line_num = find_line_number(content, &captures)?;

            findings.push(
                Finding::new(
                    "SEC001",
                    "secrets",
                    Severity::Critical,
                    format!("{} detected", pattern.name),
                )
                .with_location(format!("{}:{}", file_path, line_num))
                .with_description(pattern.description.to_string())
                .with_remediation(
                    "Remove the secret and use environment variables or a secrets manager instead.",
                ),
            );
        }
    }

    Ok(findings)
}

/// Find the line number where a regex match occurs
///
/// # Arguments
///
/// * `content` - File content
/// * `captures` - Regex captures from the match
///
/// # Returns
///
/// The line number (1-indexed)
fn find_line_number(content: &str, captures: &regex::Captures) -> Result<usize, RepoLensError> {
    let match_start = captures
        .get(0)
        .ok_or_else(|| {
            RepoLensError::Rule(crate::error::RuleError::ExecutionFailed {
                message: "No match found in pattern capture".to_string(),
            })
        })?
        .start();

    Ok(content[..match_start].matches('\n').count() + 1)
}

/// Check for sensitive files that should not be in version control
///
/// Detects files like private keys, certificates, and credential files
/// that pose a security risk if committed to the repository.
///
/// # Arguments
///
/// * `scanner` - The scanner to access repository files
/// * `_config` - The configuration (currently unused)
///
/// # Returns
///
/// A vector of findings for detected sensitive files
async fn check_sensitive_files(
    scanner: &Scanner,
    _config: &Config,
) -> Result<Vec<Finding>, RepoLensError> {
    let mut findings = Vec::new();

    // List of sensitive file patterns
    let sensitive_patterns = [
        ("*.pem", "Private key file"),
        ("*.key", "Private key file"),
        ("*.p12", "PKCS#12 certificate bundle"),
        ("*.pfx", "PKCS#12 certificate bundle"),
        ("*.jks", "Java keystore"),
        ("id_rsa", "SSH private key"),
        ("id_dsa", "SSH private key"),
        ("id_ecdsa", "SSH private key"),
        ("id_ed25519", "SSH private key"),
        (".htpasswd", "Apache password file"),
        ("credentials.json", "Credentials file"),
        ("service-account.json", "Service account credentials"),
        ("secrets.yml", "Secrets configuration"),
        ("secrets.yaml", "Secrets configuration"),
        ("secrets.json", "Secrets configuration"),
    ];

    for (pattern, description) in sensitive_patterns {
        for file in scanner.files_matching_pattern(pattern) {
            findings.push(
                Finding::new(
                    "SEC002",
                    "secrets",
                    Severity::Critical,
                    format!("{} found in repository", description),
                )
                .with_location(&file.path)
                .with_description(format!(
                    "The file '{}' appears to contain sensitive data and should not be committed to version control.",
                    file.path
                ))
                .with_remediation(
                    "Remove the file from the repository and add it to .gitignore. If the file was previously committed, consider rotating any contained credentials."
                )
            );
        }
    }

    Ok(findings)
}

/// Check for .env files that should not be committed
///
/// Detects environment files that may contain secrets. Example files
/// (.env.example, .env.template) are allowed.
///
/// # Arguments
///
/// * `scanner` - The scanner to access repository files
/// * `_config` - The configuration (currently unused)
///
/// # Returns
///
/// A vector of findings for detected .env files
async fn check_env_files(
    scanner: &Scanner,
    _config: &Config,
) -> Result<Vec<Finding>, RepoLensError> {
    let mut findings = Vec::new();

    // Check for .env files (but allow .env.example)
    let env_patterns = [
        ".env",
        ".env.local",
        ".env.production",
        ".env.development",
        ".env.test",
    ];

    for pattern in env_patterns {
        for file in scanner.files_matching_pattern(pattern) {
            // Allow example/template files
            if file.path.contains(".example")
                || file.path.contains(".template")
                || file.path.contains(".sample")
            {
                continue;
            }

            findings.push(
                Finding::new(
                    "SEC003",
                    "secrets",
                    Severity::Critical,
                    "Environment file found in repository",
                )
                .with_location(&file.path)
                .with_description(
                    "Environment files often contain sensitive configuration and secrets that should not be committed."
                )
                .with_remediation(
                    "Add the file to .gitignore and create a .env.example file as a template."
                )
            );
        }
    }

    Ok(findings)
}

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

    #[tokio::test]
    async fn test_check_hardcoded_secrets_detects_api_key() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        let config_file = root.join("config.js");

        fs::write(&config_file, "const apiKey = 'sk_test_1234567890abcdef';").unwrap();

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

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

        assert!(!findings.is_empty());
        assert!(findings.iter().any(|f| f.rule_id == "SEC001"));
        assert!(findings.iter().any(|f| f.message.contains("detected")));
    }

    #[tokio::test]
    async fn test_check_hardcoded_secrets_ignores_ignored_files() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        let config_file = root.join("config.js");

        fs::write(&config_file, "const apiKey = 'sk_test_1234567890abcdef';").unwrap();

        let scanner = Scanner::new(root.to_path_buf());
        let mut config = Config::default();
        config.secrets.ignore_files.push("config.js".to_string());

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

        assert!(findings.is_empty());
    }

    #[tokio::test]
    async fn test_check_sensitive_files_detects_pem() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        let key_file = root.join("private.pem");

        fs::write(&key_file, "-----BEGIN PRIVATE KEY-----").unwrap();

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

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

        assert!(!findings.is_empty());
        assert!(findings.iter().any(|f| f.rule_id == "SEC002"));
        assert!(
            findings
                .iter()
                .any(|f| f.message.contains("Private key file"))
        );
    }

    #[tokio::test]
    async fn test_check_env_files_detects_env() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        let env_file = root.join(".env");

        fs::write(&env_file, "API_KEY=secret123").unwrap();

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

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

        assert!(!findings.is_empty());
        assert!(findings.iter().any(|f| f.rule_id == "SEC003"));
    }

    #[tokio::test]
    async fn test_check_env_files_allows_example() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        let env_example = root.join(".env.example");

        fs::write(&env_example, "API_KEY=your_key_here").unwrap();

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

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

        assert!(findings.is_empty());
    }

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

        fs::write(root.join(".env.template"), "API_KEY=your_key_here").unwrap();

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

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

        assert!(findings.is_empty());
    }

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

        fs::write(root.join(".env.sample"), "API_KEY=your_key_here").unwrap();

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

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

        assert!(findings.is_empty());
    }

    #[tokio::test]
    async fn test_check_sensitive_files_detects_ssh_key() {
        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 config = Config::default();

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

        assert!(!findings.is_empty());
        assert!(findings.iter().any(|f| f.rule_id == "SEC002"));
    }

    #[tokio::test]
    async fn test_check_sensitive_files_detects_credentials_json() {
        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 config = Config::default();

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

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

    #[tokio::test]
    async fn test_secrets_rules_name() {
        let rules = SecretsRules;
        assert_eq!(rules.name(), "secrets");
    }

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

        fs::write(
            root.join("test_config.js"),
            "const apiKey = 'sk_test_1234567890abcdef';",
        )
        .unwrap();

        let scanner = Scanner::new(root.to_path_buf());
        let mut config = Config::default();
        config
            .secrets
            .ignore_patterns
            .push("test_config.js".to_string());

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

        // File matches ignore pattern, so findings should be suppressed
        assert!(findings.is_empty());
    }

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

        fs::write(root.join("clean.js"), "const x = 42;").unwrap();

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

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

        // No secrets in clean file
        assert!(findings.is_empty());
    }

    #[test]
    fn test_find_line_number() {
        let content = "line1\nline2\nline3\n";
        let pattern = regex::Regex::new("line2").unwrap();
        let captures = pattern.captures(content).unwrap();
        let line_num = find_line_number(content, &captures).unwrap();
        assert_eq!(line_num, 2);
    }

    #[test]
    fn test_find_line_number_first_line() {
        let content = "line1\nline2\nline3\n";
        let pattern = regex::Regex::new("line1").unwrap();
        let captures = pattern.captures(content).unwrap();
        let line_num = find_line_number(content, &captures).unwrap();
        assert_eq!(line_num, 1);
    }
}