@@ -432,6 +432,52 @@ describe("fetchDiscord", () => {
432432 expect ( message ) . not . toContain ( token ) ;
433433 expect ( message ) . not . toContain ( uniqueSecret ) ;
434434 expect ( message ) . not . toContain ( "<html" ) ;
435+ console . log (
436+ `[discord credential redaction proof] format=html status=502 authorization_received=${ authorization === `Bot ${ token } ` } token_present=${ message . includes ( token ) } unique_fragment_present=${ message . includes ( uniqueSecret ) } detail=${ message } ` ,
437+ ) ;
438+ } finally {
439+ await closeServer ( server ) ;
440+ }
441+ } ) ;
442+
443+ it ( "redacts reflected bot credentials from JSON error messages" , async ( ) => {
444+ const uniqueSecret = "discord-json-loopback-secret" ;
445+ const token = `proof-prefix-${ uniqueSecret } -proof-suffix` ;
446+ let authorization : string | undefined ;
447+ const server = createServer ( ( req , res ) => {
448+ authorization = req . headers . authorization ;
449+ res . writeHead ( 429 , { "content-type" : "application/json" } ) ;
450+ res . end (
451+ JSON . stringify ( {
452+ message : `proxy failure Authorization: ${ authorization } ; request rejected` ,
453+ retry_after : 0 ,
454+ global : false ,
455+ code : 20_028 ,
456+ } ) ,
457+ ) ;
458+ } ) ;
459+ const port = await listenLoopbackServer ( server ) ;
460+
461+ try {
462+ stubDiscordFetchToLoopback ( `http://127.0.0.1:${ port } ` ) ;
463+
464+ const error = await requestDiscord ( "/gateway/bot" , token , {
465+ retry : { attempts : 1 } ,
466+ } ) . catch ( ( err : unknown ) => err ) ;
467+
468+ expect ( error ) . toBeInstanceOf ( DiscordApiError ) ;
469+ expect ( authorization ) . toBe ( `Bot ${ token } ` ) ;
470+ expect ( ( error as DiscordApiError ) . retryAfter ) . toBe ( 0 ) ;
471+ const message = String ( error ) ;
472+ expect ( message ) . toContain ( "Discord API /gateway/bot failed (429)" ) ;
473+ expect ( message ) . toContain ( "proxy failure" ) ;
474+ expect ( message ) . toContain ( "Authorization: Bot" ) ;
475+ expect ( message ) . toContain ( "retry after 0.0s" ) ;
476+ expect ( message ) . not . toContain ( token ) ;
477+ expect ( message ) . not . toContain ( uniqueSecret ) ;
478+ console . log (
479+ `[discord credential redaction proof] format=json status=429 authorization_received=${ authorization === `Bot ${ token } ` } token_present=${ message . includes ( token ) } unique_fragment_present=${ message . includes ( uniqueSecret ) } retry_after=${ ( error as DiscordApiError ) . retryAfter } detail=${ message } ` ,
480+ ) ;
435481 } finally {
436482 await closeServer ( server ) ;
437483 }
0 commit comments