Skip to content

Find the holes before someone else does. A fast, 100% Rust vulnerability scanner: XSS, SQL injection, leaked secrets, dependency CVEs and AI agent setups.

Report unignored .env files when there is no .gitignore (#69)

Jay9ec74d08848d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b056a5a..b12e277 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,8 @@
## Unreleased
+- 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-file` and `secret.env-example-value` now catch base64 values ending in `=` padding and
passwords 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.rs
index e64b583..261bd6f 100644
--- a/src/rules/secrets.rs
+++ b/src/rules/secrets.rs
@@ -16,25 +16,38 @@ pub fn is_env(name: &str) -> bool {
}
pub fn env_not_ignored(root: &Path, env_files: &[String]) -> Option<Finding> {
- if env_files.is_empty() {
- return None;
- }
- let src = std::fs::read_to_string(root.join(".gitignore")).ok()?;
- let covered = 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 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")
+ })
});
if covered {
return None;
}
+ let has_gitignore = gitignore.is_some();
+ let (file, src) = match gitignore {
+ Some(src) => (".gitignore".to_string(), src),
+ None => (
+ first_env.clone(),
+ std::fs::read_to_string(root.join(first_env)).unwrap_or_default(),
+ ),
+ };
let text = Text::new(&src);
if text.suppressed(1) {
return None;
}
+ // `.env` contents are only needed to honor suppression directives; never
+ // copy the secret value into a diagnostic snippet.
+ let empty = Text::new("");
+ let finding_text = if has_gitignore { &text } else { &empty };
let (id, severity, message) = ENV_NOT_IGNORED;
Some(finding(
- &FileInfo::new(".gitignore"),
- &text,
+ &FileInfo::new(&file),
+ finding_text,
1,
Meta {
id,
@@ -51,18 +64,18 @@ fn any_text(_: &FileInfo) -> bool {
true
}
+fn is_env_template(name: &str) -> bool {
+ ["example", "sample", "template", "dist", "defaults"]
+ .iter()
+ .any(|s| name.ends_with(s))
+}
+
fn env_file(f: &FileInfo) -> bool {
- is_env(&f.name)
- && !["example", "sample", "template", "dist", "defaults"]
- .iter()
- .any(|s| f.name.ends_with(s))
+ is_env(&f.name) && !is_env_template(&f.name)
}
fn env_example(f: &FileInfo) -> bool {
- is_env(&f.name)
- && ["example", "sample", "template", "dist", "defaults"]
- .iter()
- .any(|s| f.name.ends_with(s))
+ is_env(&f.name) && is_env_template(&f.name)
}
const ENV_SECRET_LINE: &str = r#"(?m)^\s*(?:export\s+)?[A-Za-z0-9_]*(?i:secret|token|password|passwd|api_?key|private_?key|access_?key|auth|credential)[A-Za-z0-9_]*\s*=\s*["']?([A-Za-z0-9+/_.=!@#$%^&*\-]{16,})["']?\s*$"#;
diff --git a/tests/scan.rs b/tests/scan.rs
index a3d981e..0ea991b 100644
--- a/tests/scan.rs
+++ b/tests/scan.rs
@@ -36,6 +36,52 @@ fn secrets() {
check("secrets");
}
+#[test]
+fn secrets_without_gitignore() {
+ // Self-contained: the project is built in a temp dir so the test does not
+ // depend on a fixture folder that must exist alongside it.
+ let root =
+ std::env::temp_dir().join(format!("safe-anything-no-gitignore-{}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&root);
+ std::fs::create_dir_all(&root).unwrap();
+ std::fs::write(root.join(".env"), "SETTING=example-value\n").unwrap();
+ let report = safe_anything::scan(&root, &safe_anything::Options::offline()).unwrap();
+ let finding = report
+ .findings
+ .iter()
+ .find(|f| f.rule == "secret.env-not-ignored")
+ .expect("expected an unignored .env finding");
+ let _ = std::fs::remove_dir_all(&root);
+ assert_eq!(finding.path, ".env");
+ assert_eq!(finding.line, 1);
+ assert!(
+ finding.snippet.is_empty(),
+ "secret leaked in snippet: {}",
+ finding.snippet
+ );
+ assert!(!format!("{finding:?}").contains("example-value"));
+}
+
+#[test]
+fn env_example_without_gitignore() {
+ let root = std::env::temp_dir().join(format!(
+ "safe-anything-example-no-gitignore-{}",
+ std::process::id()
+ ));
+ let _ = std::fs::remove_dir_all(&root);
+ std::fs::create_dir_all(&root).unwrap();
+ std::fs::write(root.join(".env.example"), "SETTING=\n").unwrap();
+ let report = safe_anything::scan(&root, &safe_anything::Options::offline()).unwrap();
+ let _ = std::fs::remove_dir_all(&root);
+ assert!(
+ !report
+ .findings
+ .iter()
+ .any(|f| f.rule == "secret.env-not-ignored"),
+ "a lone .env.example should not be reported"
+ );
+}
+
#[test]
fn code() {
check("code");