@@ -212,6 +212,122 @@ describe("FilesystemWatcher", () => {
212212 expect ( content ) . not . toContain ( "plaintext-token-value" ) ;
213213 } ) ;
214214
215+ it ( "collapses multi-line PEM private-key blocks while keeping BEGIN/END markers" , async ( ) => {
216+ const dashes = "-" . repeat ( 5 ) ;
217+ const rsaBegin = `${ dashes } BEGIN RSA PRIVATE KEY${ dashes } ` ;
218+ const rsaEnd = `${ dashes } END RSA PRIVATE KEY${ dashes } ` ;
219+ const sshBegin = `${ dashes } BEGIN OPENSSH PRIVATE KEY${ dashes } ` ;
220+ const sshEnd = `${ dashes } END OPENSSH PRIVATE KEY${ dashes } ` ;
221+ writeFileSync (
222+ join ( root , "id_rsa.txt" ) ,
223+ [
224+ rsaBegin ,
225+ "MIIEowIBAAKCAQEAuRFakeRsaBodyLine1ShouldNeverLeakToObservationPipeline" ,
226+ "MoreFakeBase64BodyForRsaKeyMaterialThatMustStayRedacted" ,
227+ "YetAnotherSecretLineOfBase64KeyContentNoOneShouldRead" ,
228+ rsaEnd ,
229+ "" ,
230+ sshBegin ,
231+ "b3BlbnNzaC1mYWtlLWtleS1ib2R5LWxpbmUtb25l" ,
232+ "b3BlbnNzaC1mYWtlLWtleS1ib2R5LWxpbmUtdHdv" ,
233+ sshEnd ,
234+ "" ,
235+ ] . join ( "\n" ) ,
236+ ) ;
237+ const w = new FilesystemWatcher ( {
238+ roots : [ root ] ,
239+ baseUrl : "http://localhost:3111" ,
240+ logger : { info : vi . fn ( ) , warn : vi . fn ( ) , error : vi . fn ( ) } ,
241+ } ) ;
242+
243+ await w . flush ( root , "id_rsa.txt" ) ;
244+
245+ expect ( captured ) . toHaveLength ( 1 ) ;
246+ const content = ( captured [ 0 ] . body as { data : { content : string } } ) . data . content ;
247+ expect ( content ) . toContain ( rsaBegin ) ;
248+ expect ( content ) . toContain ( rsaEnd ) ;
249+ expect ( content ) . toContain ( sshBegin ) ;
250+ expect ( content ) . toContain ( sshEnd ) ;
251+ expect ( content ) . toContain ( "[REDACTED]" ) ;
252+ expect ( content ) . not . toContain ( "MIIEowIBAAKCAQEAuRFakeRsaBodyLine1" ) ;
253+ expect ( content ) . not . toContain ( "MoreFakeBase64BodyForRsaKeyMaterial" ) ;
254+ expect ( content ) . not . toContain ( "YetAnotherSecretLineOfBase64KeyContent" ) ;
255+ expect ( content ) . not . toContain ( "b3BlbnNzaC1mYWtlLWtleS1ib2R5LWxpbmUtb25l" ) ;
256+ expect ( content ) . not . toContain ( "b3BlbnNzaC1mYWtlLWtleS1ib2R5LWxpbmUtdHdv" ) ;
257+ } ) ;
258+
259+ it ( "redacts inline PEM blocks embedded in single-line JSON values" , async ( ) => {
260+ const dashes = "-" . repeat ( 5 ) ;
261+ const pemBegin = `${ dashes } BEGIN PRIVATE KEY${ dashes } ` ;
262+ const pemEnd = `${ dashes } END PRIVATE KEY${ dashes } ` ;
263+ const inlinePem = `${ pemBegin } \\nMIIEvgIBADANBgkqhkiG9w0FakeServiceAccountBody\\n${ pemEnd } ` ;
264+ writeFileSync (
265+ join ( root , "service-account.json" ) ,
266+ `{\n "type": "service_account",\n "private_key": "${ inlinePem } ",\n "client_email": "demo@example.com"\n}\n` ,
267+ ) ;
268+ const w = new FilesystemWatcher ( {
269+ roots : [ root ] ,
270+ baseUrl : "http://localhost:3111" ,
271+ logger : { info : vi . fn ( ) , warn : vi . fn ( ) , error : vi . fn ( ) } ,
272+ } ) ;
273+
274+ await w . flush ( root , "service-account.json" ) ;
275+
276+ expect ( captured ) . toHaveLength ( 1 ) ;
277+ const content = ( captured [ 0 ] . body as { data : { content : string } } ) . data . content ;
278+ expect ( content ) . toContain ( pemBegin ) ;
279+ expect ( content ) . toContain ( pemEnd ) ;
280+ expect ( content ) . toContain ( "[REDACTED]" ) ;
281+ expect ( content ) . not . toContain ( "MIIEvgIBADANBgkqhkiG9w0FakeServiceAccountBody" ) ;
282+ expect ( content ) . toContain ( '"client_email": "demo@example.com"' ) ;
283+ } ) ;
284+
285+ it ( "redacts standalone JWT-looking strings outside Bearer context" , async ( ) => {
286+ const jwt =
287+ "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" ;
288+ writeFileSync (
289+ join ( root , "notes.txt" ) ,
290+ [ "session token below:" , jwt , "end of token" ] . join ( "\n" ) ,
291+ ) ;
292+ const w = new FilesystemWatcher ( {
293+ roots : [ root ] ,
294+ baseUrl : "http://localhost:3111" ,
295+ logger : { info : vi . fn ( ) , warn : vi . fn ( ) , error : vi . fn ( ) } ,
296+ } ) ;
297+
298+ await w . flush ( root , "notes.txt" ) ;
299+
300+ expect ( captured ) . toHaveLength ( 1 ) ;
301+ const content = ( captured [ 0 ] . body as { data : { content : string } } ) . data . content ;
302+ expect ( content ) . toContain ( "[REDACTED]" ) ;
303+ expect ( content ) . not . toContain ( jwt ) ;
304+ expect ( content ) . toContain ( "end of token" ) ;
305+ } ) ;
306+
307+ it ( "does not redact base64-looking words that are not three-segment JWTs of sufficient length" , async ( ) => {
308+ const notJwt = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
309+ const shortThreeSegment = "eyJabc.def.ghi" ;
310+ expect ( notJwt . length ) . toBe ( 62 ) ;
311+ expect ( shortThreeSegment . length ) . toBeLessThan ( 100 ) ;
312+ writeFileSync (
313+ join ( root , "fixture.txt" ) ,
314+ [ "random base64-ish word:" , notJwt , "tiny segmented thing:" , shortThreeSegment ] . join ( "\n" ) ,
315+ ) ;
316+ const w = new FilesystemWatcher ( {
317+ roots : [ root ] ,
318+ baseUrl : "http://localhost:3111" ,
319+ logger : { info : vi . fn ( ) , warn : vi . fn ( ) , error : vi . fn ( ) } ,
320+ } ) ;
321+
322+ await w . flush ( root , "fixture.txt" ) ;
323+
324+ expect ( captured ) . toHaveLength ( 1 ) ;
325+ const content = ( captured [ 0 ] . body as { data : { content : string } } ) . data . content ;
326+ expect ( content ) . toContain ( notJwt ) ;
327+ expect ( content ) . toContain ( shortThreeSegment ) ;
328+ expect ( content ) . not . toContain ( "[REDACTED]" ) ;
329+ } ) ;
330+
215331 it ( "debounces rapid writes to a single observation" , async ( ) => {
216332 const w = new FilesystemWatcher ( {
217333 roots : [ root ] ,
0 commit comments