Skip to content

Commit 63b72d9

Browse files
committed
fix(network): scope fake-ip SSRF policy to provider hosts
1 parent 03e35b1 commit 63b72d9

11 files changed

Lines changed: 229 additions & 19 deletions

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Docs: https://docs.openclaw.ai
2929

3030
### Fixes
3131

32+
- Agents/network: allow trusted web-search providers and configured model-provider hosts to work behind Surge/Clash/sing-box fake-IP DNS by accepting RFC 2544 and IPv6 ULA synthetic answers only for the request's scoped hostname, without broad private-network access. Refs #76530 and #76549. Thanks @zqchris.
3233
- Feishu: accept and honor `channels.feishu.blockStreaming` at the top level and per account, while keeping the legacy default off so Feishu cards no longer reject documented config or silently drop block replies. Fixes #75555. Thanks @vincentkoc.
3334
- Google Chat: normalize custom Google auth transport headers before google-auth/gaxios interceptors run, restoring webhook token verification when certificate retrieval expects Fetch `Headers`. Fixes #76742. Thanks @donbowman.
3435
- Doctor/plugins: reset stale `plugins.slots.memory` and `plugins.slots.contextEngine` references during `doctor --fix`, so cleanup of missing plugin config does not leave unrecoverable slot owners behind. Fixes #76550 and #76551. Thanks @vincentkoc.

‎docs/concepts/model-providers.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,7 @@ Example (OpenAI‑compatible):
688688
- For OpenAI-compatible Completions proxies that need vendor-specific fields, set `agents.defaults.models["provider/model"].params.extra_body` (or `extraBody`) to merge extra JSON into the outbound request body.
689689
- For vLLM chat-template controls, set `agents.defaults.models["provider/model"].params.chat_template_kwargs`. The bundled vLLM plugin automatically sends `enable_thinking: false` and `force_nonempty_content: true` for `vllm/nemotron-3-*` when the session thinking level is off.
690690
- For slow local models or remote LAN/tailnet hosts, set `models.providers.<id>.timeoutSeconds`. This extends provider model HTTP request handling, including connect, headers, body streaming, and the total guarded-fetch abort, without increasing the whole agent runtime timeout.
691+
- Model provider HTTP calls allow Surge, Clash, and sing-box fake-IP DNS answers in `198.18.0.0/15` and `fc00::/7` only for the configured provider `baseUrl` hostname. Other private, loopback, link-local, and metadata destinations still require an explicit `models.providers.<id>.request.allowPrivateNetwork: true` opt-in.
691692
- If `baseUrl` is empty/omitted, OpenClaw keeps the default OpenAI behavior (which resolves to `api.openai.com`).
692693
- For safety, an explicit `compat.supportsDeveloperRole: true` is still overridden on non-native `openai-completions` endpoints.
693694
- For `api: "anthropic-messages"` on non-direct endpoints (any provider other than canonical `anthropic`, or a custom `models.providers.anthropic.baseUrl` whose host is not a public `api.anthropic.com` endpoint), OpenClaw suppresses implicit Anthropic beta headers such as `claude-code-20250219`, `interleaved-thinking-2025-05-14`, and OAuth markers, so custom Anthropic-compatible proxies do not reject unsupported beta flags. Set `models.providers.<id>.headers["anthropic-beta"]` explicitly if your proxy needs specific beta features.

‎docs/tools/web.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,18 @@ Codex-capable models can optionally use the provider-native Responses `web_searc
153153

154154
If native Codex search is enabled but the current model is not Codex-capable, OpenClaw keeps the normal managed `web_search` behavior.
155155

156+
## Network safety
157+
158+
Managed `web_search` provider calls use OpenClaw's guarded fetch path. For
159+
trusted provider API hosts, OpenClaw allows Surge, Clash, and sing-box fake-IP
160+
DNS answers in `198.18.0.0/15` and `fc00::/7` only for that provider hostname.
161+
Other private, loopback, link-local, and metadata destinations remain blocked.
162+
163+
This automatic allowance does not apply to arbitrary `web_fetch` URLs. For
164+
`web_fetch`, enable `tools.web.fetch.ssrfPolicy.allowRfc2544BenchmarkRange` and
165+
`tools.web.fetch.ssrfPolicy.allowIpv6UniqueLocalRange` explicitly only when your
166+
trusted proxy owns those synthetic ranges.
167+
156168
## Setting up web search
157169

158170
Provider lists in docs and setup flows are alphabetical. Auto-detection keeps a

‎src/agents/provider-transport-fetch.test.ts‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,67 @@ describe("buildGuardedModelFetch", () => {
7676
);
7777
});
7878

79+
it("scopes fake-IP DNS exemptions to the configured provider host", async () => {
80+
const { buildGuardedModelFetch } = await import("./provider-transport-fetch.js");
81+
const model = {
82+
id: "gpt-5.4",
83+
provider: "openai",
84+
api: "openai-responses",
85+
baseUrl: "https://api.openai.com/v1",
86+
} as unknown as Model<"openai-responses">;
87+
88+
const fetcher = buildGuardedModelFetch(model);
89+
await fetcher("https://api.openai.com/v1/responses", { method: "POST" });
90+
91+
const policy = fetchWithSsrFGuardMock.mock.calls[0]?.[0]?.policy;
92+
expect(policy).toEqual({
93+
allowRfc2544BenchmarkRange: true,
94+
allowIpv6UniqueLocalRange: true,
95+
hostnameAllowlist: ["api.openai.com"],
96+
});
97+
expect(policy?.allowedHostnames).toBeUndefined();
98+
expect(policy?.allowPrivateNetwork).toBeUndefined();
99+
expect(policy?.dangerouslyAllowPrivateNetwork).toBeUndefined();
100+
});
101+
102+
it("does not apply fake-IP exemptions to non-provider hosts", async () => {
103+
const { buildGuardedModelFetch } = await import("./provider-transport-fetch.js");
104+
const model = {
105+
id: "gpt-5.4",
106+
provider: "openai",
107+
api: "openai-responses",
108+
baseUrl: "https://api.openai.com/v1",
109+
} as unknown as Model<"openai-responses">;
110+
111+
const fetcher = buildGuardedModelFetch(model);
112+
await fetcher("https://uploads.openai.com/v1/files", { method: "POST" });
113+
114+
const policy = fetchWithSsrFGuardMock.mock.calls[0]?.[0]?.policy;
115+
expect(policy).toBeUndefined();
116+
});
117+
118+
it("merges explicit private-network opt-in into the provider-host fake-IP policy", async () => {
119+
resolveProviderRequestPolicyConfigMock.mockReturnValueOnce({ allowPrivateNetwork: true });
120+
const { buildGuardedModelFetch } = await import("./provider-transport-fetch.js");
121+
const model = {
122+
id: "qwen3:32b",
123+
provider: "ollama",
124+
api: "ollama",
125+
baseUrl: "http://10.0.0.5:11434",
126+
} as unknown as Model<"ollama">;
127+
128+
const fetcher = buildGuardedModelFetch(model);
129+
await fetcher("http://10.0.0.5:11434/api/chat", { method: "POST" });
130+
131+
const policy = fetchWithSsrFGuardMock.mock.calls[0]?.[0]?.policy;
132+
expect(policy).toEqual({
133+
allowRfc2544BenchmarkRange: true,
134+
allowIpv6UniqueLocalRange: true,
135+
hostnameAllowlist: ["10.0.0.5"],
136+
allowPrivateNetwork: true,
137+
});
138+
});
139+
79140
it("threads explicit transport timeouts into the shared guarded fetch seam", async () => {
80141
const { buildGuardedModelFetch } = await import("./provider-transport-fetch.js");
81142
const model = {

‎src/agents/provider-transport-fetch.ts‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import type { Api, Model } from "@mariozechner/pi-ai";
22
import { fetchWithSsrFGuard } from "../infra/net/fetch-guard.js";
3+
import {
4+
ssrfPolicyFromHttpBaseUrlFakeIpHostnameAllowlist,
5+
type SsrFPolicy,
6+
} from "../infra/net/ssrf.js";
37
import { resolveDebugProxySettings } from "../proxy-capture/env.js";
48
import {
59
buildProviderRequestDispatcherPolicy,
@@ -268,6 +272,44 @@ export function resolveModelRequestTimeoutMs(
268272
: undefined;
269273
}
270274

275+
function resolveHttpHostname(value: unknown): string | undefined {
276+
if (typeof value !== "string" || !value.trim()) {
277+
return undefined;
278+
}
279+
try {
280+
const parsed = new URL(value);
281+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
282+
return undefined;
283+
}
284+
return parsed.hostname.toLowerCase();
285+
} catch {
286+
return undefined;
287+
}
288+
}
289+
290+
function resolveModelTransportSsrFPolicy(params: {
291+
model: Model<Api>;
292+
url: string;
293+
allowPrivateNetwork?: boolean;
294+
}): SsrFPolicy | undefined {
295+
const baseUrl = (params.model as { baseUrl?: unknown }).baseUrl;
296+
const baseHostname = resolveHttpHostname(baseUrl);
297+
const requestHostname = resolveHttpHostname(params.url);
298+
const fakeIpPolicy =
299+
typeof baseUrl === "string" && baseHostname && requestHostname === baseHostname
300+
? ssrfPolicyFromHttpBaseUrlFakeIpHostnameAllowlist(baseUrl)
301+
: undefined;
302+
303+
if (fakeIpPolicy) {
304+
return {
305+
...fakeIpPolicy,
306+
...(params.allowPrivateNetwork ? { allowPrivateNetwork: true } : {}),
307+
};
308+
}
309+
310+
return params.allowPrivateNetwork ? { allowPrivateNetwork: true } : undefined;
311+
}
312+
271313
export function buildGuardedModelFetch(model: Model<Api>, timeoutMs?: number): typeof fetch {
272314
const requestConfig = resolveModelRequestPolicy(model);
273315
const dispatcherPolicy = buildProviderRequestDispatcherPolicy(requestConfig);
@@ -283,6 +325,11 @@ export function buildGuardedModelFetch(model: Model<Api>, timeoutMs?: number): t
283325
: (() => {
284326
throw new Error("Unsupported fetch input for transport-aware model request");
285327
})());
328+
const policy = resolveModelTransportSsrFPolicy({
329+
model,
330+
url,
331+
allowPrivateNetwork: requestConfig.allowPrivateNetwork,
332+
});
286333
const requestInit =
287334
request &&
288335
({
@@ -308,7 +355,7 @@ export function buildGuardedModelFetch(model: Model<Api>, timeoutMs?: number): t
308355
// Provider transport intentionally keeps the secure default and never
309356
// replays unsafe request bodies across cross-origin redirects.
310357
allowCrossOriginUnsafeRedirectReplay: false,
311-
...(requestConfig.allowPrivateNetwork ? { policy: { allowPrivateNetwork: true } } : {}),
358+
...(policy ? { policy } : {}),
312359
});
313360
let response = result.response;
314361
if (shouldBypassLongSdkRetry(response)) {

‎src/agents/tools/web-guarded-fetch.test.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe("web-guarded-fetch", () => {
3030
vi.clearAllMocks();
3131
});
3232

33-
it("uses strict SSRF policy for trusted web tools endpoints", async () => {
33+
it("uses a host-scoped fake-IP SSRF policy for trusted web tools endpoints", async () => {
3434
vi.mocked(fetchWithSsrFGuard).mockResolvedValue({
3535
response: new Response("ok", { status: 200 }),
3636
finalUrl: "https://example.com",
@@ -42,7 +42,11 @@ describe("web-guarded-fetch", () => {
4242
expect(fetchWithSsrFGuard).toHaveBeenCalledWith(
4343
expect.objectContaining({
4444
url: "https://example.com",
45-
policy: {},
45+
policy: {
46+
allowRfc2544BenchmarkRange: true,
47+
allowIpv6UniqueLocalRange: true,
48+
hostnameAllowlist: ["example.com"],
49+
},
4650
mode: GUARDED_FETCH_MODE.TRUSTED_ENV_PROXY,
4751
}),
4852
);
@@ -63,6 +67,7 @@ describe("web-guarded-fetch", () => {
6367
policy: expect.objectContaining({
6468
dangerouslyAllowPrivateNetwork: true,
6569
allowRfc2544BenchmarkRange: true,
70+
allowIpv6UniqueLocalRange: true,
6671
}),
6772
mode: GUARDED_FETCH_MODE.TRUSTED_ENV_PROXY,
6873
}),

‎src/agents/tools/web-guarded-fetch.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ import {
55
withStrictGuardedFetchMode,
66
withTrustedEnvProxyGuardedFetchMode,
77
} from "../../infra/net/fetch-guard.js";
8-
import type { SsrFPolicy } from "../../infra/net/ssrf.js";
8+
import {
9+
ssrfPolicyFromHttpBaseUrlFakeIpHostnameAllowlist,
10+
type SsrFPolicy,
11+
} from "../../infra/net/ssrf.js";
912

10-
const WEB_TOOLS_TRUSTED_NETWORK_SSRF_POLICY: SsrFPolicy = {};
1113
const WEB_TOOLS_SELF_HOSTED_NETWORK_SSRF_POLICY: SsrFPolicy = {
1214
dangerouslyAllowPrivateNetwork: true,
1315
allowRfc2544BenchmarkRange: true,
16+
allowIpv6UniqueLocalRange: true,
1417
};
1518

1619
type WebToolGuardedFetchOptions = Omit<
@@ -66,10 +69,11 @@ export async function withTrustedWebToolsEndpoint<T>(
6669
params: WebToolEndpointFetchOptions,
6770
run: (result: { response: Response; finalUrl: string }) => Promise<T>,
6871
): Promise<T> {
72+
const trustedPolicy = ssrfPolicyFromHttpBaseUrlFakeIpHostnameAllowlist(params.url) ?? {};
6973
return await withWebToolsNetworkGuard(
7074
{
7175
...params,
72-
policy: WEB_TOOLS_TRUSTED_NETWORK_SSRF_POLICY,
76+
policy: trustedPolicy,
7377
useEnvProxy: true,
7478
},
7579
run,

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,43 @@ describe("fetchWithSsrFGuard hardening", () => {
11761176
await result.release();
11771177
});
11781178

1179+
it("does not apply target hostname allowlists to public explicit proxy hosts", async () => {
1180+
(globalThis as Record<string, unknown>)[TEST_UNDICI_RUNTIME_DEPS_KEY] = {
1181+
Agent: agentCtor,
1182+
EnvHttpProxyAgent: envHttpProxyAgentCtor,
1183+
ProxyAgent: proxyAgentCtor,
1184+
fetch: vi.fn(async () => okResponse()),
1185+
};
1186+
const lookupFn: LookupFn = vi.fn(async (hostname: string) => {
1187+
if (hostname === "proxy.example.net") {
1188+
return [{ address: "93.184.216.34", family: 4 }];
1189+
}
1190+
return [{ address: "149.154.167.220", family: 4 }];
1191+
}) as unknown as LookupFn;
1192+
const fetchImpl = vi.fn(async () => okResponse());
1193+
1194+
const result = await fetchWithSsrFGuard({
1195+
url: "https://api.telegram.org/file/bot123/photos/test.jpg",
1196+
fetchImpl,
1197+
lookupFn,
1198+
policy: {
1199+
allowRfc2544BenchmarkRange: true,
1200+
allowIpv6UniqueLocalRange: true,
1201+
hostnameAllowlist: ["api.telegram.org"],
1202+
},
1203+
dispatcherPolicy: {
1204+
mode: "explicit-proxy",
1205+
proxyUrl: "http://proxy.example.net:6152",
1206+
},
1207+
});
1208+
1209+
expect(fetchImpl).toHaveBeenCalledTimes(1);
1210+
expect(lookupFn).toHaveBeenCalledWith("proxy.example.net", { all: true });
1211+
expect(lookupFn).toHaveBeenCalledWith("api.telegram.org", { all: true });
1212+
expect(proxyAgentCtor).toHaveBeenCalled();
1213+
await result.release();
1214+
});
1215+
11791216
it("skips target DNS pinning in trusted explicit-proxy mode after hostname-policy checks", async () => {
11801217
(globalThis as Record<string, unknown>)[TEST_UNDICI_RUNTIME_DEPS_KEY] = {
11811218
Agent: agentCtor,

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -194,21 +194,20 @@ async function assertExplicitProxyAllowed(
194194
if (!["http:", "https:"].includes(parsedProxyUrl.protocol)) {
195195
throw new Error("Explicit proxy URL must use http or https");
196196
}
197+
const proxyPolicy: SsrFPolicy | undefined =
198+
policy || dispatcherPolicy.allowPrivateProxy === true
199+
? {
200+
...policy,
201+
// The proxy hostname is operator-configured, not user input. Target-scoped
202+
// allowlists must not reject a configured proxy host before the request
203+
// target gets checked against that same allowlist below.
204+
hostnameAllowlist: undefined,
205+
...(dispatcherPolicy.allowPrivateProxy === true ? { allowPrivateNetwork: true } : {}),
206+
}
207+
: undefined;
197208
await resolvePinnedHostnameWithPolicy(parsedProxyUrl.hostname, {
198209
lookupFn,
199-
policy:
200-
dispatcherPolicy.allowPrivateProxy === true
201-
? {
202-
// The proxy hostname is operator-configured, not user input.
203-
// Clear the target-scoped hostnameAllowlist so configured proxies
204-
// like localhost or internal hosts aren't rejected by an allowlist
205-
// that was built for the target URL (for example api.example.test).
206-
// Private-network IP checks still apply via allowPrivateNetwork.
207-
...policy,
208-
allowPrivateNetwork: true,
209-
hostnameAllowlist: undefined,
210-
}
211-
: policy,
210+
policy: proxyPolicy,
212211
});
213212
}
214213

‎src/infra/net/ssrf.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
isPrivateIpAddress,
66
isSameSsrFPolicy,
77
ssrfPolicyFromHttpBaseUrlAllowedHostname,
8+
ssrfPolicyFromHttpBaseUrlFakeIpHostnameAllowlist,
89
} from "./ssrf.js";
910

1011
const privateIpCases = [
@@ -125,6 +126,26 @@ describe("ssrfPolicyFromHttpBaseUrlAllowedHostname", () => {
125126
});
126127
});
127128

129+
describe("ssrfPolicyFromHttpBaseUrlFakeIpHostnameAllowlist", () => {
130+
it("builds a host-scoped fake-IP policy from HTTP base URLs", () => {
131+
expect(
132+
ssrfPolicyFromHttpBaseUrlFakeIpHostnameAllowlist(" https://api.example.com/v1 "),
133+
).toEqual({
134+
allowRfc2544BenchmarkRange: true,
135+
allowIpv6UniqueLocalRange: true,
136+
hostnameAllowlist: ["api.example.com"],
137+
});
138+
});
139+
140+
it("ignores empty, invalid, and non-HTTP URLs", () => {
141+
expect(ssrfPolicyFromHttpBaseUrlFakeIpHostnameAllowlist("")).toBeUndefined();
142+
expect(ssrfPolicyFromHttpBaseUrlFakeIpHostnameAllowlist("not-a-url")).toBeUndefined();
143+
expect(
144+
ssrfPolicyFromHttpBaseUrlFakeIpHostnameAllowlist("ftp://api.example.com"),
145+
).toBeUndefined();
146+
});
147+
});
148+
128149
describe("isBlockedHostnameOrIp", () => {
129150
it.each([
130151
"localhost.localdomain",

0 commit comments

Comments
 (0)