pi-ext
bugabingas pi extensions
owned by bugabinga
feat(leaks): bound redaction traversal and caps
486e0cdd2e79a160d79ba245b61f3275f1923cf5
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
Browse filesCompare with HEAD
Changed files (2)
extensions/leaks/__tests__/performance.test.ts
Addednew file mode 100644 1 +import { describe, expect, it }
from "vitest";
2 +import { __test }
from "../index.ts";
4 +const secret
= "sk-or-v1-" + "a".
repeat(
64);
6 +describe(
"leaks bounded traversal and failure behavior", ()
=> {
7 + it(
"deep redaction copies objects and preserves cycles", ()
=> {
8 + const source
: { text
: string; self
?: unknown; nested
: { list
: unknown[] } }
= {
10 + nested: { list: [secret] },
12 + source.
self = source;
13 + const engine
= new __test.
LeaksEngine();
15 + const result
= engine.
redactDeep(source,
"provider");
17 + expect(result.changed).
toBe(
true);
18 + expect(result.count).
toBe(
2);
19 + expect(result.
value).not.
toBe(source);
20 + expect(result.
value.
self).
toBe(result.
value);
21 + expect(result.
value.
text).not.
toBe(secret);
22 + expect(source.
text).
toBe(secret);
25 + it(
"passes through primitive and callable values", ()
=> {
26 + const engine
= new __test.
LeaksEngine();
27 + for (
const value
of [
1,
true,
null,
1n,
Symbol.
for(
"x"), ()
=> 1]) {
28 + expect(engine.
redactDeep(value,
"provider")).
toMatchObject({
36 + it(
"caps traversal depth at 64 and warns once", ()
=> {
37 + let root
: Record<
string,
unknown>
= {};
39 + for (
let index
= 0; index
< 70; index
++) {
41 + current
= current.
next as Record<
string,
unknown>;
43 + current.
text = secret;
44 + const engine
= new __test.
LeaksEngine();
46 + const result
= engine.
redactDeep(
root,
"provider");
48 + expect(result.count).
toBe(
0);
49 + expect(engine.
stats().warnings.
map((warning)
=> warning.
code)).
toContain(
"traversal.depth");
50 + expect(engine.
stats().warnings.
filter((warning)
=> warning.
code === "traversal.depth")).
toHaveLength(
1);
53 + it(
"restores deeply without mutating originals", ()
=> {
54 + const engine
= new __test.
LeaksEngine();
55 + const placeholder
= engine.
redactText(secret,
"provider").
text;
56 + const source
= { args: [placeholder] };
57 + const restored
= engine.
restoreDeep(source);
59 + expect(restored.
value.args[
0]).
toBe(secret);
60 + expect(source.args[
0]).
toBe(placeholder);
61 + expect(restored.count).
toBe(
1);
64 + it(
"keeps precompiled rule counts stable", ()
=> {
65 + const engine
= new __test.
LeaksEngine();
66 + const before
= engine.
stats().builtInRuleCount;
67 + for (
let index
= 0; index
< 50; index
++) engine.
redactText(secret,
"provider");
68 + expect(engine.
stats().builtInRuleCount).
toBe(before);
69 + expect(__test.builtinRules).
toHaveLength(before);
72 + it(
"detector throws fail open and warn once", ()
=> {
73 + const engine
= new __test.
LeaksEngine({
74 + detectors: [()
=> {
throw new Error(
"boom"); }],
76 + engine.
redactText(secret,
"provider");
77 + engine.
redactText(secret,
"provider");
79 + expect(engine.
stats().warnings.
filter((warning)
=> warning.
code === "detector.failure")).
toHaveLength(
1);
extensions/leaks/src/engine.ts
Modified 7 7 } from
"./dotenv.ts";
8 8 import { PlaceholderStore }
from "./placeholders.ts";
9 +import { isPlainObject }
from "./safety.ts";
9 10 import { selectSecretSpans }
from "./selector.ts";
10 11 import { findStructureSpans }
from "./structures.ts";
104 105 redactDeep<
T>(value: T, origin: RedactionOrigin
= "provider"): DeepRedactionResult
<T
> {
105 - if (typeof value !==
"string") return { value,
changed:
false,
count:
0 };
106 - const result
= this.
redactText(value, origin);
108 - value: result.
text as T,
109 - changed: result.
text !== value,
110 - count: result.count,
106 + return this.walk(value, origin, false);
114 109 restoreText(text: string) {
118 113 restoreDeep<
T>(value: T): DeepRedactionResult
<T
> {
119 - if (typeof value !==
"string") return { value,
changed:
false,
count:
0 };
120 - const restored
= this.
restoreText(value);
121 - return { value: restored
as T, changed: restored
!== value, count: restored
=== value
? 0 : 1 };
114 + return this.walk(value,
"provider", true);
160 153 for (
const warning
of [
...dotenv.warnings,
...processEnv.warnings]) this.
addWarning(warning);
156 + private
walk<
T>(input: T, origin: RedactionOrigin, restore: boolean): DeepRedactionResult
<T
> {
157 + const seen
= new WeakMap<
object,
unknown>();
159 + let changed
= false;
160 + const
visit = (value
: unknown, depth
: number)
: unknown => {
161 + if (
typeof value
=== "string") {
163 + const next
= this.
restoreText(value);
164 + if (next
!== value) {
170 + const result
= this.
redactText(value, origin);
171 + if (result.
text !== value) changed
= true;
172 + count
+= result.count;
173 + return result.
text;
175 + if (value
=== null || typeof value
!== "object")
return value;
177 + this.
warnOnce(
"traversal.depth",
"maximum redaction traversal depth reached");
180 + const cached
= seen.
get(value);
181 + if (cached)
return cached;
182 + if (
Array.
isArray(value)) {
183 + const copy
: unknown[]
= [];
184 + seen.
set(value, copy);
185 + for (
const item
of value) copy.
push(
visit(item, depth
+ 1));
186 + if (copy.
some((item, index)
=> item
!== value[index])) changed
= true;
189 + if (
!isPlainObject(value))
return value;
190 + const source
= value
as Record<
string,
unknown>;
191 + const copy
: Record<
string,
unknown>
= {};
192 + seen.
set(value, copy);
193 + for (
const [key, item]
of Object.
entries(source)) copy[key]
= visit(item, depth
+ 1);
194 + if (
Object.
keys(copy).
some((key)
=> copy[key]
!== source[key])) changed
= true;
197 + return { value:
visit(input,
0)
as T, changed, count };
163 200 private
warnOnce(code: string, message: string) {
164 201 if (this.warningKeys.
has(code))
return;
165 202 this.warningKeys.
add(code);