From e1cfceae6a3442f4e27818ac8e6885dc68dbb006 Mon Sep 17 00:00:00 2001 From: Garry Tan Date: Wed, 18 Nov 2026 02:47:18 +0800 Subject: [PATCH] Secrets check stops flagging three code shapes and catches concatenated and commented secrets (GST-3004) (#4457) The secrets check no longer stops a push on three kinds of ordinary code that only look like secrets: TypeScript types with spaced generic arguments, Python arguments that read a settings constant, and camelCase dotted field paths. It also catches two kinds of real secrets it used to miss, one built with `+` from short pieces and one written in a comment, so pushes are both quieter and safer. Nothing to do; a secret in any of the loosened positions still fails the check. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> Review-Finding: p1/adversarial-native#1 reader=adversarial-native Review-Finding: p1/outside-voice#1 reader=outside-voice Review-Finding: p1/outside-voice#2 reader=outside-voice --- changelog.d/secrets-check-code-shapes.md | 1 + docs/pregate.md | 23 +++ lib/redact-patterns.ts | 166 ++++++++++++++++++-- test/pregate-secrets-source-context.test.ts | 12 ++ test/redact-env-kv-code-shapes.test.ts | 119 ++++++++++++++ test/redact-env-kv-misses.test.ts | 72 +++++++++ 6 files changed, 379 insertions(+), 14 deletions(-) create mode 100644 changelog.d/secrets-check-code-shapes.md create mode 100644 test/redact-env-kv-code-shapes.test.ts create mode 100644 test/redact-env-kv-misses.test.ts diff --git a/changelog.d/secrets-check-code-shapes.md b/changelog.d/secrets-check-code-shapes.md new file mode 100644 index 000000000..e0645620d --- /dev/null +++ b/changelog.d/secrets-check-code-shapes.md @@ -0,0 +1 @@ +- **The secrets check stops flagging three kinds of ordinary code as a secret, and catches two misses.** TypeScript types with spaced generic arguments, Python keyword arguments that read a settings constant (`aws_secret_access_key=settings.S3_SECRET_KEY,`), and camelCase dotted field paths (`"form.section.fieldName"`) no longer stop a push. A secret built with `+` from short pieces, or written in a trailing or whole-line comment, is now reported. A secret in any of those positions still fails the check. Template keys, arrays of names and string-literal generic types still report and are documented as known limits. (#4289) diff --git a/docs/pregate.md b/docs/pregate.md index 4f72ae55b..35da65766 100644 --- a/docs/pregate.md +++ b/docs/pregate.md @@ -1841,6 +1841,29 @@ those, each with its scope and its reason: Any other call's literal (`make_password("orange_monkey_pass")`), a later argument or fallback (`os.getenv("SECRET_KEY", "orange_monkey_pass")`) and a kebab passphrase still report. +- Three more code shapes are names, not credentials (GST-3004): a TypeScript + type with spaced generic arguments + (`session: Awaited>;`), a Python keyword + argument that reads a CONSTANT_CASE setting + (`aws_secret_access_key=settings.S3_SECRET_KEY,`, #4289), and a quoted + camelCase dotted field path of three word segments + (`"form.section.fieldName"`; an all-lowercase one still reports). Each is + read from the value, never from the key's name or the file's path, and a + secret in the same position, a literal after it on the line, or the same text + in a config file still reports. +- Known limits, still reported, allowlist them by value: a JS/TS template key + built from `${…}` slots (#4287), an array of names in source code + (`"get_token": [load_auth, "authOperations"]`), and a generic type with + string-literal arguments (`Pick`). Exempting + them needed an entropy test on their text alone, which let random hex and + encoded literals through. +- Two misses are closed (#4289): the string pieces of a `+` chain are joined + and checked (`KEY = "form.x" + ""`), and a secret-named assignment + right after `#`, `//`, `/*` or `", "docs/setup.html"], + ["retries: 3 # token: " + S, "deploy/values.yaml"], + ] as const) expect({ code, file, kv: kv(code, file) }).toEqual({ code, file, kv: true }); + }); + test("a secret in the code and another in its comment report separately", () => { + expect(findings('API_KEY="' + S + '" # TOKEN="' + S2 + '"', "app/settings.py").map((f) => f.col)).toEqual([10, 46]); + }); + test("a long run of spaces or comment markers scans in linear time", () => { + for (const text of [" ".repeat(40_000) + "x", "# " + " ".repeat(40_000) + "x", "#".repeat(40_000) + "x", "/".repeat(40_000)]) { + const started = performance.now(); + scan(text, { repoVisibility: "private", processEnv: {}, sourcePath: "src/client.ts", maxBytes: 1 << 20 }); + expect(performance.now() - started).toBeLessThan(500); + } + }); + test("a line whose code is exempt still reports a secret in its comment", () => { + expect(kv(' session = self.client.session # TOKEN="' + S + '"', "tests/test_views.py")).toBe(true); + }); + + test("benign: chains of key-naming text and names, prose and key-naming text in comments, and placeholders give no finding", () => { + for (const [code, file] of [ + ['sessionKey = "user:" + user_id + ":sessions_active"', "app/cache.py"], + ['token = "Bearer " + jwt', "app/client.py"], + ['KEY = "form." + "section.fieldName"', "app/forms.py"], + [" sessionKey: prefix + suffix,", "src/cache.ts"], + ["# Auth: authentication, authorization, sessions, permissions. A data file", "bin/diff-scope"], + ["// signal_key: 'test-discipline',", "hooks/preference.ts"], + ["# password: changeme", "deploy/values.yaml"], + ["# token: ${GITHUB_TOKEN}", "deploy/env.sh"], + ["x = 1 # see docs/token: for details", "app/settings.py"], + ["url = 'https://example.test/#token=abcdefgh'", "app/settings.py"], + ] as const) expect({ code, file, kv: kv(code, file) }).toEqual({ code, file, kv: false }); + }); +});