@@ -949,6 +949,57 @@ describe("fetchWithSsrFGuard hardening", () => {
949949 await result . release ( ) ;
950950 } ) ;
951951
952+ it ( "preserves redirects when response body cancellation rejects" , async ( ) => {
953+ const unhandledRejections : unknown [ ] = [ ] ;
954+ const onUnhandledRejection = ( reason : unknown ) => {
955+ unhandledRejections . push ( reason ) ;
956+ } ;
957+ const cancel = vi . fn ( ( ) => {
958+ throw new Error ( "redirect cancellation failed" ) ;
959+ } ) ;
960+ const fetchImpl = vi
961+ . fn ( )
962+ . mockResolvedValueOnce (
963+ new Response ( new ReadableStream < Uint8Array > ( { cancel } ) , {
964+ status : 302 ,
965+ headers : { location : "https://cdn.example.com/asset" } ,
966+ } ) ,
967+ )
968+ . mockResolvedValueOnce ( okResponse ( "redirected" ) ) ;
969+ process . on ( "unhandledRejection" , onUnhandledRejection ) ;
970+ let result : Awaited < ReturnType < typeof fetchWithSsrFGuard > > | undefined ;
971+
972+ try {
973+ result = await fetchWithSsrFGuard ( {
974+ url : "https://api.example.com/start" ,
975+ fetchImpl,
976+ lookupFn : createPublicLookup ( ) ,
977+ } ) ;
978+
979+ const reader = result . response . body ?. getReader ( ) ;
980+ if ( ! reader ) {
981+ throw new Error ( "expected redirected response body" ) ;
982+ }
983+ try {
984+ const firstChunk = await reader . read ( ) ;
985+ expect ( firstChunk . done ) . toBe ( false ) ;
986+ expect ( new TextDecoder ( ) . decode ( firstChunk . value ) ) . toBe ( "redirected" ) ;
987+ await expect ( reader . read ( ) ) . resolves . toEqual ( { done : true , value : undefined } ) ;
988+ } finally {
989+ reader . releaseLock ( ) ;
990+ }
991+ expect ( cancel ) . toHaveBeenCalledOnce ( ) ;
992+ await new Promise < void > ( ( resolve ) => {
993+ setImmediate ( resolve ) ;
994+ } ) ;
995+ expect ( unhandledRejections ) . toStrictEqual ( [ ] ) ;
996+ } finally {
997+ await result ?. release ( ) ;
998+ process . off ( "unhandledRejection" , onUnhandledRejection ) ;
999+ expect ( process . listeners ( "unhandledRejection" ) ) . not . toContain ( onUnhandledRejection ) ;
1000+ }
1001+ } ) ;
1002+
9521003 it ( "strips sensitive headers when redirect crosses origins" , async ( ) => {
9531004 const lookupFn = createPublicLookup ( ) ;
9541005 const fetchImpl = vi
0 commit comments