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
//! Secret Detection
//!
//! Detects hardcoded secrets, API keys, passwords, and tokens in source code.
//! CWE-798: Use of Hard-coded Credentials
use crate::detectors::base::{is_test_file, Detector, DetectorConfig};
use crate::graph::GraphStore;
use crate::models::{Finding, Severity};
use anyhow::Result;
use regex::Regex;
use std::path::{Path, PathBuf};
use std::sync::OnceLock;
use tracing::debug;
use uuid::Uuid;
/// Secret patterns with their names and severity
static SECRET_PATTERNS: OnceLock<Vec<SecretPattern>> = OnceLock::new();
struct SecretPattern {
name: &'static str,
pattern: Regex,
severity: Severity,
}
fn get_patterns() -> &'static Vec<SecretPattern> {
SECRET_PATTERNS.get_or_init(|| {
vec![
// AWS
SecretPattern {
name: "AWS Access Key ID",
pattern: Regex::new(r"AKIA[0-9A-Z]{16}").unwrap(),
severity: Severity::Critical,
},
SecretPattern {
name: "AWS Secret Access Key",
pattern: Regex::new(r"(?i)aws_secret_access_key\s*[=:]\s*[A-Za-z0-9/+=]{40}").unwrap(),
severity: Severity::Critical,
},
// GitHub
SecretPattern {
name: "GitHub Token",
pattern: Regex::new(r"ghp_[a-zA-Z0-9]{36}").unwrap(),
severity: Severity::Critical,
},
// Generic API keys
SecretPattern {
name: "Generic API Key",
pattern: Regex::new(r"(?i)api[_-]?key\s*[=:]\s*[a-zA-Z0-9_\-]{20,}").unwrap(),
severity: Severity::High,
},
SecretPattern {
name: "Generic Secret",
pattern: Regex::new(r"(?i)(secret|password|passwd|pwd)\s*[=:]\s*[^\s]{8,}").unwrap(),
severity: Severity::High,
},
// Private keys
SecretPattern {
name: "Private Key",
pattern: Regex::new(r"-----BEGIN (RSA |EC |DSA |OPENSSH )?PRIVATE KEY-----").unwrap(),
severity: Severity::Critical,
},
// Slack
SecretPattern {
name: "Slack Token",
pattern: Regex::new(r"xox[baprs]-[0-9]{10,13}-[0-9]{10,13}[a-zA-Z0-9-]*").unwrap(),
severity: Severity::Critical,
},
// Stripe
SecretPattern {
name: "Stripe API Key",
pattern: Regex::new(r"sk_live_[a-zA-Z0-9]{24,}").unwrap(),
severity: Severity::Critical,
},
// Database URLs
SecretPattern {
name: "Database URL with Password",
pattern: Regex::new(r"(?i)(postgres|mysql|mongodb|redis)://[^:]+:[^@]+@").unwrap(),
severity: Severity::Critical,
},
// SendGrid
SecretPattern {
name: "SendGrid API Key",
pattern: Regex::new(r"SG\.[a-zA-Z0-9_-]{22}\.[a-zA-Z0-9_-]{43}").unwrap(),
severity: Severity::High,
},
]
})
}
pub struct SecretDetector {
config: DetectorConfig,
repository_path: PathBuf,
max_findings: usize,
}
impl SecretDetector {
pub fn new(repository_path: impl Into<PathBuf>) -> Self {
Self {
config: DetectorConfig::default(),
repository_path: repository_path.into(),
max_findings: 100,
}
}
/// Convert absolute path to relative path for consistent output
fn relative_path(&self, path: &Path) -> PathBuf {
path.strip_prefix(&self.repository_path)
.unwrap_or(path)
.to_path_buf()
}
/// Check if a Python os.environ.get() or os.getenv() call has a fallback (second argument)
/// Pattern: os.environ.get("KEY", "fallback") or os.getenv("KEY", "fallback")
fn has_python_env_fallback(line: &str) -> bool {
// Look for the pattern: os.environ.get( or os.getenv( followed by args with a comma
// This indicates a default value is provided
let line_lower = line.to_lowercase();
for pattern in ["os.environ.get(", "os.getenv("] {
if let Some(start) = line_lower.find(pattern) {
let after_pattern = &line[start + pattern.len()..];
// Count parentheses to find the matching close
let mut depth = 1;
let mut found_comma_at_depth_1 = false;
for ch in after_pattern.chars() {
match ch {
'(' => depth += 1,
')' => {
depth -= 1;
if depth == 0 {
break;
}
}
',' if depth == 1 => {
found_comma_at_depth_1 = true;
break;
}
_ => {}
}
}
if found_comma_at_depth_1 {
return true;
}
}
}
false
}
/// Check if a Go os.Getenv() call has fallback handling on the same line
/// Common patterns:
/// - `if val := os.Getenv("X"); val == "" { ... }` (short variable declaration with check)
/// - `val := os.Getenv("X"); if val == "" { val = "default" }`
/// - Using with a helper: `getEnvOr(os.Getenv("X"), "default")`
/// - Ternary-style: `func() string { if v := os.Getenv("X"); v != "" { return v }; return "default" }()`
fn has_go_env_fallback(line: &str) -> bool {
// Check for common fallback indicators on the same line
let has_empty_check = line.contains(r#"== """#) || line.contains(r#"!= """#);
let has_if_statement = line.contains("if ");
let has_fallback_helper = line.to_lowercase().contains("getenvdefault")
|| line.to_lowercase().contains("getenvor")
|| line.to_lowercase().contains("envdefault");
has_fallback_helper || (has_empty_check && has_if_statement)
}
fn scan_file(&self, path: &Path) -> Vec<Finding> {
let mut findings = vec![];
// Skip test files - they often contain test certificates/keys
if is_test_file(path) {
return findings;
}
// Use global cache for file content
let content = match crate::cache::global_cache().get_content(path) {
Some(c) => c,
None => return findings,
};
// Skip binary files
if content.contains('\0') {
return findings;
}
for (line_num, line) in content.lines().enumerate() {
// Skip comments that look like documentation
let trimmed = line.trim();
if trimmed.starts_with("//") && trimmed.contains("example") {
continue;
}
for pattern in get_patterns() {
if let Some(m) = pattern.pattern.find(line) {
let matched = m.as_str();
// Skip obvious false positives
if matched.len() < 10 {
continue;
}
if matched.contains("example") || matched.contains("EXAMPLE") {
continue;
}
if matched.contains("placeholder") || matched.contains("xxxx") {
continue;
}
// Determine effective severity based on context
let line_lower = line.to_lowercase();
let mut effective_severity = pattern.severity.clone();
// Dev fallback pattern: process.env.X || 'fallback' or process.env.X ?? 'fallback'
// These are typically local dev defaults, not production credentials
if line_lower.contains("process.env") && (line.contains("||") || line.contains("??")) {
effective_severity = Severity::Low;
}
// Python fallback patterns: os.environ.get("KEY", "fallback") or os.getenv("KEY", "fallback")
// The second argument is the default value, indicating a fallback
else if (line_lower.contains("os.environ.get(") || line_lower.contains("os.getenv("))
&& Self::has_python_env_fallback(line)
{
effective_severity = Severity::Low;
}
// Go fallback patterns: os.Getenv with fallback handling
// os.LookupEnv returns (value, found) - implies fallback handling
// Also check for common inline fallback patterns
else if line.contains("os.LookupEnv(")
|| (line.contains("os.Getenv(") && Self::has_go_env_fallback(line))
{
effective_severity = Severity::Low;
}
// Localhost URLs are lower risk - typically dev/test environments
else if matched.contains("localhost") || matched.contains("127.0.0.1") {
effective_severity = Severity::Low;
}
// Check file path for seed/script/test patterns
else if let Some(rel_path) = path.to_str() {
let rel_lower = rel_path.to_lowercase();
if rel_lower.contains("/seed")
|| rel_lower.contains("/script")
|| rel_lower.contains("/fixture")
|| rel_lower.contains(".seed.")
|| rel_lower.contains(".script.")
{
effective_severity = Severity::Low;
}
}
let line_start = line_num as u32 + 1;
findings.push(Finding {
id: Uuid::new_v4().to_string(),
detector: "SecretDetector".to_string(),
severity: effective_severity,
title: format!("Hardcoded {}", pattern.name),
description: format!(
"Potential {} found in source code at line {}. \
Secrets should be stored in environment variables or secret management systems.",
pattern.name, line_start
),
affected_files: vec![self.relative_path(path)],
line_start: Some(line_start),
line_end: Some(line_start),
suggested_fix: Some("Move this secret to an environment variable or secrets manager".to_string()),
estimated_effort: Some("15 minutes".to_string()),
category: Some("security".to_string()),
cwe_id: Some("CWE-798".to_string()),
why_it_matters: Some("Hardcoded secrets can be extracted from source code, leading to credential theft".to_string()),
..Default::default()
});
}
}
}
findings
}
}
impl Detector for SecretDetector {
fn name(&self) -> &'static str {
"secret-detection"
}
fn description(&self) -> &'static str {
"Detects hardcoded secrets, API keys, and passwords"
}
fn detect(&self, _graph: &GraphStore) -> Result<Vec<Finding>> {
let mut findings = vec![];
let walker = ignore::WalkBuilder::new(&self.repository_path)
.hidden(false)
.git_ignore(true)
.build();
for entry in walker.filter_map(|e| e.ok()) {
if findings.len() >= self.max_findings {
break;
}
let path = entry.path();
if !path.is_file() {
continue;
}
// Skip certain directories
let path_str = path.to_string_lossy();
if path_str.contains("node_modules")
|| path_str.contains(".git")
|| path_str.contains("vendor")
|| path_str.contains("target")
{
continue;
}
// Only scan text files
let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
let scannable = matches!(ext,
"py" | "js" | "ts" | "jsx" | "tsx" | "rs" | "go" | "java" |
"rb" | "php" | "cs" | "cpp" | "c" | "h" | "hpp" |
"yaml" | "yml" | "json" | "toml" | "env" | "conf" | "config" |
"sh" | "bash" | "zsh" | "properties" | "xml"
);
if !scannable {
continue;
}
debug!("Scanning for secrets: {}", path.display());
findings.extend(self.scan_file(path));
}
Ok(findings)
}
}