repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/leaks/__tests__/structures.test.ts
RawFile outline
- secret line 4
- jwt line 5
- privateKey line 10
- leaks structured component parsers line 13
- redacts credentialed URL passwords while preserving safe URL structure line 14
- redacts sensitive query values and preserves names line 24
- does not redact URLs without credentials even for database keys line 34
- redacts validated header values only line 41
- redacts sensitive cookie values only line 56
- redacts only provider-shaped assignment values line 68
- redacts shell header value without redacting whole command line 84
- requires exactly three decoded JWT object segments line 96
- rejects malformed structured contexts line 111
- uses complete query and cookie names line 126
- redacts JWTs and private keys as whole spans line 146
- skips assignment keys that are common programming nouns line 157
- does not redact sensitive-name code references line 193
- keeps ambiguous sensitive-name assignments visible line 210
- stoplist is case-insensitive for common programming nouns line 216
import { describe, expect, it } from "vitest";
import { __test } from "../index.ts";
const secret = ["structure", "fixture", "secret", "123456789"].join("-");
const jwt = [
"eyJhbGciOiJIUzI1NiJ9",
"eyJzdWIiOiIxMjM0NTY3ODkwIn0",
"c2lnbmF0dXJl",
].join(".");
const privateKey =
"-----BEGIN OPENSSH PRIVATE KEY-----\nfixture-private-key-body\n-----END OPENSSH PRIVATE KEY-----";
describe("leaks structured component parsers", () => {
it("redacts credentialed URL passwords while preserving safe URL structure", () => {
const engine = new __test.LeaksEngine();
const text = `DATABASE_URL=postgres://alice:${secret}@db/app`;
const result = engine.redactText(text, "provider");
expect(result.text).toContain("DATABASE_URL=postgres://alice:");
expect(result.text).toContain("@db/app");
expect(result.text.includes(secret)).toBe(false);
});
it("redacts sensitive query values and preserves names", () => {
const engine = new __test.LeaksEngine();
const text = `https://api.example.test/callback?token=${secret}&user=me`;
const result = engine.redactText(text, "provider");
expect(result.text).toContain(["token", "="].join(""));
expect(result.text).toContain("&user=me");
expect(result.text.includes(secret)).toBe(false);
});
it("does not redact URLs without credentials even for database keys", () => {
const engine = new __test.LeaksEngine();
const text =
"DATABASE_URL=postgres://db/app REDIS_URL=redis://cache MONGO_URL=mongodb://mongo/app";
expect(engine.redactText(text, "provider").text).toBe(text);
});
it("redacts validated header values only", () => {
const engine = new __test.LeaksEngine();
const basic = Buffer.from("alice:password").toString("base64");
const text = [
["Authorization", ` Bearer ${secret}`].join(":"),
["Authorization", ` Basic ${basic}`].join(":"),
["X-API-Key", ` ${secret}`].join(":"),
].join("\n");
const result = engine.redactText(text, "provider");
expect(result.text).not.toContain(secret);
expect(result.text).not.toContain(basic);
expect(result.count).toBe(3);
});
it("redacts sensitive cookie values only", () => {
const engine = new __test.LeaksEngine();
const result = engine.redactText(
`Cookie: session=${secret}; theme=dark`,
"provider",
);
expect(result.text).toContain("Cookie: session=");
expect(result.text).toContain("; theme=dark");
expect(result.text.includes(secret)).toBe(false);
});
it("redacts only provider-shaped assignment values", () => {
const engine = new __test.LeaksEngine();
const shaped = "ghp_" + "A".repeat(36);
for (const sample of [
`TOKEN=${shaped}`,
`token: ${shaped}`,
`"token": "${shaped}"`,
`<token>${shaped}</token>`,
]) {
expect(engine.redactText(sample, "provider").text).not.toContain(shaped);
}
expect(engine.redactText(`TOKEN=${secret}`, "provider").text).toContain(
secret,
);
});
it("redacts shell header value without redacting whole command", () => {
const engine = new __test.LeaksEngine();
const result = engine.redactText(
`curl -H 'Authorization: Bearer ${secret}' https://example.test`,
"provider",
);
expect(result.text).toContain("curl -H '");
expect(result.text).toContain("https://example.test");
expect(result.text.includes(secret)).toBe(false);
});
it("requires exactly three decoded JWT object segments", () => {
const dotted = ["agendaFlick", "visibleArea", "yPosition"].join(".");
const shortPayload = [
Buffer.from(JSON.stringify({ alg: "A" })).toString("base64url"),
Buffer.from(JSON.stringify({})).toString("base64url"),
Buffer.from([0]).toString("base64url"),
].join(".");
expect(__test.findJwtSpans(dotted)).toEqual([]);
expect(__test.findJwtSpans(`${jwt}.extra`)).toEqual([]);
expect(__test.findJwtSpans(shortPayload).map((span) => span.value)).toEqual(
[shortPayload],
);
expect(__test.findJwtSpans(jwt).map((span) => span.value)).toEqual([jwt]);
});
it("rejects malformed structured contexts", () => {
const ordinary = ["ordinary", "metadata", "value", "123456"].join("-");
expect(
__test.findSensitiveQuerySpans(
["https://[bad/?tok", `en=${ordinary}`].join(""),
),
).toEqual([]);
expect(
__test.findCookieSpans(["Set-Cook", `ie: session=${ordinary}`].join("")),
).toEqual([]);
expect(
__test.findHeaderSpans(["Authorization", ` Basic ${ordinary}`].join(":")),
).toEqual([]);
});
it("uses complete query and cookie names", () => {
const ordinary = ["ordinary", "metadata", "value", "123456"].join("-");
expect(
__test.findSensitiveQuerySpans(["?mon", `key=${ordinary}`].join("")),
).toEqual([]);
expect(
__test.findCookieSpans(["Cookie", `: keyboard=${ordinary}`].join("")),
).toEqual([]);
expect(
__test
.findSensitiveQuerySpans(`?token=${secret}`)
.map((span) => span.value),
).toEqual([secret]);
expect(
__test
.findCookieSpans(["Cookie", `: session=${secret}`].join(""))
.map((span) => span.value),
).toEqual([secret]);
});
it("redacts JWTs and private keys as whole spans", () => {
const engine = new __test.LeaksEngine();
const jwtResult = engine.redactText(`jwt=${jwt}`, "provider");
const keyResult = engine.redactText(privateKey, "provider");
expect(jwtResult.text).toMatch(/^jwt=__LEAK_JWT_[a-f0-9]{12}__$/);
expect(keyResult.text).toMatch(
/^__LEAK_OPENSSH_PRIVATE_KEY_[a-f0-9]{12}__$/,
);
});
it("skips assignment keys that are common programming nouns", () => {
const longValue = "abcdef1234567890";
for (const key of [
"seed",
"id",
"name",
"count",
"type",
"value",
"path",
"ts",
"time",
"date",
"idx",
"mode",
"flag",
"opts",
"params",
"args",
"options",
"label",
"field",
"tag",
"node",
"host",
"port",
"version",
]) {
const text = `${key}=${longValue}`;
expect(
__test.findAssignmentSpans(text),
`expected ${key}=<value> to produce zero spans`,
).toEqual([]);
}
});
it("does not redact sensitive-name code references", () => {
const engine = new __test.LeaksEngine();
const samples = [
"const token = loadToken(email);",
"const key = match[1] as string;",
"console.log(` access_token: ${token.access_token ? 'present' : 'missing'}`);",
"const key = `${redaction.path}: ${redaction.labels.join(', ')}`;",
"let session: BaseTestSession;",
"const value: ${account.hasAccessToken = token.access_token;",
];
for (const sample of samples) {
expect(engine.redactText(sample, "provider").text, sample).toBe(sample);
expect(__test.findAssignmentSpans(sample), sample).toEqual([]);
}
});
it("keeps ambiguous sensitive-name assignments visible", () => {
const ordinary = "abcdef1234567890";
expect(__test.findAssignmentSpans(`KEY=${ordinary}`)).toEqual([]);
expect(__test.findAssignmentSpans(`TOKEN=${ordinary}`)).toEqual([]);
});
it("stoplist is case-insensitive for common programming nouns", () => {
const longValue = "abcdef1234567890";
for (const key of ["Seed", "SEED", "Id", "ID", "Name", "Type"]) {
const text = `${key}=${longValue}`;
expect(
__test.findAssignmentSpans(text),
`expected ${key}=<value> to produce zero spans`,
).toEqual([]);
}
});
});