@@ -514,6 +514,61 @@ describe("downloadMSTeamsBotFrameworkAttachment", () => {
514514 { status : 500 } ,
515515 ] ) ;
516516 } ) ;
517+
518+ it ( "bounds an unbounded attachmentInfo JSON body and cancels the stream" , async ( ) => {
519+ const state = { canceled : false , enqueued : 0 } ;
520+ const chunkBytes = 1024 * 1024 ;
521+ const stream = new ReadableStream < Uint8Array > ( {
522+ pull ( controller ) {
523+ if ( state . enqueued >= 64 ) {
524+ controller . close ( ) ;
525+ return ;
526+ }
527+ state . enqueued += 1 ;
528+ controller . enqueue ( new Uint8Array ( chunkBytes ) . fill ( 0x61 ) ) ;
529+ } ,
530+ cancel ( ) {
531+ state . canceled = true ;
532+ } ,
533+ } ) ;
534+ const jsonSpy = vi . spyOn ( Response . prototype , "json" ) . mockImplementation ( async ( ) => {
535+ throw new Error ( "raw response.json() should not be used" ) ;
536+ } ) ;
537+ const fetchFn : typeof fetch = vi . fn ( async ( ) => {
538+ return new Response ( stream , {
539+ status : 200 ,
540+ headers : { "content-type" : "application/json" } ,
541+ } ) ;
542+ } ) ;
543+
544+ try {
545+ const warn = vi . fn ( ) ;
546+ const media = await downloadMSTeamsBotFrameworkAttachment ( {
547+ serviceUrl : "https://smba.trafficmanager.net/amer" ,
548+ attachmentId : "att-1" ,
549+ tokenProvider : buildTokenProvider ( ) ,
550+ maxBytes : 10_000_000 ,
551+ fetchFn,
552+ fetchFnSupportsDispatcher : true ,
553+ resolveFn : resolvePublicHost ,
554+ logger : { warn } ,
555+ } ) ;
556+
557+ expect ( media ) . toBeUndefined ( ) ;
558+ expect ( jsonSpy ) . not . toHaveBeenCalled ( ) ;
559+ // Enforced well before the 64 MiB test ceiling; an unbounded reader would keep pulling.
560+ expect ( state . enqueued ) . toBeLessThan ( 32 ) ;
561+ expect ( state . canceled ) . toBe ( true ) ;
562+ expect ( warn ) . toHaveBeenCalledWith (
563+ "msteams botFramework attachmentInfo parse failed" ,
564+ expect . objectContaining ( {
565+ error : expect . stringMatching ( / J S O N r e s p o n s e e x c e e d s 1 6 7 7 7 2 1 6 b y t e s / ) ,
566+ } ) ,
567+ ) ;
568+ } finally {
569+ jsonSpy . mockRestore ( ) ;
570+ }
571+ } ) ;
517572 } ) ;
518573} ) ;
519574
0 commit comments