Find the holes before someone else does. A fast, 100% Rust vulnerability scanner: XSS, SQL injection, leaked secrets, dependency CVEs and AI agent setups.
Rust ai-agentsclaude-codeclaude-code-pluginclicodexcursordevsecopsmcposvowasprustsastsecret-scanningsecretssecuritysecurity-scannersql-injectionvulnerabilityvulnerability-scannerxss
fix(secrets): match prefixed names like db_password in hardcoded-password (#195)
Pranav Aryab7012b1ec9e0
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 0dbc7b6..851f500 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -73,6 +73,12 @@matched name, so `connect(user="testuser", password="...")` and`{"env": "none", "password": "..."}` are reported, while a placeholder in the password's ownvalue stays quiet.+- Secrets: `secret.hardcoded-password` now also matches names where the secret word follows+ `_`-separated words (`db_password`, `ADMIN_PWD`, `stripe_api_key`). The word still has to end+ the name, so `password_hash` and `api_keys_count` stay quiet. It no longer runs on `.env` and+ `.env.*` files, which `secret.env-file` and `secret.env-example-value` already cover, so a+ committed `.env` is reported once and placeholders in `.env.example` stay quiet (#188, #195 by+ @pranavarya007).- Secrets: `secret.env-file` and `secret.env-example-value` now also match variable names where`PASS` or `PWD` is a whole `_`-separated word (`DB_PASS`, `SMTP_PASS`, `PASS_FILE`, `MYSQL_PWD`),while keeping names like `PASSPORT_ID`, `BYPASS_CACHE` and `COMPASS_URL` quiet.diff --git a/src/rules.rs b/src/rules.rsindex f0cb7f7..9a17265 100644--- a/src/rules.rs+++ b/src/rules.rs@@ -1177,7 +1177,9 @@ mod tests {let pub_sec = ["VITE", "_ADMIN_SECRET"].concat();let overlap_public = format!("{pub_sec}=\"{gh1}\"\n");let found = check(&FileInfo::new("app.js"), &overlap_public, false);- assert_eq!(found.len(), 2);+ // The name-only public rule, the GitHub token and `secret.hardcoded-password`+ // (the name ends in `_SECRET`).+ assert_eq!(found.len(), 3);for f in &found {assert_eq!(f.snippet, format!("{pub_sec}****"));}@@ -1378,12 +1380,16 @@ mod tests {"{{\"mcpServers\":{{\"s\":{{\"env\":{{\"API_TOKEN\":\"{short_mb}\",\"DB_PASSWORD\":\"{long_mb}\"}}}}}}}}\n");let found = check(&FileInfo::new(".mcp.json"), &prefix_leak_mcp, false);- assert_eq!(found.len(), 1);- assert!(- !found[0].snippet.contains(&short_mb),- "short multi-byte secret leaked via another secret's prefix: {}",- found[0].snippet- );+ // `agent.mcp-plaintext-secret`, plus `secret.hardcoded-password` for `DB_PASSWORD`.+ assert_eq!(found.len(), 2);+ for f in &found {+ assert!(+ !f.snippet.contains(&short_mb),+ "short multi-byte secret leaked via another secret's prefix in {}: {}",+ f.rule,+ f.snippet+ );+ }// Multi-line password assignment also masks a copy on the assignment's first line.let multiline_pw = format!("log(\"{pass}\"); password =\n \"{pass}\";\n");diff --git a/src/rules/secrets.rs b/src/rules/secrets.rsindex 3a552f1..8de466e 100644--- a/src/rules/secrets.rs+++ b/src/rules/secrets.rs@@ -81,6 +81,10 @@ fn any_text(_: &FileInfo) -> bool {true}+fn not_env(f: &FileInfo) -> bool {+ !is_env(&f.name)+}+fn is_env_template(name: &str) -> bool {["example", "sample", "template", "dist", "defaults"].iter()@@ -488,10 +492,10 @@ pub static RULES: &[Rule] = &[id: "secret.hardcoded-password",family: Family::Secret,severity: Severity::High,- files: any_text,- pattern: r#"(?i)\b(password|passwd|pwd|secret|api_?key|access_?token|auth_?token|client_?secret)\b["']?\s*[:=]\s*["']([^"'\s]{8,})["']"#,+ files: not_env,+ pattern: r#"(?i)\b(?:[a-z0-9]+_)*(password|passwd|pwd|secret|api_?key|access_?token|auth_?token|client_?secret)\b["']?\s*[:=]\s*["']([^"'\s]{8,})["']"#,unless: Some(- r#"(?i)\b(password|passwd|pwd|secret|api_?key|access_?token|auth_?token|client_?secret)\b["']?\s*[:=]\s*["'](?:[^"']*(?:\.\.\.|…)|(\$|changeme|password|secret|example|x{3,}|your[-_]|<|\$\{|\{\{|%\(|dummy|test|placeholder|redacted|\*{3,}|none|null))"#,+ r#"(?i)\b(?:[a-z0-9]+_)*(password|passwd|pwd|secret|api_?key|access_?token|auth_?token|client_?secret)\b["']?\s*[:=]\s*["'](?:[^"']*(?:\.\.\.|…)|(\$|changeme|password|secret|example|x{3,}|your[-_]|<|\$\{|\{\{|%\(|dummy|test|placeholder|redacted|\*{3,}|none|null))"#,),message: "A password or secret is written in the code.",fix: ROTATE,@@ -787,6 +791,63 @@ mod tests {}}+ #[test]+ fn hardcoded_password_leaves_env_files_to_the_env_rules() {+ let v = ["Zq7Wd0000", "Kx9Fake"].concat();+ let src = format!("DB_PASSWORD=\"{v}\"\nMQTT_PASSWORD=\"a_secure_password\"\n");+ for name in [+ ".env",+ ".env.example",+ ".env.local",+ "config/.env.production",+ ] {+ let hits: Vec<_> = check(&FileInfo::new(name), &src, false)+ .into_iter()+ .filter(|f| f.rule == "secret.hardcoded-password")+ .collect();+ assert!(hits.is_empty(), "{name} => {hits:#?}");+ }+ let hits = check(+ &FileInfo::new("settings.env.py"),+ &format!("DB_PASSWORD=\"{v}\"\n"),+ false,+ )+ .into_iter()+ .filter(|f| f.rule == "secret.hardcoded-password")+ .count();+ assert_eq!(hits, 1);+ }++ #[test]+ fn hardcoded_password_matches_prefixed_names() {+ let v = ["Zq7Wd0000", "Kx9Fake"].concat();+ let hits = |src: &str| -> Vec<usize> {+ check(&FileInfo::new("settings.py"), src, false)+ .into_iter()+ .filter(|f| f.rule == "secret.hardcoded-password")+ .map(|f| f.line)+ .collect()+ };+ let src = format!(+ "db_password = \"{v}\"\nADMIN_PWD = \"{v}\"\nstripe_api_key = \"{v}\"\nprod_db_client_secret = \"{v}\"\npassword = \"{v}\"\n"+ );+ assert_eq!(hits(&src), vec![1, 2, 3, 4, 5], "{src:?}");+ for name in [+ "password_hash",+ "db_password_hash",+ "passwords_match",+ "api_keys_count",+ "db_passwords",+ ] {+ let src = format!("{name} = \"{v}\"\n");+ assert!(hits(&src).is_empty(), "{src:?}");+ }+ for value in ["changeme123", "${DB_PASSWORD}", "your-password-here"] {+ let src = format!("db_password = \"{value}\"\n");+ assert!(hits(&src).is_empty(), "{src:?}");+ }+ }+#[test]fn age_key_after_json_escape() {let bech32 = &"QPZRY9X8GF2TVDW0S3JN54KHCE6MUA7L".repeat(2)[..58];diff --git a/tests/fixtures/agent/EXPECTED b/tests/fixtures/agent/EXPECTEDindex 0a11118..7438eb4 100644--- a/tests/fixtures/agent/EXPECTED+++ b/tests/fixtures/agent/EXPECTED@@ -31,6 +31,7 @@.mcp.json:63 agent.mcp-plaintext-secret.mcp.json:69 agent.mcp-plaintext-secret.mcp.json:75 agent.mcp-plaintext-secret+.mcp.json:75 secret.hardcoded-password.mcp.json:76 agent.mcp-plaintext-secret.mcp.json:5 agent.mcp-unpinned.mcp.json:7 agent.mcp-plaintext-secretdiff --git a/tests/fixtures/secrets/EXPECTED b/tests/fixtures/secrets/EXPECTEDindex 4ed7d1b..45973ac 100644--- a/tests/fixtures/secrets/EXPECTED+++ b/tests/fixtures/secrets/EXPECTED@@ -51,6 +51,7 @@ aws/credentials:3 secret.aws-secret-keyconfig.py:43 secret.aws-secret-keyconfig.py:47 secret.aws-secret-keyconfig.py:48 secret.aws-secret-key+config.py:49 secret.hardcoded-passwordconfig.py:55 secret.aws-secret-keyconfig.py:57 secret.google-oauth-secretconfig.py:58 secret.google-oauth-secret