repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

test(leaks): map spec validation batches

6ba1bcec87aeccfbbb0d261d8315e503a48bb3dc

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (5)

extensions/leaks/__tests__/regex-fuzz.test.ts

Added
new file mode 100644
1 +import { describe, expect, it } from "vitest";
2 +import { __test } from "../index.ts";
4 +const adversarial = [
5 + "a".repeat(20_000),
6 + "(".repeat(8_000) + ")".repeat(8_000),
7 + "token=" + "x".repeat(20_000),
8 + "https://" + "a".repeat(10_000) + ":" + "b".repeat(10_000),
9 + "Authorization: Bearer " + "x".repeat(20_000),
10 + "Cookie: " + Array.from({ length: 1_000 }, (_, index) => `k${index}=v${index}`).join("; "),
11 +].join("\n");
13 +describe("leaks regex fuzz safety", () => {
14 + it("runs every exported runtime regex on adversarial text quickly", () => {
15 + const start = performance.now();
16 + for (const target of __test.runtimeRegexFuzzTargets) {
17 + target.regex.lastIndex = 0;
18 + for (const _match of adversarial.matchAll(target.regex)) {
19 + // exhaust iterator
20 + }
21 + }
22 + expect(performance.now() - start).toBeLessThan(250);
23 + });
25 + it("does not redact generated benign metadata corpus", () => {
26 + const engine = new __test.LeaksEngine();
27 + const corpus = Array.from({ length: 500 }, (_, index) => [
28 + `branch=feature/metadata-${index}`,
29 + `host=svc-${index}.internal.example.test`,
30 + `url=https://example.test/path/${index}?user=alice`,
31 + `id=550e8400-e29b-41d4-a716-${String(index).padStart(12, "0")}`,
32 + ].join(" ")).join("\n");
34 + expect(engine.redactText(corpus, "provider").text).toBe(corpus);
35 + });
37 + it("keeps positive regex controls effective after fuzz corpus", () => {
38 + const engine = new __test.LeaksEngine();
39 + const value = "sk-or-v1-" + "f".repeat(64);
40 + expect(engine.redactText(`${adversarial}\n${value}`, "provider").text).not.toContain(value);
41 + });
42 +});

extensions/leaks/__tests__/validation-batches.test.ts

Added
new file mode 100644
1 +import { existsSync, readFileSync } from "node:fs";
2 +import path from "node:path";
3 +import { describe, expect, it } from "vitest";
5 +const root = path.resolve(import.meta.dirname, "..");
6 +const tests = path.join(root, "__tests__");
8 +const batches = new Map([
9 + ["V0 scope", ["ledger.test.ts"]],
10 + ["V1 provider boundary", ["harness.test.ts"]],
11 + ["V2 placeholders", ["placeholders.test.ts", "selector.test.ts"]],
12 + ["V3 settings", ["settings.test.ts", "regex-fuzz.test.ts"]],
13 + ["V4 built-ins", ["detectors.test.ts"]],
14 + ["V5 learned values", ["dotenv.test.ts"]],
15 + ["V6 component parsers", ["structures.test.ts"]],
16 + ["V7 false positives", ["false-positives.test.ts"]],
17 + ["V8 UX", ["ux.test.ts"]],
18 + ["V9 performance/failure", ["performance.test.ts", "regex-fuzz.test.ts"]],
19 +]);
21 +describe("leaks validation batch wrapper", () => {
22 + it("maps every V0-V9 batch to executable test files", () => {
23 + for (const [batch, files] of batches) {
24 + expect(files.length, batch).toBeGreaterThan(0);
25 + for (const file of files) expect(existsSync(path.join(tests, file)), `${batch} ${file}`).toBe(true);
26 + }
27 + });
29 + it("keeps detector batches backed by positive and negative controls", () => {
30 + const detectorText = readFileSync(path.join(tests, "detectors.test.ts"), "utf8");
31 + const falsePositiveText = readFileSync(path.join(tests, "false-positives.test.ts"), "utf8");
32 + expect(detectorText).toContain("positives");
33 + expect(detectorText).toContain("negatives");
34 + expect(falsePositiveText).toContain("negatives");
35 + expect(falsePositiveText).toContain("custom regex override");
36 + });
38 + it("records regex fuzz evidence for runtime regex safety", () => {
39 + const fuzzText = readFileSync(path.join(tests, "regex-fuzz.test.ts"), "utf8");
40 + expect(fuzzText).toContain("runtimeRegexFuzzTargets");
41 + expect(fuzzText).toContain("adversarial");
42 + });
43 +});

extensions/leaks/index.ts

Modified
1 -import { builtinRules, findBuiltinSpans } from "./src/detectors.ts";
1 +import { builtinRegexFuzzTargets, builtinRules, findBuiltinSpans } from "./src/detectors.ts";
2 2 import {
3 3 discoverDotenvFiles,
4 4 highConfidenceSecretShape,
28 28 findPrivateKeySpans,
29 29 findSensitiveQuerySpans,
30 30 findStructureSpans,
31 + structureRegexFuzzTargets,
31 32 } from "./src/structures.ts";
32 33 import {
33 34 isExistingPlaceholder,
44 45 PlaceholderStore,
45 46 builtinRules,
46 47 findBuiltinSpans,
48 + runtimeRegexFuzzTargets: [...builtinRegexFuzzTargets, ...structureRegexFuzzTargets],
47 49 discoverDotenvFiles,
48 50 parseDotenv,
49 51 loadDotenvValues,

extensions/leaks/src/detectors.ts

Modified
31 31 rule("minisign-secret", "MINISIGN_SECRET_KEY", "private-key", /untrusted comment:\s*minisign secret key\s+RW[A-Za-z0-9+\/]{40,}/dgi, 0, "private-key", true),
34 +export const builtinRegexFuzzTargets = builtinRules.map((item) => ({
35 + name: `builtin:${item.id}`,
36 + regex: item.regex,
37 +}));
34 39 export function findBuiltinSpans(text: string): SecretSpan[] {
35 40 const spans: SecretSpan[] = [];
36 41 for (const item of builtinRules) {

extensions/leaks/src/structures.ts

Modified
7 7 const AUTH_RE = /Authorization:\s*(Bearer|Basic)\s+([^\s'"`]+)/dgi;
8 8 const X_API_RE = /\b(X-API-Key|X-Api-Key|Api-Key):\s*([^\s'"`]+)/dg;
9 9 const COOKIE_LINE_RE = /Cookie:\s*([^\n]+)/dgi;
10 -const ASSIGN_RE = /(?:export\s+)?["']?([A-Za-z_][A-Za-z0-9_-]*)["']?\s*(?:=|:|=>)\s*["']?([^\s'",;}]+)["']?/dgi;
11 -const XML_RE = /<([A-Za-z_][A-Za-z0-9_-]*)>([^<]+)<\/\1>/dgi;
10 +const ASSIGN_RE = /(?:^|[\n\r\s{,;])(?:export\s+)?["']?([A-Za-z_][A-Za-z0-9_-]*)["']?\s*(?:=|:|=>)\s*["']?([^\s'",;}]{1,512})["']?/dgi;
11 +const XML_RE = /<([A-Za-z_][A-Za-z0-9_-]*)>([^<]{1,512})<\/\1>/dgi;
12 12 const JWT_RE = /\b[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{8,}\b/dg;
13 13 const PRIVATE_KEY_RE = /-----BEGIN ([A-Z0-9 ]*PRIVATE KEY)-----[\s\S]*?-----END \1-----/dg;
15 +export const structureRegexFuzzTargets = [
16 + ["url-credential", URL_CREDENTIAL_RE],
17 + ["query", QUERY_RE],
18 + ["auth-header", AUTH_RE],
19 + ["api-header", X_API_RE],
20 + ["cookie-line", COOKIE_LINE_RE],
21 + ["assignment", ASSIGN_RE],
22 + ["xml", XML_RE],
23 + ["jwt", JWT_RE],
24 + ["private-key", PRIVATE_KEY_RE],
25 +].map(([name, regex]) => ({ name: `structure:${name}`, regex: regex as RegExp }));
15 27 export function findStructureSpans(text: string): SecretSpan[] {
16 28 return [
17 29 ...findPrivateKeySpans(text),