惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
Jina AI
Jina AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
美团技术团队
V
Visual Studio Blog
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
有赞技术团队
有赞技术团队
GbyAI
GbyAI
宝玉的分享
宝玉的分享
腾讯CDC
M
MIT News - Artificial intelligence
博客园 - 【当耐特】
Google DeepMind News
Google DeepMind News
月光博客
月光博客
MyScale Blog
MyScale Blog
Last Week in AI
Last Week in AI
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
Recent Announcements
Recent Announcements
MongoDB | Blog
MongoDB | Blog

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
fix(web_fetch): sanitize URL whitespace from LLM tool cal...
zenglingbiao · 2026-06-11 · via Recent Commits to openclaw:main
Original file line numberDiff line numberDiff line change

@@ -42,6 +42,11 @@ function expectRawFetchSuccessDetails(details: unknown) {

4242

expect(typedDetails.extractor).toBe("raw");

4343

}

4444
45+

function firstFetchUrl(fetchSpy: ReturnType<typeof setMockFetch>): string {

46+

const input = fetchSpy.mock.calls[0]?.[0];

47+

return input instanceof Request ? input.url : input instanceof URL ? input.href : input;

48+

}

49+
4550

function createWebFetchToolForTest(params?: {

4651

firecrawlApiKey?: string;

4752

useTrustedEnvProxy?: boolean;

@@ -166,6 +171,36 @@ describe("web_fetch SSRF protection", () => {

166171

expectRawFetchSuccessDetails(result?.details);

167172

});

168173
174+

it("preserves trailing Unicode URL text through tool argument parsing", async () => {

175+

lookupMock.mockResolvedValue([{ address: "93.184.216.34", family: 4 }]);

176+

const fetchSpy = setMockFetch().mockResolvedValue(textResponse("ok"));

177+

const tool = createWebFetchToolForTest();

178+
179+

await tool?.execute?.("call", { url: "https://example.com/a\u00a0" });

180+
181+

expect(firstFetchUrl(fetchSpy)).toBe("https://example.com/a%C2%A0");

182+

});

183+
184+

it("trims leading Unicode whitespace through tool argument parsing", async () => {

185+

lookupMock.mockResolvedValue([{ address: "93.184.216.34", family: 4 }]);

186+

const fetchSpy = setMockFetch().mockResolvedValue(textResponse("ok"));

187+

const tool = createWebFetchToolForTest();

188+
189+

await tool?.execute?.("call", { url: "\u00a0\ufeffhttps://example.com" });

190+
191+

expect(firstFetchUrl(fetchSpy)).toBe("https://example.com/");

192+

});

193+
194+

it("trims trailing Unicode whitespace after a bare authority", async () => {

195+

lookupMock.mockResolvedValue([{ address: "93.184.216.34", family: 4 }]);

196+

const fetchSpy = setMockFetch().mockResolvedValue(textResponse("ok"));

197+

const tool = createWebFetchToolForTest();

198+
199+

await tool?.execute?.("call", { url: "https://example.com\u2003" });

200+
201+

expect(firstFetchUrl(fetchSpy)).toBe("https://example.com/");

202+

});

203+
169204

it("allows RFC2544 benchmark-range URLs only when web_fetch ssrfPolicy opts in", async () => {

170205

// Benchmark ranges are fake-IP infrastructure in some deployments, but

171206

// remain denied unless the web_fetch config opts in.

Original file line numberDiff line numberDiff line change

@@ -0,0 +1,62 @@

1+

import { describe, it, expect } from "vitest";

2+

import { sanitizeWebFetchUrl } from "./web-fetch.js";

3+
4+

describe("sanitizeWebFetchUrl", () => {

5+

it("removes whitespace between scheme and authority (reported bug)", () => {

6+

expect(sanitizeWebFetchUrl("https:// docs.openclaw.ai")).toBe("https://docs.openclaw.ai");

7+

});

8+
9+

it("trims leading and trailing whitespace", () => {

10+

expect(sanitizeWebFetchUrl(" https://example.com ")).toBe("https://example.com");

11+

});

12+
13+

it("trims leading Unicode whitespace", () => {

14+

expect(sanitizeWebFetchUrl("\u00a0\ufeffhttps://example.com")).toBe("https://example.com");

15+

});

16+
17+

it("trims trailing newlines", () => {

18+

expect(sanitizeWebFetchUrl("https://example.com\n")).toBe("https://example.com");

19+

});

20+
21+

it("preserves trailing Unicode whitespace in paths", () => {

22+

expect(sanitizeWebFetchUrl("https://example.com/a\u00a0")).toBe("https://example.com/a\u00a0");

23+

});

24+
25+

it("trims trailing Unicode whitespace after a bare authority", () => {

26+

expect(sanitizeWebFetchUrl("https://example.com\u00a0")).toBe("https://example.com");

27+

});

28+
29+

it("preserves spaces in the path component", () => {

30+

// WHATWG URL parser percent-encodes path spaces — they must not be stripped

31+

const result = sanitizeWebFetchUrl("https://example.com/a b");

32+

expect(result).toBe("https://example.com/a b");

33+

});

34+
35+

it("preserves spaces in the query component", () => {

36+

const result = sanitizeWebFetchUrl("https://example.com?q=a b");

37+

expect(result).toBe("https://example.com?q=a b");

38+

});

39+
40+

it("preserves scheme-like text in the path component", () => {

41+

const result = sanitizeWebFetchUrl("https://example.com/a:// b");

42+

expect(result).toBe("https://example.com/a:// b");

43+

});

44+
45+

it("preserves scheme-like text in the query component", () => {

46+

const result = sanitizeWebFetchUrl("https://example.com?q=x:// y");

47+

expect(result).toBe("https://example.com?q=x:// y");

48+

});

49+
50+

it("preserves percent-encoded characters in path", () => {

51+

const result = sanitizeWebFetchUrl("https://example.com/a%20b");

52+

expect(result).toBe("https://example.com/a%20b");

53+

});

54+
55+

it("does not modify already-valid URLs", () => {

56+

expect(sanitizeWebFetchUrl("https://docs.openclaw.ai")).toBe("https://docs.openclaw.ai");

57+

});

58+
59+

it("handles https:// with tab after scheme", () => {

60+

expect(sanitizeWebFetchUrl("https://\texample.com")).toBe("https://example.com");

61+

});

62+

});

Original file line numberDiff line numberDiff line change

@@ -332,6 +332,24 @@ function throwIfFetchAborted(signal: AbortSignal | undefined): void {

332332

throw signal.reason instanceof Error ? signal.reason : new Error("aborted");

333333

}

334334
335+

/**

336+

* Sanitize a web_fetch URL parameter that may contain LLM-injected whitespace.

337+

*

338+

* Fixes the reported case where a model emits a space between the scheme and

339+

* authority (e.g. `https:// docs.openclaw.ai`), which causes `new URL()` to

340+

* throw. Path and query whitespace is intentionally preserved — the WHATWG URL

341+

* parser percent-encodes those characters correctly per RFC 3986.

342+

*/

343+

export function sanitizeWebFetchUrl(raw: string): string {

344+

let end = raw.length;

345+

while (end > 0 && raw.charCodeAt(end - 1) <= 0x20) {

346+

end -= 1;

347+

}

348+

const trimmed = raw.slice(0, end).replace(/^\s+/, "");

349+

const repaired = trimmed.replace(/^(https?:\/\/)\s+/i, "$1");

350+

return repaired.replace(/^(https?:\/\/[^/?#\s]+)\s+$/i, "$1");

351+

}

352+
335353

function normalizeProviderWebFetchPayload(params: {

336354

providerId: string;

337355

payload: unknown;

@@ -703,7 +721,9 @@ export function createWebFetchTool(options?: {

703721

return providerFallbackCache;

704722

};

705723

const params = args as Record<string, unknown>;

706-

const url = readStringParam(params, "url", { required: true });

724+

const url = sanitizeWebFetchUrl(

725+

readStringParam(params, "url", { required: true, trim: false }),

726+

);

707727

const extractMode = readStringParam(params, "extractMode") === "text" ? "text" : "markdown";

708728

const maxChars = readPositiveIntegerParam(params, "maxChars");

709729

const maxCharsCap = resolveFetchMaxCharsCap(executionFetch);