@@ -339,6 +339,126 @@ describe("ReefTransportClient response body bounds", () => {
339339 } ) ;
340340} ) ;
341341
342+ describe ( "ReefTransportClient credential redaction" , ( ) => {
343+ it ( "redacts setup tokens and bearer sessions from loopback relay errors" , async ( ) => {
344+ const setupToken = "reef.token[abc]+?/" ;
345+ const session = `reef-session-${ "a" . repeat ( 96 ) } -tail` ;
346+ const sessionPrefix = session . slice ( 0 , 6 ) ;
347+ const sessionSuffix = session . slice ( - 4 ) ;
348+ const receivedAuthorization : string [ ] = [ ] ;
349+ const receivedSignatures : string [ ] = [ ] ;
350+ const receivedTokens : string [ ] = [ ] ;
351+ const server = http . createServer ( ( request , response ) => {
352+ const chunks : Buffer [ ] = [ ] ;
353+ request . on ( "data" , ( chunk : Buffer ) => chunks . push ( chunk ) ) ;
354+ request . on ( "end" , ( ) => {
355+ const authorization = request . headers . authorization ?? "" ;
356+ receivedAuthorization . push ( authorization ) ;
357+ const signatureHeader = request . headers [ "x-reef-sig" ] ;
358+ const signature = typeof signatureHeader === "string" ? signatureHeader : "" ;
359+ if ( signature ) {
360+ receivedSignatures . push ( signature ) ;
361+ }
362+ const body = Buffer . concat ( chunks ) . toString ( "utf8" ) ;
363+ const token = body ? ( JSON . parse ( body ) as { token ?: unknown } ) . token : undefined ;
364+ if ( typeof token === "string" ) {
365+ receivedTokens . push ( token ) ;
366+ }
367+ const reflectedCredential =
368+ authorization || ( typeof token === "string" ? token : "" ) || signature ;
369+ response . writeHead ( 401 , { "content-type" : "application/json" } ) ;
370+ response . end (
371+ JSON . stringify ( { error : `${ reflectedCredential } relay rejected` , marker : "safe" } ) ,
372+ ) ;
373+ } ) ;
374+ } ) ;
375+ await new Promise < void > ( ( resolve , reject ) => {
376+ server . once ( "error" , reject ) ;
377+ server . listen ( 0 , "127.0.0.1" , resolve ) ;
378+ } ) ;
379+ const address = server . address ( ) ;
380+ if ( ! address || typeof address === "string" ) {
381+ throw new Error ( "Reef credential redaction test server did not bind a TCP port" ) ;
382+ }
383+
384+ const client = createClient ( fetch , ( ) => ts , `http://127.0.0.1:${ address . port } ` ) ;
385+ try {
386+ const tokenError = await client . authComplete ( setupToken ) . catch ( ( error : unknown ) => error ) ;
387+ const createHandleError = await client
388+ . createHandle ( session , "approve" )
389+ . catch ( ( error : unknown ) => error ) ;
390+ const sessionError = await client . listOwnHandles ( session ) . catch ( ( error : unknown ) => error ) ;
391+ const signedError = await client . listFriends ( ) . catch ( ( error : unknown ) => error ) ;
392+
393+ expect ( tokenError ) . toMatchObject ( { name : "ReefRelayError" , status : 401 } ) ;
394+ expect ( createHandleError ) . toMatchObject ( { name : "ReefRelayError" , status : 401 } ) ;
395+ expect ( sessionError ) . toMatchObject ( { name : "ReefRelayError" , status : 401 } ) ;
396+ expect ( tokenError ) . toMatchObject ( { message : expect . stringContaining ( "relay rejected" ) } ) ;
397+ expect ( createHandleError ) . toMatchObject ( {
398+ message : expect . stringContaining ( "relay rejected" ) ,
399+ } ) ;
400+ expect ( sessionError ) . toMatchObject ( { message : expect . stringContaining ( "relay rejected" ) } ) ;
401+ expect ( signedError ) . toMatchObject ( {
402+ name : "ReefRelayError" ,
403+ status : 401 ,
404+ message : expect . stringContaining ( "relay rejected" ) ,
405+ } ) ;
406+ expect ( ( tokenError as Error ) . message ) . not . toContain ( setupToken ) ;
407+ expect ( ( createHandleError as Error ) . message ) . not . toContain ( session ) ;
408+ expect ( ( sessionError as Error ) . message ) . not . toContain ( session ) ;
409+ expect ( ( createHandleError as Error ) . message ) . not . toContain ( sessionPrefix ) ;
410+ expect ( ( createHandleError as Error ) . message ) . not . toContain ( sessionSuffix ) ;
411+ expect ( ( sessionError as Error ) . message ) . not . toContain ( sessionPrefix ) ;
412+ expect ( ( sessionError as Error ) . message ) . not . toContain ( sessionSuffix ) ;
413+ expect ( receivedAuthorization ) . toContain ( `Bearer ${ session } ` ) ;
414+ expect ( receivedSignatures ) . toHaveLength ( 1 ) ;
415+ expect ( receivedSignatures [ 0 ] ) . toBeTruthy ( ) ;
416+ expect ( ( signedError as Error ) . message ) . not . toContain ( receivedSignatures [ 0 ] ) ;
417+ expect ( receivedTokens ) . toContain ( setupToken ) ;
418+ } finally {
419+ await new Promise < void > ( ( resolve , reject ) => {
420+ server . close ( ( error ) => ( error ? reject ( error ) : resolve ( ) ) ) ;
421+ } ) ;
422+ }
423+ } ) ;
424+
425+ it ( "redacts signed body credentials across bounded error text" , async ( ) => {
426+ const code = "friend.code[123]+?/" ;
427+ const prefix = "x" . repeat ( 32_760 ) ;
428+ const suffix = "y" . repeat ( 32 ) ;
429+ const receivedBodies : string [ ] = [ ] ;
430+ const client = createClient ( async ( _url , init ) => {
431+ const body = init ?. body ;
432+ receivedBodies . push (
433+ body instanceof Uint8Array
434+ ? new TextDecoder ( ) . decode ( body )
435+ : typeof body === "string"
436+ ? body
437+ : "" ,
438+ ) ;
439+ const responseBody = JSON . stringify ( { error : `${ prefix } ${ code } ${ suffix } ` } ) ;
440+ const splitAt = responseBody . indexOf ( code ) + Math . floor ( code . length / 2 ) ;
441+ return new Response (
442+ new ReadableStream ( {
443+ start ( controller ) {
444+ controller . enqueue ( new TextEncoder ( ) . encode ( responseBody . slice ( 0 , splitAt ) ) ) ;
445+ controller . enqueue ( new TextEncoder ( ) . encode ( responseBody . slice ( splitAt ) ) ) ;
446+ controller . close ( ) ;
447+ } ,
448+ } ) ,
449+ { status : 401 , headers : { "content-type" : "application/json" } } ,
450+ ) ;
451+ } ) ;
452+
453+ const error = await client . requestFriend ( "bob" , code ) . catch ( ( failure : unknown ) => failure ) ;
454+
455+ expect ( error ) . toMatchObject ( { name : "ReefRelayError" , status : 401 } ) ;
456+ expect ( receivedBodies ) . toHaveLength ( 1 ) ;
457+ expect ( receivedBodies [ 0 ] ) . toContain ( `"code":"${ code } "` ) ;
458+ expect ( ( error as Error ) . message ) . not . toContain ( code ) ;
459+ } ) ;
460+ } ) ;
461+
342462const INBOX_WEBSOCKET_MAX_PAYLOAD_BYTES = 64 * 1024 ;
343463
344464class ControlledSocket {
@@ -587,12 +707,16 @@ describe("ReefInboxConnection recovery", () => {
587707 const socket = new ControlledSocket ( ) ;
588708 const states : string [ ] = [ ] ;
589709 const errors : string [ ] = [ ] ;
710+ let socketUrl = "" ;
590711 const client = createClient ( async ( ) => Response . json ( { entries : [ ] , cursor : 0 } ) ) ;
591712 const abort = new AbortController ( ) ;
592713 const inbox = new ReefInboxConnection (
593714 client ,
594715 async ( ) => { } ,
595- ( ) => socket as unknown as WebSocketLike ,
716+ ( url ) => {
717+ socketUrl = url ;
718+ return socket as unknown as WebSocketLike ;
719+ } ,
596720 {
597721 onState : ( state ) => states . push ( state ) ,
598722 onError : ( error ) => {
@@ -604,11 +728,17 @@ describe("ReefInboxConnection recovery", () => {
604728
605729 const running = inbox . start ( abort . signal ) ;
606730 socket . emit ( "open" ) ;
607- socket . emit ( "close" , { code : 1008 , reason : "policy" } ) ;
731+ const signature = new URL ( socketUrl ) . searchParams . get ( "sig" ) ;
732+ if ( ! signature ) {
733+ throw new Error ( "Reef WebSocket test URL did not contain a signature" ) ;
734+ }
735+ socket . emit ( "close" , { code : 1008 , reason : `policy ${ signature } ` } ) ;
608736 await running ;
609737
610738 expect ( states ) . toEqual ( [ "connected" , "disconnected" ] ) ;
611- expect ( errors ) . toEqual ( [ "reef inbox socket closed unexpectedly code=1008 reason=policy" ] ) ;
739+ expect ( errors ) . toEqual ( [
740+ "reef inbox socket closed unexpectedly code=1008 reason=policy <redacted>" ,
741+ ] ) ;
612742 } ) ;
613743
614744 it ( "resets reconnect backoff after a socket completes catch-up" , async ( ) => {
0 commit comments