fix(web_fetch): sanitize URL to strip markdown backticks and quotes before validation

LLM-generated tool calls may wrap URLs in markdown backticks or quotes
(e.g. \https://example.com\), causing urlparse to produce empty scheme
and netloc, which leads to all fetch attempts failing silently.

Add URL cleaning at the top of WebFetchTool.execute to strip whitespace,
backticks, double quotes, and single quotes, plus an early rejection guard
for non-http(s) URLs after cleaning.
This commit is contained in:
彭星杰
2026-05-01 19:58:19 +08:00
committed by Xubin Ren
parent 43a58335f6
commit 5dc96505e8
2 changed files with 142 additions and 0 deletions
+3
View File
@@ -388,6 +388,9 @@ class WebFetchTool(Tool):
max_chars: int | None = None,
**kwargs: Any,
) -> Any:
url = url.strip().strip("`").strip('"').strip("'")
if not (url.startswith("http://") or url.startswith("https://")):
return json.dumps({"error": "Invalid URL after cleaning", "url": url}, ensure_ascii=False)
extract_mode = kwargs.pop("extractMode", extract_mode)
max_chars = kwargs.pop("maxChars", max_chars) or self.max_chars
is_valid, error_msg = _validate_url_safe(url)