Skip to content

Commit 997c273

Browse files
mogulOpenCode Agent
andcommitted
fix(scripts): close $-leading secret bypass in sensitive-terms lookahead
The lookahead added in 94674a3 to skip runtime secret reads ($(...), $VAR) sat one char after the quote and skipped ANY value starting with $ or backtick — so a hardcoded literal that merely begins with $ (e.g. password="$3cr3tLiteral") escaped detection, a real bypass in secret-scanning code (flagged in #220 review). Tighten the negative lookahead to match only the var-ref / cmd-sub SHAPES it actually means — ${... | $(... | $<letter|underscore> | backtick — not merely a leading $/backtick. A literal starting with $ + digit is a valid shell literal (not a var ref) and is still flagged. Add a regression test for the $-leading-literal case so the limitation is documented, not accidental. Co-authored-by: OpenCode Agent <[email protected]>
1 parent 94674a3 commit 997c273

2 files changed

Lines changed: 34 additions & 8 deletions

File tree

‎scripts/tests/test_validate_sensitive_terms.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,24 @@ def test_runtime_password_assignment_not_flagged(self, tmp_path):
283283
tier1_matches, _ = scan_file(file_path)
284284

285285
assert tier1_matches == [], f"unexpected matches: {tier1_matches}"
286+
287+
def test_dollar_leading_literal_still_flagged(self, tmp_path):
288+
"""A literal secret that merely STARTS with '$' (not a var ref) is still flagged.
289+
290+
Regression guard for the lookahead: skipping any '$'-leading value would
291+
let ``password="$3cr3tLiteral"`` bypass detection. The lookahead only
292+
skips genuine var-ref / cmd-sub shapes (``${...``, ``$(...``,
293+
``$<letter|underscore>``, backtick), so a '$' followed by a digit — a
294+
valid shell literal, not a variable reference — must still be caught.
295+
"""
296+
file_path = tmp_path / "app.sh"
297+
file_path.write_text(
298+
'password="$3cr3tLiteralPass"\n'
299+
'api_key="$1234567890abcdef1234"\n'
300+
)
301+
302+
tier1_matches, _ = scan_file(file_path)
303+
304+
descs = [desc.lower() for _, desc, _ in tier1_matches]
305+
assert any("password" in d for d in descs), f"password literal not flagged: {tier1_matches}"
306+
assert any("api key" in d for d in descs), f"api key literal not flagged: {tier1_matches}"

‎scripts/validate_sensitive_terms.py‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,19 @@
2424
(r"BEGIN.*PRIVATE KEY", "Private key detected"),
2525
(r"AWS_SECRET_ACCESS_KEY", "AWS secret key"),
2626
(r"AWS_ACCESS_KEY_ID", "AWS access key"),
27-
# Assignment patterns. The negative lookahead skips values that are shell
28-
# command substitutions ($(...), `...`) or variable references ($VAR,
29-
# ${VAR}) — those read a secret at runtime, they don't hardcode one, and
30-
# flagging them produces false positives on legitimate scripts.
31-
(r"(?:password|passwd|pwd)\s*=\s*['\"]?(?![`$])[^'\"\\s]{8,}", "Hardcoded password"),
32-
(r"(?:api_key|apikey)\s*=\s*['\"]?(?![`$])[^'\"\\s]{16,}", "API key"),
33-
(r"(?:token|auth_token)\s*=\s*['\"]?(?![`$])[^'\"\\s]{16,}", "Auth token"),
34-
(r"secret\s*=\s*['\"]?(?![`$])[^'\"\\s]{16,}", "Secret value"),
27+
# Assignment patterns. The negative lookahead skips values that are ONLY a
28+
# shell command substitution ($(...), `...`) or variable reference
29+
# (${VAR}, $VAR) — those read a secret at runtime, they don't hardcode one,
30+
# and flagging them produces false positives on legitimate scripts. The
31+
# lookahead matches the specific var-ref / cmd-sub SHAPES:
32+
# ${... | $(... | $<letter|underscore>... | `...
33+
# NOT merely a leading $/backtick. A literal secret that happens to start
34+
# with $ followed by a digit (e.g. password="$3cr3tLiteral") is a valid
35+
# shell literal (not a var ref) and is STILL flagged.
36+
(r"(?:password|passwd|pwd)\s*=\s*['\"]?(?!\$\{|\$\(|\$[A-Za-z_]|`)[^'\"\\s]{8,}", "Hardcoded password"),
37+
(r"(?:api_key|apikey)\s*=\s*['\"]?(?!\$\{|\$\(|\$[A-Za-z_]|`)[^'\"\\s]{16,}", "API key"),
38+
(r"(?:token|auth_token)\s*=\s*['\"]?(?!\$\{|\$\(|\$[A-Za-z_]|`)[^'\"\\s]{16,}", "Auth token"),
39+
(r"secret\s*=\s*['\"]?(?!\$\{|\$\(|\$[A-Za-z_]|`)[^'\"\\s]{16,}", "Secret value"),
3540
(r"\.gov\s+internal-only", "Internal-only government content"),
3641
(r"do\s+not\s+distribute", "Distribution restriction marker"),
3742
]

0 commit comments

Comments
 (0)