repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/leaks/__tests__/detectors.test.ts
RawFile outline
- validAgeKey line 4
- validMinisignSecret line 41
- expectRedacts line 47
- expectVisible line 54
- leaks built-in detectors line 59
- covers minimum positive detector families line 60
- bounds PuTTY private payloads line 85
- rejects an invalid EOF PuTTY payload line line 104
- redacts supported private-key material line 113
- accepts Base64URL tokens ending in alphabet punctuation line 127
- validates age checksums and minisign payload encoding line 141
- keeps opaque sensitive-name assignments visible line 153
- keeps explicit negative controls visible line 174
- exports a stable built-in rule count line 197
import { describe, expect, it } from "vitest";
import { __test } from "../index.ts";
function validAgeKey() {
const hrp = "age-secret-key-";
const charset = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
const data = Array.from({ length: 52 }, (_, index) => index % 32);
const values = [
...[...hrp].map((char) => char.charCodeAt(0) >> 5),
0,
...[...hrp].map((char) => char.charCodeAt(0) & 31),
...data,
0,
0,
0,
0,
0,
0,
];
let checksum = 1;
for (const item of values) {
const top = checksum >>> 25;
checksum = (((checksum & 0x1ffffff) << 5) ^ item) >>> 0;
for (let bit = 0; bit < 5; bit++)
if ((top >>> bit) & 1)
checksum =
(checksum ^
[0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3][
bit
]) >>>
0;
}
checksum ^= 1;
const tail = Array.from(
{ length: 6 },
(_, index) => (checksum >>> (5 * (5 - index))) & 31,
);
return `AGE-SECRET-KEY-1${[...data, ...tail].map((item) => charset[item]).join("")}`;
}
function validMinisignSecret() {
const record = Buffer.alloc(158, 7);
record.write("EdScB2", 0, "ascii");
return `untrusted comment: minisign encrypted secret key\n${record.toString("base64")}`;
}
function expectRedacts(text: string, label: string) {
const engine = new __test.LeaksEngine();
const result = engine.redactText(text, "provider");
expect(result.count, label).toBeGreaterThan(0);
expect(result.text, label).not.toBe(text);
}
function expectVisible(text: string, label: string) {
const engine = new __test.LeaksEngine();
expect(engine.redactText(text, "provider").text, label).toBe(text);
}
describe("leaks built-in detectors", () => {
it("covers minimum positive detector families", () => {
const databasePassword = "database-fixture-password-123456";
const positives = [
["ai-openrouter", "sk-or-v1-" + "a".repeat(64)],
["ai-openai", "sk-proj-" + "b".repeat(48)],
["vcs-github", "ghp_" + "A".repeat(36)],
["package-npm", "npm_" + "c".repeat(36)],
["cloud-aws-access", "AKIA" + "A".repeat(16)],
["cloud-google", "AIza" + "B".repeat(35)],
["payment-stripe", "sk_live_" + "c".repeat(32)],
["messaging-slack", "xoxb-" + "d".repeat(24)],
[
"webhook",
new URL(`/services/${"e".repeat(24)}`, "https://hooks.slack.com").href,
],
["mail", "SG." + "f".repeat(22) + "." + "g".repeat(43)],
["auth-cookie", "Cookie: session=session-fixture-secret-123456789"],
["oauth", "ya29." + "h".repeat(80)],
["db", `postgres://alice:${databasePassword}@db/app`],
["redis", `redis://:${databasePassword}@cache:6379/0`],
];
for (const [label, value] of positives) expectRedacts(value, label);
});
it("bounds PuTTY private payloads", () => {
const block = [
"PuTTY-User-Key-File-3: ssh-rsa",
"Encryption: none",
"Comment: fixture",
"Private-Lines: 2",
"QUJDRA==",
"RUZHSA==",
].join("\n");
const trailing = "ordinary trailing metadata";
const result = new __test.LeaksEngine().redactText(
`${block}\nPrivate-MAC: 00\n${trailing}`,
"provider",
);
expect(result.labels).toContain("PUTTY_PRIVATE_KEY");
expect(result.text).toContain(`Private-MAC: 00\n${trailing}`);
});
it("rejects an invalid EOF PuTTY payload line", () => {
const block = [
"PuTTY-User-Key-File-3: ssh-rsa",
"Private-Lines: 1",
"ordinary prose",
].join("\n");
expect(__test.findPrivateKeySpans(block)).toEqual([]);
});
it("redacts supported private-key material", () => {
const pgpLabel = "PGP PRIVATE KEY BLOCK";
const cosignLabel = "ENCRYPTED COSIGN PRIVATE KEY";
const blocks = [
`-----BEGIN ${pgpLabel}-----\nbody\n-----END ${pgpLabel}-----`,
`-----BEGIN ${cosignLabel}-----\nbody\n-----END ${cosignLabel}-----`,
"PuTTY-User-Key-File-3: ssh-rsa\nPrivate-Lines: 1\nbody",
validAgeKey(),
validMinisignSecret(),
];
for (const block of blocks) expectRedacts(block, block.slice(0, 16));
});
it("accepts Base64URL tokens ending in alphabet punctuation", () => {
expect(__test.findBuiltinSpans(`sk-proj-${"A".repeat(31)}-`)).toHaveLength(
1,
);
expect(
__test.findBuiltinSpans(`sk-ant-api03-${"A".repeat(31)}_`),
).toHaveLength(1);
expect(__test.findBuiltinSpans(`AIza${"A".repeat(34)}-`)).toHaveLength(1);
expect(
__test.findBuiltinSpans(`SG.${"A".repeat(16)}.${"A".repeat(31)}-`),
).toHaveLength(1);
expect(__test.findBuiltinSpans(`ya29.${"A".repeat(31)}-`)).toHaveLength(1);
});
it("validates age checksums and minisign payload encoding", () => {
const validAge = validAgeKey();
const invalidAge = `${validAge.slice(0, -1)}q`;
const validMinisign = validMinisignSecret();
const invalidMinisign = `${validMinisign}!`;
expect(__test.findBuiltinSpans(validAge.toUpperCase())).toHaveLength(1);
expect(__test.findBuiltinSpans(invalidAge)).toEqual([]);
expect(__test.findBuiltinSpans(validMinisign)).toHaveLength(1);
expect(__test.findBuiltinSpans(invalidMinisign)).toEqual([]);
});
it("keeps opaque sensitive-name assignments visible", () => {
for (const key of [
"KEY",
"KEY_DATA",
"PASS",
"PAT",
"PASSPHRASE",
"PEPPER",
"LICENSE_KEY",
"MASTER_KEY",
"ROUTING_KEY",
"ENCRYPTION_KEY",
"SECRET_KEY_BASE",
"UNSEAL_KEY",
"MNEMONIC",
]) {
const text = `${key}=assignment-fixture-secret-123456789`;
expectVisible(text, key);
}
});
it("keeps explicit negative controls visible", () => {
const negatives = [
["account", "account_id=123456789012"],
["customer", "customer_id=cus_1234567890"],
["org", "org_id=org_1234567890"],
["project", "project_id=proj_1234567890"],
["hostname", "db.internal.example.test"],
["domain", "example.com"],
["lineage", "lineage=alpha-beta-gamma-delta"],
["value", "value=opaque-but-not-secret-123456789"],
[
"ssh-public",
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMetadataOnlyKey user@example",
],
[
"certificate",
"-----BEGIN CERTIFICATE-----\nbody\n-----END CERTIFICATE-----",
],
];
for (const [label, value] of negatives) expectVisible(value, label);
});
it("exports a stable built-in rule count", () => {
expect(__test.builtinRules.length).toBeGreaterThanOrEqual(12);
expect(new __test.LeaksEngine().stats().builtInRuleCount).toBe(
__test.builtinRules.length,
);
});
});