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
@@ -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)
|
||||
@@ -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<ReturnType<typeof launch>>;`), 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<Session, "id" | "forkedFrom">`). 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" + "<secret>"`), and a secret-named assignment
|
||||
right after `#`, `//`, `/*` or `<!--` reports in a trailing or
|
||||
whole-line comment unless its value is prose or key-naming text
|
||||
(letters-only words joined by `_ . - | : / #`). An array whose first
|
||||
element is a quoted secret (`"get_token": ["<secret>"]`) still reports
|
||||
only through a provider rule such as `stripe.secret`.
|
||||
|
||||
The `secrets` row says `N allowlisted (<ids>)` and lists each allowlisted
|
||||
finding with the entry (`allowlisted env.kv (evals/receipts/t1.json, added line
|
||||
|
||||
+152
-14
@@ -1065,6 +1065,33 @@ function expressionTail(line: string, comment: string): string | null {
|
||||
return depth > 0 || /(?:[=+\\]|\?\?|\|\||&&|\b(?:or|and))\s*$/.test(rest) ? null : rest;
|
||||
}
|
||||
|
||||
/**
|
||||
* GST-3004: the value capture stops at a type's first space, so an annotation
|
||||
* with spaced generic arguments (`session: Awaited<ReturnType<typeof launch>>;`)
|
||||
* never reached the name-chain test as a closed type. Read on its line, the
|
||||
* type is names, `<>`, `[]`, `|`, `&`, `,` and `?` only; it ends at a `,`
|
||||
* `;`, `)` or `=` outside the brackets, or at the line end, and the rest of
|
||||
* the line carries no secret literal or template. A string-literal type
|
||||
* (CAPY-1's `Pick<Session, "id" | "forkedFrom">`) still reports: an entropy
|
||||
* test on the literal alone let random hex through, so that shape is a known
|
||||
* limit, allowlisted by value.
|
||||
*/
|
||||
const GENERIC_TYPE_HEAD = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*</;
|
||||
function isGenericType(line: string): boolean {
|
||||
let depth = 0;
|
||||
let i = 0;
|
||||
for (; i < line.length; i += 1) {
|
||||
const c = line[i]!;
|
||||
if (c === "<") depth += 1;
|
||||
else if (c === ">") {
|
||||
if (--depth < 0) return false;
|
||||
} else if (depth === 0 && /[,;)=]/.test(c)) break;
|
||||
else if (!/[\w$.\s|&[\],?]/.test(c)) return false;
|
||||
}
|
||||
const rest = line.slice(i);
|
||||
return depth === 0 && !carriesSecretLiteral(rest) && !rest.includes("`");
|
||||
}
|
||||
|
||||
function isSourceExpression(span: string, match: RegExpExecArray, sourcePath: string | undefined): boolean {
|
||||
if (!sourcePath || !CODE_SOURCE.test(sourcePath)) return false;
|
||||
const { start, end } = spanBounds(match);
|
||||
@@ -1078,7 +1105,8 @@ function isSourceExpression(span: string, match: RegExpExecArray, sourcePath: st
|
||||
} else {
|
||||
const head = match[0].slice(0, match[0].length - span.length).trimEnd();
|
||||
const separator = head.slice(-1);
|
||||
if (separator !== ":" || !CODE_NAME_CHAIN.test(span.replace(/[,;)=|]+$/, ""))) return false;
|
||||
if (separator !== ":") return false;
|
||||
if (!CODE_NAME_CHAIN.test(span.replace(/[,;)=|]+$/, ""))) return GENERIC_TYPE_HEAD.test(span) && isGenericType(match.input.slice(start).split("\n", 1)[0]!) && !insideString(match.input, start, sourcePath);
|
||||
const rest = (span.endsWith("=") ? "=" : "") + match.input.slice(end).split("\n", 1)[0];
|
||||
if (!/[,;]$/.test(span)) {
|
||||
const tail = expressionTail(rest, /\.pyi?$/i.test(sourcePath) ? "#" : "//");
|
||||
@@ -1096,15 +1124,22 @@ function isSourceExpression(span: string, match: RegExpExecArray, sourcePath: st
|
||||
* the same line inside a `"""`/`'''` block still reports. Every segment must be
|
||||
* name-shaped (snake_case, camelCase, PascalCase or CONSTANT_CASE), so a dotted
|
||||
* random token still reports, and only a `;` and a comment may follow it on
|
||||
* the line: a literal after it (`or "…"`, `, "…"`), a continuation or a
|
||||
* keyword argument's comma still reports. A quoted value, a bare name, a
|
||||
* the line: a literal after it (`or "…"`, `, "…"`) or a continuation still
|
||||
* reports. A keyword argument's closing `,` or `)` is allowed only when the
|
||||
* last segment is a CONSTANT_CASE setting
|
||||
* (`aws_secret_access_key=settings.S3_SECRET_KEY,`, #4289), so
|
||||
* `password=request.password,` still reports. A quoted value, a bare name, a
|
||||
* config, .env or TS file, and a scan without a path are unchanged.
|
||||
*/
|
||||
const ATTRIBUTE_SEGMENT = /^_*(?:[a-z][a-z0-9]*(?:_+[a-z0-9]+)*|[A-Z][A-Z0-9]*(?:_+[A-Z0-9]+)*|[A-Za-z][a-z0-9]*(?:[A-Z][a-z0-9]+)*)_*$/;
|
||||
const CONSTANT_SEGMENT = /^_*[A-Z][A-Z0-9]*(?:_+[A-Z0-9]+)*_*$/;
|
||||
function isAttributeReference(span: string, match: RegExpExecArray, sourcePath: string | undefined): boolean {
|
||||
if (!sourcePath || !STRING_SPAN_SOURCE.test(sourcePath)) return false;
|
||||
const chain = span.replace(/;$/, "").split(".");
|
||||
const read = span.replace(/[,;)]+$/, "");
|
||||
const chain = read.split(".");
|
||||
if (chain.length < 2 || !chain.every((segment) => ATTRIBUTE_SEGMENT.test(segment))) return false;
|
||||
const closer = span.slice(read.length);
|
||||
if (closer !== "" && closer !== ";" && !CONSTANT_SEGMENT.test(chain.at(-1)!)) return false;
|
||||
const { start, end } = spanBounds(match);
|
||||
if (!/=[ \t]*$/.test(match.input.slice(match.index, start))) return false;
|
||||
const rest = match.input.slice(end).split("\n", 1)[0];
|
||||
@@ -1112,6 +1147,93 @@ function isAttributeReference(span: string, match: RegExpExecArray, sourcePath:
|
||||
return !insideString(match.input, start, sourcePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* GST-3004: text that names or builds a key rather than holding one. Read with
|
||||
* its whitespace removed, so a password with spaces is read whole, it must be
|
||||
* short (under 8), a placeholder, under 3.0 bits, or letters-only words,
|
||||
* lowercase or camelCase with an optional leading capital, joined by
|
||||
* `_ . - | : / #` (`over_tough`, `secretsOperations`, `|over_tough|`). A
|
||||
* generated credential carries digits or adjacent capitals, so it keeps
|
||||
* reporting; a letters-only passphrase in this shape (`correctHorseBattery`)
|
||||
* is the accepted residue. Comment prose is read run by run instead
|
||||
* (isCommentText).
|
||||
*/
|
||||
const KEY_TEXT = /^[_.\-|:/#]*[A-Za-z][a-z]+(?:[A-Z][a-z]+)*(?:[_.\-|:/#]+[A-Za-z][a-z]+(?:[A-Z][a-z]+)*)*[_.\-|:/#]*$/;
|
||||
function isSecretRun(run: string): boolean {
|
||||
return run.length >= 8 && !isPlaceholderSpan(run) && shannonEntropy(run) >= 3.0 && !KEY_TEXT.test(run);
|
||||
}
|
||||
function holdsSecretText(text: string): boolean {
|
||||
return isSecretRun(text.replace(/\s+/g, ""));
|
||||
}
|
||||
|
||||
/** The text of the `"…"` or `'…'` literal whose quote is at `i` and the index after it, or null when it does not close on the line. */
|
||||
function quotedAt(line: string, i: number): { text: string; end: number } | null {
|
||||
for (let j = i + 1; j < line.length; j += line[j] === "\\" ? 2 : 1) if (line[j] === line[i]) return { text: line.slice(i + 1, j), end: j + 1 };
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* #4289: `KEY = "form.x" + "<secret>"` builds the value from pieces, and the
|
||||
* value capture read only the first. When the value is a `+` chain of quoted
|
||||
* literals and names on its line, the literals' text is joined, so a secret
|
||||
* placed after a short piece or split across pieces is whole again, and checked
|
||||
* with holdsSecretText. Names add no text. A chain that breaks off at a call,
|
||||
* template or other operand is checked as far as it reads.
|
||||
*/
|
||||
const CHAIN_NAME = /[A-Za-z_$][\w$]*(?:\??\.[A-Za-z_$][\w$]*)*/y;
|
||||
const CHAIN_PLUS = /[ \t]*\+[ \t]*/y;
|
||||
function concatenationHoldsSecret(match: RegExpExecArray): boolean {
|
||||
const { start } = spanBounds(match);
|
||||
const line = match.input.slice(/["']/.test(match.input[start - 1] ?? "") ? start - 1 : start).split("\n", 1)[0]!;
|
||||
let joined = "";
|
||||
let pieces = 0;
|
||||
for (let i = 0; ; i = CHAIN_PLUS.lastIndex) {
|
||||
const literal = line[i] === '"' || line[i] === "'" ? quotedAt(line, i) : null;
|
||||
CHAIN_NAME.lastIndex = i;
|
||||
if (!literal && !CHAIN_NAME.exec(line)) break;
|
||||
pieces += 1;
|
||||
if (literal) joined += literal.text;
|
||||
CHAIN_PLUS.lastIndex = literal ? literal.end : CHAIN_NAME.lastIndex;
|
||||
if (!CHAIN_PLUS.exec(line)) break;
|
||||
}
|
||||
return pieces > 1 && holdsSecretText(joined);
|
||||
}
|
||||
|
||||
/**
|
||||
* #4289: the regex also starts a name right after a comment marker, and
|
||||
* comments are prose: `# Auth: authentication, authorization, sessions`. A
|
||||
* match that starts after other text on its line is a comment's, and its value,
|
||||
* less trailing punctuation, reports only when it is a secret run by
|
||||
* isSecretRun. Code before the comment is matched from the line start and
|
||||
* checked as before.
|
||||
*/
|
||||
function isCommentText(span: string, match: RegExpExecArray): boolean {
|
||||
const lineStart = match.input.lastIndexOf("\n", match.index - 1) + 1;
|
||||
if (/^(?:[ +-]?\+|[ +-]?-(?! [^ \t]))?[ \t]*$/.test(match.input.slice(lineStart, match.index))) return false;
|
||||
return !isSecretRun(span.replace(/[,;.)]+$/, ""));
|
||||
}
|
||||
|
||||
/**
|
||||
* #4289: a quoted dotted path of three or more letters-only segments, each
|
||||
* lowercase or camelCase and at least one camelCase
|
||||
* (`KEY = "form.section.fieldName"`), names a form field. A segment with a
|
||||
* digit, a capital first letter or adjacent capitals (`"form.<secret>"`)
|
||||
* reports, and so do a path of one or two segments and an all-lowercase one,
|
||||
* which #4209 keeps reporting as a quoted literal (`session = "self.client.session"`).
|
||||
* Only source files qualify, and the rest of the line must carry no secret
|
||||
* literal or template, so `PASSWORD="form.section.fieldName"` in a config
|
||||
* file and `password = "form.section.fieldName" or "<secret>"` still report.
|
||||
*/
|
||||
const DOTTED_FIELD_PATH = /^[a-z]+(?:[A-Z][a-z]+)*(?:\.[a-z]+(?:[A-Z][a-z]+)*){2,}$/;
|
||||
function isDottedFieldPath(span: string, match: RegExpExecArray, sourcePath: string | undefined): boolean {
|
||||
if (!sourcePath || !CODE_SOURCE.test(sourcePath)) return false;
|
||||
const { start, end } = spanBounds(match);
|
||||
const quote = match.input[start - 1];
|
||||
if ((quote !== '"' && quote !== "'") || match.input[end] !== quote || !DOTTED_FIELD_PATH.test(span) || !/[A-Z]/.test(span)) return false;
|
||||
const rest = match.input.slice(end + 1).split("\n", 1)[0]!;
|
||||
return !carriesSecretLiteral(rest) && !rest.includes("`");
|
||||
}
|
||||
|
||||
/** The files isPropertyRead exempts in: TS, Dart and Python. JSX text in .tsx and .jsx can hold a host on any line, so those report as before. */
|
||||
const PROPERTY_READ_SOURCE = /\.(?:[cm]?ts|dart|pyi?)$/i;
|
||||
/** How far back on its line isPropertyRead reads from a span. A span further from its line start than this is never exempted. */
|
||||
@@ -1565,7 +1687,15 @@ export const PATTERNS: RedactPattern[] = [
|
||||
// and text is a Markdown bullet or YAML list item, not a marker, so those
|
||||
// read as they did before. ENV_KV_NAME and SCHEMA_FIELD_NAME take the same
|
||||
// marker so the name-shape and schema exemptions hold on a diff line.
|
||||
regex: /^(?:[ +-]?\+|[ +-]?-(?! [^ \t]))?[ \t]*(?:export[ \t]+)?["']?[A-Za-z0-9_.-]*(?:KEY|TOKEN|SECRET|PASSWORD|PASSWD|CREDENTIALS?|DSN|AUTH|COOKIE|SESSION|PRIVATE)["']?[ \t]*[:=][ \t]*["']?([^\s'"]{8,})["']?/i,
|
||||
// GST-3004 (#4289): a name may also start right after a comment marker
|
||||
// (`#`, `//`, `/*`, `<!--`) at the line start or after a space, so a
|
||||
// secret in a trailing or whole-line comment reports; that start is a
|
||||
// bounded lookbehind (a run of spaces is matched after it, not inside it,
|
||||
// so a long run stays linear), and the code before the comment is still
|
||||
// matched on its own from the line start. A short first piece followed by
|
||||
// `+` is captured so concatenationHoldsSecret can read the rest of the
|
||||
// chain.
|
||||
regex: /(?:^(?:[ +-]?\+|[ +-]?-(?! [^ \t]))?|(?<=(?:^|[ \t])(?:#{1,6}|\/{2,3}|\/\*{1,2}|<!--)))[ \t]*(?:export[ \t]+)?["']?[A-Za-z0-9_.-]*(?:KEY|TOKEN|SECRET|PASSWORD|PASSWD|CREDENTIALS?|DSN|AUTH|COOKIE|SESSION|PRIVATE)["']?[ \t]*[:=][ \t]*["']?([^\s'"]{8,}|[^\s'"]{1,7}(?=["']?[ \t]*\+))["']?/i,
|
||||
// Only fire on credential-shaped names with high-entropy values — kills
|
||||
// `FOO_KEY=changeme` and `cacheKey: <entropic-id>` FPs. #2912: a value
|
||||
// that is exactly an environment read (`os.environ["X"]`,
|
||||
@@ -1578,17 +1708,25 @@ export const PATTERNS: RedactPattern[] = [
|
||||
// #4209: so is a dotted attribute read assigned with = in Python or Dart (isAttributeReference).
|
||||
// #3208: so is a schema field identifier (isSchemaFieldIdentifier).
|
||||
// GST-1894: so is a self-named fixture label in a test file (isTestFixtureLabel).
|
||||
// GST-3004: so is a dotted field path (isDottedFieldPath, #4289), and so
|
||||
// is key-naming text in a comment
|
||||
// (isCommentText). A secret literal in a `+` chain reports first,
|
||||
// whatever the exemptions say about the chain's first piece
|
||||
// (concatenationHoldsSecret).
|
||||
validate: (span, match, opts) =>
|
||||
isCredentialShapedEnvName(match[0]) &&
|
||||
!isPlaceholderSpan(span) &&
|
||||
!/^\$\{?[A-Za-z_]/.test(span) &&
|
||||
!isBareEnvRead(span, match) &&
|
||||
!isCallExpression(span, match) &&
|
||||
!isSourceExpression(span, match, opts?.sourcePath) &&
|
||||
!isAttributeReference(span, match, opts?.sourcePath) &&
|
||||
!isSchemaFieldIdentifier(span, match) &&
|
||||
!isTestFixtureLabel(span, opts?.sourcePath) &&
|
||||
shannonEntropy(span) >= 3.0,
|
||||
(concatenationHoldsSecret(match) ||
|
||||
(!isPlaceholderSpan(span) &&
|
||||
!/^\$\{?[A-Za-z_]/.test(span) &&
|
||||
!isBareEnvRead(span, match) &&
|
||||
!isCallExpression(span, match) &&
|
||||
!isSourceExpression(span, match, opts?.sourcePath) &&
|
||||
!isAttributeReference(span, match, opts?.sourcePath) &&
|
||||
!isDottedFieldPath(span, match, opts?.sourcePath) &&
|
||||
!isCommentText(span, match) &&
|
||||
!isSchemaFieldIdentifier(span, match) &&
|
||||
!isTestFixtureLabel(span, opts?.sourcePath) &&
|
||||
shannonEntropy(span) >= 3.0)),
|
||||
},
|
||||
{
|
||||
id: "auth.bearer",
|
||||
|
||||
@@ -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