@@ -35,7 +35,20 @@ function setMockFetch(
3535
3636async function createWebFetchToolForTest ( params ?: {
3737 firecrawlApiKey ?: string ;
38- ssrfPolicy ?: { allowRfc2544BenchmarkRange ?: boolean } ;
38+ globalSsrFPolicy ?: {
39+ allowPrivateNetwork ?: boolean ;
40+ dangerouslyAllowPrivateNetwork ?: boolean ;
41+ allowedHostnames ?: string [ ] ;
42+ hostnameAllowlist ?: string [ ] ;
43+ allowRfc2544BenchmarkRange ?: boolean ;
44+ } ;
45+ agentSsrFPolicy ?: {
46+ allowPrivateNetwork ?: boolean ;
47+ dangerouslyAllowPrivateNetwork ?: boolean ;
48+ allowedHostnames ?: string [ ] ;
49+ hostnameAllowlist ?: string [ ] ;
50+ allowRfc2544BenchmarkRange ?: boolean ;
51+ } ;
3952 cacheTtlMinutes ?: number ;
4053} ) {
4154 const { createWebFetchTool } = await import ( "./web-tools.js" ) ;
@@ -58,12 +71,21 @@ async function createWebFetchToolForTest(params?: {
5871 web : {
5972 fetch : {
6073 cacheTtlMinutes : params ?. cacheTtlMinutes ?? 0 ,
61- ssrfPolicy : params ?. ssrfPolicy ,
74+ ssrfPolicy : params ?. globalSsrFPolicy ,
6275 ...( params ?. firecrawlApiKey ? { provider : "firecrawl" } : { } ) ,
6376 } ,
6477 } ,
6578 } ,
6679 } ,
80+ agentTools : params ?. agentSsrFPolicy
81+ ? {
82+ web : {
83+ fetch : {
84+ ssrfPolicy : params . agentSsrFPolicy ,
85+ } ,
86+ } ,
87+ }
88+ : undefined ,
6789 lookupFn : lookupMock ,
6890 } ) ;
6991}
@@ -165,7 +187,7 @@ describe("web_fetch SSRF protection", () => {
165187
166188 const fetchSpy = setMockFetch ( ) . mockResolvedValue ( textResponse ( "benchmark ok" ) ) ;
167189 const allowedTool = await createWebFetchToolForTest ( {
168- ssrfPolicy : { allowRfc2544BenchmarkRange : true } ,
190+ globalSsrFPolicy : { allowRfc2544BenchmarkRange : true } ,
169191 cacheTtlMinutes : 1 ,
170192 } ) ;
171193
@@ -178,4 +200,52 @@ describe("web_fetch SSRF protection", () => {
178200 const stricterTool = await createWebFetchToolForTest ( { cacheTtlMinutes : 1 } ) ;
179201 await expectBlockedUrl ( stricterTool , url , / p r i v a t e | i n t e r n a l | b l o c k e d / i) ;
180202 } ) ;
203+
204+ it ( "allows private-network targets when a named agent opts in" , async ( ) => {
205+ const url = "http://127.0.0.1/private" ;
206+ const deniedTool = await createWebFetchToolForTest ( {
207+ globalSsrFPolicy : { dangerouslyAllowPrivateNetwork : false } ,
208+ } ) ;
209+ await expectBlockedUrl ( deniedTool , url , / p r i v a t e | i n t e r n a l | b l o c k e d / i) ;
210+
211+ const fetchSpy = setMockFetch ( ) . mockResolvedValue ( textResponse ( "agent ok" ) ) ;
212+ const allowedTool = await createWebFetchToolForTest ( {
213+ globalSsrFPolicy : { dangerouslyAllowPrivateNetwork : false } ,
214+ agentSsrFPolicy : { dangerouslyAllowPrivateNetwork : true } ,
215+ } ) ;
216+
217+ const result = await allowedTool ?. execute ?.( "call" , { url } ) ;
218+ expect ( result ?. details ) . toMatchObject ( { status : 200 , extractor : "raw" } ) ;
219+ expect ( fetchSpy ) . toHaveBeenCalledTimes ( 1 ) ;
220+ } ) ;
221+
222+ it ( "allows narrow hostname exceptions from a named agent" , async ( ) => {
223+ lookupMock . mockResolvedValue ( [ { address : "10.0.0.5" , family : 4 } ] ) ;
224+ const fetchSpy = setMockFetch ( ) . mockResolvedValue ( textResponse ( "hostname ok" ) ) ;
225+ const tool = await createWebFetchToolForTest ( {
226+ globalSsrFPolicy : { allowedHostnames : [ "global-only.internal" ] } ,
227+ agentSsrFPolicy : { allowedHostnames : [ "matrix.home.arpa" ] } ,
228+ } ) ;
229+
230+ const result = await tool ?. execute ?.( "call" , { url : "https://matrix.home.arpa/status" } ) ;
231+ expect ( result ?. details ) . toMatchObject ( { status : 200 , extractor : "raw" } ) ;
232+ expect ( fetchSpy ) . toHaveBeenCalledTimes ( 1 ) ;
233+ } ) ;
234+
235+ it ( "does not reuse broader-policy cache entries for stricter policies" , async ( ) => {
236+ const url = "http://198.18.0.153/file" ;
237+ lookupMock . mockResolvedValue ( [ { address : "198.18.0.153" , family : 4 } ] ) ;
238+
239+ const fetchSpy = setMockFetch ( ) . mockResolvedValue ( textResponse ( "broad cache" ) ) ;
240+ const broadTool = await createWebFetchToolForTest ( {
241+ agentSsrFPolicy : { dangerouslyAllowPrivateNetwork : true } ,
242+ cacheTtlMinutes : 1 ,
243+ } ) ;
244+ await broadTool ?. execute ?.( "call" , { url } ) ;
245+ expect ( fetchSpy ) . toHaveBeenCalledTimes ( 1 ) ;
246+
247+ const strictTool = await createWebFetchToolForTest ( { cacheTtlMinutes : 1 } ) ;
248+ await expectBlockedUrl ( strictTool , url , / p r i v a t e | i n t e r n a l | b l o c k e d / i) ;
249+ expect ( fetchSpy ) . toHaveBeenCalledTimes ( 1 ) ;
250+ } ) ;
181251} ) ;
0 commit comments