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): find private keys written with \n escapes in JSON (#93)

Onur Ugur5290a2f20e26
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5bf3383..d6902a8 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -37,6 +37,8 @@
local package) are no longer counted or looked up on PyPI.
- Secrets: `secret.url-credentials` skips a connection string whose password is an angle-bracket placeholder
such as `mongodb+srv://<username>:<password>@host/db`, the form MongoDB Atlas shows in its docs.
+- Secrets: `secret.private-key` also finds a key written with `\n` escapes inside a string, the way a Google
+ service-account JSON file stores it. The whole key body is masked in the output.
- Dependencies: `pnpm-lock.yaml` is read again. A blank line ended the `packages:` section, and
pnpm writes one right after it, so every pnpm project reported 0 packages checked.
- Agent: new rule `agent.mcp-auto-approve` (high) for `"enableAllProjectMcpServers": true` in
diff --git a/src/rules/secrets.rs b/src/rules/secrets.rs
index 261bd6f..8ebb430 100644
--- a/src/rules/secrets.rs
+++ b/src/rules/secrets.rs
@@ -175,7 +175,10 @@ pub static RULES: &[Rule] = &[
secret!(
"secret.private-key",
Critical,
- r"-----BEGIN (?:(?:RSA|EC|DSA|OPENSSH|PGP|ENCRYPTED) )?PRIVATE KEY(?: BLOCK)?-----\r?\n([A-Za-z0-9+/=]{20,})",
+ // A PEM file has real line breaks. A JSON string (a Google
+ // service-account key) has `\n` escapes instead; there the whole body
+ // is on the line, so all of it is captured for redaction.
+ r"-----BEGIN (?:(?:RSA|EC|DSA|OPENSSH|PGP|ENCRYPTED) )?PRIVATE KEY(?: BLOCK)?-----(?:\r?\n([A-Za-z0-9+/=]{20,})|\\n([A-Za-z0-9+/=]{20,}(?:\\n[A-Za-z0-9+/=]+)*))",
"A private key is stored in the repository."
),
Rule {
diff --git a/tests/cli.rs b/tests/cli.rs
index e0813d1..8a9dd6b 100644
--- a/tests/cli.rs
+++ b/tests/cli.rs
@@ -192,6 +192,12 @@ fn secrets_are_redacted_in_every_output() {
let token_tail = &token[token.len() - 16..];
let password = common::fake("password");
let aws = common::fake("aws");
+ let key_json = common::fake("private-key-json");
+ let key_parts: Vec<&str> = key_json
+ .split("\\n")
+ .filter(|part| !part.contains("PRIVATE KEY") && !part.is_empty())
+ .collect();
+ assert_eq!(key_parts.len(), 3);
for args in [
vec![fixture("agent"), "--offline".into()],
vec![fixture("agent"), "--offline".into(), "--json".into()],
@@ -203,6 +209,12 @@ fn secrets_are_redacted_in_every_output() {
assert!(!out.contains(token_tail), "token leaked with {args:?}");
assert!(!out.contains(&password), "password leaked with {args:?}");
assert!(!out.contains(&aws[4..]), "AWS key leaked with {args:?}");
+ for part in &key_parts {
+ assert!(
+ !out.contains(&part[4..]),
+ "private key leaked with {args:?}"
+ );
+ }
}
let (_, out) = run(&[&fixture("agent"), "--offline"]);
assert!(
diff --git a/tests/common/mod.rs b/tests/common/mod.rs
index 7ea3947..cbfe7f6 100644
--- a/tests/common/mod.rs
+++ b/tests/common/mod.rs
@@ -9,6 +9,8 @@ const MIXED: &str = "Q7mZ2xK9pL4vR8tN3wB6yH1cF5jD0sG";
const UPPER: &str = "Q7MZ2XK9PL4VR8TN3WB6YH1CF5JD0SG";
const LETTERS: &str = "QmZxKpLvRtNwByHcFjDsG";
const HEX: &str = "a3f91c07d5e284b6";
+// Unlike the others, so a leak of this body is not mistaken for another token.
+const KEY_BODY: &str = "Zr8Wq2Xn5Mb7Vc4Lk1Jh6Gf9Dd3Ss0AaUe";
pub fn fake(name: &str) -> String {
match name {
@@ -54,6 +56,21 @@ pub fn fake(name: &str) -> String {
&body(48, MIXED),
]
.concat(),
+ // How a Google service-account JSON file stores a key: one JSON
+ // string, with `\n` escapes instead of line breaks.
+ "private-key-json" => [
+ "-----BEGIN ",
+ "PRIVATE KEY-----\\n",
+ "MIIEvQIBADANBgkq",
+ &body(48, KEY_BODY),
+ "\\n",
+ &body(64, KEY_BODY),
+ "\\n",
+ &body(40, KEY_BODY),
+ "=\\n-----END ",
+ "PRIVATE KEY-----\\n",
+ ]
+ .concat(),
other => panic!("no fake value named {other}"),
}
}
diff --git a/tests/fixtures/secrets/EXPECTED b/tests/fixtures/secrets/EXPECTED
index aaa16bf..4edb705 100644
--- a/tests/fixtures/secrets/EXPECTED
+++ b/tests/fixtures/secrets/EXPECTED
@@ -12,6 +12,7 @@ config.py:6 secret.stripe-live-key
config.py:8 secret.anthropic-key
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:5 secret.public-env-secret
.gitignore:1 secret.env-not-ignored
diff --git a/tests/fixtures/secrets/docs/service-account.json b/tests/fixtures/secrets/docs/service-account.json
new file mode 100644
index 0000000..6466b4e
--- /dev/null
+++ b/tests/fixtures/secrets/docs/service-account.json
@@ -0,0 +1,5 @@
+{
+ "help": "Paste the key from the console. It starts with -----BEGIN PRIVATE KEY-----\n and ends with -----END PRIVATE KEY-----\n",
+ "private_key": "-----BEGIN PRIVATE KEY-----\nYOUR_KEY_HERE\n-----END PRIVATE KEY-----\n",
+ "example": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n"
+}
diff --git a/tests/fixtures/secrets/keys/service-account.json b/tests/fixtures/secrets/keys/service-account.json
new file mode 100644
index 0000000..aac200b
--- /dev/null
+++ b/tests/fixtures/secrets/keys/service-account.json
@@ -0,0 +1,9 @@
+{
+ "type": "service_account",
+ "project_id": "example-project",
+ "private_key_id": "0123456789abcdef0123456789abcdef01234567",
+ "private_key": "{{fake:private-key-json}}",
+ "client_email": "[email protected]",
+ "client_id": "123456789012345678901",
+ "token_uri": "https://oauth2.googleapis.com/token"
+}