@@ -196,26 +196,74 @@ describe("safeFetch", () => {
196196 expect ( called ) . toBe ( false ) ;
197197 } ) ;
198198
199- it ( "returns the redirect response when dispatcher is provided by an outer guard" , async ( ) => {
200- const redirectedTo = "https://cdn.sharepoint.com/storage/file.pdf" ;
201- const fetchMock = mockFetchWithRedirect ( {
202- "https://teams.sharepoint.com/file.pdf" : redirectedTo ,
203- } ) ;
204- const res = await safeFetch ( {
205- url : "https://teams.sharepoint.com/file.pdf" ,
206- allowHosts : [ "sharepoint.com" ] ,
207- fetchFn : fetchMock as unknown as typeof fetch ,
208- requestInit : { dispatcher : { } } as RequestInit ,
209- resolveFn : publicResolve ,
210- } ) ;
211- expect ( res . status ) . toBe ( 302 ) ;
212- expect ( res . headers . get ( "location" ) ) . toBe ( redirectedTo ) ;
213- expect ( fetchMock ) . toHaveBeenCalledOnce ( ) ;
199+ it . each ( [ 301 , 302 , 303 , 307 , 308 ] ) (
200+ "returns dispatcher-mode %i responses to the outer guard" ,
201+ async ( status ) => {
202+ const redirectedTo = "https://cdn.sharepoint.com/storage/file.pdf" ;
203+ const response = new Response ( null , {
204+ status,
205+ headers : { location : redirectedTo } ,
206+ } ) ;
207+ const fetchMock = vi . fn ( async ( _url : string , _init ?: RequestInit ) => response ) ;
208+ const resolveFn = vi . fn ( publicResolve ) ;
209+ const headers = new Headers ( { Authorization : "Bearer fixture-token" } ) ;
210+ const res = await safeFetch ( {
211+ url : "https://teams.sharepoint.com/file.pdf" ,
212+ allowHosts : [ "sharepoint.com" ] ,
213+ authorizationAllowHosts : [ "teams.sharepoint.com" ] ,
214+ fetchFn : fetchMock as unknown as typeof fetch ,
215+ requestInit : { dispatcher : { } , headers } as RequestInit ,
216+ resolveFn,
217+ } ) ;
218+ expect ( res ) . toBe ( response ) ;
219+ expect ( fetchMock ) . toHaveBeenCalledOnce ( ) ;
220+ expect ( resolveFn ) . toHaveBeenCalledExactlyOnceWith ( "teams.sharepoint.com" ) ;
221+ const sentHeaders = new Headers ( fetchMock . mock . calls [ 0 ] ?. [ 1 ] ?. headers ) ;
222+ expect ( sentHeaders . get ( "authorization" ) ) . toBeNull ( ) ;
223+ expect ( headers . get ( "authorization" ) ) . toBe ( "Bearer fixture-token" ) ;
224+ } ,
225+ ) ;
226+
227+ it . each ( [ 200 , 302 ] ) (
228+ "returns dispatcher-mode %i responses without a location unchanged" ,
229+ async ( status ) => {
230+ const response = new Response ( null , { status } ) ;
231+ const fetchMock = vi . fn ( async ( ) => response ) ;
232+ const res = await safeFetch ( {
233+ url : "https://teams.sharepoint.com/file.pdf" ,
234+ allowHosts : [ "sharepoint.com" ] ,
235+ fetchFn : fetchMock as typeof fetch ,
236+ requestInit : { dispatcher : { } } as RequestInit ,
237+ } ) ;
238+ expect ( res ) . toBe ( response ) ;
239+ expect ( fetchMock ) . toHaveBeenCalledOnce ( ) ;
240+ } ,
241+ ) ;
242+
243+ it . each ( [
244+ [ "private DNS" , privateResolve ( "127.0.0.1" ) ] ,
245+ [ "failed DNS" , failingResolve ] ,
246+ ] ) ( "blocks dispatcher-mode fetch before dispatch on %s" , async ( _label , resolveFn ) => {
247+ const fetchMock = vi . fn ( ) ;
248+ await expect (
249+ safeFetch ( {
250+ url : "https://teams.sharepoint.com/file.pdf" ,
251+ allowHosts : [ "sharepoint.com" ] ,
252+ fetchFn : fetchMock as typeof fetch ,
253+ requestInit : { dispatcher : { } } as RequestInit ,
254+ resolveFn,
255+ } ) ,
256+ ) . rejects . toThrow ( "Initial download URL blocked" ) ;
257+ expect ( fetchMock ) . not . toHaveBeenCalled ( ) ;
214258 } ) ;
215259
216- it ( "still enforces allowlist checks before returning dispatcher-mode redirects" , async ( ) => {
260+ it . each ( [
261+ [ "https://evil.example.com/steal" , "blocked by allowlist" ] ,
262+ [ "http://teams.sharepoint.com/file.pdf" , "blocked by allowlist" ] ,
263+ [ "https://[invalid" , "Invalid redirect URL" ] ,
264+ ] ) ( "rejects dispatcher-mode redirect %s" , async ( location , error ) => {
217265 const fetchMock = mockFetchWithRedirect ( {
218- "https://teams.sharepoint.com/file.pdf" : "https://evil.example.com/steal" ,
266+ "https://teams.sharepoint.com/file.pdf" : location ,
219267 } ) ;
220268 await expect (
221269 safeFetch ( {
@@ -225,7 +273,7 @@ describe("safeFetch", () => {
225273 requestInit : { dispatcher : { } } as RequestInit ,
226274 resolveFn : publicResolve ,
227275 } ) ,
228- ) . rejects . toThrow ( "blocked by allowlist" ) ;
276+ ) . rejects . toThrow ( error ) ;
229277 expect ( fetchMock ) . toHaveBeenCalledOnce ( ) ;
230278 } ) ;
231279
@@ -597,63 +645,69 @@ describe("Graph shared-link helpers", () => {
597645describe ( "msteams inline image limits" , ( ) => {
598646 const smallPngDataUrl = "data:image/png;base64,aGVsbG8=" ; // "hello" (5 bytes)
599647
600- it ( "rejects inline data images above per-image limit" , ( ) => {
601- const attachments = [
602- {
603- contentType : "text/html" ,
604- content : `<img src="${ smallPngDataUrl } " />` ,
605- } ,
606- ] ;
607- const out = extractInlineImageCandidates ( attachments , { maxInlineBytes : 4 } ) ;
608- expect ( out ) . toStrictEqual ( [ { kind : "unavailable" } ] ) ;
609- } ) ;
610-
611- it ( "accepts inline data images within limit" , ( ) => {
612- const attachments = [
613- {
614- contentType : "text/html" ,
615- content : `<img src="${ smallPngDataUrl } " />` ,
616- } ,
617- ] ;
618- const out = extractInlineImageCandidates ( attachments , { maxInlineBytes : 10 } ) ;
619- expect ( out . length ) . toBe ( 1 ) ;
620- expect ( out [ 0 ] ?. kind ) . toBe ( "data" ) ;
621- if ( out [ 0 ] ?. kind === "data" ) {
622- expect ( out [ 0 ] . data . byteLength ) . toBeGreaterThan ( 0 ) ;
623- expect ( out [ 0 ] . contentType ) . toBe ( "image/png" ) ;
624- }
625- } ) ;
626-
627- it ( "rejects inline data images with malformed base64 padding" , ( ) => {
648+ it . each ( [
649+ [ "AA==" , "00" ] ,
650+ [ "AAA=" , "0000" ] ,
651+ [ "AAAA" , "000000" ] ,
652+ [ "Z E = =" , "64" ] ,
653+ [ "A\tA==" , "00" ] ,
654+ ] ) ( "enforces exact decoded-size limits for %s" , ( payload , hex ) => {
655+ const data = Buffer . from ( hex , "hex" ) ;
628656 const attachments = [
629657 {
630658 contentType : "text/html" ,
631- content : `<img src="data:image/png;base64,aGV=sbG8= " />` ,
659+ content : `<img src="data:image/png;base64,${ payload } " />` ,
632660 } ,
633661 ] ;
634- const out = extractInlineImageCandidates ( attachments , { maxInlineBytes : 10 } ) ;
635- expect ( out ) . toStrictEqual ( [ { kind : "unavailable" } ] ) ;
636- } ) ;
662+ expect (
663+ extractInlineImageCandidates ( attachments , { maxInlineBytes : data . length - 1 } ) ,
664+ ) . toStrictEqual ( [ { kind : "unavailable" } ] ) ;
665+ expect (
666+ extractInlineImageCandidates ( attachments , { maxInlineBytes : data . length } ) ,
667+ ) . toStrictEqual ( [ { kind : "data" , data, contentType : "image/png" } ] ) ;
668+ } ) ;
669+
670+ it . each ( [ "aGV=sbG8=" , "A===" , "AA" , "-AAA" , "A!AA" ] ) (
671+ "rejects malformed inline base64 %s" ,
672+ ( payload ) => {
673+ const attachments = [
674+ {
675+ contentType : "text/html" ,
676+ content : `<img src="data:image/png;base64,${ payload } " />` ,
677+ } ,
678+ ] ;
679+ const out = extractInlineImageCandidates ( attachments , { maxInlineBytes : 10 } ) ;
680+ expect ( out ) . toStrictEqual ( [ { kind : "unavailable" } ] ) ;
681+ } ,
682+ ) ;
637683
638- it ( "enforces cumulative inline size limit across attachments" , ( ) => {
639- const attachments = [
640- {
641- contentType : "text/html" ,
642- content : `<img src="${ smallPngDataUrl } " />` ,
643- } ,
644- {
645- contentType : "text/html" ,
646- content : `<img src="${ smallPngDataUrl } " />` ,
647- } ,
648- ] ;
649- const out = extractInlineImageCandidates ( attachments , {
650- maxInlineBytes : 10 ,
651- maxInlineTotalBytes : 6 ,
652- } ) ;
653- expect ( out . length ) . toBe ( 2 ) ;
654- expect ( out [ 0 ] ?. kind ) . toBe ( "data" ) ;
655- expect ( out [ 1 ] ) . toStrictEqual ( { kind : "unavailable" } ) ;
656- } ) ;
684+ it . each ( [
685+ [ 9 , [ "data" , "unavailable" , "unavailable" ] ] ,
686+ [ 10 , [ "data" , "data" , "unavailable" ] ] ,
687+ ] ) (
688+ "enforces cumulative inline size limit %i across attachments" ,
689+ ( maxInlineTotalBytes , kinds ) => {
690+ const attachments = [
691+ {
692+ contentType : "text/html" ,
693+ content : `<img src="${ smallPngDataUrl } " />` ,
694+ } ,
695+ {
696+ contentType : "text/html" ,
697+ content : `<img src="${ smallPngDataUrl } " />` ,
698+ } ,
699+ {
700+ contentType : "text/html" ,
701+ content : `<img src="${ smallPngDataUrl } " />` ,
702+ } ,
703+ ] ;
704+ const out = extractInlineImageCandidates ( attachments , {
705+ maxInlineBytes : 10 ,
706+ maxInlineTotalBytes,
707+ } ) ;
708+ expect ( out . map ( ( candidate ) => candidate . kind ) ) . toEqual ( kinds ) ;
709+ } ,
710+ ) ;
657711} ) ;
658712
659713describe ( "normalizeContentType case-insensitivity" , ( ) => {
0 commit comments