repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/leaks/__fuzz__/detectors.fuzz.test.ts
Rawimport { fc } from "@fast-check/vitest";
import { expect } from "vitest";
import { __test } from "../index.ts";
import { prop } from "./replay.ts";
const alphaNum = fc.constantFrom(
..."ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
);
const alphaNumText = (minLength: number, maxLength: number) =>
fc.string({ unit: alphaNum, minLength, maxLength });
const opaque = alphaNumText(8, 80).map((value) => `ordinary-${value}`);
const validJwt = fc
.record({
alg: alphaNumText(1, 16),
sub: alphaNumText(1, 40),
signature: fc
.uint8Array({ minLength: 6, maxLength: 48 })
.map((bytes) => Buffer.from(bytes).toString("base64url")),
})
.map(
({ alg, sub, signature }) =>
`${Buffer.from(JSON.stringify({ alg })).toString("base64url")}.${Buffer.from(
JSON.stringify({ sub }),
).toString("base64url")}.${signature}`,
);
function restored(text: string) {
const engine = new __test.LeaksEngine();
return engine.restoreText(engine.redactText(text, "provider").text);
}
prop("engine-determinism", { token: validJwt })(
"is deterministic across fresh engines",
({ token }) => {
const first = new __test.LeaksEngine();
const second = new __test.LeaksEngine();
const firstResult = first.redactText(token, "provider");
const secondResult = second.redactText(token, "provider");
expect(first.redactText(token, "provider").text).toBe(firstResult.text);
expect(second.redactText(token, "provider").text).toBe(secondResult.text);
expect(first.restoreText(firstResult.text)).toBe(token);
expect(second.restoreText(secondResult.text)).toBe(token);
},
);
prop("jwt-positive", { token: validJwt })(
"accepts valid JWT objects",
({ token }) => {
const spans = __test.findJwtSpans(token);
expect(spans.map((span) => span.value)).toEqual([token]);
expect(restored(`before ${token} after`)).toBe(`before ${token} after`);
},
);
prop("jwt-negative", {
parts: fc.tuple(
alphaNumText(10, 40),
alphaNumText(10, 40),
alphaNumText(8, 40),
),
})("rejects dotted identifiers", ({ parts }) => {
const value = parts.join(".");
fc.pre(!value.startsWith("ey"));
expect(__test.findJwtSpans(value)).toEqual([]);
});
prop("query", { value: opaque })("uses complete query names", ({ value }) => {
expect(__test.findSensitiveQuerySpans(`?monkey=${value}`)).toEqual([]);
expect(
__test.findSensitiveQuerySpans(`?token=${value}`).map((span) => span.value),
).toEqual([value]);
});
prop("cookie", { value: opaque })("uses complete cookie names", ({ value }) => {
const harmless = ["Cook", `ie: keyboard=${value}`].join("");
const sensitive = ["Cook", `ie: session=${value}`].join("");
expect(__test.findCookieSpans(harmless)).toEqual([]);
expect(__test.findCookieSpans(sensitive).map((span) => span.value)).toEqual([
value,
]);
});
prop("assignments", { value: opaque })(
"rejects ambiguous assignments",
({ value }) => {
for (const text of [
`TOKEN=${value}`,
`token: ${value}`,
`const STATUS_KEY = "${value}";`,
`const result = { session: ${value} };`,
]) {
expect(__test.findAssignmentSpans(text), text).toEqual([]);
}
},
);
prop("providers", {
family: fc.constantFrom(
["ghp_", 36] as const,
["npm_", 36] as const,
["hf_", 36] as const,
["sk_live_", 32] as const,
),
body: alphaNumText(36, 64),
})("keeps provider prefixes strict", ({ family: [prefix, length], body }) => {
const valid = prefix + body.slice(0, length);
expect(__test.findBuiltinSpans(valid)).toHaveLength(1);
expect(
__test.findBuiltinSpans(`x${prefix}${body.slice(0, length - 1)}`),
).toEqual([]);
});
prop("environment", { value: opaque })(
"does not learn environment metadata",
({ value }) => {
const learned = __test.learnEnvironmentValues({
GPG_KEY_ID: value,
SESSION_MANAGER: value,
PROD_ACCESS_TOKEN: value,
});
expect(learned.values.map((item) => item.value)).toEqual([value]);
},
);
prop("custom-capture", {
digits: fc.string({
unit: fc.constantFrom(..."0123456789"),
minLength: 1,
maxLength: 40,
}),
})("custom regex returns exact capture spans", ({ digits }) => {
const compiled = __test.compileCustomRegex({
regex: [{ pattern: "CUSTOM_([0-9]+)", label: "CUSTOM" }],
});
const text = `before CUSTOM_${digits} after`;
const span = __test.customRegexSpans(text, compiled.rules)[0];
expect(span.value).toBe(digits);
expect(text.slice(span.start, span.end)).toBe(digits);
});
prop("custom", { text: fc.string() })(
"custom regex never changes unmatched text",
({ text }) => {
const compiled = __test.compileCustomRegex({
regex: [{ pattern: "CUSTOM_[0-9]+", label: "CUSTOM" }],
});
fc.pre(!/CUSTOM_[0-9]+/.test(text));
expect(__test.customRegexSpans(text, compiled.rules)).toEqual([]);
},
);