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): only count .gitignore lines that really ignore .env (#95)
Onur Ugurbd80f2c78d16
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 1d9041a..de77bad 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -10,6 +10,8 @@- Agent: `agent.unrestricted-shell` now reads `permissions.allow` in `.claude/settings*.json` instead of matching `"Bash"` anywhere in the file, so entries in `deny`, `ask`, or hook matchers are no longer flagged.- Secrets: `secret.env-not-ignored` is reported at the `.env` file when the project has no `.gitignore`at all. Example files like `.env.example` alone no longer trigger it.+- Secrets: `secret.env-not-ignored` reads `.gitignore` the way git does, so only a line that really ignores+ `.env` counts. `.envrc`, `.env.local`, `.env/`, `# .env` or a later `!.env` no longer hide it.- Secrets: `secret.env-file` and `secret.env-example-value` now catch base64 values ending in `=` padding andpasswords with `!@#$%^&*`. A value that is only `*` characters or starts with `$` (a reference like`$POSTGRES_PASSWORD`) is skipped.diff --git a/src/rules/secrets.rs b/src/rules/secrets.rsindex 8ebb430..0d593bd 100644--- a/src/rules/secrets.rs+++ b/src/rules/secrets.rs@@ -1,5 +1,7 @@use std::path::Path;+use ignore::gitignore::GitignoreBuilder;+use super::{FileInfo, Meta, Rule, Text, finding};use crate::{Family, Finding, Severity};@@ -19,12 +21,9 @@ pub fn env_not_ignored(root: &Path, env_files: &[String]) -> Option<Finding> {let first_env = env_files.iter().filter(|p| !is_env_template(p)).min()?;// With no .gitignore nothing is ignored, so report at the env file itself.let gitignore = std::fs::read_to_string(root.join(".gitignore")).ok();- let covered = gitignore.as_deref().is_some_and(|src| {- src.lines().map(str::trim).any(|l| {- let l = l.trim_start_matches("**/").trim_start_matches('/');- l.starts_with(".env") || l.starts_with("*.env")- })- });+ let covered = gitignore+ .as_deref()+ .is_some_and(|src| ignores_env(root, src));if covered {return None;}@@ -60,6 +59,20 @@ pub fn env_not_ignored(root: &Path, env_files: &[String]) -> Option<Finding> {))}+/// Whether `.gitignore` text really ignores a `.env` file at the root, read+/// the way git reads it: comments, negations, anchors, directory-only+/// patterns, trailing spaces and a leading byte order mark all count. A line+/// that is not a valid pattern is skipped.+fn ignores_env(root: &Path, src: &str) -> bool {+ let mut builder = GitignoreBuilder::new(root);+ for line in src.trim_start_matches('\u{feff}').lines() {+ let _ = builder.add_line(None, line);+ }+ builder+ .build()+ .is_ok_and(|gitignore| gitignore.matched(root.join(".env"), false).is_ignore())+}+fn any_text(_: &FileInfo) -> bool {true}diff --git a/tests/scan.rs b/tests/scan.rsindex 1d82e18..3a60f6c 100644--- a/tests/scan.rs+++ b/tests/scan.rs@@ -82,6 +82,53 @@ fn env_example_without_gitignore() {);}+#[test]+fn gitignore_must_really_match_env() {+ // (.gitignore contents, env file in the project, expect the finding)+ let cases = [+ (".envrc\n", ".env", true),+ (".env.local\n", ".env", true),+ (".env.example\n", ".env", true),+ (".env.*\n", ".env", true),+ ("# .env\n", ".env", true),+ (".env/\n", ".env", true),+ ("config/.env\n", ".env", true),+ (".env*\n!.env\n", ".env", true),+ ("node_modules\n", ".env", true),+ (".env\n", ".env.production", false),+ ("/.env\n", ".env.production", false),+ ("**/.env\n", ".env.production", false),+ ("*.env\n", ".env.production", false),+ (".env \n", ".env.production", false),+ (".env\r\n", ".env.production", false),+ ("\u{feff}.env\n", ".env.production", false),+ (".env*\n!.env.production\n", ".env.production", false),+ (".env\n!.env.example\n", ".env.production", false),+ ];+ let root =+ std::env::temp_dir().join(format!("safe-anything-env-patterns-{}", std::process::id()));+ let mut wrong = Vec::new();+ for (gitignore, env, expected) in cases {+ let _ = std::fs::remove_dir_all(&root);+ std::fs::create_dir_all(&root).unwrap();+ std::fs::write(root.join(".gitignore"), gitignore).unwrap();+ std::fs::write(root.join(env), "DEBUG=1\n").unwrap();+ let report = safe_anything::scan(&root, &safe_anything::Options::offline()).unwrap();+ let found = report+ .findings+ .iter()+ .any(|f| f.rule == "secret.env-not-ignored");+ if found != expected {+ wrong.push((gitignore, env, expected));+ }+ }+ let _ = std::fs::remove_dir_all(&root);+ assert!(+ wrong.is_empty(),+ "(gitignore, env file, expected finding): {wrong:#?}"+ );+}+#[test]fn code() {check("code");