@@ -257,6 +257,7 @@ describe("fetchWithSsrFGuard hardening", () => {
257257 } ) ;
258258
259259 expect ( fetchImpl ) . toHaveBeenCalledTimes ( 1 ) ;
260+ expect ( lookupFn ) . toHaveBeenCalledTimes ( params . expectEnvProxy ? 0 : 1 ) ;
260261 if ( params . expectEnvProxy ) {
261262 expect ( envHttpProxyAgentCtor ) . toHaveBeenCalledTimes ( 1 ) ;
262263 expect ( envHttpProxyAgentCtor ) . toHaveBeenCalledWith ( {
@@ -1415,6 +1416,94 @@ describe("fetchWithSsrFGuard hardening", () => {
14151416 } ) ;
14161417 } ) ;
14171418
1419+ it ( "does not resolve target DNS before strict managed-proxy dispatch" , async ( ) => {
1420+ installManagedProxyRuntime ( ) ;
1421+ const lookupFn : LookupFn = vi . fn ( async ( hostname : string ) => {
1422+ throw new Error ( `unexpected target DNS lookup for ${ hostname } ` ) ;
1423+ } ) as unknown as LookupFn ;
1424+ const fetchImpl = vi . fn ( async ( _input : RequestInfo | URL , init ?: RequestInit ) => {
1425+ const requestInit = init as RequestInit & { dispatcher ?: unknown } ;
1426+ expectDispatcherAttached ( requestInit . dispatcher ) ;
1427+ return okResponse ( ) ;
1428+ } ) ;
1429+
1430+ const result = await fetchWithSsrFGuard ( {
1431+ url : "https://public.example/resource" ,
1432+ fetchImpl,
1433+ lookupFn,
1434+ } ) ;
1435+
1436+ expect ( fetchImpl ) . toHaveBeenCalledTimes ( 1 ) ;
1437+ expect ( lookupFn ) . not . toHaveBeenCalled ( ) ;
1438+ expect ( envHttpProxyAgentCtor ) . toHaveBeenCalledTimes ( 1 ) ;
1439+ await result . release ( ) ;
1440+ } ) ;
1441+
1442+ it ( "falls back to strict DNS pinning when active managed proxy does not apply" , async ( ) => {
1443+ installManagedProxyRuntime ( ) ;
1444+ vi . stubEnv ( "NO_PROXY" , "public.example" ) ;
1445+ const lookupFn = createPublicLookup ( ) ;
1446+ const fetchImpl = vi . fn ( async ( _input : RequestInfo | URL , init ?: RequestInit ) => {
1447+ const requestInit = init as RequestInit & { dispatcher ?: unknown } ;
1448+ expectDispatcherAttached ( requestInit . dispatcher ) ;
1449+ expect ( getDispatcherClassName ( requestInit . dispatcher ) ) . not . toBe ( "EnvHttpProxyAgent" ) ;
1450+ return okResponse ( ) ;
1451+ } ) ;
1452+
1453+ const result = await fetchWithSsrFGuard ( {
1454+ url : "https://public.example/resource" ,
1455+ fetchImpl,
1456+ lookupFn,
1457+ } ) ;
1458+
1459+ expect ( fetchImpl ) . toHaveBeenCalledTimes ( 1 ) ;
1460+ expect ( lookupFn ) . toHaveBeenCalledWith ( "public.example" , { all : true } ) ;
1461+ expect ( envHttpProxyAgentCtor ) . not . toHaveBeenCalled ( ) ;
1462+ expect ( agentCtor ) . toHaveBeenCalledTimes ( 1 ) ;
1463+ await result . release ( ) ;
1464+ } ) ;
1465+
1466+ it . each ( [
1467+ "http://127.0.0.1:8080/internal" ,
1468+ "http://metadata.google.internal/computeMetadata/v1/" ,
1469+ ] ) ( "blocks %s before strict managed-proxy dispatch" , async ( url ) => {
1470+ installManagedProxyRuntime ( ) ;
1471+ const lookupFn = vi . fn ( ) as unknown as LookupFn ;
1472+ const fetchImpl = vi . fn ( async ( ) => okResponse ( ) ) ;
1473+
1474+ await expect (
1475+ fetchWithSsrFGuard ( {
1476+ url,
1477+ fetchImpl,
1478+ lookupFn,
1479+ } ) ,
1480+ ) . rejects . toThrow ( / p r i v a t e | i n t e r n a l | b l o c k e d / i) ;
1481+
1482+ expect ( lookupFn ) . not . toHaveBeenCalled ( ) ;
1483+ expect ( fetchImpl ) . not . toHaveBeenCalled ( ) ;
1484+ expect ( envHttpProxyAgentCtor ) . not . toHaveBeenCalled ( ) ;
1485+ } ) ;
1486+
1487+ it ( "revalidates redirects before strict managed-proxy dispatch" , async ( ) => {
1488+ installManagedProxyRuntime ( ) ;
1489+ const lookupFn = vi . fn ( ) as unknown as LookupFn ;
1490+ const fetchImpl = vi
1491+ . fn ( )
1492+ . mockResolvedValueOnce ( redirectResponse ( "http://127.0.0.1:8080/internal" ) ) ;
1493+
1494+ await expect (
1495+ fetchWithSsrFGuard ( {
1496+ url : "https://public.example/start" ,
1497+ fetchImpl,
1498+ lookupFn,
1499+ } ) ,
1500+ ) . rejects . toThrow ( / p r i v a t e | i n t e r n a l | b l o c k e d / i) ;
1501+
1502+ expect ( fetchImpl ) . toHaveBeenCalledTimes ( 1 ) ;
1503+ expect ( lookupFn ) . not . toHaveBeenCalled ( ) ;
1504+ expect ( envHttpProxyAgentCtor ) . toHaveBeenCalledTimes ( 1 ) ;
1505+ } ) ;
1506+
14181507 it . each ( [
14191508 {
14201509 name : "an exact configured local provider origin" ,
@@ -1648,6 +1737,28 @@ describe("fetchWithSsrFGuard hardening", () => {
16481737 } ) ;
16491738 } ) ;
16501739
1740+ it ( "honors proxy.loopbackMode=block for configured local origins before NO_PROXY fallback" , async ( ) => {
1741+ installManagedProxyRuntime ( "block" ) ;
1742+ vi . stubEnv ( "NO_PROXY" , "127.0.0.1,localhost" ) ;
1743+ vi . stubEnv ( "no_proxy" , "127.0.0.1,localhost" ) ;
1744+ const fetchImpl = vi . fn ( async ( ) => okResponse ( ) ) ;
1745+
1746+ await expect (
1747+ fetchConfiguredLocalOriginWithSsrFGuard ( {
1748+ url : "http://127.0.0.1:11434/api/embed" ,
1749+ fetchImpl,
1750+ lookupFn : createLoopbackLookup ( ) ,
1751+ policy : { allowedOrigins : [ "http://127.0.0.1:11434" ] } ,
1752+ configuredLocalOriginBaseUrl : "http://127.0.0.1:11434" ,
1753+ auditContext : "ollama-memory-embedding" ,
1754+ } ) ,
1755+ ) . rejects . toThrow ( "blocked by proxy.loopbackMode" ) ;
1756+
1757+ expect ( fetchImpl ) . not . toHaveBeenCalled ( ) ;
1758+ expect ( agentCtor ) . not . toHaveBeenCalled ( ) ;
1759+ expect ( envHttpProxyAgentCtor ) . not . toHaveBeenCalled ( ) ;
1760+ } ) ;
1761+
16511762 it ( "routes through env proxy when trusted proxy mode is explicitly enabled" , async ( ) => {
16521763 await runProxyModeDispatcherExpectation ( {
16531764 mode : GUARDED_FETCH_MODE . TRUSTED_ENV_PROXY ,
0 commit comments