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): match six provider token rules after escaped newlines (#261)

Onur Ugur341ee6aa1733
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 85d116c..57949b2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,11 @@
## Unreleased
+- Secrets: `secret.dockerhub-token`, `secret.groq-key`, `secret.perplexity-key`,
+ `secret.mailgun-key`, `secret.sendgrid-key`, and `secret.digitalocean-token` now report tokens
+ that come right after an escaped newline, tab or carriage return in a quoted string
+ (`"line\ndckr_pat_..."`, `"line\ngsk_..."`, etc.) (#252, #253, #254, #256, #257, #258, #261 by
+ @OnurrUgur).
- Code: `code.sql-injection` now reads each string literal to its real closing quote in
JavaScript, TypeScript, PHP, Ruby and Go, so `"... WHERE name = '" + name + "'"` and PHP
`'... WHERE id = \'' . $id . '\''` are reported. It also reports Ruby `+` concatenation, Go
diff --git a/src/rules.rs b/src/rules.rs
index 9a17265..7072a3c 100644
--- a/src/rules.rs
+++ b/src/rules.rs
@@ -690,8 +690,25 @@ pub fn check(file: &FileInfo, src: &str, include_tests: bool) -> Vec<Finding> {
{
continue;
}
- for caps in c.re.captures_iter(src) {
+ let mut pos = 0;
+ while pos <= src.len() {
+ let Some(caps) = c.re.captures_at(src, pos) else {
+ break;
+ };
let m = caps.get(0).expect("whole match");
+ let next_after_match = if m.end() > m.start() {
+ m.end()
+ } else if let Some(ch) = src[m.end()..].chars().next() {
+ m.end() + ch.len_utf8()
+ } else {
+ src.len() + 1
+ };
+ let next_after_start = if let Some(ch) = src[m.start()..].chars().next() {
+ m.start() + ch.len_utf8()
+ } else {
+ src.len() + 1
+ };
+ pos = next_after_match;
// A name-only rule matches a variable name; what follows it on the line holds the
// value. It is read at most once per line, so a long line with many names is not read
// again for each of them.
@@ -714,6 +731,7 @@ pub fn check(file: &FileInfo, src: &str, include_tests: bool) -> Vec<Finding> {
continue;
}
if !secrets::plausible(c.rule.id, secret, &src[m.end()..]) {
+ pos = next_after_start;
continue;
}
// Record secret spans before suppression so an ignore marker silences the finding
diff --git a/src/rules/secrets.rs b/src/rules/secrets.rs
index f041fc6..7ceb0a5 100644
--- a/src/rules/secrets.rs
+++ b/src/rules/secrets.rs
@@ -166,14 +166,21 @@ const AWS_SECRET_KEY: &str = concat!(
r#"[ \t]{0,8}["'`]?([A-Za-z0-9/+]{40})"#,
);
-/// Whether a matched value can be a secret for the rule. Only `secret.aws-secret-key` is checked:
-/// its value has no prefix, so placeholders and other 40-character strings have to be told apart
-/// by the value alone, and its pattern stops after the 40 characters, so `after` (the text that
-/// follows the match) must not go on with another base64 character.
+/// Whether a matched value can be a secret for the rule. `secret.aws-secret-key` has no prefix so
+/// its value and following character are checked here; `secret.dockerhub-token` and
+/// `secret.sendgrid-key` check their trailing boundary here instead of consuming a trailing
+/// character in the regex so adjacent tokens separated by `\n` both match and redact.
pub fn plausible(id: &str, value: Option<&str>, after: &str) -> bool {
- id != "secret.aws-secret-key"
- || (value.is_some_and(plausible_aws_secret)
- && !after.starts_with(|c: char| c.is_ascii_alphanumeric() || "/+=".contains(c)))
+ match id {
+ "secret.aws-secret-key" => {
+ value.is_some_and(plausible_aws_secret)
+ && !after.starts_with(|c: char| c.is_ascii_alphanumeric() || "/+=".contains(c))
+ }
+ "secret.dockerhub-token" | "secret.sendgrid-key" => {
+ !after.starts_with(|c: char| c.is_ascii_alphanumeric() || "_-".contains(c))
+ }
+ _ => true,
+ }
}
/// Words that read as a name or a path: a first word in small letters, then words that start
@@ -318,7 +325,7 @@ pub static RULES: &[Rule] = &[
secret!(
"secret.dockerhub-token",
Critical,
- r"\b(dckr_pat_[A-Za-z0-9_\-]{27})(?:[^A-Za-z0-9_\-]|\z)",
+ r"(?:\\[nrt]|\b)(dckr_pat_[A-Za-z0-9_\-]{27})",
"A Docker Hub personal access token is written in the file."
),
secret!(
@@ -342,13 +349,13 @@ pub static RULES: &[Rule] = &[
secret!(
"secret.groq-key",
Critical,
- r"\bgsk_[A-Za-z0-9]{52}\b",
+ r"(?:\\[nrt]|\b)(gsk_[A-Za-z0-9]{52})\b",
"A Groq API key is written in the file."
),
secret!(
"secret.perplexity-key",
Critical,
- r"\bpplx-[A-Za-z0-9]{48}\b",
+ r"(?:\\[nrt]|\b)(pplx-[A-Za-z0-9]{48})\b",
"A Perplexity API key is written in the file."
),
secret!(
@@ -378,13 +385,13 @@ pub static RULES: &[Rule] = &[
secret!(
"secret.mailgun-key",
Critical,
- r"\bkey-[0-9a-f]{32}\b",
+ r"(?:\\[nrt]|\b)(key-[0-9a-f]{32})\b",
"A Mailgun API key is written in the file."
),
secret!(
"secret.sendgrid-key",
Critical,
- r"\b(SG\.[A-Za-z0-9_\-]{22}\.[A-Za-z0-9_\-]{43})(?:[^A-Za-z0-9_\-]|\z)",
+ r"(?:\\[nrt]|\b)(SG\.[A-Za-z0-9_\-]{22}\.[A-Za-z0-9_\-]{43})",
"A SendGrid API key is written in the file."
),
secret!(
@@ -396,7 +403,7 @@ pub static RULES: &[Rule] = &[
secret!(
"secret.digitalocean-token",
Critical,
- r"\bdo[opr]_v1_[a-f0-9]{64}\b",
+ r"(?:\\[nrt]|\b)(do[opr]_v1_[a-f0-9]{64})\b",
"A DigitalOcean token is written in the file."
),
secret!(
@@ -956,4 +963,127 @@ mod tests {
}
}
}
+
+ #[test]
+ fn provider_tokens_after_json_escape() {
+ let cases: &[(&str, String, String, &str)] = &[
+ (
+ "secret.dockerhub-token",
+ [
+ "dckr",
+ "_pat_",
+ &"Q7mZ2xK9pL4vR8tN3wB6yH1cF5".repeat(2)[..26],
+ "-",
+ ]
+ .concat(),
+ ["dckr", "_pat_", "short123"].concat(),
+ "a",
+ ),
+ (
+ "secret.groq-key",
+ [
+ "gs",
+ "k_",
+ &"Q7mZ2xK9pL4vR8tN3wB6yH1cF5jD0sG".repeat(2)[..52],
+ ]
+ .concat(),
+ ["gs", "k_", "short123"].concat(),
+ "a",
+ ),
+ (
+ "secret.perplexity-key",
+ [
+ "pp",
+ "lx-",
+ &"Q7mZ2xK9pL4vR8tN3wB6yH1cF5jD0sG".repeat(2)[..48],
+ ]
+ .concat(),
+ ["pp", "lx-", "short123"].concat(),
+ "a",
+ ),
+ (
+ "secret.mailgun-key",
+ ["ke", "y-", &"a3f91c07d5e284b6".repeat(2)].concat(),
+ ["ke", "y-", "0123abcd"].concat(),
+ "a",
+ ),
+ (
+ "secret.sendgrid-key",
+ [
+ "S",
+ "G.",
+ &"Q7mZ2xK9pL4vR8tN3wB6yH1cF5jD0sG"[..22],
+ ".",
+ &"Q7mZ2xK9pL4vR8tN3wB6yH1cF5jD0sG".repeat(2)[..43],
+ ]
+ .concat(),
+ ["S", "G.", "short.short"].concat(),
+ "a",
+ ),
+ (
+ "secret.digitalocean-token",
+ ["do", "p_v1_", &"a3f91c07d5e284b6".repeat(4)].concat(),
+ ["do", "p_v1_", "0123abcd"].concat(),
+ "a",
+ ),
+ ];
+
+ for (rule, token, short, extra) in cases {
+ let hits = |src: &str| -> Vec<_> {
+ check(&FileInfo::new("cfg.json"), src, false)
+ .into_iter()
+ .filter(|f| f.rule == *rule)
+ .collect()
+ };
+ for esc in [r"\n", r"\r", r"\t"] {
+ let src = format!(r#"{{"note":"line{esc}{token}"}}"#);
+ let found = hits(&src);
+ assert_eq!(found.len(), 1, "{rule} {src} => {found:#?}");
+ assert!(
+ !found[0].snippet.contains(token),
+ "{rule} leaked in snippet"
+ );
+ // Capture group 1 must keep the escape sequence out of the masked value.
+ assert!(
+ found[0].snippet.contains(&format!("line{esc}")),
+ "{rule} swallowed escape in snippet: {}",
+ found[0].snippet
+ );
+ }
+ for src in [
+ format!(r#"{{"note":"line\n{short}"}}"#),
+ format!(r#"{{"note":"my{token}"}}"#),
+ format!(r#"{{"note":"line\n{token}{extra}"}}"#),
+ ] {
+ assert!(hits(&src).is_empty(), "{rule} false positive on {src}");
+ }
+ // Two adjacent tokens separated by an escaped newline must both be found and redacted
+ // (trailing boundary checks must not consume the `\` starting the second `\n`).
+ let pair = format!(r#"{{"note":"{token}\n{token}"}}"#);
+ let pair_hits = hits(&pair);
+ assert!(
+ !pair_hits.is_empty(),
+ "{rule} missed adjacent tokens in {pair}"
+ );
+ for f in &pair_hits {
+ assert!(
+ !f.snippet.contains(token),
+ "{rule} leaked second adjacent token in snippet: {}",
+ f.snippet
+ );
+ }
+ // A rejected prefix match (e.g. `dckr_pat_old-` overlapping a real token) must
+ // resume right after the rejected start so the real token is still caught and masked.
+ if *rule == "secret.dockerhub-token" {
+ let overlapped = format!(r#"{{"note":"dckr_pat_old-{token}"}}"#);
+ let found = hits(&overlapped);
+ assert_eq!(found.len(), 1, "{rule} missed overlapped token: {found:#?}");
+ assert!(
+ !found[0].snippet.contains(token),
+ "{rule} leaked overlapped token in snippet: {}",
+ found[0].snippet
+ );
+ }
+ }
+ }
}
diff --git a/tests/fixtures/secrets/EXPECTED b/tests/fixtures/secrets/EXPECTED
index b66cbfb..57253e8 100644
--- a/tests/fixtures/secrets/EXPECTED
+++ b/tests/fixtures/secrets/EXPECTED
@@ -91,3 +91,9 @@ config.py:124 secret.pulumi-token
config.py:125 secret.supabase-token
config.py:126 secret.grafana-token
config.py:127 secret.rubygems-token
+config.py:128 secret.dockerhub-token
+config.py:130 secret.groq-key
+config.py:132 secret.perplexity-key
+config.py:134 secret.mailgun-key
+config.py:136 secret.sendgrid-key
+config.py:138 secret.digitalocean-token
diff --git a/tests/fixtures/secrets/config.py b/tests/fixtures/secrets/config.py
index 3e64de5..ec27fe4 100644
--- a/tests/fixtures/secrets/config.py
+++ b/tests/fixtures/secrets/config.py
@@ -125,3 +125,15 @@ pulumi_escaped = "line\\n{{fake:pulumi-token}}"
supabase_escaped = "line\\n{{fake:supabase}}"
grafana_escaped = "line\\n{{fake:grafana}}"
rubygems_escaped = "line\\n{{fake:rubygems}}"
+dockerhub_escaped = "line\\n{{fake:dockerhub}}"
+dockerhub_escaped_lookalike = "line\\ndckr_pat_short123"
+groq_escaped = "line\\n{{fake:groq}}"
+groq_escaped_lookalike = "line\\ngsk_short123"
+perplexity_escaped = "line\\n{{fake:perplexity}}"
+perplexity_escaped_lookalike = "line\\npplx-short123"
+mailgun_escaped = "line\\n{{fake:mailgun}}"
+mailgun_escaped_lookalike = "line\\nkey-0123abcd"
+sendgrid_escaped = "line\\n{{fake:sendgrid-key}}"
+sendgrid_escaped_lookalike = "line\\nSG.short.short"
+digitalocean_escaped = "line\\n{{fake:digitalocean-token}}"
+digitalocean_escaped_lookalike = "line\\ndop_v1_0123abcd"