repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/leaks/__tests__/settings.test.ts
RawFile outline
- fixtureSecret line 4
- leaks settings and custom regex line 6
- accepts only leaks.regex line 7
- warns for unsupported regex entry fields and ignores flags line 25
- accepts 512-character patterns and rejects 513-character patterns line 49
- accepts strict labels and falls back for missing or invalid labels line 62
- rejects invalid regex syntax line 82
- redacts full match for zero captures line 92
- redacts group 1 for one capture line 107
- redacts group 1 for multiple captures and warns line 125
- rejects cheap unsafe regex constructs line 145
- keeps warning text secret-free line 167
import { describe, expect, it } from "vitest";
import { __test } from "../index.ts";
const fixtureSecret = "foo_secret_fixture_value_123456789";
describe("leaks settings and custom regex", () => {
it("accepts only leaks.regex", () => {
const loaded = __test.loadLeaksSettings({
leaks: {
enabled: false,
keywords: ["nope"],
exclude: ["nope"],
secretFiles: ["nope"],
regex: [{ pattern: "foo_[a-z]+", label: "FOO_TOKEN" }],
},
});
expect(loaded.settings).toEqual({
regex: [{ pattern: "foo_[a-z]+", label: "FOO_TOKEN" }],
});
expect(JSON.stringify(loaded.settings)).not.toContain("enabled");
expect(JSON.stringify(loaded.settings)).not.toContain("secretFiles");
});
it("warns for unsupported regex entry fields and ignores flags", () => {
const loaded = __test.loadLeaksSettings({
leaks: {
regex: [
{
pattern: "foo_[a-z]+",
label: "FOO_TOKEN",
flags: "i",
extra: true,
},
],
},
});
expect(loaded.warnings.map((warning) => warning.code)).toEqual([
"settings.regex.unsupported-field",
"settings.regex.flags",
]);
expect(loaded.settings.regex[0]).toEqual({
pattern: "foo_[a-z]+",
label: "FOO_TOKEN",
});
});
it("accepts 512-character patterns and rejects 513-character patterns", () => {
const accepted = __test.compileCustomRegex({
regex: [{ pattern: "a".repeat(512), label: "LONG_TOKEN" }],
});
const rejected = __test.compileCustomRegex({
regex: [{ pattern: "a".repeat(513), label: "LONG_TOKEN" }],
});
expect(accepted.rules).toHaveLength(1);
expect(rejected.rules).toHaveLength(0);
expect(rejected.invalidRegexCount).toBe(1);
});
it("accepts strict labels and falls back for missing or invalid labels", () => {
const compiled = __test.compileCustomRegex({
regex: [
{ pattern: "foo_[a-z]+", label: "FOO_TOKEN" },
{ pattern: "bar_[a-z]+" },
{ pattern: "baz_[a-z]+", label: "bad-label" },
],
});
expect(compiled.rules.map((rule) => rule.label)).toEqual([
"FOO_TOKEN",
"CUSTOM_REGEX",
"CUSTOM_REGEX",
]);
expect(compiled.warnings.map((warning) => warning.code)).toEqual([
"settings.regex.missing-label",
"settings.regex.invalid-label",
]);
});
it("rejects invalid regex syntax", () => {
const compiled = __test.compileCustomRegex({
regex: [{ pattern: "(", label: "BROKEN" }],
});
expect(compiled.rules).toEqual([]);
expect(compiled.invalidRegexCount).toBe(1);
expect(compiled.warnings[0].code).toBe("settings.regex.syntax");
});
it("redacts full match for zero captures", () => {
const engine = new __test.LeaksEngine({
settings: {
regex: [
{ pattern: "foo_secret_fixture_value_[0-9]+", label: "FOO_TOKEN" },
],
},
});
const result = engine.redactText(`send ${fixtureSecret}`, "provider");
expect(result.text).not.toContain(fixtureSecret);
expect(result.labels).toEqual(["FOO_TOKEN"]);
});
it("redacts group 1 for one capture", () => {
const engine = new __test.LeaksEngine({
settings: {
regex: [
{
pattern: "token=(foo_secret_fixture_value_[0-9]+)",
label: "FOO_TOKEN",
},
],
},
});
const result = engine.redactText(`token=${fixtureSecret}`, "provider");
expect(result.text).toContain("token=");
expect(result.text).not.toContain(fixtureSecret);
});
it("redacts group 1 for multiple captures and warns", () => {
const engine = new __test.LeaksEngine({
settings: {
regex: [
{
pattern: "(foo_secret_fixture_value_[0-9]+)-(tail)",
label: "FOO_TOKEN",
},
],
},
});
const result = engine.redactText(`${fixtureSecret}-tail`, "provider");
expect(result.text).toMatch(/^__LEAK_V1_FOO_TOKEN_[a-f0-9]{32}__-tail$/);
expect(engine.stats().warnings.map((warning) => warning.code)).toContain(
"settings.regex.multiple-captures",
);
});
it("rejects cheap unsafe regex constructs", () => {
const patterns = [
"(foo)\\1",
"(?=foo)foo",
"(foo+)+",
"(foo|bar)+",
"foo(|bar)",
".*secret.*",
];
const compiled = __test.compileCustomRegex({
regex: patterns.map((pattern) => ({ pattern, label: "UNSAFE" })),
});
expect(compiled.rules).toHaveLength(0);
expect(compiled.invalidRegexCount).toBe(patterns.length);
expect(
compiled.warnings.every(
(warning) => warning.code === "settings.regex.unsafe",
),
).toBe(true);
});
it("keeps warning text secret-free", () => {
const compiled = __test.compileCustomRegex({
regex: [{ pattern: `(${fixtureSecret}`, label: "BROKEN" }],
});
expect(
compiled.warnings.map((warning) => warning.message).join("\n"),
).not.toContain(fixtureSecret);
});
});