repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/leaks/src/engine.ts
RawFile outline
- LeaksEngineOptions line 29
- LeaksEngine line 35
- settings line 36
- detectors line 37
- warnings line 38
- warningKeys line 39
- ephemeralEntries line 40
- placeholders line 41
- customRules line 44
- invalidRegexCount line 45
- learned line 46
- lastProviderRedactionCount line 47
- totalProviderRedactionCount line 48
- recentLabels line 49
- latestTags line 50
- notifiedPlaceholders line 51
- constructor line 53
- reload line 63
- redactText line 70
- redactDeep line 132
- configurePersistence line 139
- restoreText line 146
- hydrateToolInput line 150
- inspect line 156
- label line 160
- path line 160
- restoreDeep line 200
- clear line 204
- stats line 212
- compileSettings line 232
- learn line 239
- walk line 247
- visit line 256
- warnOnce line 319
- addWarning line 325
- placeholderLabel line 330
- childPath line 335
import { builtinRules, findBuiltinSpans } from "./detectors.ts";
import {
type LearnedValue,
learnEnvironmentValues,
learnedValueSpans,
loadDotenvValues,
} from "./dotenv.ts";
import { type PersistedEntry, PlaceholderStore } from "./placeholders.ts";
import { isPlainObject } from "./safety.ts";
import { selectSecretSpans } from "./selector.ts";
import {
type CompiledCustomRule,
compileCustomRegex,
customRegexSpans,
} from "./settings.ts";
import { findStructureSpans } from "./structures.ts";
import type {
DeepRedactionResult,
Detector,
EngineStats,
LeaksSettings,
RedactionOrigin,
RedactionRecord,
RedactionResult,
SecretSpan,
WarningRecord,
} from "./types.ts";
export type LeaksEngineOptions = {
settings?: Partial<LeaksSettings>;
env?: Record<string, string | undefined>;
detectors?: Detector[];
};
export class LeaksEngine {
settings: LeaksSettings;
private readonly detectors: Detector[];
private readonly warnings: WarningRecord[] = [];
private readonly warningKeys = new Set<string>();
private readonly ephemeralEntries: PersistedEntry[] = [];
private readonly placeholders = new PlaceholderStore({
onWarning: (warning) => this.addWarning(warning),
});
private customRules: CompiledCustomRule[] = [];
private invalidRegexCount = 0;
private learned: LearnedValue[] = [];
private lastProviderRedactionCount = 0;
private totalProviderRedactionCount = 0;
private recentLabels: string[] = [];
private latestTags: string[] = [];
readonly notifiedPlaceholders = new Set<string>();
constructor(options: LeaksEngineOptions = {}) {
this.placeholders.configure(this.ephemeralEntries, (customType, data) =>
this.ephemeralEntries.push({ type: "custom", customType, data }),
);
this.settings = { regex: options.settings?.regex ?? [] };
this.detectors = options.detectors ?? [];
this.compileSettings();
if (options.env) this.learn(process.cwd(), options.env);
}
reload(cwd = process.cwd(), settings = this.settings, env = process.env) {
this.settings = { regex: settings.regex };
this.compileSettings();
this.learn(cwd, env);
this.notifiedPlaceholders.clear();
}
redactText(
text: string,
origin: RedactionOrigin = "provider",
): RedactionResult {
let spans: SecretSpan[] = [
...findStructureSpans(text),
...findBuiltinSpans(text),
...customRegexSpans(text, this.customRules),
...learnedValueSpans(text, this.learned),
];
for (const detector of this.detectors) {
try {
spans = spans.concat(detector(text));
} catch {
this.warnOnce(
"detector.failure",
"detector failure; redaction pass left unchanged",
);
if (origin === "provider") this.lastProviderRedactionCount = 0;
return {
text,
count: 0,
labels: [],
placeholders: [],
redactions: [],
};
}
}
const selected = selectSecretSpans(text, spans);
let redacted = text;
const placeholders: string[] = [];
const labels: string[] = [];
const redactions: RedactionRecord[] = [];
for (const span of selected) {
const spanLabels = span.labels.map((label) => label.tag);
const placeholder = this.placeholders.create(span.value, span.labels);
if (!placeholder) continue;
redacted = `${redacted.slice(0, span.start)}${placeholder}${redacted.slice(span.end)}`;
placeholders.push(placeholder);
labels.push(...spanLabels);
redactions.push({
path: "",
labels: spanLabels,
placeholders: [placeholder],
});
}
const count = placeholders.length;
if (origin === "provider") {
this.lastProviderRedactionCount = count;
this.totalProviderRedactionCount += count;
}
if (labels.length > 0) this.recentLabels = labels.slice(-12);
if (placeholders.length > 0) {
this.latestTags = placeholders.map((placeholder) =>
placeholder.replace(/^__LEAK_V1_/, "").replace(/_[a-f0-9]{32}__$/, ""),
);
}
return { text: redacted, count, labels, placeholders, redactions };
}
redactDeep<T>(
value: T,
origin: RedactionOrigin = "provider",
): DeepRedactionResult<T> {
return this.walk(value, origin, false);
}
configurePersistence(
entries: PersistedEntry[],
append: (customType: string, data: unknown) => void,
) {
this.placeholders.configure(entries, append);
}
restoreText(text: string) {
return this.placeholders.restore(text);
}
hydrateToolInput<T>(value: T): {
value: T;
issue?: { path: string; label: string };
} {
const restored = this.restoreDeep(value).value;
const seen = new WeakSet<object>();
const inspect = (
item: unknown,
depth: number,
path: string,
): { path: string; label: string } | undefined => {
if (typeof item === "string") {
const unresolved = this.placeholders.unresolved(item)[0];
return unresolved
? { path: path || "$", label: placeholderLabel(unresolved) }
: undefined;
}
if (item === null || typeof item !== "object") return undefined;
if (depth >= 64) return { path: path || "$", label: "TRAVERSAL_DEPTH" };
if (seen.has(item))
return { path: path || "$", label: "TRAVERSAL_CYCLE" };
seen.add(item);
if (Array.isArray(item)) {
for (let index = 0; index < item.length; index++) {
const issue = inspect(item[index], depth + 1, `${path}[${index}]`);
if (issue) {
seen.delete(item);
return issue;
}
}
seen.delete(item);
return undefined;
}
if (!isPlainObject(item)) {
seen.delete(item);
return { path: path || "$", label: "UNSUPPORTED_OBJECT" };
}
for (const [key, child] of Object.entries(item)) {
const issue = inspect(child, depth + 1, childPath(path, key));
if (issue) {
seen.delete(item);
return issue;
}
}
seen.delete(item);
return undefined;
};
return { value: restored, issue: inspect(restored, 0, "") };
}
restoreDeep<T>(value: T): DeepRedactionResult<T> {
return this.walk(value, "provider", true);
}
clear() {
this.placeholders.clear();
this.lastProviderRedactionCount = 0;
this.recentLabels = [];
this.latestTags = [];
this.notifiedPlaceholders.clear();
}
stats(): EngineStats {
return {
active: true,
builtInRuleCount: builtinRules.length,
customRegexCount: this.customRules.length,
regexWarningCount: this.warnings.filter((warning) =>
warning.code.startsWith("settings.regex."),
).length,
warningCount: this.warnings.length,
invalidRegexCount: this.invalidRegexCount,
learnedCount: this.learned.length,
mappingCount: this.placeholders.stats().mappingCount,
lastProviderRedactionCount: this.lastProviderRedactionCount,
totalProviderRedactionCount: this.totalProviderRedactionCount,
recentLabels: [...this.recentLabels],
latestTags: [...this.latestTags],
warnings: [...this.warnings],
};
}
private compileSettings() {
const compiled = compileCustomRegex(this.settings);
this.customRules = compiled.rules;
this.invalidRegexCount = compiled.invalidRegexCount;
for (const warning of compiled.warnings) this.addWarning(warning);
}
private learn(cwd: string, env: Record<string, string | undefined>) {
const dotenv = loadDotenvValues(cwd);
const processEnv = learnEnvironmentValues(env);
this.learned = [...dotenv.values, ...processEnv.values].slice(0, 8192);
for (const warning of [...dotenv.warnings, ...processEnv.warnings])
this.addWarning(warning);
}
private walk<T>(
input: T,
origin: RedactionOrigin,
restore: boolean,
): DeepRedactionResult<T> {
const seen = new WeakMap<object, unknown>();
const redactions: RedactionRecord[] = [];
let count = 0;
let changed = false;
const visit = (value: unknown, depth: number, trace: string): unknown => {
if (typeof value === "string") {
if (restore) {
const next = this.restoreText(value);
if (next !== value) {
changed = true;
count++;
}
return next;
}
const result = this.redactText(value, origin);
if (result.text !== value) changed = true;
count += result.count;
for (const item of result.redactions)
redactions.push({ ...item, path: trace || "$" });
return result.text;
}
if (value === null || typeof value !== "object") return value;
if (depth >= 64) {
this.warnOnce(
"traversal.depth",
"maximum redaction traversal depth reached",
);
return value;
}
const cached = seen.get(value);
if (cached) {
this.warnOnce(
"traversal.cycle",
"cyclic value left unchanged during redaction traversal",
);
return cached;
}
if (Array.isArray(value)) {
const copy: unknown[] = [];
seen.set(value, copy);
for (let index = 0; index < value.length; index++)
copy.push(visit(value[index], depth + 1, `${trace}[${index}]`));
if (copy.some((item, index) => item !== value[index])) changed = true;
return copy;
}
if (!isPlainObject(value)) {
this.warnOnce(
"traversal.unsupported",
"unsupported object left unchanged during redaction traversal",
);
return value;
}
const source = value as Record<string, unknown>;
const copy: Record<string, unknown> = {};
seen.set(value, copy);
for (const [key, item] of Object.entries(source))
copy[key] = visit(item, depth + 1, childPath(trace, key));
if (Object.keys(copy).some((key) => copy[key] !== source[key]))
changed = true;
return copy;
};
const value = visit(input, 0, "") as T;
if (origin === "provider" && !restore)
this.lastProviderRedactionCount = count;
return { value, changed, count, redactions };
}
private warnOnce(code: string, message: string) {
if (this.warningKeys.has(code)) return;
this.warningKeys.add(code);
this.addWarning({ code, message, severity: "warning" });
}
private addWarning(warning: WarningRecord) {
this.warnings.push(warning);
}
}
function placeholderLabel(placeholder: string) {
const match = /^__LEAK_(?:V1_)?([A-Z0-9_]+)/.exec(placeholder);
return match?.[1]?.replace(/_+$/, "") ?? "UNKNOWN";
}
function childPath(parent: string, key: string) {
if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(key))
return parent ? `${parent}.${key}` : key;
return `${parent}[${JSON.stringify(key)}]`;
}