Skip to content

Commit 4441c8f

Browse files
garrytantianxchen
andcommitted
fix(redact): TypeScript types and JSX expressions are code, not env.kv secrets (#3048)
A credential-named parameter or prop with an unquoted code value (`session: SessionState,`, `key={turn.requestId + turn.role}`, `apiKey={settings.apiKey}`) cleared the entropy gate and blocked pushes as a MEDIUM env.kv finding. One rule replaces the PR's two keyword heuristics: a value that reads as a code name (no digits, word-like camelCase or dotted parts) with a code terminator, or a brace expression of such names with no quoted literal, is source syntax. Paired controls keep firing on quoted, brace-wrapped, concatenated, YAML-bare and random-letter literals. Co-authored-by: tianxchen <[email protected]>
1 parent 3cb6ef0 commit 4441c8f

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

‎lib/redact-patterns.ts‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,36 @@ function isCallExpression(span: string, match: RegExpExecArray): boolean {
589589
return !carriesSecretLiteral(call);
590590
}
591591

592+
/**
593+
* #3048: an unquoted value that names code is source syntax, not a literal:
594+
* a TypeScript parameter or property type (`session: SessionState,`) or a JSX
595+
* expression (`key={turn.requestId + turn.role}`, `apiKey={settings.apiKey}`).
596+
* A code name has no digits and reads as words: every camelCase/dotted part
597+
* is 2+ letters, they average 3+ letters, and each 3+ letter part has a vowel.
598+
* Random tokens fail that (mixed case gives 1-letter parts; most hold digits).
599+
* The bare form needs a code terminator (`,` `;` `)`) and no quote before it;
600+
* the JSX form needs its closing brace on the line and no quoted literal.
601+
*/
602+
function readsAsCodeName(text: string): boolean {
603+
if (!/^[A-Za-z_$][A-Za-z_$]*(?:\.[A-Za-z_$][A-Za-z_$]*)*$/.test(text)) return false;
604+
const parts = text.split(/[._$]+|(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])/).filter(Boolean);
605+
return parts.length > 0 && parts.every(p => p.length >= 2 && (p.length < 3 || /[aeiouy]/i.test(p))) &&
606+
parts.reduce((n, p) => n + p.length, 0) / parts.length >= 3;
607+
}
608+
609+
function isCodeReference(span: string, match: RegExpExecArray): boolean {
610+
const { start } = spanBounds(match);
611+
if (match.input[start - 1] === '"' || match.input[start - 1] === "'") return false;
612+
if (span.startsWith("{")) {
613+
const line = match.input.slice(start + 1).split("\n", 1)[0];
614+
const close = line.indexOf("}");
615+
if (close < 0 || /["'`]/.test(line.slice(0, close))) return false;
616+
return line.slice(0, close).split("+").every(term => readsAsCodeName(term.trim()));
617+
}
618+
const m = span.match(/^(.+?)[,;)]+$/);
619+
return m !== null && readsAsCodeName(m[1]!);
620+
}
621+
592622
export const PATTERNS: RedactPattern[] = [
593623
// ===== HIGH — genuinely-secret credentials (block) =====
594624
{
@@ -863,13 +893,14 @@ export const PATTERNS: RedactPattern[] = [
863893
// literal (`os.getenv("X", "<secret>")`). A literal appended to the read
864894
// itself (`process.env.X||"…"`) is not an exact read and still fires.
865895
// #2899: a function call assigned to the name is code, not a value (see
866-
// isCallExpression).
896+
// isCallExpression). #3048: so is a type or JSX expression (isCodeReference).
867897
validate: (span, match) =>
868898
isCredentialShapedEnvName(match[0]) &&
869899
!isPlaceholderSpan(span) &&
870900
!/^\$\{?[A-Za-z_]/.test(span) &&
871901
!isBareEnvRead(span, match) &&
872902
!isCallExpression(span, match) &&
903+
!isCodeReference(span, match) &&
873904
shannonEntropy(span) >= 3.0,
874905
},
875906
{

‎test/redact-engine.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,22 @@ describe("MEDIUM demoted credential-shaped patterns (TENSION-1)", () => {
234234
expect(ids(`authToken: ${v}`)).toContain("env.kv"); // (iv) credential camel
235235
expect(ids(`clientSecret: ${v}`)).toContain("env.kv"); // (iv) credential camel
236236
});
237+
// #3048 — a TypeScript type or JSX expression names code, not a literal;
238+
// credential literals in the same shapes must still fire.
239+
test("env.kv skips TypeScript types and JSX expressions, keeps literals in those shapes (#3048)", () => {
240+
const v = "8Fk2pQ9vXz4wL7mN3rT6yB1cD5eG0hJ";
241+
for (const code of [
242+
"export async function issueServerCoachItem(\n db: Pool,\n session: SessionState,\n item: IssuedCoachItem,\n): Promise<void> {}",
243+
"interface Props {\n session: SessionState;\n}",
244+
"key={turn.requestId + turn.role + index}",
245+
"apiKey={settings.apiKey}",
246+
]) expect({ code, ids: ids(code) }).toEqual({ code, ids: ids(code).filter(id => id !== "env.kv") });
247+
for (const literal of [
248+
`session: ${v},`, `session: "${v}",`, `key={${v}}`, `API_KEY={${v}}`,
249+
`key={settings.apiKey + "${v}"}`, `key="${v}"`, `api_key: ${v}`,
250+
"token: QxZpLmNvTrKbWdHgJcFs,", "token: QxZpLmNvTrKbWdHgJcFs",
251+
]) expect({ literal, kv: ids(literal).includes("env.kv") }).toEqual({ literal, kv: true });
252+
});
237253
// #2912 — a line that READS a secret from the environment holds no secret;
238254
// it must not fire (and so must not be masked or withhold a /cso source file).
239255
test("env.kv skips exact environment reads (#2912)", () => {

0 commit comments

Comments
 (0)