Skip to content

Commit e3bbf53

Browse files
committed
feat(outbound): route Clash fake-IP DNS answers through the configured proxy (#1748)
Scoped re-implementation of PR #1748 per the campaign disposition (REDESIGN-SMALL, outbound-only): - resolvePublicAddresses gains an explicit allowBenchmarkAddresses opt-in: a HOSTNAME answer in 198.18.0.0/15 (IANA benchmark space, the Clash/Surge/Mihomo fake-IP DNS range) is accepted without marking the destination private. Literal 198.18.x URLs still reject, mixed answers containing any other non-public address still reject, and callers that do not pass the flag (image fetch, Lab fetch) keep rejecting — the SSRF widening the original PR had is avoided. - provider-outbound passes allowBenchmarkAddresses only when an outbound HTTP(S) proxy is configured, so the hostname rides the proxy CONNECT instead of pin-connecting to the fake IP. NO_PROXY corner documented. - 5 destination-policy cases + proxy integration cases. Credit: luvs01 (original PR #1748).
1 parent 69650fa commit e3bbf53

4 files changed

Lines changed: 154 additions & 1 deletion

File tree

‎src/lib/destination-policy.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,18 @@ export function assessUrlDestination(url: string): UrlDestinationAssessment | nu
241241
* Returns the validated addresses so direct callers can pin the connect peer and
242242
* avoid a second, rebindable resolution. DNS failures remain fail-closed here;
243243
* the provider proxy wrapper alone may recognize that typed failure and degrade.
244+
*
245+
* `allowBenchmarkAddresses` is an explicit outbound-only opt-in for Clash/Surge/
246+
* Mihomo fake-IP DNS (IANA benchmark space 198.18.0.0/15, credit #1748): a hostname
247+
* answer in that range is accepted without marking the destination private, so the
248+
* caller can keep the hostname on its configured HTTP(S) proxy path. It applies to
249+
* resolved answers only — a literal 198.18.x URL still rejects — and mixed answers
250+
* that include any other non-public address still fail. Callers that do not pass it
251+
* (image and Lab fetch) keep rejecting benchmark space.
244252
*/
245253
export async function resolvePublicAddresses(
246254
url: string,
247-
options?: string | { context?: string; allowPrivateNetwork?: boolean },
255+
options?: string | { context?: string; allowPrivateNetwork?: boolean; allowBenchmarkAddresses?: boolean },
248256
): Promise<{
249257
hostname: string;
250258
addresses: { address: string; family: number }[];
@@ -254,6 +262,7 @@ export async function resolvePublicAddresses(
254262
? `${options.trim() || "image"} URL`
255263
: options?.context?.trim() || "image URL";
256264
const privateNetworkAllowed = typeof options === "object" && options?.allowPrivateNetwork === true;
265+
const benchmarkAllowed = typeof options === "object" && options?.allowBenchmarkAddresses === true;
257266
let hostname: string;
258267
try {
259268
hostname = normalizeHostname(new URL(url.trim()).hostname);
@@ -294,6 +303,14 @@ export async function resolvePublicAddresses(
294303
const ipKind = isIP(address) || (family === 4 || family === 6 ? family : 0);
295304
const assessment = ipKind === 4 ? classifyIpv4(address) : ipKind === 6 ? classifyIpv6(normalizeHostname(address)) : null;
296305
if (!assessment || assessment.kind !== "public") {
306+
// Hostname → 198.18.0.0/15 under the explicit opt-in is Clash/Surge/Mihomo
307+
// fake-IP DNS, not a LAN provider. Accept it without allowPrivateNetwork and
308+
// do not mark the destination private, so the caller's HTTP(S)_PROXY path
309+
// still applies (credit #1748).
310+
if (benchmarkAllowed && assessment?.kind === "private" && assessment.detail === "benchmark address") {
311+
validatedAddresses.push({ address, family: ipKind === 4 || ipKind === 6 ? ipKind : (family || 4) });
312+
continue;
313+
}
297314
const allowedPrivateAddress = privateNetworkAllowed
298315
&& assessment
299316
&& (assessment.kind === "loopback" || assessment.kind === "private");

‎src/lib/provider-outbound.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,17 @@ async function providerOutboundRequest(
148148
resolved = await resolveAddresses(url, {
149149
context: "provider URL",
150150
allowPrivateNetwork: allowPrivate,
151+
// Clash/Surge/Mihomo fake-IP DNS (198.18.0.0/15) answers are admitted only
152+
// when an outbound proxy is configured: the hostname then rides the proxy as
153+
// an ordinary CONNECT instead of failing as a private destination or being
154+
// pin-connected to the fake-IP (credit #1748). Without a proxy, benchmark
155+
// answers keep rejecting. Image/Lab fetch never passes this flag.
156+
// Known corner: the opt-in arms on the GLOBAL proxy config, not per-host. If
157+
// NO_PROXY excludes this host, Bun bypasses the proxy and direct-connects to
158+
// the benchmark answer — non-routable space typically intercepted by the
159+
// local fake-IP TUN, so not an SSRF widening, but the CONNECT claim does not
160+
// hold for NO_PROXY-excluded hosts.
161+
allowBenchmarkAddresses: proxyConfigured,
151162
});
152163
} catch (error) {
153164
const dnsResolutionFailed = error instanceof DestinationDnsResolutionError

‎tests/destination-policy-resolved.test.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,56 @@ describe("resolvePublicAddresses — caller-specific diagnostics", () => {
200200
expect(resolved.privateNetwork).toBe(true);
201201
expect(resolved.addresses).toEqual([{ address: "192.168.1.50", family: 4 }]);
202202
});
203+
204+
test("hostname Clash fake-IP answers are accepted only under the explicit benchmark opt-in (#1748)", async () => {
205+
lookupMock.mockResolvedValueOnce([{ address: "198.18.56.214", family: 4 }]);
206+
207+
const resolved = await resolvePublicAddresses(
208+
"https://www.packyapi.com/v1/models",
209+
{ context: "provider URL", allowBenchmarkAddresses: true },
210+
);
211+
212+
expect(resolved.privateNetwork).toBe(false);
213+
expect(resolved.addresses).toEqual([{ address: "198.18.56.214", family: 4 }]);
214+
});
215+
216+
test("hostname Clash fake-IP answers still reject without the benchmark opt-in", async () => {
217+
lookupMock.mockResolvedValueOnce([{ address: "198.18.56.214", family: 4 }]);
218+
219+
await expect(resolvePublicAddresses(
220+
"https://www.packyapi.com/v1/models",
221+
{ context: "provider URL" },
222+
)).rejects.toThrow("benchmark address (198.18.56.214)");
223+
});
224+
225+
test("benchmark opt-in mixed with RFC1918 still requires the private-network opt-in", async () => {
226+
lookupMock.mockResolvedValueOnce([
227+
{ address: "198.18.56.214", family: 4 },
228+
{ address: "10.0.0.5", family: 4 },
229+
]);
230+
231+
await expect(resolvePublicAddresses(
232+
"https://rebind.example.com/v1/models",
233+
{ context: "provider URL", allowBenchmarkAddresses: true },
234+
)).rejects.toThrow("private-network address (10.0.0.5)");
235+
});
236+
237+
test("benchmark opt-in does not admit a literal 198.18.x URL", async () => {
238+
await expect(resolvePublicAddresses(
239+
"https://198.18.56.214/v1/models",
240+
{ context: "provider URL", allowBenchmarkAddresses: true },
241+
)).rejects.toThrow("benchmark address");
242+
});
243+
244+
test("image/Lab fetch (no opt-in) still rejects hostnames resolving to 198.18.x (#1748 SSRF guard)", async () => {
245+
lookupMock.mockResolvedValueOnce([{ address: "198.18.4.2", family: 4 }]);
246+
await expect(resolvePublicAddresses("https://fakeip.example.com/img.png"))
247+
.rejects.toThrow("image URL hostname fakeip.example.com resolves to benchmark address (198.18.4.2)");
248+
249+
lookupMock.mockResolvedValueOnce([{ address: "198.19.7.9", family: 4 }]);
250+
await expect(resolvePublicAddresses(
251+
"https://fakeip.example.com/v1/models",
252+
{ context: "Lab provider destination", allowPrivateNetwork: false },
253+
)).rejects.toThrow("benchmark address (198.19.7.9)");
254+
});
203255
});

‎tests/provider-outbound.test.ts‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,79 @@ describe("provider outbound GET transport", () => {
9797
expect(captured.address).toBeUndefined();
9898
});
9999

100+
test("Clash fake-IP behind a configured proxy uses hostname CONNECT instead of NO_PROXY (#1748)", async () => {
101+
const proxyUrl = "http://127.0.0.1:9";
102+
process.env.HTTPS_PROXY = proxyUrl;
103+
process.env.https_proxy = proxyUrl;
104+
process.env.NO_PROXY = "localhost,127.0.0.1,::1,[::1]";
105+
process.env.no_proxy = "localhost,127.0.0.1,::1,[::1]";
106+
const originalFetch = globalThis.fetch;
107+
const fetchMock = mock(async (url: string | URL | Request, init?: RequestInit) => {
108+
expect(String(url)).toBe("https://www.packyapi.com/v1/models");
109+
expect(init?.redirect).toBe("manual");
110+
return new Response('{"data":[{"id":"gpt-5.5"}]}', {
111+
status: 200,
112+
headers: { "content-type": "application/json" },
113+
});
114+
}) as typeof fetch;
115+
globalThis.fetch = fetchMock;
116+
try {
117+
const { providerOutboundGet } = await import("../src/lib/provider-outbound");
118+
const resolveOptions: { allowBenchmarkAddresses?: boolean }[] = [];
119+
const { dependencies, captured } = directDependencies(new Response(null, { status: 500 }));
120+
const innerResolve = dependencies.resolveAddresses!;
121+
dependencies.resolveAddresses = mock(async (url: string, options?: { allowBenchmarkAddresses?: boolean }) => {
122+
resolveOptions.push({ allowBenchmarkAddresses: options?.allowBenchmarkAddresses });
123+
await innerResolve(url, options);
124+
// What the real resolver returns for a fake-IP-only answer under the
125+
// outbound benchmark opt-in: accepted, and NOT marked private.
126+
return {
127+
hostname: "www.packyapi.com",
128+
addresses: [{ address: "198.18.56.214", family: 4 }],
129+
privateNetwork: false,
130+
};
131+
}) as ProviderOutboundDependencies["resolveAddresses"];
132+
133+
const response = await providerOutboundGet(
134+
"packy",
135+
{ baseUrl: "https://www.packyapi.com/v1" },
136+
"https://www.packyapi.com/v1/models",
137+
{},
138+
dependencies,
139+
);
140+
141+
expect(await response.json()).toEqual({ data: [{ id: "gpt-5.5" }] });
142+
expect(fetchMock).toHaveBeenCalledTimes(1);
143+
expect(captured.address).toBeUndefined();
144+
// The wrapper enables the benchmark opt-in only because a proxy is configured.
145+
expect(resolveOptions).toEqual([{ allowBenchmarkAddresses: true }]);
146+
} finally {
147+
globalThis.fetch = originalFetch;
148+
}
149+
});
150+
151+
test("Clash fake-IP without a configured proxy is not granted the benchmark opt-in (#1748)", async () => {
152+
for (const key of proxyKeys) delete process.env[key];
153+
const { providerOutboundGet } = await import("../src/lib/provider-outbound");
154+
const resolveOptions: { allowBenchmarkAddresses?: boolean }[] = [];
155+
const { dependencies, captured } = directDependencies(new Response(null, { status: 500 }));
156+
dependencies.resolveAddresses = mock(async (_url: string, options?: { allowBenchmarkAddresses?: boolean }) => {
157+
resolveOptions.push({ allowBenchmarkAddresses: options?.allowBenchmarkAddresses });
158+
// What the real resolver does without the opt-in: benchmark answers reject.
159+
throw new Error("provider URL hostname www.packyapi.com resolves to benchmark address (198.18.56.214)");
160+
}) as ProviderOutboundDependencies["resolveAddresses"];
161+
162+
await expect(providerOutboundGet(
163+
"packy",
164+
{ baseUrl: "https://www.packyapi.com/v1" },
165+
"https://www.packyapi.com/v1/models",
166+
{},
167+
dependencies,
168+
)).rejects.toThrow(/benchmark address/);
169+
expect(captured.address).toBeUndefined();
170+
expect(resolveOptions).toEqual([{ allowBenchmarkAddresses: false }]);
171+
});
172+
100173
test("built-in ollama admits loopback discovery without an explicit allowPrivateNetwork flag (#758)", async () => {
101174
for (const key of proxyKeys) delete process.env[key];
102175
const { providerOutboundGet } = await import("../src/lib/provider-outbound");

0 commit comments

Comments
 (0)