gstack icon indicating copy to clipboard operation
gstack copied to clipboard

redact env.kv: Python settings reads and camelCase field-path constants are false positives; concat and comments are missed

Open LimpingWolf opened this issue 3 weeks ago • 2 comments

The env.kv pattern in lib/redact-patterns.ts (~line 965) produces recurring false positives on Python code, and has two blind spots. A downstream repo has added four path- and pattern-scoped pregate allowlist entries for these false positives so far. Found on gstack 1.91.369.0 (34c65288b). We reproduced each case with the real redact-engine.scan and pregate/allowlist.applyAllowlist, and the gstack-redact --from-file --json CLI gave the same results.

1. False positive: Python attribute reads of settings

aws_secret_access_key=settings.S3_SECRET_KEY,

This fires env.kv (MEDIUM, blocking). isBareEnvRead (#2912) exempts os.environ[...], os.getenv(...) and process.env.X, but not settings.X, config.X or conf.X. isSourceExpression is TS-only (~line 684).

Suggested fix: when the whole value is an unquoted dotted name ending in an UPPER_SNAKE attribute, with nothing but ,, ; or ) after it, treat it as a read, the same way isBareEnvRead does. A one-line ENV_READ_SPAN extension passed the existing engine suites in a local copy. It still let through password=settings.DB_PASSWORD; token=<S>, so the fix needs that case as a regression test.

2. False positive: a bare KEY constant holding a dotted camelCase field path

KEY = "form.section.fieldName"

This fires because a bare KEY counts as credential-shaped (isCredentialShapedEnvName rule ii) and camelCase dotted paths pass the entropy ≥ 3.0 threshold. isSchemaFieldIdentifier only exempts <word>_key names with lowercase snake values. The same happens for field_key="form.section.fieldName", when the value is camelCase.

3. Missed detection: a short first token followed by a concatenated secret

KEY = "form.x" + "Qx9vL2mZp8Rt4Kw7Yn3Bh2Jd"

This gives no finding. The regex captures only the first token, which falls below the entropy threshold, and the rest of the line is never checked.

4. Blind spot: comment text

x = 1  # API_KEY = "Qx9vL2mZp8Rt4Kw7Yn3Bh2Jd"

This gives no env.kv finding because the regex is anchored at ^[ \t]*. A secret in a trailing comment is caught only when the code before it happens to fire. So an allowlist entry for that code silently hides the comment as well. (Provider-shaped tokens like AKIA, ghp_ and sk_live_ still fire under their own ids.)

Regression cases

<S> is a 24–32-character mixed-case alphanumeric literal.

Line Expected
aws_secret_access_key=settings.S3_SECRET_KEY, no finding
aws_secret_access_key=settings.S3_SECRET_KEY) no finding
aws_secret_access_key=settings.S3_SECRET_KEY or "<S>", env.kv
aws_secret_access_key=settings.S3_SECRET_KEY + "<S>", env.kv
aws_secret_access_key=settings.S3_SECRET_KEY.replace("a", "<S>"), env.kv
password=settings.DB_PASSWORD; token="<S>" env.kv
SECRET_KEY=settings.S3_SECRET_KEY in a .env file env.kv (not Python)
KEY = "form.section.fieldName" no finding
field_key="form.section.fieldName", no finding
KEY = "form.<S>" env.kv
KEY = "form.x" + "<S>" env.kv (currently no finding)
x = 1 # API_KEY = "<S>" env.kv (currently no finding)

Allowlist note, for anyone writing entries in the meantime: patternHits only forbids [\w-] after the match (pregate/allowlist.ts:37). A pattern without an end-of-line lookahead therefore also allowlists <benign> or "<secret>" on the same line. It may be worth documenting this in docs/pregate.md#allowlist.

LimpingWolf avatar Nov 11 '26 01:11 LimpingWolf

Thank you, @LimpingWolf, for the detailed cases and the regression table. The two false positives and the two missed detections are clear.

We've labelled this S3. Changing the env.kv pattern changes the pregate gate, and gstack is in a tooling freeze through 2026-11-21, so the fix is queued for after the freeze. Until then, confirming each hit by its rule and line works, and your note about ending allowlist patterns at end-of-line is a good one to follow.

capy-ai[bot] avatar Nov 11 '26 16:11 capy-ai[bot]

Fixed on main in #4457 (e1cfcea), thanks to your regression table, @LimpingWolf.

  • aws_secret_access_key=settings.S3_SECRET_KEY, and ...) no longer report in Python. Every "env.kv" row of your table still reports, and so does password=request.password,.
  • KEY = "form.section.fieldName" and field_key="form.section.fieldName", no longer report in source files. A digit, a secret segment, two segments, an all-lowercase path, a literal after it on the line, or the same text in a .env or YAML file still report.
  • KEY = "form.x" + "<secret>" now reports: the string pieces of a + chain are joined and checked.
  • x = 1 # API_KEY = "<secret>" now reports in trailing and whole-line comments after #, //, /* or <!--, while comment prose doesn't.

It ships in the next gstack release. Closing this; reopen if a case still misbehaves on that release.

capy-ai[bot] avatar Nov 17 '26 18:11 capy-ai[bot]