JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.
Python
fix(web-fetch): 拦截 doc/pdf 等二进制,避免 fetch_webpage 乱码
openJiuwen-bot4188d39c5c82
diff --git a/jiuwenswarm/agents/harness/common/tools/web_fetch_tools.py b/jiuwenswarm/agents/harness/common/tools/web_fetch_tools.pyindex 15e4ca7241..80d8e36343 100644--- a/jiuwenswarm/agents/harness/common/tools/web_fetch_tools.py+++ b/jiuwenswarm/agents/harness/common/tools/web_fetch_tools.py@@ -42,6 +42,73 @@ _CHARSET_META_RE = re.compile(br"""<meta[^>]+charset=["']?\s*([A-Za-z0-9._-]+)""",flags=re.IGNORECASE,)+# Non-webpage binaries that historically leaked as mojibake via text decode.+_BINARY_URL_SUFFIXES = (+ ".doc",+ ".docx",+ ".xls",+ ".xlsx",+ ".ppt",+ ".pptx",+ ".pdf",+ ".zip",+ ".rar",+ ".7z",+ ".gz",+ ".tar",+ ".tgz",+ ".exe",+ ".dll",+ ".msi",+ ".dmg",+ ".apk",+ ".iso",+ ".mp3",+ ".mp4",+ ".avi",+ ".mov",+ ".mkv",+ ".wav",+ ".png",+ ".jpg",+ ".jpeg",+ ".gif",+ ".webp",+ ".bmp",+ ".ico",+ ".woff",+ ".woff2",+ ".ttf",+ ".otf",+ ".eot",+)+_BINARY_CONTENT_TYPE_MARKERS = (+ "application/msword",+ "application/pdf",+ "application/zip",+ "application/x-zip",+ "application/gzip",+ "application/x-gzip",+ "application/x-rar",+ "application/x-7z-compressed",+ "application/vnd.openxmlformats",+ "application/vnd.ms-",+ "application/vnd.oasis",+ "image/",+ "audio/",+ "video/",+ "font/",+)+_BINARY_MAGIC_PREFIXES = (+ (b"\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1", "ole/cfbf"),+ (b"%PDF", "pdf"),+ (b"PK\x03\x04", "zip"),+ (b"\x89PNG", "png"),+ (b"\xff\xd8\xff", "jpeg"),+ (b"GIF8", "gif"),+ (b"Rar!\x1a\x07", "rar"),+ (b"7z\xbc\xaf'\x1c", "7z"),+)def _extract_declared_charset(response: requests.Response) -> str:@@ -193,6 +260,95 @@ def _normalize_url(url: str) -> str:return f"https://{decoded}"+def _binary_url_reason(url: str) -> str:+ """Return a short reason when the URL path looks like a non-webpage binary."""+ path = unquote(urlparse(url).path or "").lower()+ for suffix in _BINARY_URL_SUFFIXES:+ if path.endswith(suffix):+ return f"url ends with {suffix}"+ return ""+++def _binary_magic_reason(raw: bytes) -> str:+ if not raw:+ return ""+ for magic, name in _BINARY_MAGIC_PREFIXES:+ if raw.startswith(magic):+ return f"magic:{name}"+ return ""+++def _looks_like_textual_payload(raw: bytes) -> bool:+ """Best-effort sniff for text/HTML when Content-Type is octet-stream."""+ sample = (raw or b"")[:2048].lstrip(b"\xef\xbb\xbf \t\r\n")+ if not sample:+ return True+ if sample.startswith(+ (b"<!DOCTYPE", b"<!doctype", b"<html", b"<HTML", b"<?xml", b"{", b"[")+ ):+ return True+ if sample.count(b"\x00") > 2:+ return False+ for enc in ("utf-8", "gb18030"):+ try:+ text = sample.decode(enc)+ except UnicodeDecodeError:+ continue+ if not text:+ return True+ printable = sum(1 for c in text if c.isprintable() or c in "\n\r\t")+ return (printable / len(text)) >= 0.9+ return False+++def _binary_content_type_reason(content_type: str, raw: bytes) -> str:+ ct = (content_type or "").lower().split(";", 1)[0].strip()+ if not ct:+ return ""+ if (+ "html" in ct+ or ct.startswith("text/")+ or ct+ in {+ "application/json",+ "application/xml",+ "application/javascript",+ "application/xhtml+xml",+ "image/svg+xml",+ }+ ):+ return ""+ if ct == "application/octet-stream":+ magic = _binary_magic_reason(raw)+ if magic:+ return f"content-type={ct}; {magic}"+ if _looks_like_textual_payload(raw):+ return ""+ return f"content-type={ct}"+ for marker in _BINARY_CONTENT_TYPE_MARKERS:+ if marker in ct:+ return f"content-type={ct}"+ return ""+++def _unsupported_binary_reason(+ *,+ url: str,+ content_type: str = "",+ raw: bytes | None = None,+) -> str:+ """Detect non-text payloads that must not be decoded as webpage text."""+ url_reason = _binary_url_reason(url)+ if url_reason:+ return url_reason+ if raw is None:+ return ""+ magic = _binary_magic_reason(raw)+ if magic:+ return magic+ return _binary_content_type_reason(content_type, raw)++def _fetch_via_jina_reader_sync(url: str, timeout_seconds: int) -> dict[str, str | int]:reader_url = f"https://r.jina.ai/{url}"response = _http_get(reader_url, headers=_REQUEST_HEADERS, timeout=timeout_seconds)@@ -206,6 +362,10 @@ def _fetch_via_jina_reader_sync(url: str, timeout_seconds: int) -> dict[str, strdef _fetch_webpage_sync(url: str, timeout_seconds: int) -> dict[str, str | int]:+ binary_reason = _binary_url_reason(url)+ if binary_reason:+ raise ValueError(f"unsupported binary content ({binary_reason})")+response = _http_get(url, headers=_REQUEST_HEADERS, timeout=timeout_seconds)if response.status_code in {401, 403, 429} and _env_bool(_JINA_FETCH_ENABLED_ENV,@@ -214,8 +374,16 @@ def _fetch_webpage_sync(url: str, timeout_seconds: int) -> dict[str, str | int]:return _fetch_via_jina_reader_sync(url, timeout_seconds)response.raise_for_status()+ content_type = response.headers.get("Content-Type", "") or ""+ raw = response.content or b""+ binary_reason = _unsupported_binary_reason(url=url, content_type=content_type, raw=raw)+ final_url = str(response.url or "")+ if not binary_reason and final_url and final_url != url:+ binary_reason = _binary_url_reason(final_url)+ if binary_reason:+ raise ValueError(f"unsupported binary content ({binary_reason})")+text = _decode_response_text(response)- content_type = response.headers.get("Content-Type", "")title_match = re.search(r"<title[^>]*>(.*?)</title>", text, flags=re.IGNORECASE | re.DOTALL)title = _strip_tags(title_match.group(1)) if title_match else ""@@ -260,6 +428,18 @@ async def _fetch_single_url("error": "url cannot be empty.",}+ binary_reason = _binary_url_reason(url)+ if binary_reason:+ return {+ "url": url,+ "status_code": None,+ "title": "",+ "content": "",+ "provider": "",+ "from_cache": False,+ "error": f"unsupported binary content ({binary_reason})",+ }+# 模式 1:查缓存if cache is not None and use_cache:cached = await cache.get(url)diff --git a/tests/unit_tests/tools/test_web_fetch_tools.py b/tests/unit_tests/tools/test_web_fetch_tools.pyindex f15ba7fc6b..d040601fa4 100644--- a/tests/unit_tests/tools/test_web_fetch_tools.py+++ b/tests/unit_tests/tools/test_web_fetch_tools.py@@ -22,8 +22,8 @@ _MODULE_PATH = ()-def _load_module():- if "web_fetch_tools_mod" in sys.modules:+def _load_module(*, reload: bool = False):+ if not reload and "web_fetch_tools_mod" in sys.modules:return sys.modules["web_fetch_tools_mod"]tools_pkg = sys.modules.get("jiuwenswarm.agents.harness.common.tools")@@ -33,6 +33,8 @@ def _load_module():tools_pkg.__package__ = "jiuwenswarm.agents.harness.common.tools"sys.modules["jiuwenswarm.agents.harness.common.tools"] = tools_pkg+ # Drop cached module so source edits are visible under pytest re-runs.+ sys.modules.pop("web_fetch_tools_mod", None)spec = importlib.util.spec_from_file_location("web_fetch_tools_mod", str(_MODULE_PATH))mod = importlib.util.module_from_spec(spec)sys.modules["web_fetch_tools_mod"] = mod@@ -40,7 +42,7 @@ def _load_module():return mod-_mod = _load_module()+_mod = _load_module(reload=True)def _response(status_code: int, *, url: str = "https://example.com") -> Mock:@@ -112,3 +114,80 @@ def test_successful_direct_fetch_does_not_use_jina(monkeypatch):assert result["status_code"] == 200assert result["title"] == "Example"jina_fetch.assert_not_called()+++def test_fetch_rejects_doc_url_without_http():+ doc_url = "https://www.stats.gov.cn/xw/tjxw/tzgg/202302/P020230227339331821024.doc"+ with patch.object(_mod, "_http_get") as http_get, pytest.raises(ValueError, match="unsupported binary content"):+ _mod._fetch_webpage_sync(doc_url, 8)+ http_get.assert_not_called()+++def test_fetch_rejects_ole_body_even_with_html_url():+ ole = bytes([0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1]) + b"\x00" * 32+ response = _response(200, url="https://example.com/article")+ response.headers = {"Content-Type": "application/msword"}+ response.content = ole+ response.encoding = "ISO-8859-1"+ response.apparent_encoding = "ISO-8859-1"++ with (+ patch.object(_mod, "_http_get", return_value=response),+ pytest.raises(ValueError, match="unsupported binary content"),+ ):+ _mod._fetch_webpage_sync("https://example.com/article", 8)+++def test_fetch_rejects_pdf_magic_with_octet_stream():+ response = _response(200, url="https://example.com/download")+ response.headers = {"Content-Type": "application/octet-stream"}+ response.content = b"%PDF-1.7 binary-junk"++ with (+ patch.object(_mod, "_http_get", return_value=response),+ pytest.raises(ValueError, match="unsupported binary content"),+ ):+ _mod._fetch_webpage_sync("https://example.com/download", 8)+++def test_fetch_allows_octet_stream_html():+ response = _response(200, url="https://example.com/page")+ response.headers = {"Content-Type": "application/octet-stream"}+ response.content = b"<!DOCTYPE html><html><title>Ok</title><body>hello</body></html>"+ response.encoding = "utf-8"+ response.apparent_encoding = "utf-8"++ with patch.object(_mod, "_http_get", return_value=response):+ result = _mod._fetch_webpage_sync("https://example.com/page", 8)++ assert result["status_code"] == 200+ assert "hello" in result["content"] or "Ok" in result["title"]+++async def test_fetch_single_url_returns_error_for_doc_extension():+ doc_url = "https://www.stats.gov.cn/file/report.doc"+ result = await _mod._fetch_single_url(+ doc_url,+ max_chars=8000,+ timeout_seconds=8,+ use_cache=True,+ cache=None,+ )+ assert result["error"]+ assert "unsupported binary content" in result["error"]+ assert result["content"] == ""+++def test_plain_text_json_still_allowed():+ response = _response(200, url="https://example.com/data.json")+ response.headers = {"Content-Type": "application/json; charset=utf-8"}+ response.content = b'{"hello":"world"}'+ response.encoding = "utf-8"+ response.apparent_encoding = "utf-8"++ with patch.object(_mod, "_http_get", return_value=response):+ result = _mod._fetch_webpage_sync("https://example.com/data.json", 8)++ assert result["status_code"] == 200+ assert "hello" in result["content"]