11import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
2- import type { LookupFn } from "../../infra/net/ssrf.js" ;
2+ import * as ssrf from "../../infra/net/ssrf.js" ;
33import { type FetchMock , withFetchPreconnect } from "../../test-utils/fetch-mock.js" ;
44import { makeFetchHeaders } from "./web-fetch.test-harness.js" ;
55import "./web-fetch.test-mocks.js" ;
66
77const lookupMock = vi . fn ( ) ;
8+ const resolvePinnedHostname = ssrf . resolvePinnedHostname ;
89
910function redirectResponse ( location : string ) : Response {
1011 return {
@@ -32,7 +33,11 @@ function setMockFetch(
3233 return fetchSpy ;
3334}
3435
35- async function createWebFetchToolForTest ( params ?: { firecrawlApiKey ?: string } ) {
36+ async function createWebFetchToolForTest ( params ?: {
37+ firecrawlApiKey ?: string ;
38+ ssrfPolicy ?: { allowRfc2544BenchmarkRange ?: boolean } ;
39+ cacheTtlMinutes ?: number ;
40+ } ) {
3641 const { createWebFetchTool } = await import ( "./web-tools.js" ) ;
3742 return createWebFetchTool ( {
3843 config : {
@@ -52,13 +57,14 @@ async function createWebFetchToolForTest(params?: { firecrawlApiKey?: string })
5257 tools : {
5358 web : {
5459 fetch : {
55- cacheTtlMinutes : 0 ,
60+ cacheTtlMinutes : params ?. cacheTtlMinutes ?? 0 ,
61+ ssrfPolicy : params ?. ssrfPolicy ,
5662 ...( params ?. firecrawlApiKey ? { provider : "firecrawl" } : { } ) ,
5763 } ,
5864 } ,
5965 } ,
6066 } ,
61- lookupFn : lookupMock as unknown as LookupFn ,
67+ lookupFn : lookupMock ,
6268 } ) ;
6369}
6470
@@ -74,13 +80,15 @@ describe("web_fetch SSRF protection", () => {
7480 const priorFetch = global . fetch ;
7581
7682 beforeEach ( ( ) => {
77- vi . stubEnv ( "FIRECRAWL_API_KEY" , "" ) ;
83+ vi . spyOn ( ssrf , "resolvePinnedHostname" ) . mockImplementation ( ( hostname ) =>
84+ resolvePinnedHostname ( hostname , lookupMock ) ,
85+ ) ;
7886 } ) ;
7987
8088 afterEach ( ( ) => {
8189 global . fetch = priorFetch ;
8290 lookupMock . mockClear ( ) ;
83- vi . unstubAllEnvs ( ) ;
91+ vi . restoreAllMocks ( ) ;
8492 } ) ;
8593
8694 it ( "blocks localhost hostnames before fetch/firecrawl" , async ( ) => {
@@ -147,4 +155,31 @@ describe("web_fetch SSRF protection", () => {
147155 extractor : "raw" ,
148156 } ) ;
149157 } ) ;
158+
159+ it ( "allows RFC2544 benchmark-range URLs only when web_fetch ssrfPolicy opts in" , async ( ) => {
160+ const url = "http://198.18.0.153/file" ;
161+ lookupMock . mockResolvedValue ( [ { address : "198.18.0.153" , family : 4 } ] ) ;
162+
163+ const deniedTool = await createWebFetchToolForTest ( { cacheTtlMinutes : 1 } ) ;
164+ 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) ;
165+
166+ const fetchSpy = setMockFetch ( ) . mockResolvedValue ( textResponse ( "benchmark ok" ) ) ;
167+ const allowedTool = await createWebFetchToolForTest ( {
168+ ssrfPolicy : { allowRfc2544BenchmarkRange : true } ,
169+ cacheTtlMinutes : 1 ,
170+ } ) ;
171+
172+ const allowed = await allowedTool ?. execute ?.( "call" , { url } ) ;
173+ expect ( allowed ?. details ) . toMatchObject ( {
174+ status : 200 ,
175+ extractor : "raw" ,
176+ } ) ;
177+ expect ( fetchSpy ) . toHaveBeenCalledTimes ( 1 ) ;
178+
179+ // A stricter tool instance must still block the same URL instead of reusing
180+ // the permissive-policy cache entry.
181+ const stricterTool = await createWebFetchToolForTest ( { cacheTtlMinutes : 1 } ) ;
182+ 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) ;
183+ } ) ;
184+
150185} ) ;
0 commit comments