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
//! Security analyzer — scans for hardcoded secrets, .env files, and unsafe blocks.
use std::path::{Path, PathBuf};
use crate::assessment::{analyzer::AnalysisContext, Finding};
use crate::assessment::analyzer::Analyzer;
/// Analyzer that detects common security anti-patterns.
pub struct SecurityAnalyzer;
/// Patterns that suggest hardcoded secrets (case-insensitive prefix match).
const SECRET_PATTERNS: &[&str] = &[
"api_key=",
"api_key =",
"apikey=",
"apikey =",
"password=",
"password =",
"passwd=",
"passwd =",
"secret=",
"secret =",
"token=",
"token =",
"aws_secret",
"private_key",
];
impl Analyzer for SecurityAnalyzer {
fn id(&self) -> &str {
"security"
}
fn name(&self) -> &str {
"Security Analyzer"
}
fn categories(&self) -> &[&str] {
&["security"]
}
fn analyze(&self, project: &Path, files: &[PathBuf], _context: &AnalysisContext) -> Vec<Finding> {
let mut findings = Vec::new();
for path in files {
let rel = path.strip_prefix(project).unwrap_or(path);
let rel_str = rel.display().to_string();
let name = path
.file_name()
.and_then(|n| n.to_str())
.unwrap_or("");
// Detect committed .env files
if name == ".env" || name.starts_with(".env.") {
findings.push(Finding {
severity: "error".into(),
category: "security".into(),
file: rel_str.clone(),
line: None,
message: "Environment file should not be committed to version control"
.into(),
});
continue;
}
// Skip test files for secret detection to reduce noise
let is_test = rel_str.contains("test")
|| rel_str.contains("spec")
|| rel_str.contains("fixture")
|| rel_str.contains("mock");
let content = match std::fs::read_to_string(path) {
Ok(c) => c,
Err(_) => continue,
};
let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
// Hardcoded secret patterns (skip test files)
if !is_test {
for (i, line) in content.lines().enumerate() {
let lower = line.to_lowercase();
// Skip comment lines
let trimmed = lower.trim();
if trimmed.starts_with("//")
|| trimmed.starts_with('#')
|| trimmed.starts_with("/*")
|| trimmed.starts_with('*')
{
continue;
}
for pattern in SECRET_PATTERNS {
if lower.contains(pattern) {
// Verify there is an actual value (not just the key name)
if let Some(pos) = lower.find('=') {
let after = lower[pos + 1..].trim();
// Skip empty assignments and placeholder values
if !after.is_empty()
&& after != "\"\""
&& after != "''"
&& !after.starts_with("env")
&& !after.starts_with("std::env")
&& !after.starts_with("process.env")
&& !after.starts_with("os.environ")
{
findings.push(Finding {
severity: "error".into(),
category: "security".into(),
file: rel_str.clone(),
line: Some(i + 1),
message: format!(
"Possible hardcoded secret (matched '{pattern}')"
),
});
break; // one finding per line
}
}
}
}
}
}
// Detect unsafe blocks in Rust files
if ext == "rs" {
for (i, line) in content.lines().enumerate() {
let trimmed = line.trim();
if trimmed.contains("unsafe {") || trimmed.contains("unsafe{") || trimmed == "unsafe {" {
findings.push(Finding {
severity: "warning".into(),
category: "security".into(),
file: rel_str.clone(),
line: Some(i + 1),
message: "Unsafe block detected — review for memory safety".into(),
});
}
}
}
}
findings
}
}