Skip to content

Find code by asking what it does. A CLI for coding agents that uses Jev to discover relevant files and source context.

Filter files whose content looks like stored secrets (fixes #51)

juliasadler9e67dfc2cbe7
diff --git a/packages/core/src/filesystem.ts b/packages/core/src/filesystem.ts
index 014b431..1ab5d32 100644
--- a/packages/core/src/filesystem.ts
+++ b/packages/core/src/filesystem.ts
@@ -4,6 +4,7 @@ import { lstat, open, opendir, realpath } from "node:fs/promises";
import { isAbsolute, join, relative, resolve, sep } from "node:path";
import ignore, { type Ignore } from "ignore";
import { storageDirectory } from "./storage";
+import { containsLikelySecret } from "./secrets";
/** These independent switches broaden only their named exclusion category. */
export type FilesystemPolicy = {
@@ -378,6 +379,8 @@ export async function createFilesystem(options: FilesystemOptions) {
}
if (!policy.includeSensitive && /-----BEGIN (?:[A-Z0-9]+ )*PRIVATE KEY-----/.test(source))
return excluded("private_key");
+ if (!policy.includeSensitive && containsLikelySecret(source))
+ return excluded("sensitive_content");
return {
status: "ok",
snapshot: Object.freeze({
diff --git a/packages/core/src/secrets.ts b/packages/core/src/secrets.ts
new file mode 100644
index 0000000..a931fb5
--- /dev/null
+++ b/packages/core/src/secrets.ts
@@ -0,0 +1,251 @@
+/**
+ * Content-level secret detection for file snapshots.
+ *
+ * Name-based filtering (`.env`, fixed names, key suffixes) cannot catch keys
+ * stored in ordinary config files, so `readSnapshot` also scans content for
+ * values that look like credentials. Two independent signals:
+ *
+ * - Signal A: an assignment-shaped line (`KEY=value`, `key: value`,
+ * `"key": "value"`, `const key = "value"`) whose key name looks
+ * credential-ish AND whose value looks like a real secret (long,
+ * high-entropy, non-placeholder).
+ * - Signal B: a known provider key prefix (with a word-boundary guard) followed
+ * by enough key material, or a Slack webhook URL — distinctive enough to flag
+ * without assignment context.
+ *
+ * Deliberate trade-offs (documented, not accidental):
+ * - Whole file is excluded rather than redacted: preserves the snapshot
+ * `contentHash = sha256(raw bytes)` contract and matches the existing
+ * `private_key` exclusion semantics.
+ * - Length >= 16 and entropy >= 3.5 bits/char: tuned so
+ * `password: "SuperSecret123!@#"` (17 chars, ~3.57 bits) is caught while
+ * `password123` is not. UUID-shaped values (~3.4 bits) are knowingly missed;
+ * raising the bar would trade fewer false positives for more leaks — the
+ * wrong direction for this filter.
+ * - bcrypt/argon2 password hashes are flagged too: not directly usable, but
+ * offline-crackable, so conservative exclusion is acceptable.
+ * - An assignment whose value opens a quote that never closes is treated as
+ * suspicious (fail-closed) rather than ignored.
+ * - `;` does NOT start an inline comment: it would truncate multi-statement
+ * source lines (`const a = "x"; const apiKey = "…"`) and cause misses.
+ */
+
+/** Distinctive provider prefixes; each must be followed by >= 16 key chars to flag. */
+const KNOWN_PREFIXES = [
+ "sk-live-",
+ "sk_test_",
+ "sk_live_",
+ "sk-ant-",
+ "npm_",
+ "pypi-",
+ "hf_",
+ "dop_v1_",
+ "sq0csp-",
+ "sq0atp-",
+ "ya29.",
+ "SG.",
+ "sk-",
+ "ghp_",
+ "gho_",
+ "ghu_",
+ "ghs_",
+ "github_pat_",
+ "xoxb-",
+ "xoxp-",
+ "xoxa-",
+ "xoxr-",
+ "AKIA",
+ "ASIA",
+ "AIza",
+ "tvly-",
+ "dsk-",
+ "rk_live_",
+ "whsec_",
+] as const;
+
+const TOKEN_TAIL = "[A-Za-z0-9_\\-]{16,}";
+
+/**
+ * Precompiled prefix matchers, longest prefix first so `sk-live-` wins over
+ * `sk-` at the same position. The lookbehind keeps `sk-` from firing inside a
+ * longer word (`disk-…` must not match).
+ */
+const KNOWN_PREFIX_PATTERNS = [...KNOWN_PREFIXES]
+ .sort((a, b) => b.length - a.length)
+ .map(
+ (prefix) =>
+ new RegExp(`(?<![A-Za-z0-9_])${escapeRegExp(prefix)}(${TOKEN_TAIL})`, "g"),
+ );
+
+/** Slack webhook URLs carry the secret in the path, not in a token prefix. */
+const SLACK_WEBHOOK_PATTERN = /hooks\.slack\.com\/services\/[A-Za-z0-9_\-/]{24,}/;
+
+/**
+ * Key names that suggest the assigned value is a credential.
+ * Bare `token`/`pwd`/`credential` are too noisy alone (`getToken()`,
+ * tokenizers); only compound forms (`auth_token`, `bearer_token`) count.
+ */
+const KEY_NAME_PATTERN =
+ /(?:^|[^a-z0-9])(?:api[_-]?key|client[_-]?secret|passwd|password|private[_-]?key|secret|auth[_-]?token|access[_-]?token|api[_-]?token|bearer[_-]?token)(?:[^a-z]|$)/i;
+
+/** Assignment shapes across .env / YAML / JSON / TOML / INI and source files, with optional quotes and declaration keywords. */
+const ASSIGNMENT_PATTERN =
+ /^\s*(?:(?:const|let|var|export|default|public|private|protected|static|readonly|final)\s+)*["']?([^"'=\s:]+)["']?\s*[:=]\s*([\s\S]+?)\s*$/;
+
+const PLACEHOLDER_PATTERN =
+ /^(?:your|example|test|fake|dummy|sample|changeme|placeholder|redacted|masked|todo|fixme|none|null|undefined|nil|tbd)[\w-]*$|^x+$|^<.*>$|^\*+$|^\$\{[^}]*\}$|^\{\{[^}]*\}\}$/i;
+
+const MIN_SECRET_LENGTH = 16;
+const MIN_SECRET_ENTROPY = 3.5;
+const LONG_TOKEN_RUN = /[A-Za-z0-9_\-+=/]{32,}/;
+
+function escapeRegExp(text: string) {
+ return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
+}
+
+function shannonEntropy(value: string): number {
+ const frequencies = new Map<string, number>();
+ for (const char of value) frequencies.set(char, (frequencies.get(char) ?? 0) + 1);
+ let entropy = 0;
+ for (const count of frequencies.values()) {
+ const probability = count / value.length;
+ entropy -= probability * Math.log2(probability);
+ }
+ return entropy;
+}
+
+/** True when the tail of a prefix-matched token looks like redacted/example material. */
+function isPlaceholderTail(tail: string): boolean {
+ const lower = tail.toLowerCase();
+ return (
+ /xxx|example|placeholder|redacted|masked/.test(lower) || new Set(lower).size === 1
+ );
+}
+
+/** Returns the matched secret token, or undefined when no known prefix fires. */
+function findSecretToken(line: string): string | undefined {
+ // A placeholder match suppresses shorter-prefix matches at the same position
+ // (e.g. `sk-` must not re-fire inside an already-redacted `sk-live-XXXX`).
+ const placeholderAt = new Set<number>();
+ for (const pattern of KNOWN_PREFIX_PATTERNS) {
+ pattern.lastIndex = 0;
+ let match: RegExpExecArray | null;
+ while ((match = pattern.exec(line)) !== null) {
+ if (!placeholderAt.has(match.index)) {
+ if (match[1] !== undefined && !isPlaceholderTail(match[1])) return match[0];
+ placeholderAt.add(match.index);
+ }
+ if (match.index === pattern.lastIndex) pattern.lastIndex++;
+ }
+ }
+ return undefined;
+}
+
+function cleanValue(raw: string): string {
+ const value = raw.trim().replace(/[,;]$/, "").trim();
+ const quote = value[0];
+ if (value.length < 2 || (quote !== '"' && quote !== "'" && quote !== "`")) return value;
+ for (let i = 1; i < value.length; i++) {
+ if (value[i] === "\\") {
+ i++;
+ continue;
+ }
+ if (value[i] === quote) return value.slice(1, i).trim();
+ }
+ return value;
+}
+
+function isPlaceholder(value: string): boolean {
+ return value.length === 0 || PLACEHOLDER_PATTERN.test(value);
+}
+
+/** True when a value opens a quote that is never closed (fail-closed: suspicious). */
+function hasUnclosedQuote(raw: string): boolean {
+ const text = raw.trim();
+ const quote = text[0];
+ if (quote !== '"' && quote !== "'" && quote !== "`") return false;
+ for (let i = 1; i < text.length; i++) {
+ if (text[i] === "\\") {
+ i++;
+ continue;
+ }
+ if (text[i] === quote) return false;
+ }
+ return true;
+}
+
+function looksLikeSecretValue(value: string): boolean {
+ if (value.length < MIN_SECRET_LENGTH || isPlaceholder(value)) return false;
+ if (findSecretToken(value) !== undefined) return true;
+ if (LONG_TOKEN_RUN.test(value)) return true;
+ return shannonEntropy(value) >= MIN_SECRET_ENTROPY;
+}
+
+/**
+ * Join physical lines into logical lines, tracking quote state across lines so
+ * multi-line quoted values (e.g. PEM blocks in `KEY="…"`) stay one unit.
+ * Quote-aware comment handling: `#` and `//` start a comment only outside
+ * quotes, so `#` inside a value (`pass#word`) and `//` in URLs (`https://…`)
+ * are preserved. `//` after `:` is not a comment (URL schemes).
+ */
+function logicalLines(source: string): string[] {
+ const lines: string[] = [];
+ let current = "";
+ let quote: string | null = null;
+ for (const physical of source.split("\n")) {
+ let stripped = "";
+ let i = 0;
+ while (i < physical.length) {
+ const char = physical[i]!;
+ if (quote !== null) {
+ if (char === "\\" && i + 1 < physical.length) {
+ stripped += char + physical[i + 1];
+ i += 2;
+ continue;
+ }
+ if (char === quote) quote = null;
+ stripped += char;
+ i++;
+ } else if (char === '"' || char === "'" || char === "`") {
+ quote = char;
+ stripped += char;
+ i++;
+ } else if (char === "#") {
+ break;
+ } else if (char === "/" && physical[i + 1] === "/" && physical[i - 1] !== ":") {
+ break;
+ } else {
+ stripped += char;
+ i++;
+ }
+ }
+ current += (current.length > 0 ? "\n" : "") + stripped;
+ if (quote === null) {
+ lines.push(current);
+ current = "";
+ }
+ }
+ if (current.length > 0) lines.push(current);
+ return lines;
+}
+
+/** True when the source contains at least one line that looks like a stored secret. */
+export function containsLikelySecret(source: string): boolean {
+ for (const line of logicalLines(source)) {
+ const trimmed = line.trim();
+ if (!trimmed || trimmed.startsWith("#") || trimmed.startsWith("//")) continue;
+ if (findSecretToken(trimmed) !== undefined) return true;
+ if (SLACK_WEBHOOK_PATTERN.test(trimmed)) return true;
+ const assignment = ASSIGNMENT_PATTERN.exec(trimmed);
+ if (
+ assignment?.[1] !== undefined &&
+ assignment[2] !== undefined &&
+ KEY_NAME_PATTERN.test(assignment[1])
+ ) {
+ const rawValue = assignment[2];
+ if (hasUnclosedQuote(rawValue)) return true;
+ if (looksLikeSecretValue(cleanValue(rawValue))) return true;
+ }
+ }
+ return false;
+}
diff --git a/packages/core/test/filesystem.test.ts b/packages/core/test/filesystem.test.ts
index d821e50..43c5eac 100644
--- a/packages/core/test/filesystem.test.ts
+++ b/packages/core/test/filesystem.test.ts
@@ -465,3 +465,39 @@ test("valid UTF-8 control-byte binary and invalid UTF-8 remain excluded under ev
await reader.close();
}
});
+
+test("readSnapshot excludes files whose content looks like stored secrets", async () => {
+ const fakeKey = "sk-K7q2Mv9xT4pL8nR3wY6bD1fH5jG0cS9aE2dZ7xU4v"; // synthetic and invalid
+ const root = await fixture({
+ "settings.json": `{\n "env": {\n "OPENAI_API_KEY": "${fakeKey}"\n }\n}\n`,
+ "src/settings.ts": `export function loadSettings() {\n return "settings.json";\n}\n`,
+ "credentials.json": "SECRET_SENTINEL",
+ });
+ const reader = await createFilesystem({ root });
+ try {
+ expect(await reader.readSnapshot("settings.json")).toMatchObject({
+ status: "excluded",
+ reason: "sensitive_content",
+ });
+ expect(await reader.readSnapshot("src/settings.ts")).toMatchObject({ status: "ok" });
+ expect(await reader.readSnapshot("credentials.json")).toMatchObject({
+ status: "excluded",
+ reason: "sensitive_name",
+ });
+ } finally {
+ await reader.close();
+ }
+});
+
+test("includeSensitive bypasses content-level secret filtering", async () => {
+ const fakeKey = "sk-K7q2Mv9xT4pL8nR3wY6bD1fH5jG0cS9aE2dZ7xU4v"; // synthetic and invalid
+ const root = await fixture({
+ "settings.json": `{\n "env": {\n "OPENAI_API_KEY": "${fakeKey}"\n }\n}\n`,
+ });
+ const reader = await createFilesystem({ root, policy: { includeSensitive: true } });
+ try {
+ expect(await reader.readSnapshot("settings.json")).toMatchObject({ status: "ok" });
+ } finally {
+ await reader.close();
+ }
+});
diff --git a/packages/core/test/secrets.test.ts b/packages/core/test/secrets.test.ts
new file mode 100644
index 0000000..ae3ba10
--- /dev/null
+++ b/packages/core/test/secrets.test.ts
@@ -0,0 +1,121 @@
+import { expect, test } from "bun:test";
+import { containsLikelySecret } from "../src/secrets";
+
+// All key-like values below are synthetic and invalid; they only exercise the
+// detector's shape heuristics. Realistic shapes are deliberate: the detector
+// must fire on the shapes from dzhng/jevgrep#51.
+const FAKE_OPENAI = "sk-K7q2Mv9xT4pL8nR3wY6bD1fH5jG0cS9aE2dZ7xU4v";
+const FAKE_STRIPE = "sk_live_9fH2kL7mQ4vT8wX3yZ6bN1pA5sD0gJcE";
+const FAKE_GITHUB = "ghp_aB3dE5fG7hJ9kL1mN3pQ5rS7tU9vW1xY3zAbC5d";
+const FAKE_TAVILY = "tvly-DEV-4fK8qN2mT6vX9yZ3bD7hJ1pL5sW8aC0eG";
+const FAKE_PASSWORD = "xQ9#vL2$mK8@nB4!pT6&wZ3*";
+
+test("flags the four secret shapes from jevgrep#51", () => {
+ // settings.json: an env block with OPENAI_API_KEY
+ expect(
+ containsLikelySecret(`{\n "env": {\n "OPENAI_API_KEY": "${FAKE_OPENAI}"\n }\n}`),
+ ).toBe(true);
+ // keys.env: STRIPE_SECRET_KEY
+ expect(containsLikelySecret(`STRIPE_SECRET_KEY=${FAKE_STRIPE}\n`)).toBe(true);
+ // config.toml: api_key
+ expect(containsLikelySecret(`[service]\napi_key = "${FAKE_GITHUB}"\n`)).toBe(true);
+ // keys.json: bare key array (no key-like name; caught by known prefix)
+ expect(containsLikelySecret(`{\n "keys": ["${FAKE_TAVILY}"]\n}`)).toBe(true);
+});
+
+test("flags yaml-style secrets and bearer tokens", () => {
+ expect(containsLikelySecret(`database:\n password: "${FAKE_PASSWORD}"\n`)).toBe(true);
+ expect(
+ containsLikelySecret(`headers:\n authorization: Bearer ${FAKE_GITHUB}\n`),
+ ).toBe(true);
+});
+
+test("ignores placeholders, short and low-entropy values", () => {
+ expect(containsLikelySecret(`OPENAI_API_KEY=YOUR_API_KEY_HERE\n`)).toBe(false);
+ expect(containsLikelySecret(`OPENAI_API_KEY=\n`)).toBe(false);
+ expect(containsLikelySecret(`api_key = "abc123"\n`)).toBe(false);
+ expect(containsLikelySecret(`"password": "password123"\n`)).toBe(false);
+ expect(containsLikelySecret(`api_key = "changeme"\n`)).toBe(false);
+ expect(containsLikelySecret(`secret: "***"\n`)).toBe(false);
+});
+
+test("ignores ordinary code that merely mentions key-like words", () => {
+ expect(
+ containsLikelySecret(`const token = getToken();\nfunction authenticate(token) {\n return token;\n}\n`),
+ ).toBe(false);
+ expect(
+ containsLikelySecret(`// Set your api_key in the config file before running.\n`),
+ ).toBe(false);
+ expect(
+ containsLikelySecret(`# The password field is required by the login form.\n`),
+ ).toBe(false);
+ expect(
+ containsLikelySecret(
+ `export function loadSettings() {\n return readFileSync("settings.json", "utf8");\n}\n`,
+ ),
+ ).toBe(false);
+});
+
+test("regression: review blockers #1-#3", () => {
+ // #1: `sk-` inside a longer word must not flag (`disk-` contains `sk-`).
+ expect(containsLikelySecret(`backup: disk-abcdef1234567890ABCDEF\n`)).toBe(false);
+ // #1 control: a real `sk-` token still flags.
+ expect(
+ containsLikelySecret(`key: "sk-abcdef1234567890ABCDEF"\n`),
+ ).toBe(true);
+ // #2: multi-line quoted values are joined before detection.
+ expect(
+ containsLikelySecret(
+ 'PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\n' +
+ "MIIBOgIBAAJBAK7i3vF5pL8nW3yB6dF1hJ5kA0cE2gZ\n" +
+ '-----END RSA PRIVATE KEY-----"\n',
+ ),
+ ).toBe(true);
+ // #2 conservative: an assignment whose quote never closes is suspicious.
+ expect(containsLikelySecret(`API_KEY="abcdef1234567890abcdef\n`)).toBe(true);
+ // #3: environment-variable / template placeholders are not secrets.
+ expect(containsLikelySecret(`API_KEY=${"${SOME_VERY_LONG_VARIABLE_NAME_HERE}"}\n`)).toBe(false);
+ expect(containsLikelySecret(`api_key: "{{MY_SECRET_VALUE_HERE}}"\n`)).toBe(false);
+ expect(containsLikelySecret(`password = "REDACTED"\n`)).toBe(false);
+ // #3 control: redacted doc examples with a real prefix shape stay unflagged.
+ expect(containsLikelySecret(`# e.g. sk-live-XXXXXXXXXXXXXXXX\n`)).toBe(false);
+});
+
+test("regression: review majors #4-#7", () => {
+ // #4: more provider prefixes.
+ expect(
+ containsLikelySecret(`NPM_TOKEN=npm_abcDEF1234567890abcdef1234567890ab\n`),
+ ).toBe(true);
+ expect(
+ containsLikelySecret(`pypi_token = "pypi-AgEIcHlwaS5vcmcCJGU5Y2YxMjM0NTY3ODkw"\n`),
+ ).toBe(true);
+ // #5: bearer_token key name.
+ expect(
+ containsLikelySecret(`bearer_token = "7fK8qN2mT6vX9yZ3bD4hJ1pL5sW8aC0eG2i"\n`),
+ ).toBe(true);
+ // #6: inline comments are stripped quote-aware; they must not decide detection.
+ expect(
+ containsLikelySecret(`API_KEY=abc # a very long production comment adding entropy here\n`),
+ ).toBe(false);
+ expect(
+ containsLikelySecret(
+ `API_KEY=sk-K7q2Mv9xT4pL8nR3wY6bD1fH5jG0cS9aE2dZ7xU4v # production\n`,
+ ),
+ ).toBe(true);
+ // `#` inside a quoted value is preserved, not treated as a comment.
+ expect(containsLikelySecret(`password = "xQ9#vL2$mK8@nB4!pT6&wZ3*"\n`)).toBe(true);
+ // #7: short-but-strong values (16+) are caught.
+ expect(containsLikelySecret(`password: "SuperSecret123!@#"\n`)).toBe(true);
+ // Declaration keywords in source files are handled.
+ expect(
+ containsLikelySecret(`const password = "SuperSecret123!@#";\n`),
+ ).toBe(true);
+ // CRLF line endings work.
+ expect(containsLikelySecret(`"OPENAI_API_KEY": "sk-K7q2Mv9xT4pL8nR3wY6bD1fH5jG0cS9aE2dZ7xU4v"\r\n`)).toBe(true);
+ // Slack webhook URLs flag.
+ expect(
+ containsLikelySecret(
+ `url = "https://hooks.slack.com/services/T12345678/B87654321/abcdefghijklmnopqrstuvwx"\n`,
+ ),
+ ).toBe(true);
+});