repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/leaks/__tests__/placeholders.test.ts
RawFile outline
- secretA line 6
- secretB line 7
- label line 9
- persistentStore line 17
- leaks placeholder store line 28
- persists tagged V1 HMAC32 mappings before emission line 29
- reconstructs authenticated mappings from the active branch line 41
- uses random generation keys and stable same-value mappings line 51
- orders and bounds tags line 64
- leaves colliding and overflow values visible line 81
- rejects collisions before the cap line 96
- disables new redaction when authentication fails line 109
- leaves values visible when append fails line 127
- retains hydration but disables creation after failed clear line 137
- retains authenticated mappings when an unsupported version disables creation line 157
- rejects orphan, over-capacity, and reverse-conflicting records line 176
- lets a later valid generation shadow unsupported history line 210
- rejects noncanonical persisted encodings and malformed bounds line 231
- restores exact known placeholders and reports unknown ones line 254
import { describe, expect, it } from "vitest";
import { __test } from "../index.ts";
import type { PersistedEntry } from "../src/placeholders.ts";
import type { SpanLabel } from "../src/types.ts";
const secretA = "placeholder-fixture-secret-A-123456";
const secretB = "placeholder-fixture-secret-B-123456";
function label(
tag: string,
priority: number,
source: SpanLabel["source"],
): SpanLabel {
return { tag, priority, source };
}
function persistentStore(
options: ConstructorParameters<typeof __test.PlaceholderStore>[0] = {},
) {
const entries: PersistedEntry[] = [];
const store = new __test.PlaceholderStore(options);
store.configure(entries, (customType, data) =>
entries.push({ type: "custom", customType, data }),
);
return { entries, store };
}
describe("leaks placeholder store", () => {
it("persists tagged V1 HMAC32 mappings before emission", () => {
const { entries, store } = persistentStore();
const placeholder = store.create(secretA, [label("API_KEY", 10, "dotenv")]);
expect(placeholder).toMatch(/^__LEAK_V1_API_KEY_[a-f0-9]{32}__$/);
expect(JSON.stringify(entries)).not.toContain(secretA);
expect(entries.map((entry) => entry.customType)).toEqual([
"leaks-generation-v1",
"leaks-mapping-v1",
]);
});
it("reconstructs authenticated mappings from the active branch", () => {
const { entries, store } = persistentStore();
const placeholder = store.create(secretA, [label("TOKEN", 10, "dotenv")]);
const resumed = new __test.PlaceholderStore();
resumed.configure(entries, () => {});
expect(resumed.restore(`run ${placeholder}`)).toBe(`run ${secretA}`);
expect(resumed.stats().mappingCount).toBe(1);
});
it("uses random generation keys and stable same-value mappings", () => {
const left = persistentStore().store;
const right = persistentStore().store;
const first = left.create(secretA, [label("FIRST", 10, "dotenv")]);
const repeated = left.create(secretA, [label("SECOND", 90, "builtin")]);
const otherGeneration = right.create(secretA, [
label("FIRST", 10, "dotenv"),
]);
expect(repeated).toBe(first);
expect(otherGeneration).not.toBe(first);
});
it("orders and bounds tags", () => {
const { store } = persistentStore({ digest: () => "0123456789ab" });
const placeholder = store.create(secretA, [
label("GENERIC", 50, "generic"),
label("BUILTIN", 40, "builtin"),
label("CUSTOM", 30, "custom"),
label("STRUCTURE", 20, "structure"),
label("ENV_KEY", 10, "env"),
label("EXTRA", 60, "generic"),
label("x".repeat(80), 5, "dotenv"),
]);
expect(placeholder).toBe(
`__LEAK_V1_${"X".repeat(64)}_ENV_KEY_STRUCTURE_CUSTOM_BUILTIN_GENERIC_0123456789ab00000000000000000000__`,
);
});
it("leaves colliding and overflow values visible", () => {
const warnings: string[] = [];
const { store } = persistentStore({
digest: () => "abcdef123456",
cap: 1,
onWarning: (warning) => warnings.push(warning.code),
});
const first = store.create(secretA, [label("TOKEN", 10, "dotenv")]);
const collision = store.create(secretB, [label("TOKEN", 10, "dotenv")]);
expect(first).toBeDefined();
expect(collision).toBeUndefined();
expect(warnings).toEqual(["placeholder.cap"]);
});
it("rejects collisions before the cap", () => {
const warnings: string[] = [];
const { store } = persistentStore({
digest: () => "abcdef123456",
onWarning: (warning) => warnings.push(warning.code),
});
store.create(secretA, [label("TOKEN", 10, "dotenv")]);
expect(
store.create(secretB, [label("TOKEN", 10, "dotenv")]),
).toBeUndefined();
expect(warnings).toEqual(["placeholder.collision"]);
});
it("disables new redaction when authentication fails", () => {
const { entries, store } = persistentStore();
const placeholder = store.create(secretA, [label("TOKEN", 10, "dotenv")]);
const mapping = entries[1].data as { authTag: string };
mapping.authTag = `${mapping.authTag[0] === "A" ? "B" : "A"}${mapping.authTag.slice(1)}`;
const warnings: string[] = [];
const resumed = new __test.PlaceholderStore({
onWarning: (warning) => warnings.push(warning.code),
});
resumed.configure(entries, () => {});
expect(resumed.restore(placeholder as string)).toBe(placeholder);
expect(
resumed.create(secretB, [label("TOKEN", 10, "dotenv")]),
).toBeUndefined();
expect(warnings).toContain("placeholder.state");
});
it("leaves values visible when append fails", () => {
const store = new __test.PlaceholderStore();
store.configure([], () => {
throw new Error("disk full");
});
expect(
store.create(secretA, [label("TOKEN", 10, "dotenv")]),
).toBeUndefined();
});
it("retains hydration but disables creation after failed clear", () => {
const entries: PersistedEntry[] = [];
let fail = false;
const store = new __test.PlaceholderStore();
store.configure(entries, (customType, data) => {
if (fail) throw new Error("disk full");
entries.push({ type: "custom", customType, data });
});
const placeholder = store.create(secretA, [
label("TOKEN", 10, "dotenv"),
]) as string;
fail = true;
store.clear();
expect(store.restore(placeholder)).toBe(secretA);
expect(
store.create(secretB, [label("TOKEN", 10, "dotenv")]),
).toBeUndefined();
});
it("retains authenticated mappings when an unsupported version disables creation", () => {
const { entries, store } = persistentStore();
const placeholder = store.create(secretA, [
label("TOKEN", 10, "dotenv"),
]) as string;
entries.push({
type: "custom",
customType: "leaks-generation-v2",
data: { version: 2 },
});
const resumed = new __test.PlaceholderStore();
resumed.configure(entries, () => {});
expect(resumed.restore(placeholder)).toBe(secretA);
expect(
resumed.create(secretB, [label("TOKEN", 10, "dotenv")]),
).toBeUndefined();
});
it("rejects orphan, over-capacity, and reverse-conflicting records", () => {
const { entries, store } = persistentStore();
store.create(secretA, [label("FIRST", 10, "dotenv")]);
const orphan = new __test.PlaceholderStore();
orphan.configure(entries.slice(1), () => {});
expect(orphan.stats().persistenceEnabled).toBe(false);
store.create(secretB, [label("SECOND", 10, "dotenv")]);
const duplicate = new __test.PlaceholderStore({ cap: 1 });
duplicate.configure([entries[0], entries[1], entries[1]], () => {});
expect(duplicate.stats()).toMatchObject({
mappingCount: 1,
persistenceEnabled: true,
});
const capped = new __test.PlaceholderStore({ cap: 1 });
capped.configure(entries, () => {});
expect(capped.stats()).toMatchObject({
mappingCount: 1,
persistenceEnabled: false,
});
const siblingEntries = [structuredClone(entries[0])];
const sibling = new __test.PlaceholderStore();
sibling.configure(siblingEntries, (customType, data) =>
siblingEntries.push({ type: "custom", customType, data }),
);
sibling.create(secretA, [label("SECOND", 10, "dotenv")]);
const conflict = new __test.PlaceholderStore();
conflict.configure([entries[0], entries[1], siblingEntries[1]], () => {});
expect(conflict.stats().persistenceEnabled).toBe(false);
});
it("lets a later valid generation shadow unsupported history", () => {
const { entries, store } = persistentStore();
const placeholder = store.create(secretA, [
label("TOKEN", 10, "dotenv"),
]) as string;
const resumed = new __test.PlaceholderStore();
resumed.configure(
[
{
type: "custom",
customType: "leaks-generation-v2",
data: { version: 2 },
},
...entries,
],
() => {},
);
expect(resumed.stats().persistenceEnabled).toBe(true);
expect(resumed.restore(placeholder)).toBe(secretA);
});
it("rejects noncanonical persisted encodings and malformed bounds", () => {
const { entries, store } = persistentStore();
store.create(secretA, [label("TOKEN", 10, "dotenv")]);
const mutations = [
(data: Record<string, unknown>) => {
data.nonce = `${data.nonce}=`;
},
(data: Record<string, unknown>) => {
data.authTag = "AA";
},
(data: Record<string, unknown>) => {
data.placeholder = `${data.placeholder}suffix`;
},
];
for (const mutate of mutations) {
const copy = structuredClone(entries);
mutate(copy[1].data as Record<string, unknown>);
const resumed = new __test.PlaceholderStore();
resumed.configure(copy, () => {});
expect(resumed.stats().persistenceEnabled).toBe(false);
}
});
it("restores exact known placeholders and reports unknown ones", () => {
const { store } = persistentStore();
const placeholder = store.create(secretA, [
label("TOKEN", 10, "dotenv"),
]) as string;
const unknown = "__LEAK_TOKEN_abcdef123456__";
expect(store.restore(`run ${placeholder}`)).toBe(`run ${secretA}`);
expect(store.restore(placeholder.slice(0, -2))).not.toContain(secretA);
expect(store.unresolved(unknown)).toEqual([unknown]);
});
});