Skip to content

Commit e9a39b1

Browse files
momothemagejoshavant
authored andcommitted
fix: route strict guarded fetches through managed proxy without local DNS
1 parent 1a508d7 commit e9a39b1

3 files changed

Lines changed: 149 additions & 16 deletions

File tree

‎src/infra/net/configured-local-origin-bypass.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ function isPinnedLoopbackTarget(addresses: readonly string[]): boolean {
6060
return addresses.length > 0 && addresses.every((address) => isLoopbackIpAddress(address));
6161
}
6262

63+
/** Return whether proving a configured local-origin bypass requires target DNS. */
64+
export function shouldResolveConfiguredLocalOriginManagedProxyBypass(params: {
65+
url: URL;
66+
managedProxyBypass: ConfiguredLocalOriginManagedProxyBypass | undefined;
67+
}): boolean {
68+
return isExactConfiguredLocalOriginBypass(params);
69+
}
70+
6371
/** Return whether a configured local provider origin may bypass the managed proxy. */
6472
export function shouldUseConfiguredLocalOriginManagedProxyBypass(params: {
6573
url: URL;

‎src/infra/net/fetch-guard.ssrf.test.ts‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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(/private|internal|blocked/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(/private|internal|blocked/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,

‎src/infra/net/fetch-guard.ts‎

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import {
99
} from "../fetch-headers.js";
1010
import {
1111
shouldUseConfiguredLocalOriginManagedProxyBypass,
12+
shouldResolveConfiguredLocalOriginManagedProxyBypass,
1213
type ConfiguredLocalOriginManagedProxyBypass,
1314
} from "./configured-local-origin-bypass.js";
14-
import { hasProxyEnvConfigured, shouldUseEnvHttpProxyForUrl } from "./proxy-env.js";
15+
import { shouldUseEnvHttpProxyForUrl } from "./proxy-env.js";
1516
import { retainSafeHeadersForCrossOriginRedirect as retainSafeRedirectHeaders } from "./redirect-headers.js";
1617
import {
1718
fetchWithRuntimeDispatcher,
@@ -502,8 +503,17 @@ async function fetchWithSsrFGuardInternal(
502503
usesTrustedExplicitProxyMode ? false : params.pinDns,
503504
);
504505
await assertExplicitProxyAllowed(dispatcherPolicy, params.lookupFn, params.policy);
506+
const isStrictManagedProxyActive =
507+
mode === GUARDED_FETCH_MODE.STRICT && isManagedProxyActive();
508+
const shouldCheckManagedProxyBypass =
509+
isStrictManagedProxyActive &&
510+
shouldResolveConfiguredLocalOriginManagedProxyBypass({
511+
url: parsedUrl,
512+
managedProxyBypass: params.managedProxyBypass,
513+
});
505514
const canUseManagedProxy =
506-
mode === GUARDED_FETCH_MODE.STRICT && isManagedProxyActive() && hasProxyEnvConfigured();
515+
isStrictManagedProxyActive &&
516+
(shouldUseEnvHttpProxyForUrl(parsedUrl.toString()) || shouldCheckManagedProxyBypass);
507517
const canUseTrustedEnvProxy =
508518
(mode === GUARDED_FETCH_MODE.TRUSTED_ENV_PROXY ||
509519
(params.useEnvProxyForEligibleUrls === true && !canUseManagedProxy)) &&
@@ -518,26 +528,30 @@ async function fetchWithSsrFGuardInternal(
518528
params.pinDns !== false;
519529
const timeoutMs = resolveDispatcherTimeoutMs(params.timeoutMs);
520530

521-
// Trusted env-proxy and pinDns=false can skip local DNS pinning, so keep
522-
// the pre-DNS hostname/IP policy checks from the pinned path.
523-
if (canUseTrustedEnvProxy || params.pinDns === false) {
531+
// Trusted env-proxy, managed proxy, and pinDns=false can skip local DNS
532+
// pinning, so keep the pre-DNS hostname/IP policy checks from the pinned path.
533+
if (canUseTrustedEnvProxy || canUseManagedProxy || params.pinDns === false) {
524534
assertHostnameAllowedWithPolicy(parsedUrl.hostname, policyForUrl);
525535
}
526536

527537
if (canUseTrustedEnvProxy) {
528538
dispatcher = createHttp1EnvHttpProxyAgent(undefined, timeoutMs);
529539
} else if (canUseManagedProxy) {
530-
const pinned = await resolvePinnedHostnameWithPolicy(parsedUrl.hostname, {
531-
lookupFn: params.lookupFn,
532-
policy: policyForUrl,
533-
});
534-
dispatcher = shouldUseConfiguredLocalOriginManagedProxyBypass({
535-
url: parsedUrl,
536-
managedProxyBypass: params.managedProxyBypass,
537-
resolvedAddresses: pinned.addresses,
538-
})
539-
? createPinnedDispatcher(pinned, dispatcherPolicy, policyForUrl, timeoutMs)
540-
: createHttp1EnvHttpProxyAgent(undefined, timeoutMs);
540+
if (shouldCheckManagedProxyBypass) {
541+
const pinned = await resolvePinnedHostnameWithPolicy(parsedUrl.hostname, {
542+
lookupFn: params.lookupFn,
543+
policy: policyForUrl,
544+
});
545+
dispatcher = shouldUseConfiguredLocalOriginManagedProxyBypass({
546+
url: parsedUrl,
547+
managedProxyBypass: params.managedProxyBypass,
548+
resolvedAddresses: pinned.addresses,
549+
})
550+
? createPinnedDispatcher(pinned, dispatcherPolicy, policyForUrl, timeoutMs)
551+
: createHttp1EnvHttpProxyAgent(undefined, timeoutMs);
552+
} else {
553+
dispatcher = createHttp1EnvHttpProxyAgent(undefined, timeoutMs);
554+
}
541555
} else if (usesTrustedExplicitProxyMode) {
542556
// Explicit proxy targets are still checked against the caller's hostname
543557
// policy, but the proxy does the DNS resolution for the final target.

0 commit comments

Comments
 (0)