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
|
// Copyright (c) 2026 Adrian Lorenz <a.lorenz@noa-x.de>
// SPDX-License-Identifier: MIT
use anyhow::{Context, Result};
use colored::Colorize;
use regex::Regex;
use serde::Serialize;
use sha2::{Digest, Sha256};
use std::collections::HashSet;
use std::path::Path;
use walkdir::WalkDir;
use crate::config::Config;
use crate::rules::Rule;
#[derive(Debug, Serialize)]
pub struct Finding {
pub rule_id: String,
pub description: String,
pub severity: String,
pub file: String,
pub line_number: usize,
pub line: String,
pub secret: String,
pub secret_hash: String,
pub tags: Vec<String>,
}
pub struct ScanStats {
pub files: usize,
pub lines: usize,
}
pub struct Scanner {
rules: Vec<(Rule, Regex)>,
max_size_bytes: u64,
allowed_extensions: HashSet<String>,
exclude_paths: Vec<String>,
exclude_files: HashSet<String>,
}
const SUPPRESS_MARKERS: &[&str] = &["# leakguard-ignore", "# noqa-secrets", "# nosec-secrets"];
const SKIP_DIRS: &[&str] = &[".git","node_modules","target","vendor","__pycache__","dist","build",".eggs",".tox",".mypy_cache"];
// Any directory whose name starts with one of these prefixes is skipped (.venv, .venv2, venv, venv3, .virtualenv, β¦)
const SKIP_DIR_PREFIXES: &[&str] = &[".venv", "venv", ".virtualenv", "virtualenv"];
impl Scanner {
pub fn new(rules: Vec<Rule>, max_size_kb: u64, cfg: &Config) -> Result<Self> {
let disabled = cfg.disabled_rules();
let compiled: Result<Vec<_>> = rules.into_iter()
.filter(|r| !disabled.contains(r.id))
.map(|rule| {
let re = Regex::new(rule.pattern).with_context(|| format!("Bad regex in rule '{}'", rule.id))?;
Ok((rule, re))
})
.collect();
Ok(Self {
rules: compiled?,
max_size_bytes: max_size_kb.saturating_mul(1024),
allowed_extensions: cfg.allowed_extensions(),
exclude_paths: cfg.scan.exclude_paths.clone(),
exclude_files: cfg.scan.exclude_files.iter().cloned().collect(),
})
}
fn is_allowed_path(&self, path: &Path) -> bool {
let fname = path.file_name().map(|n| n.to_string_lossy()).unwrap_or_default();
if fname == ".env" || fname.starts_with(".env.") || fname.ends_with(".env") { return false; }
let ext = path.extension().and_then(|e| e.to_str()).map(|e| e.to_lowercase()).unwrap_or_default();
if ext == "env" { return false; }
if !self.allowed_extensions.is_empty() && !self.allowed_extensions.contains(&ext) { return false; }
if self.exclude_files.contains(fname.as_ref()) { return false; }
let path_str = path.to_string_lossy();
for excl in &self.exclude_paths {
if path_str.contains(excl.as_str()) { return false; }
}
true
}
pub fn scan_text(&self, text: &str, source_name: &str) -> Vec<Finding> {
let mut findings = Vec::new();
for (ln, line) in text.lines().enumerate() {
if SUPPRESS_MARKERS.iter().any(|&m| line.contains(m)) { continue; }
for (rule, regex) in &self.rules {
for caps in regex.captures_iter(line) {
let secret = caps.get(rule.secret_group).map(|m| m.as_str().to_string()).unwrap_or_default();
if !is_valid_secret_match(rule, &secret) { continue; }
findings.push(Finding {
rule_id: rule.id.to_string(),
description: rule.description.to_string(),
severity: rule.severity.to_string(),
file: source_name.to_string(),
line_number: ln + 1,
line: truncate_line(line.trim()),
secret: redact_secret(&secret),
secret_hash: sha256_hex(&secret),
tags: rule.tags.iter().map(|t| t.to_string()).collect(),
});
}
}
}
findings
}
pub fn replace_text(&self, text: &str, replacement: &str) -> String {
self.replace_text_with_stats(text, replacement).0
}
pub fn replace_text_with_stats(&self, text: &str, replacement: &str) -> (String, usize) {
let mut out = String::with_capacity(text.len());
let mut replaced_total = 0usize;
for chunk in text.split_inclusive('\n') {
let (line, newline) = match chunk.strip_suffix('\n') {
Some(line_wo_nl) => (line_wo_nl, "\n"),
None => (chunk, ""),
};
let (replaced, count) = self.replace_line_secrets(line, replacement);
replaced_total += count;
out.push_str(&replaced);
out.push_str(newline);
}
(out, replaced_total)
}
fn replace_line_secrets(&self, line: &str, replacement: &str) -> (String, usize) {
if SUPPRESS_MARKERS.iter().any(|&m| line.contains(m)) {
return (line.to_string(), 0);
}
let mut spans: Vec<(usize, usize)> = Vec::new();
for (rule, regex) in &self.rules {
for caps in regex.captures_iter(line) {
let Some(m) = caps.get(rule.secret_group) else { continue; };
if !is_valid_secret_match(rule, m.as_str()) { continue; }
spans.push((m.start(), m.end()));
}
}
if spans.is_empty() {
return (line.to_string(), 0);
}
spans.sort_by_key(|(s, e)| (*s, *e));
let mut merged: Vec<(usize, usize)> = Vec::with_capacity(spans.len());
for (start, end) in spans {
if let Some((_, last_end)) = merged.last_mut() {
if start <= *last_end {
*last_end = (*last_end).max(end);
continue;
}
}
merged.push((start, end));
}
let mut out = line.to_string();
let replaced_count = merged.len();
for (start, end) in merged.iter().copied().rev() {
out.replace_range(start..end, replacement);
}
(out, replaced_count)
}
pub fn scan_file(&self, path: &Path) -> Result<(Vec<Finding>, ScanStats)> {
if !self.is_allowed_path(path) { return Ok((vec![], ScanStats { files: 0, lines: 0 })); }
let meta = std::fs::metadata(path)?;
if meta.len() == 0 || meta.len() > self.max_size_bytes { return Ok((vec![], ScanStats { files: 1, lines: 0 })); }
let content = std::fs::read(path)?;
let sample = &content[..content.len().min(512)];
if sample.iter().filter(|&&b| b < 7 || b == 127).count() * 100 / sample.len().max(1) > 30 { return Ok((vec![], ScanStats { files: 1, lines: 0 })); }
let text = String::from_utf8_lossy(&content);
let line_count = text.lines().count();
let findings = self.scan_text(&text, &path.display().to_string());
Ok((findings, ScanStats { files: 1, lines: line_count }))
}
pub fn scan_directory(&self, dir: &Path, verbose: bool) -> Result<(Vec<Finding>, ScanStats)> {
let mut all = Vec::new();
let mut stats = ScanStats { files: 0, lines: 0 };
for entry in WalkDir::new(dir).follow_links(false).into_iter()
.filter_entry(|e| {
let n = e.file_name().to_string_lossy();
!SKIP_DIRS.iter().any(|&s| n == s)
&& !SKIP_DIR_PREFIXES.iter().any(|&p| n.starts_with(p))
})
.filter_map(|e| e.ok())
.filter(|e| e.file_type().is_file())
{
let path = entry.path();
if verbose {
if self.is_allowed_path(path) { eprintln!("{} {}", "Scanning:".dimmed(), path.display().to_string().dimmed()); }
else { eprintln!("{} {} (skipped)", "Ignored:".dimmed(), path.display().to_string().dimmed()); }
}
match self.scan_file(path) {
Ok((f, s)) => { all.extend(f); stats.files += s.files; stats.lines += s.lines; }
Err(e) => { if verbose { eprintln!("{} {}: {}", "Warning:".yellow(), path.display(), e); } }
}
}
Ok((all, stats))
}
}
/// Returns true if the matched text looks like a variable/property reference
/// on the right-hand side of an assignment (e.g. `settings.DB_PASSWORD`, `config.key`).
fn is_var_ref(s: &str) -> bool {
let val = s.find('=')
.map(|i| s[i + 1..].trim())
.unwrap_or("");
!val.is_empty()
&& val.contains('.')
&& val.chars().all(|c| c.is_alphanumeric() || c == '_' || c == '.')
}
fn is_valid_secret_match(rule: &Rule, secret: &str) -> bool {
if secret.is_empty() {
return false;
}
// Skip template variables: {VAR}, ${VAR}, %(var)s
if secret.contains('{') || secret.contains("%(") || secret.contains('$') {
return false;
}
// Skip variable/property references on RHS (e.g. settings.DB_PASSWORD, config.key)
if is_var_ref(secret) {
return false;
}
if rule.id == "http-insecure-url"
&& (secret.contains("localhost") || secret.contains("127.0.") || secret.contains("0.0.0.0"))
{
return false;
}
true
}
fn truncate_line(s: &str) -> String {
let mut chars = s.chars();
let head: String = chars.by_ref().take(200).collect();
if chars.next().is_some() { format!("{head}β¦") } else { head }
}
fn redact_secret(s: &str) -> String {
let len = s.len();
if len <= 8 { return "*".repeat(len); }
let v = len.min(4);
format!("{}...[REDACTED]...{}", &s[..v], &s[len-v..])
}
fn sha256_hex(s: &str) -> String {
let mut hasher = Sha256::new();
hasher.update(s.as_bytes());
let out = hasher.finalize();
format!("{out:x}")
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::Config;
use crate::rules::builtin_rules;
use std::io::Write;
use tempfile::{NamedTempFile, TempDir};
fn default_scanner() -> Scanner {
Scanner::new(builtin_rules(), 1024, &Config::default()).unwrap()
}
// ββ redact_secret ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
#[test]
fn redact_short_keeps_asterisks() {
assert_eq!(redact_secret("abc"), "***");
assert_eq!(redact_secret("12345678"), "********");
}
#[test]
fn redact_long_shows_prefix_and_suffix() {
let r = redact_secret("AKIAIOSFODNN7EXAMPLE");
assert!(r.starts_with("AKIA"));
assert!(r.ends_with("MPLE"));
assert!(r.contains("...[REDACTED]..."));
}
#[test]
fn sha256_is_stable_hex() {
let a = sha256_hex("secret-value");
let b = sha256_hex("secret-value");
assert_eq!(a, b);
assert_eq!(a.len(), 64);
assert!(a.chars().all(|c| c.is_ascii_hexdigit()));
}
// ββ is_allowed_path ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
#[test]
fn blocks_dotenv_file() {
let scanner = default_scanner();
let dir = TempDir::new().unwrap();
for name in &[".env", ".env.local", "production.env"] {
let path = dir.path().join(name);
std::fs::write(&path, "SECRET=hunter2\n").unwrap();
let findings = scanner.scan_file(&path).unwrap().0;
assert!(findings.is_empty(), "{name} should be blocked");
}
}
#[test]
fn extension_allowlist_filters_other_files() {
let toml = r#"[scan]
extensions = ["rs"]
"#;
let cfg: Config = toml::from_str(toml).unwrap();
let scanner = Scanner::new(builtin_rules(), 1024, &cfg).unwrap();
let mut py_file = NamedTempFile::with_suffix(".py").unwrap();
writeln!(py_file, "key = AKIAIOSFODNN7EXAMPLE1234").unwrap();
let findings = scanner.scan_file(py_file.path()).unwrap().0;
assert!(findings.is_empty(), ".py should be skipped when only rs is allowed");
}
// ββ scan_file: detection βββββββββββββββββββββββββββββββββββββββββββββββββ
#[test]
fn detects_aws_access_key() {
let scanner = default_scanner();
let mut f = NamedTempFile::with_suffix(".txt").unwrap();
writeln!(f, "aws_key = AKIAIOSFODNN7EXAMPLE1234").unwrap();
let findings = scanner.scan_file(f.path()).unwrap().0;
assert!(findings.iter().any(|f| f.rule_id == "aws-access-key"), "aws-access-key not detected");
}
#[test]
fn detects_github_pat() {
let scanner = default_scanner();
let mut f = NamedTempFile::with_suffix(".txt").unwrap();
writeln!(f, "token = ghp_ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890").unwrap();
let findings = scanner.scan_file(f.path()).unwrap().0;
assert!(findings.iter().any(|f| f.rule_id == "github-pat"), "github-pat not detected");
}
#[test]
fn detects_stripe_secret_key() {
let scanner = default_scanner();
let mut f = NamedTempFile::with_suffix(".txt").unwrap();
writeln!(f, "STRIPE_KEY=sk_live_{}", "AbCdEfGhIjKlMnOpQrStUvWx").unwrap();
let findings = scanner.scan_file(f.path()).unwrap().0;
assert!(findings.iter().any(|f| f.rule_id == "stripe-secret"), "stripe-secret not detected");
}
#[test]
fn detects_private_key_header() {
let scanner = default_scanner();
let mut f = NamedTempFile::with_suffix(".txt").unwrap();
writeln!(f, "-----BEGIN RSA PRIVATE KEY-----").unwrap();
let findings = scanner.scan_file(f.path()).unwrap().0;
assert!(findings.iter().any(|f| f.rule_id == "private-key-header"), "private-key-header not detected");
}
// ββ suppression ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
#[test]
fn leakguard_ignore_suppresses_finding() {
let scanner = default_scanner();
let mut f = NamedTempFile::with_suffix(".txt").unwrap();
writeln!(f, "aws_key = AKIAIOSFODNN7EXAMPLE1234 # leakguard-ignore").unwrap();
let findings = scanner.scan_file(f.path()).unwrap().0;
assert!(findings.is_empty(), "# leakguard-ignore should suppress the finding");
}
#[test]
fn replace_text_replaces_detected_secret_values() {
let scanner = default_scanner();
let input = "aws_key = AKIAIOSFODNN7EXAMPLE\nkeep = 1\n";
let out = scanner.replace_text(input, "[MASKED]");
assert!(out.contains("aws_key = [MASKED]"));
assert!(out.contains("keep = 1"));
assert!(!out.contains("AKIAIOSFODNN7EXAMPLE"));
}
#[test]
fn replace_text_respects_suppress_markers() {
let scanner = default_scanner();
let input = "aws_key = AKIAIOSFODNN7EXAMPLE # leakguard-ignore\n";
let out = scanner.replace_text(input, "[MASKED]");
assert_eq!(out, input);
}
#[test]
fn replace_text_with_stats_reports_replacement_count() {
let scanner = default_scanner();
let input = "a=AKIAIOSFODNN7EXAMPLE\nb=AKIAIOSFODNN7EXAMPLE\n";
let (out, count) = scanner.replace_text_with_stats(input, "[MASKED]");
assert_eq!(count, 2);
assert!(out.contains("[MASKED]"));
assert!(!out.contains("AKIAIOSFODNN7EXAMPLE"));
}
#[test]
fn noqa_secrets_suppresses_finding() {
let scanner = default_scanner();
let mut f = NamedTempFile::with_suffix(".txt").unwrap();
writeln!(f, "aws_key = AKIAIOSFODNN7EXAMPLE1234 # noqa-secrets").unwrap();
let findings = scanner.scan_file(f.path()).unwrap().0;
assert!(findings.is_empty(), "# noqa-secrets should suppress the finding");
}
// ββ disabled rules βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
#[test]
fn disabled_rule_is_not_triggered() {
let toml = r#"[rules]
disable = ["github-pat"]
"#;
let cfg: Config = toml::from_str(toml).unwrap();
let scanner = Scanner::new(builtin_rules(), 1024, &cfg).unwrap();
let mut f = NamedTempFile::with_suffix(".txt").unwrap();
writeln!(f, "token = ghp_ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890").unwrap();
let findings = scanner.scan_file(f.path()).unwrap().0;
assert!(!findings.iter().any(|f| f.rule_id == "github-pat"), "disabled rule should not fire");
}
// ββ localhost exclusion ββββββββββββββββββββββββββββββββββββββββββββββββββ
#[test]
fn localhost_http_url_not_flagged() {
let scanner = default_scanner();
let mut f = NamedTempFile::with_suffix(".txt").unwrap();
writeln!(f, "url = http://localhost:8080/api").unwrap();
let findings = scanner.scan_file(f.path()).unwrap().0;
assert!(!findings.iter().any(|f| f.rule_id == "http-insecure-url"), "localhost http should not be flagged");
}
}
|