pi-ext bugabingas pi extensions
owned by bugabinga
README Source History Refs Reviews Compare Notes Search Review guide CI runs feat(leaks): report redaction metadata
4b5050ca518b04507af2857278754ee2f77a9b00
Oliver Jan Krylow <oliver@bugabinga.net > · 11 Jun 2026
unsigned
Browse files Compare with HEAD
Changed files (6) Diff layout Auto Unified Split
+2 / −0 text lines
163 163 secret-internal transformations, high performance and correctness on every secret-internal transformations, high performance and correctness on every164 164 message, fail-open tolerance for faulty regex input, and human-friendly status message, fail-open tolerance for faulty regex input, and human-friendly status165 165 and notifications without exposing secrets and notifications without exposing secrets
166 + - Report leaks provider redaction paths, labels, and placeholders in safe167 + metadata and show unbounded path/label details in redaction notificationsextensions/leaks/__tests__/unit.test.ts Modified +48 / −0 text lines
22 22 changed : false , changed : false ,25 26 expect (engine.redactDeep (null , "provider" )).toEqual ({ expect (engine.redactDeep (null , "provider" )).toEqual ({26 27 value: null , value: null ,27 28 changed: false , changed: false ,30 32 const image = { type: "image" , data: fixtureSecret }; const image = { type: "image" , data: fixtureSecret };31 33 expect (engine.redactDeep (image, "provider" ).value ).toEqual (image); expect (engine.redactDeep (image, "provider" ).value ).toEqual (image);
43 + it ("reports provider redaction paths and labels without secret bytes" , () => {44 + const sensitiveValue = "alpha-secret" ;45 + const engine = new __test.LeaksEngine ({48 + const start = text.indexOf (sensitiveValue);49 + if (start === - 1 ) return [];53 + end: start + sensitiveValue.length ,54 + value: sensitiveValue,71 + const result = engine.redactDeep (72 + { messages: [{ content: `send ${sensitiveValue}` }] },76 + expect (JSON .stringify (result.value )).not.toContain (sensitiveValue);77 + expect (result.redactions).toEqual ([79 + path: "messages[0].content" ,80 + labels: ["ALPHA_SECRET" ],82 + expect.stringMatching (/ ^ __LEAK_ALPHA_SECRET_[a-f0-9] {12} __ $ / ),86 + expect (JSON .stringify (result.redactions)).not.toContain (sensitiveValue);41 89 it ("fails open when a detector throws" , () => { it ("fails open when a detector throws" , () => {42 90 const engine = new __test.LeaksEngine ({ const engine = new __test.LeaksEngine ({43 91 detectors: [ detectors: [extensions/leaks/__tests__/ux.test.ts Modified +24 / −0 text lines
30 30 ).toHaveLength (1 ); ).toHaveLength (1 );
33 + it ("notification reports every provider redaction path and label" , async () => {34 + const sensitiveValue = "tdd-secret-value-" + "x" .repeat (32 );35 + t = await createTestSession ({36 + extensions: [extPath ()],37 + env: { ["ALPHA_" + "SECRET" ]: sensitiveValue },39 + await t.session.extensionRunner.emitBeforeProviderRequest ({41 + { content: `first ${sensitiveValue}` },42 + { content: `second ${sensitiveValue}` },48 + .uiCallsFor ("notify" )49 + .map ((call) => String (call.args[0 ]))50 + .find ((item) => item.includes ("leaks redacted" )) ?? "" ;51 + expect (notice).toContain ("leaks redacted 2 provider value(s)" );52 + expect (notice).toContain ("messages[0].content: ENV_KEY" );53 + expect (notice).toContain ("messages[1].content: ENV_KEY" );54 + expect (notice).not.toContain (sensitiveValue);33 57 it ("status reports required safe fields" , async () => { it ("status reports required safe fields" , async () => {34 58 t = await createTestSession ({ extensions: [extPath ()] }); t = await createTestSession ({ extensions: [extPath ()] });35 59 await t.session.extensionRunner.emitBeforeProviderRequest ({ text: secret }); await t.session.extensionRunner.emitBeforeProviderRequest ({ text: secret });extensions/leaks/src/engine.ts Modified +34 / −7 text lines
20 20 EngineStats, EngineStats,21 21 LeaksSettings, LeaksSettings,22 22 RedactionOrigin, RedactionOrigin,23 24 RedactionResult, RedactionResult,24 25 SecretSpan, SecretSpan,25 26 WarningRecord, WarningRecord,79 80 "detector failure; redaction pass left unchanged" , "detector failure; redaction pass left unchanged" ,81 82 if (origin === "provider" ) this.lastProviderRedactionCount = 0 ; if (origin === "provider" ) this.lastProviderRedactionCount = 0 ;82 - return { text, count: 0 , labels: [], placeholders: [] };87 94 let redacted = text; let redacted = text;88 95 const placeholders: string [] = []; const placeholders: string [] = [];89 96 const labels: string [] = []; const labels: string [] = [];
97 + const redactions: RedactionRecord [] = [];90 98 for (const span of selected) { for (const span of selected) {
99 + const spanLabels = span.labels.map ((label) => label.tag);91 100 const placeholder = this.placeholders.create (span.value , span.labels); const placeholder = this.placeholders.create (span.value , span.labels);92 101 redacted = `${redacted. slice (0, span. start )}${placeholder}${redacted. slice (span.end)}` ; redacted = `${redacted. slice (0, span. start )}${placeholder}${redacted. slice (span.end)}` ;93 102 placeholders.push (placeholder); placeholders.push (placeholder);94 - labels.push (... span.labels.map ((label) => label.tag));103 + labels.push (... spanLabels);107 + placeholders: [placeholder],97 111 const count = selected.length ; const count = selected.length ;107 121 .replace (/_[a-f0-9] {12} (?:_\d + ) ? __ $ / , "" ), .replace (/_[a-f0-9] {12} (?:_\d + ) ? __ $ / , "" ),110 - return { text: redacted, count, labels, placeholders };124 + return { text: redacted, count, labels, placeholders, redactions };113 127 redactDeep <T >( redactDeep <T >(173 187 restore : boolean, restore : boolean,174 188 ): DeepRedactionResult< T> { ): DeepRedactionResult< T> {175 189 const seen = new WeakMap <object , unknown >(); const seen = new WeakMap <object , unknown >();
190 + const redactions: RedactionRecord[] = [];176 191 let count = 0 ; let count = 0 ;177 192 let changed = false ; let changed = false ;178 - const visit = (value: unknown , depth: number ): unknown => {193 + const visit = (value: unknown , depth: number , trace : string ): unknown => {179 194 if (typeof value === "string" ) { if (typeof value === "string" ) {180 195 if (restore) { if (restore) {181 196 const next = this.restoreText (value); const next = this.restoreText (value);188 203 const result = this.redactText (value, origin); const result = this.redactText (value, origin);189 204 if (result.text !== value) changed = true ; if (result.text !== value) changed = true ;190 205 count += result.count; count += result.count;
206 + for (const item of result.redactions)207 + redactions.push ({ ... item, path: trace || "$" });191 208 return result.text ; return result.text ;193 210 if (value === null || typeof value !== "object" ) return value; if (value === null || typeof value !== "object" ) return value;203 220 if (Array .isArray (value)) { if (Array .isArray (value)) {204 221 const copy: unknown [] = []; const copy: unknown [] = [];205 222 seen.set (value, copy); seen.set (value, copy);206 - for (const item of value) copy.push (visit (item, depth + 1 ));223 + for (let index = 0 ; index < value.length ; index++ )224 + copy.push (visit (value[index], depth + 1 , `${trace}[${index}]` ));207 225 if (copy.some ((item, index) => item !== value[index])) changed = true ; if (copy.some ((item, index) => item !== value[index])) changed = true ;208 226 return copy; return copy;212 230 const copy: Record <string , unknown > = {}; const copy: Record <string , unknown > = {};213 231 seen.set (value, copy); seen.set (value, copy);214 232 for (const [key, item] of Object .entries (source)) for (const [key, item] of Object .entries (source))215 - copy[key] = visit (item, depth + 1 );233 + copy[key] = visit (item, depth + 1 , childPath ( trace , key )) ;216 234 if (Object .keys (copy).some ((key) => copy[key] !== source[key])) if (Object .keys (copy).some ((key) => copy[key] !== source[key]))217 235 changed = true ; changed = true ;218 236 return copy; return copy;220 - return { value: visit (input, 0 ) as T , changed, count };238 + const value = visit (input, 0 , "" ) as T ;239 + if (origin === "provider" && ! restore)240 + this.lastProviderRedactionCount = count;241 + return { value, changed, count, redactions };223 244 private warnOnce (code: string, message: string) { private warnOnce (code: string, message: string) {230 251 this.warnings.push (warning); this.warnings.push (warning);
255 + function childPath (parent: string , key: string ) {256 + if (/ ^ [A-Za-z_$][A-Za-z0-9_$] *$ / .test (key))257 + return parent ? `${parent}.${key}` : key;258 + return `${parent}[${ JSON . stringify (key)}]` ;extensions/leaks/src/pi.ts Modified +13 / −5 text lines
20 20 const result = engine.redactDeep (event .payload, "provider" ); const result = engine.redactDeep (event .payload, "provider" );21 21 if (ctx.hasUI) { if (ctx.hasUI) {22 22 ctx.ui.setStatus ("leaks" , `leaks on | last ${result.count}` ); ctx.ui.setStatus ("leaks" , `leaks on | last ${result.count}` );23 - if (result.count > 0 )25 - `leaks redacted ${result.count} provider value(s)` ,23 + if (result.count > 0 ) ctx.ui.notify (formatNotice (result), "info" );29 25 return result.value ; return result.value ;
87 + function formatNotice (result: {89 + redactions: Array <{ path: string ; labels: string [] }>;92 + `leaks redacted ${result.count} provider value(s)` ,93 + ... result.redactions.map (94 + (redaction) => `${redaction.path}: ${redaction.labels. join (", ")}` ,91 99 function formatStatus (engine: LeaksEngine ) {function formatStatus (engine: LeaksEngine ) {92 100 const stats = engine.stats (); const stats = engine.stats ();extensions/leaks/src/types.ts Modified +8 / −0 text lines
40 40 export type RedactionOrigin = "context" | "provider" | "diagnostic" ;export type RedactionOrigin = "context" | "provider" | "diagnostic" ;
42 + export type RedactionRecord = {45 + placeholders: string [];42 48 export type RedactionResult = {export type RedactionResult = {43 49 text: string ; text: string ;44 50 count: number ; count: number ;45 51 labels: string []; labels: string [];46 52 placeholders: string []; placeholders: string [];
53 + redactions: RedactionRecord [];49 56 export type DeepRedactionResult <T = unknown > = {export type DeepRedactionResult <T = unknown > = {51 58 changed: boolean ; changed: boolean ;52 59 count: number ; count: number ;
60 + redactions: RedactionRecord [];55 63 export type EngineStats = {export type EngineStats = {