mirror of
https://github.com/garrytan/gstack.git
synced 2026-11-18 06:04:33 +01:00
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
This commit is contained in:
1 parent
bc64f5e210
commit
e1cfceae6a
6 files changed
+379
-14
No files matched your search
@@ -117,6 +117,18 @@ describe('pregate secrets: source context and diff integrity', () => {
|
||||
} finally { cleanup(fx); }
|
||||
});
|
||||
|
||||
// Value: protects=a TS annotation with spaced generic arguments and a Python settings read pass the never-downgradable secrets check, while a secret
|
||||
// in a + chain or a trailing comment fails it; fails_when=the GST-3004 false positives return at the pregate level, or the concatenation or comment
|
||||
// read is lost there; why_new=GST-3004's cases came from pregate runs, and TS files reach the scanner as added lines under their base name; seam=fixture repo; no_claim=Dart files
|
||||
test('a spaced generic type and a Python settings read pass; a secret in a + chain or a trailing comment still fails (GST-3004)', () => {
|
||||
const secret = ['Qx9vL2mZ', 'p8Rt4Kw7', 'Yn3Bh2Jd'].join('');
|
||||
const ts = 'export interface Launch {\n session: Awaited<ReturnType<typeof launchSession>>;\n credentials: Record<string, CredentialEntry>;\n}\n';
|
||||
const py = 'client = boto3.client(\n "s3",\n aws_secret_access_key=settings.S3_SECRET_KEY,\n)\n';
|
||||
expect(repo({ 'src/launch.ts': ts, 'app/storage.py': py }).secrets()).toMatchObject({ status: 'pass', lines: [] });
|
||||
const r = repo({ 'src/launch.ts': ts + 'export const client = {\n apiKey: "sk" + "' + secret + '",\n', 'app/storage.py': py + 'RETRIES = 3 # API_KEY = "' + secret + '"\n' }).secrets();
|
||||
expect({ status: r.status, rows: r.lines.filter((l) => l.includes('env.kv')).sort() }).toEqual({ status: 'fail', rows: [expect.stringContaining('(app/storage.py, line 5)'), expect.stringContaining('(src/launch.ts, added line 6)')] });
|
||||
});
|
||||
|
||||
test('parseAddedLineNumbers numbers each added line from its hunk header', () => {
|
||||
const diff = ['diff --git a/a.py b/a.py', '--- a/a.py', '+++ b/a.py', '@@ -3,0 +4,2 @@', '+one', '+two', '@@ -9 +11 @@', '-old', '+new'].join('\n');
|
||||
expect([...parseAddedLineNumbers(diff)]).toEqual([['a.py', [4, 5, 11]]]);
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
/**
|
||||
* GST-3004: env.kv ("secret-named assignment with high-entropy value") flagged
|
||||
* code as a secret, and the pregate secrets check, which cannot be downgraded,
|
||||
* stopped pushes on it. Three shapes are now read as code, each by the value's
|
||||
* shape and never by the key's name or the file's path, and each pass has a
|
||||
* hold case putting a real-looking secret in the same position: a TypeScript
|
||||
* type with spaced generic arguments, a Python keyword argument reading a
|
||||
* CONSTANT_CASE setting, and a quoted camelCase dotted field path (#4289).
|
||||
* Three reported shapes stay known limits: a JS/TS template key (#4287), an
|
||||
* array of names (GBRA-615) and a string-literal generic type (CAPY-1). An
|
||||
* entropy test on their text alone let random hex and encoded literals through
|
||||
* (the second-model audit's bypasses below), so they still report.
|
||||
*/
|
||||
// Value: protects=env.kv passes three code shapes while a secret in the same position, a config file or a string still reports, and the three known-limit shapes and the audit's bypass lines keep reporting; fails_when=one of the fixed false positives returns, or an exemption lets a high-entropy, hex, encoded or provider-shaped value through; why_new=the cases had no test; seam=none; no_claim=scanner-level only, the pregate case lives in pregate-secrets-source-context.test.ts
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { scan } from "../lib/redact-engine";
|
||||
|
||||
const ids = (text: string, sourcePath?: string) =>
|
||||
scan(text, { repoVisibility: "private", processEnv: {}, ...(sourcePath ? { sourcePath } : {}) }).findings.map((f) => f.id);
|
||||
const kv = (text: string, sourcePath?: string) => ids(text, sourcePath).includes("env.kv");
|
||||
const S = "Qx9vL2mZp8Rt4Kw7Yn3Bh2Jd";
|
||||
const HEX = ["9d1e7ebb", "51b069eb", "1bd6"].join("");
|
||||
const STRIPE = ["sk", "live", S].join("_");
|
||||
const SPACED = "R7m!2pX K4v@8Q j9W#3s";
|
||||
const BT = "~";
|
||||
const passes = (cases: ReadonlyArray<readonly [string, string | undefined]>) => {
|
||||
for (const [code, file] of cases) expect({ code, file, kv: kv(code, file) }).toEqual({ code, file, kv: false });
|
||||
};
|
||||
const holds = (cases: ReadonlyArray<readonly [string, string | undefined]>) => {
|
||||
for (const [code, file] of cases) expect({ code, file, kv: kv(code, file) }).toEqual({ code, file, kv: true });
|
||||
};
|
||||
|
||||
describe("env.kv: code shapes are not secrets (GST-3004)", () => {
|
||||
test("a TypeScript annotation with spaced generic arguments passes", () => {
|
||||
passes([
|
||||
[" session: Awaited<ReturnType<typeof launchClaudePty>>;", "test/trust.test.ts"],
|
||||
[" credentials: Record<string, CredentialEntry>;", "src/vault.ts"],
|
||||
[" auth: Map<string, AuthInfo> | undefined,", "src/context.ts"],
|
||||
[" session: Pick<ClaudePtySession, Send | Close> | null,", "src/launch.ts"],
|
||||
]);
|
||||
});
|
||||
test("hold: a literal type, a default value, an object-literal value and a config file still report", () => {
|
||||
holds([
|
||||
[' session: Pick<Session, "' + S + '"> | null,', "src/orchestrator.ts"],
|
||||
[' session: Pick<Session, "' + HEX + '">,', "src/orchestrator.ts"],
|
||||
['password: Pick<Session, "' + SPACED + '">,', "src/client.ts"],
|
||||
[' session: Awaited<ReturnType<typeof launch>> = "' + S + '",', "src/orchestrator.ts"],
|
||||
[' session: Map<string, AuthInfo>, other: "' + S + '",', "src/orchestrator.ts"],
|
||||
[' session: "' + S + '",', "src/orchestrator.ts"],
|
||||
[" session: Awaited<ReturnType<typeof " + S + ">>;", "deploy/values.yaml"],
|
||||
[" session: Awaited<ReturnType<typeof launchClaudePty>>;", undefined],
|
||||
]);
|
||||
expect(ids(' session: Pick<Session, "' + STRIPE + '">,', "src/a.ts")).toEqual(expect.arrayContaining(["env.kv", "stripe.secret"]));
|
||||
});
|
||||
|
||||
test("a Python keyword argument reading a CONSTANT_CASE setting passes (#4289)", () => {
|
||||
passes([
|
||||
[" aws_secret_access_key=settings.S3_SECRET_KEY,", "app/storage.py"],
|
||||
[" aws_secret_access_key=settings.S3_SECRET_KEY)", "app/storage.py"],
|
||||
[" aws_secret_access_key=settings.S3_SECRET_KEY, # from the env", "app/storage.py"],
|
||||
[" password=django_settings.DATABASES_DEFAULT_PASSWORD,", "app/db.py"],
|
||||
]);
|
||||
});
|
||||
test("hold: a literal beside the read, a lowercase attribute, a .env file and a string still report (#4289's table)", () => {
|
||||
holds([
|
||||
[' aws_secret_access_key=settings.S3_SECRET_KEY or "' + S + '",', "app/storage.py"],
|
||||
[' aws_secret_access_key=settings.S3_SECRET_KEY + "' + S + '",', "app/storage.py"],
|
||||
[' aws_secret_access_key=settings.S3_SECRET_KEY.replace("a", "' + S + '"),', "app/storage.py"],
|
||||
[' password=settings.DB_PASSWORD; token="' + S + '"', "app/storage.py"],
|
||||
[" aws_secret_access_key=settings.S3_SECRET_KEY, region=" + S, "app/storage.py"],
|
||||
[" password=request.password,", "app/api/handlers.py"],
|
||||
["SECRET_KEY=settings.S3_SECRET_KEY", ".env"],
|
||||
['CONFIG = """\n aws_secret_access_key=settings.S3_SECRET_KEY,\n"""', "app/storage.py"],
|
||||
]);
|
||||
});
|
||||
|
||||
test("a quoted camelCase dotted field path of three word segments passes (#4289)", () => {
|
||||
passes([
|
||||
['KEY = "form.section.fieldName"', "app/forms.py"],
|
||||
[' field_key="form.section.fieldName",', "app/forms.py"],
|
||||
[" KEY = 'form.section.fieldName';", "src/forms.ts"],
|
||||
]);
|
||||
});
|
||||
test("hold: a secret segment, a digit, a two-segment or all-lowercase path and an unclosed quote still report", () => {
|
||||
holds([
|
||||
[' session = "self.client.session"', "tests/test_views.py"],
|
||||
['KEY = "form.' + S + '"', "app/forms.py"],
|
||||
['KEY = "form.section.' + S + '"', "app/forms.py"],
|
||||
['KEY = "form.section2.fieldName"', "app/forms.py"],
|
||||
['KEY = "section.fieldNameValue"', "app/forms.py"],
|
||||
['KEY = "form.section.fieldName', "app/forms.py"],
|
||||
]);
|
||||
});
|
||||
test("hold: a dotted field path with a secret literal after it, or outside source code, still reports", () => {
|
||||
holds([
|
||||
['password = "form.section.fieldName" or "' + S + '"', "app/forms.py"],
|
||||
['SECRET_KEY = "form.section.fieldName", "' + S + '"', "app/forms.py"],
|
||||
[' apiKey: "form.section.fieldName" || "' + S + '",', "src/forms.ts"],
|
||||
['PASSWORD="form.section.fieldName"', ".env"],
|
||||
['session_key: "form.section.fieldName"', "config/app.yml"],
|
||||
['PASSWORD="form.section.fieldName"', undefined],
|
||||
]);
|
||||
});
|
||||
|
||||
test("known limits: template keys, arrays of names and string-literal generic types still report, and so do the audit's bypass lines", () => {
|
||||
const template = (text: string) => text.replaceAll(BT, "\x60").replaceAll("@{", "$" + "{");
|
||||
holds([
|
||||
[template(" key: ~@{over.day}|@{over.groomerId}|over_tough|~,"), "src/cache.ts"],
|
||||
[' "reveal_secret": [load_secrets, "secretsOperations"],', "src/core/operation-loaders.generated.ts"],
|
||||
[' session: Pick<Session, "id" | "forkedFrom"> | null,', "src/orchestrator.ts"],
|
||||
[template("password = ~" + SPACED + "@{suffix}~;"), "src/client.ts"],
|
||||
[template("password = ~@{tenant}" + HEX + "~;"), "src/client.ts"],
|
||||
[template("password = ~@{/m4Fq8Wz2Rn7Yb3Hd9Vk6Lp5C/.source}~;"), "src/client.ts"],
|
||||
[template("password = ~@{String.fromCharCode(109,52,70,113,56,87,122,50)}~;"), "src/client.ts"],
|
||||
['password: [load_auth, "' + SPACED + '"],', "src/client.ts"],
|
||||
['password: [load_auth, "' + HEX + '"],', "src/client.ts"],
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* GST-3004 (#4289 cases 3 and 4): env.kv read only the first token after a
|
||||
* secret-named assignment at the start of a line, so a secret placed after a
|
||||
* short concatenated piece (`KEY = "form.x" + "<secret>"`) or written in a
|
||||
* comment (`x = 1 # API_KEY = "<secret>"`) gave no finding. A `+` chain's
|
||||
* literals are now joined and checked, and a name may start right after a
|
||||
* comment marker. The benign cases are the same shapes holding text that names
|
||||
* a key, so the new reads add no finding there.
|
||||
*/
|
||||
// Value: protects=env.kv reports a secret in a + chain or a comment; fails_when=either miss returns, or the comment read starts reporting prose and key-naming text; why_new=#4289 cases 3 and 4 had no test; seam=none; no_claim=scanner-level only, the pregate case lives in pregate-secrets-source-context.test.ts
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { scan } from "../lib/redact-engine";
|
||||
|
||||
const findings = (text: string, sourcePath?: string) =>
|
||||
scan(text, { repoVisibility: "private", processEnv: {}, ...(sourcePath ? { sourcePath } : {}) }).findings.filter((f) => f.id === "env.kv");
|
||||
const kv = (text: string, sourcePath?: string) => findings(text, sourcePath).length > 0;
|
||||
const S = "Qx9vL2mZp8Rt4Kw7Yn3Bh2Jd";
|
||||
const S2 = ["Zz8Kq2Lm", "7Np4Rs9T", "v3Wx6Yb"].join("");
|
||||
|
||||
describe("env.kv: secrets built by concatenation or written in comments (GST-3004, #4289)", () => {
|
||||
test("a secret after a short concatenated piece, or split across pieces, reports", () => {
|
||||
for (const [code, file] of [
|
||||
['KEY = "form.x" + "' + S + '"', "app/forms.py"],
|
||||
["KEY = 'form.x' + '" + S + "'", "app/forms.py"],
|
||||
[' apiKey: "sk" + "' + S + '",', "src/client.ts"],
|
||||
[' apiKey: base + "' + S + '",', "src/client.ts"],
|
||||
['TOKEN = "Qx9vL2m" + "Zp8Rt4K" + "w7Yn3Bh"', "app/client.py"],
|
||||
['TOKEN = "Qx9vL2m" + region + "Zp8Rt4K" + "w7Yn3Bh"', "app/client.py"],
|
||||
['TOKEN = "Qx9vL2m" + "Zp8Rt4K" + fetch_rest()', "app/client.py"],
|
||||
['TOKEN="abc"+"' + S + '"', ".env"],
|
||||
] as const) expect({ code, file, kv: kv(code, file) }).toEqual({ code, file, kv: true });
|
||||
});
|
||||
test("a secret in a trailing or whole-line comment reports", () => {
|
||||
for (const [code, file] of [
|
||||
['x = 1 # API_KEY = "' + S + '"', "app/settings.py"],
|
||||
["# API_KEY=" + S, "deploy/env.sh"],
|
||||
['const a = 1; // apiKey: "' + S + '"', "src/client.ts"],
|
||||
['// apiKey: "' + S + '"', "src/client.ts"],
|
||||
['/* token: "' + S + '" */', "src/client.ts"],
|
||||
["<!-- password: " + S + " -->", "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 });
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user