Skip to content

fix(redact): env.kv's code-expression exemption reaches Dart, not only TS - #3418

Closed
hbarney wants to merge 1 commit into
garrytan:mainfrom
hbarney:fix/env-kv-dart-source-expression
Closed

hbarney wants to merge 1 commit into
garrytan:mainfrom
hbarney:fix/env-kv-dart-source-expression

Conversation

@hbarney

@hbarney hbarney commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Why (in your own words)

env.kv already knows that a value which is code rather than a literal is not a
leaked credential — that is isSourceExpression, added for #3048 — and the regex it
uses for the decision, CODE_NAME_CHAIN, already accepts a Dart member access such
as request.password. It just never got the chance to run. isSourceExpression's
first line gated the whole exemption to TypeScript/JSX:

const TS_SOURCE = /\.(?:[cm]?tsx?|jsx)$/i;
if (!sourcePath || !TS_SOURCE.test(sourcePath)) return false;

So every non-TypeScript language kept the false positive the exemption exists to
remove. This widens the gate to .dart (and renames the constant to CODE_SOURCE,
since it is no longer TS-only).

Dart qualifies on the same ground TypeScript does, and the ground is a property of
the language rather than a judgement about any particular repo: Dart has no
unquoted string literal
, so a bare name chain after : or = cannot be a value —
it can only be a variable read.

What it cost in real use: on a Dart backend, a refactor moved two pre-existing lines
into new files. Both forward the request's own password field by name — one as a
requireFields map entry, one as a named argument — and neither holds a literal.
Because the secrets scan is over ADDED lines, a file move re-presents long-standing
content as new, so gstack-pregate failed its secrets check and refused
publication
(PREGATE_SECRETS → PREGATE_VERIFY: refused, and
gstack-ship-receipt wrote nothing) over two lines byte-identical to the base
branch. There was no way around it either: secrets findings cannot be downgraded,
and .gstack-redact-allowlist is read by the pre-push hook, not by the pre-gate.

Live evidence

The experiment. One byte-identical line, 'password': request.password,, scanned
under three file extensions and nothing else changed. Run against this repo's
lib/redact-engine.ts on unmodified main (64d8210) and on this branch:

$ bun run probe.ts   # BEFORE — origin/main, unmodified
.ts   -> clean
.dart -> FIRES ["env.kv"]
.py   -> FIRES ["env.kv"]

$ bun run probe.ts   # AFTER — this branch
.ts   -> clean
.dart -> clean
.py   -> FIRES ["env.kv"]

The .ts line and the .dart line are the same bytes, so the only thing that
differed before this change was the extension. lib/pregate/checks/secrets.ts:61
writes each diff hunk to a temp file with path.basename(part.file), so the real
extension does reach the validator — which is why the gate was reachable at all.

.py still fires on purpose; see the follow-up note at the bottom.

Downstream effect on the repo that hit this: the pre-gate secrets check goes
fail → warn, and the pre-push hook drops from 3 MEDIUM findings to 1.

This does not weaken detection, and a guard was needed to make that true. The
note below revises what this section first claimed — worth reading, since the first
version of this description overstated the case. A real credential in a Dart file
still fires when quoted in either Dart quote style, when assigned with = (the
separator check in isSourceExpression takes : alone, so an = value is never
exempt), when an unquoted token begins with a digit, and inside a triple-quoted
string
. Eight cases are in the #3048 contract test, five that must fire and three
that must stay exempt:

[`      'password': '${v}',`, "…/onboarding_handlers.dart"],   // single-quoted literal -> fires
[`  password: "${v}",`,       "…/config.dart"],                // double-quoted literal -> fires
[`  password = "${v}";`,      "…/config.dart"],                // `=` assignment        -> fires
[`  password: ${v},`,         "…/config.dart"],                // unquoted, DIGIT-first -> fires
[`const fixture = '''\npassword: ${vIdent}\n''';`,              // embedded config block -> fires
                             "test/fixtures.dart"],
["const doc = '''usage''';\n  password: request.password,",      // CLOSED block above it -> exempt
                             "…/config.dart"],

The guard, and why this change does not go in without it. withFlags forces the
m flag, so a password: line inside a ''' block is matched on its own, and the
before check reads only the character preceding the span — it cannot see a quote
that opened three lines earlier. An embedded config block is exactly where the value
after the colon is a literal, and a ''' fixture or inline manifest is ordinary
Dart. Measured: that line reports on main, and stopped reporting the moment .dart
joined the gate. insideTripleQuotedString refuses the exemption there, scanning
left to right for whichever delimiter opened rather than counting delimiters, since a
''' inside a """ block is content and a parity count would call such text open when
it is closed.

shape                                   main    this PR
embedded config in a Dart ''' block     FIRES   FIRES    <- held, by the guard
embedded config in a TS `` template     clean   clean    <- #3048's own, left alone
session: SessionState,   (the #3048 FP) clean   clean

Both of the guard's failure modes land on the behaviour that already shipped: a diff
hunk that starts mid-string shows no opener and the line stays exempt — the miss
survives there, which is the side that keeps #3048 working — and a stray ''' inside
a single-line string costs an exemption rather than a report. TS/JSX template
literals have the same hole and have had it since #3048; closing that changes
behaviour no language added here is responsible for, so it is left alone. Say the
word if you would rather it came in too.

Correction. This section first claimed the unquoted case holds "because
CODE_NAME_CHAIN requires an identifier first character and a random high-entropy
token rarely has one." That is wrong twice: a base62 token starts with a letter
about 84% of the time, not rarely, and I have since checked that
password: Fk2pQ9vXz4wL7mN3rT6yB1cD5eG0hJx in a .dart file is exempt — the
fixture 8Fk2pQ… fires only because it begins with a digit. The same is true in
.ts on main today, so this is #3048 behaviour Dart inherits, not something
this PR introduces. I think the outcome is right for the grammar reason the rest
of the design rests on — an unquoted token after : in a code file is a name, and
code that pastes a raw secret there does not run — but that is not the reason I
gave, and the fourth case pins less than I said it did. Measured in #3419.

Scope

  • Changed: lib/redact-patterns.ts — TS_SOURCE → CODE_SOURCE, widened from
    /\.(?:[cm]?tsx?|jsx)$/i to /\.(?:[cm]?tsx?|jsx|dart)$/i; the new
    insideTripleQuotedString guard and the reasoning behind it; the comment explaining
    why Dart qualifies, and two comment touch-ups.
    test/redact-engine.test.ts — 8 cases added to the existing fix(redact): ignore TypeScript parameter types and dynamic JSX values #3048 contract test
    (3 that must now be exempt, 5 that must still fire). +82/−4 across 2 files.
  • Verified live by: the three-extension probe and the embedded-config table above,
    both run against this repo's own engine on main and on this branch; a vacuity check
    that removes only the guard call and watches the embedded-config case fail (102 pass,
    1 fail); plus the four test/typecheck commands above, in a fresh clone. The downstream numbers (fail → warn, pre-push 3 MEDIUM
    → 1) are from @hbarney's own gstack-pregate and pre-push runs on the Dart backend
    that hit this; I am reporting them, not re-measuring them, since that repo is private.
  • Did NOT test: Python, Go, Ruby, Kotlin or any other language — deliberately, see
    below. No change to isCallExpression, isSchemaFieldIdentifier, or any other
    env.kv validator, and no change to the pre-gate or the pre-push hook themselves.

Liveness proof (required for external contributors)

gh api user --jq .login → hbarney.

I am Claude Code, an agent, working on @hbarney's machine at their direction, so I am
reporting this rather than claiming it: the GSTACK PR liveness screenshot is not
something an agent can honestly produce
— the point of the requirement is that a
human typed it into a live surface. @hbarney will attach it to this PR. If it is not
here, treat the requirement as unmet rather than satisfied.

Checklist

  • Liveness screenshot attached (GSTACK PR typed live into a real surface) — to be attached by @hbarney, see above
  • This is not a generated-file-only diff (I edited the source/template and regenerated)
  • No ETHOS.md edits, and no changes to voice / founder perspective / YC references
  • New public command / external service / host adapter has an accepted issue linked (or N/A) — N/A
  • Linked issue or reproduction: builds directly on fix(redact): ignore TypeScript parameter types and dynamic JSX values #3048 (isSourceExpression); reproduction is the three-extension probe above

Python is the same hole, and it is #3419, stacked on this

The same probe shows it — .py fires on the line .ts does not, before and after
this change. It is not in this diff on purpose: the case for Dart above rests on a
property of Dart's grammar, so Python deserves that argument made for Python rather
than a gate widened on the assumption it carries over.

I have since done that work, as #3419. Worth knowing before you review it: making the
argument properly showed Python gains less than Dart does. Dart's two live shapes
both use :; Python's keyword argument and plain assignment use =, which the
separator check rejects outright, so those keep firing. That PR says so and pins the
firing half in tests. Merge order is this PR first.

Still open after both, and not mine to decide: TS/JSX template literals keep the
embedded-config hole described above. The guard here covers triple quotes only.

🤖 Generated with Claude Code

@trunk-io

trunk-io Bot commented Oct 25, 2026

Copy link
Copy Markdown

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

…y TS

`env.kv` already exempts a value that is code rather than a literal
(`isSourceExpression`, garrytan#3048), and that function's own `CODE_NAME_CHAIN`
accepts a Dart member access such as `request.password`. It never ran,
because the exemption was gated to TypeScript/JSX by
`TS_SOURCE = /\.(?:[cm]?tsx?|jsx)$/i`, so every other language kept the
false positive.

Found on a Dart backend where a refactor moved two existing lines into new
files. Both forward the request's own password field by name, one as a
`requireFields` map entry and one as a named argument, and neither holds a
literal. Because the scan is over ADDED lines, a file move re-presents
long-standing content as new, so the pre-gate's `secrets` check failed and
refused publication on two lines that were byte-identical to the base branch.
The pre-push hook reported them too.

Demonstrated with one byte-identical line under three extensions: `.ts`
clean, `.dart` fires, `.py` fires. `lib/pregate/checks/secrets.ts` writes
each hunk to a temp file with `path.basename(part.file)`, so the real
extension does reach the validator.

Dart qualifies on the same ground TypeScript does, and the ground is a
property of the language rather than a judgement about any repo: Dart has no
unquoted string literal, so a bare name chain after `:` or `=` cannot be a
value, only a variable read. Renamed the constant to `CODE_SOURCE` since it
is no longer TS-only.

Widening the gate alone WOULD have cost a real detection, so it does not go
in alone. `withFlags` forces the `m` flag, so a `password:` line inside a
triple-quoted string is matched on its own, and the `before` check reads only
the character preceding the span — it cannot see a quote that opened three
lines earlier. An embedded config block is exactly where the value after the
colon IS a literal, and a `'''` fixture or inline manifest is ordinary Dart.
Measured: that line reports on `main` and stopped reporting once `.dart`
joined the gate. `insideTripleQuotedString` refuses the exemption there. It
scans left to right for whichever delimiter opened rather than counting
delimiters, because a `'''` inside a `"""` block is content and a combined
parity count would call such text open when it is closed. Its two failure
modes both land on the behaviour that already shipped: a diff hunk starting
mid-string shows no opener and stays exempt, and a stray `'''` inside a
single-line string costs an exemption rather than a report. TS/JSX template
literals have the same hole and have had it since garrytan#3048; closing that changes
behaviour no language added here is responsible for, so it is left alone.

So detection holds. A credential in a Dart file still fires when quoted in
either Dart quote style, when assigned with `=` (the separator check takes
`:` alone, so an `=` value is never exempt), when the unquoted token begins
with a digit, and inside a triple-quoted block.

One thing this does NOT claim, because measuring it proved otherwise: an
unquoted token is not reliably caught. `CODE_NAME_CHAIN` rejects a token only
when it does not start with an identifier character, which a random base62
token does about 84% of the time, so `password: Fk2pQ9…` is exempt while
`password: 8Fk2pQ…` fires. That is true of `.ts` on `main` today and is
garrytan#3048's existing bargain — an unquoted token after `:` in a code file is a
name, and code that pastes a raw secret there does not run. This change
neither widens nor narrows it.

Eight cases are added to the garrytan#3048 contract test: three that must now be
exempt (the two Dart shapes, plus a closed `'''` block, which must not cost
the exemption) and five that must still fire (both quote styles, an `=`
assignment, a digit-first unquoted token, and a config line inside an open
`'''` block). The last uses a letter-first token deliberately, so the chain
matches and the guard is the only thing making it report; with `v`, which
starts with a digit, it would have passed with or without the guard.

Co-Authored-By: Claude Opus 5 <[email protected]>
@hbarney
hbarney force-pushed the fix/env-kv-dart-source-expression branch from 2933165 to 7c8f4c7 Compare October 26, 2026 16:01
@capy-ai

capy-ai Bot commented Oct 31, 2026

Copy link
Copy Markdown
Contributor

Thank you for this fix. A crew lane is picking it up, together with your other redact PR, to land on current main with Co-authored-by credit to you.

@capy-ai

capy-ai Bot commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

Thanks @hbarney. This landed in #3742 (merged as 64fb6ec, v1.92.8.0). That PR rewrote the fix on current main and combined it with #3419 and #3605. Your idea of extending the source-syntax exemption to Dart is in it, credited with Co-authored-by and in the changelog. The triple-quote guard was rewritten as a string-span pass that also handles comments, raw strings and line continuations. Closing in favor of #3742.

@capy-ai capy-ai Bot closed this Nov 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant