@@ -5,17 +5,11 @@ import {
55 encodeGraphShareId ,
66 extractInlineImageCandidates ,
77 isDownloadableAttachment ,
8- isGraphSharedLinkUrl ,
98 isLikelyImageAttachment ,
10- isPrivateOrReservedIP ,
119 isUrlAllowed ,
1210 normalizeContentType ,
13- resolveAndValidateIP ,
1411 resolveAttachmentFetchPolicy ,
15- resolveAllowedHosts ,
16- resolveAuthAllowedHosts ,
1712 resolveMediaSsrfPolicy ,
18- safeFetch ,
1913 safeFetchWithPolicy ,
2014 tryBuildGraphSharesUrlForSharedLink ,
2115} from "./shared.js" ;
@@ -26,6 +20,29 @@ const failingResolve = async () => {
2620 throw new Error ( "DNS failure" ) ;
2721} ;
2822
23+ const resolveAllowedHosts = ( input ?: string [ ] ) =>
24+ resolveAttachmentFetchPolicy ( { allowHosts : input } ) . allowHosts ;
25+ const resolveAuthAllowedHosts = ( input ?: string [ ] ) =>
26+ resolveAttachmentFetchPolicy ( { authAllowHosts : input } ) . authAllowHosts ;
27+ const isGraphSharedLinkUrl = ( url : string ) =>
28+ tryBuildGraphSharesUrlForSharedLink ( url ) !== undefined ;
29+
30+ type SafeFetchParams = Omit < Parameters < typeof safeFetchWithPolicy > [ 0 ] , "policy" > & {
31+ allowHosts : string [ ] ;
32+ authorizationAllowHosts ?: string [ ] ;
33+ } ;
34+
35+ async function safeFetch ( params : SafeFetchParams ) {
36+ const { allowHosts, authorizationAllowHosts, ...request } = params ;
37+ return await safeFetchWithPolicy ( {
38+ ...request ,
39+ policy : {
40+ allowHosts,
41+ authAllowHosts : authorizationAllowHosts ?? [ ] ,
42+ } ,
43+ } ) ;
44+ }
45+
2946function mockFetchWithRedirect ( redirectMap : Record < string , string > , finalBody = "ok" ) {
3047 return vi . fn ( async ( url : string , init ?: RequestInit ) => {
3148 const target = redirectMap [ url ] ;
@@ -112,56 +129,6 @@ describe("msteams attachment allowlists", () => {
112129 } ) ;
113130 expect ( resolveMediaSsrfPolicy ( [ "*" ] ) ) . toBeUndefined ( ) ;
114131 } ) ;
115-
116- it . each ( [
117- [ "999.999.999.999" , true ] ,
118- [ "256.0.0.1" , true ] ,
119- [ "10.0.0.256" , true ] ,
120- [ "-1.0.0.1" , false ] ,
121- [ "1.2.3.4.5" , false ] ,
122- [ "0:0:0:0:0:0:0:1" , true ] ,
123- ] as const ) ( "malformed/expanded %s → %s (SDK fails closed)" , ( ip , expected ) => {
124- expect ( isPrivateOrReservedIP ( ip ) ) . toBe ( expected ) ;
125- } ) ;
126- } ) ;
127-
128- // ─── resolveAndValidateIP ────────────────────────────────────────────────────
129-
130- describe ( "resolveAndValidateIP" , ( ) => {
131- it ( "accepts a hostname resolving to a public IP" , async ( ) => {
132- const ip = await resolveAndValidateIP ( "teams.sharepoint.com" , publicResolve ) ;
133- expect ( ip ) . toBe ( "13.107.136.10" ) ;
134- } ) ;
135-
136- it ( "rejects a hostname resolving to 10.x.x.x" , async ( ) => {
137- await expect ( resolveAndValidateIP ( "evil.test" , privateResolve ( "10.0.0.1" ) ) ) . rejects . toThrow (
138- "private/reserved IP" ,
139- ) ;
140- } ) ;
141-
142- it ( "rejects a hostname resolving to 169.254.169.254" , async ( ) => {
143- await expect (
144- resolveAndValidateIP ( "evil.test" , privateResolve ( "169.254.169.254" ) ) ,
145- ) . rejects . toThrow ( "private/reserved IP" ) ;
146- } ) ;
147-
148- it ( "rejects a hostname resolving to loopback" , async ( ) => {
149- await expect ( resolveAndValidateIP ( "evil.test" , privateResolve ( "127.0.0.1" ) ) ) . rejects . toThrow (
150- "private/reserved IP" ,
151- ) ;
152- } ) ;
153-
154- it ( "rejects a hostname resolving to IPv6 loopback" , async ( ) => {
155- await expect ( resolveAndValidateIP ( "evil.test" , privateResolve ( "::1" ) ) ) . rejects . toThrow (
156- "private/reserved IP" ,
157- ) ;
158- } ) ;
159-
160- it ( "throws on DNS resolution failure" , async ( ) => {
161- await expect ( resolveAndValidateIP ( "nonexistent.test" , failingResolve ) ) . rejects . toThrow (
162- "DNS resolution failed" ,
163- ) ;
164- } ) ;
165132} ) ;
166133
167134// ─── safeFetch ───────────────────────────────────────────────────────────────
0 commit comments