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.

fix(secrets): keep env secret matching on a single line (#149)

Brixton Pham1b8636a2bf04
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0c9d502..4d2a291 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,11 @@
## Unreleased
+- Secrets: `secret.env-file` and `secret.env-example-value` stay on one line instead of matching
+ across line breaks, so an empty assignment like `API_KEY=` followed by another variable is no
+ longer reported, a blank line above a placeholder like `your-placeholder-key-here` no longer
+ breaks placeholder skipping, and a real secret after a blank line is reported on its own line
+ instead of the blank line above it.
- Config and agent files: `remote-script` also reports a shell option with a value before `-c`
(`bash -o pipefail -c "$(curl ...)"`, `sh -e -o nounset -c ...`) and sudo flags combined with a
value flag in one word (`curl ... | sudo -Eu root bash`, `sudo -HEu "$USER" bash`,
diff --git a/src/rules/secrets.rs b/src/rules/secrets.rs
index 1456661..eb8fabf 100644
--- a/src/rules/secrets.rs
+++ b/src/rules/secrets.rs
@@ -95,9 +95,9 @@ fn env_example(f: &FileInfo) -> bool {
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*$"#;
+const ENV_SECRET_LINE: &str = r#"(?m)^[ \t]*(?:export[ \t]+)?[A-Za-z0-9_]*(?i:secret|token|password|passwd|api_?key|private_?key|access_?key|auth|credential)[A-Za-z0-9_]*[ \t]*=[ \t]*["']?([A-Za-z0-9+/_.=!@#$%^&*\-]{16,})["']?[ \t]*\r?$"#;
-const ENV_PLACEHOLDER: &str = r"^\s*(?:export\s+)?(?:NEXT_PUBLIC|VITE|REACT_APP|EXPO_PUBLIC|NUXT_PUBLIC|GATSBY|VUE_APP|PUBLIC)_|(?i)=\s*[\x22']?(?:\S*(?:your|example|change|replace|placeholder|dummy|xxxx|\.\.\.|<)|sk_test_|pk_test_|(?-i:[a-z]+[-_][a-z]+[-_][a-z]+))|=\s*[\x22']?\*+[\x22']?\s*$|=\s*[\x22']?\$";
+const ENV_PLACEHOLDER: &str = r"^[ \t]*(?:export[ \t]+)?(?:NEXT_PUBLIC|VITE|REACT_APP|EXPO_PUBLIC|NUXT_PUBLIC|GATSBY|VUE_APP|PUBLIC)_|(?i)=[ \t]*[\x22']?(?:\S*(?:your|example|change|replace|placeholder|dummy|xxxx|\.\.\.|<)|sk_test_|pk_test_|(?-i:[a-z]+[-_][a-z]+[-_][a-z]+))|=[ \t]*[\x22']?\*+[\x22']?[ \t]*$|=[ \t]*[\x22']?\$";
// A name with `aws` and `secret` in it, then `=` or `:` and a 40-character value. `aws` and
// `secret` each start a word: at the start of the line, after a character that is not a letter
@@ -429,6 +429,53 @@ pub static RULES: &[Rule] = &[
#[cfg(test)]
mod tests {
use super::plausible_aws_secret;
+ use crate::rules::{FileInfo, check};
+
+ #[test]
+ fn env_secret_line_does_not_cross_newlines() {
+ let s = ["Fake0000", "Value0000", "Q7mZ"].concat();
+ for (name, src) in [
+ (".env.example", "API_KEY=\nNEXT_VAR=some_regular_value\n"),
+ (".env", "API_SECRET=\nAPP_ENVIRONMENT=production\n"),
+ (".env.example", "API_KEY=\n\nNEXT_VAR=some_regular_value\n"),
+ (".env.example", "API_KEY= \nNEXT_VAR=some_regular_value\n"),
+ (
+ ".env.example",
+ "API_KEY=\r\nNEXT_VAR=some_regular_value\r\n",
+ ),
+ (".env", "API_SECRET=\r\nAPP_ENVIRONMENT=production\r\n"),
+ (".env.example", "\nAPI_KEY=your-placeholder-key-here\n"),
+ (".env.example", "\r\nAPI_KEY=your-placeholder-key-here\r\n"),
+ (
+ ".env.example",
+ "export\nAPI_KEY=your-placeholder-key-here\n",
+ ),
+ ] {
+ let found = check(&FileInfo::new(name), src, false);
+ assert!(found.is_empty(), "{name} {src:?} => {found:#?}");
+ }
+ let (ex, ef) = ("secret.env-example-value", "secret.env-file");
+ for (name, rule, src, line) in [
+ (".env.example", ex, format!("\nAPI_KEY={s}\n"), 2),
+ (".env.example", ex, format!("\r\nAPI_KEY={s}\r\n"), 2),
+ (".env", ef, format!("\n export API_KEY = {s}\n"), 2),
+ (
+ ".env",
+ ef,
+ format!("\n export SECRET_KEY = \"{s}\" \r\n"),
+ 2,
+ ),
+ (".env", ef, format!("API_KEY={s} \r\n"), 1),
+ ] {
+ let found: Vec<_> = check(&FileInfo::new(name), &src, false)
+ .into_iter()
+ .filter(|f| f.rule == rule)
+ .collect();
+ assert_eq!(found.len(), 1, "{name} {src:?} => {found:#?}");
+ assert_eq!(found[0].line, line);
+ assert!(found[0].snippet.contains('=') && !found[0].snippet.contains(&s));
+ }
+ }
#[test]
fn aws_secret_value_check() {
diff --git a/tests/fixtures/secrets/.env b/tests/fixtures/secrets/.env
index 47d4187..0c7b361 100644
--- a/tests/fixtures/secrets/.env
+++ b/tests/fixtures/secrets/.env
@@ -6,3 +6,8 @@ NEXT_PUBLIC_SEARCH_API_KEY=a63d9f0c2b7e4d18a5f3
JWT_SECRET={{fake:base64-padded}}
DB_PASSWORD={{fake:symbol-secret}}
ADMIN_PASSWORD=$POSTGRES_PASSWORD_PROD
+
+API_SECRET=
+APP_ENVIRONMENT=production
+
+ export AUTH_TOKEN = {{fake:random}}
diff --git a/tests/fixtures/secrets/.env.example b/tests/fixtures/secrets/.env.example
index 32db54e..792ddf2 100644
--- a/tests/fixtures/secrets/.env.example
+++ b/tests/fixtures/secrets/.env.example
@@ -8,3 +8,10 @@ STRIPE_SECRET_KEY=sk_test_xxxxxxxxxxxxxxxxxxxxxxxx
TOKEN_SECRET="this-is-a-secret-value-with-at-least-32-characters"
MQTT_PASSWORD="a_secure_password"
PASSWORD=****************
+
+API_KEY=your-placeholder-key-here
+
+GOOGLE_GENAI_API_KEY=
+GOOGLE_GENAI_USE_VERTEXAI=0
+
+ export SECRET_KEY = "{{fake:random}}"
diff --git a/tests/fixtures/secrets/EXPECTED b/tests/fixtures/secrets/EXPECTED
index babeca6..3187aa2 100644
--- a/tests/fixtures/secrets/EXPECTED
+++ b/tests/fixtures/secrets/EXPECTED
@@ -1,6 +1,7 @@
.env:2 secret.env-file
.env:6 secret.env-file
.env:7 secret.env-file
+.env:13 secret.env-file
app/db.js:1 secret.url-credentials
config.py:1 secret.aws-access-key
config.py:10 secret.google-api-key
@@ -14,6 +15,7 @@ config.py:9 secret.openai-key
keys/deploy.pem:1 secret.private-key
keys/service-account.json:5 secret.private-key
.env.example:2 secret.env-example-value
+.env.example:17 secret.env-example-value
.env.example:5 secret.public-env-secret
.gitignore:1 secret.env-not-ignored
app/client.ts:1 secret.public-env-secret