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(skill): safely extract downloaded skill archives
GeorgeGcsbdcda1035b1b
diff --git a/jiuwenswarm/server/runtime/skill/skill_manager.py b/jiuwenswarm/server/runtime/skill/skill_manager.pyindex 7457cbed2c..ae4d44b9e6 100644--- a/jiuwenswarm/server/runtime/skill/skill_manager.py+++ b/jiuwenswarm/server/runtime/skill/skill_manager.py@@ -1145,10 +1145,7 @@ class SkillManager:with tempfile.TemporaryDirectory(prefix="jiuwenclari_clawhub_") as tmpdir:tmp_path = Path(tmpdir)- # 保存 zip 文件- zip_content = io.BytesIO(response.content)- with zipfile.ZipFile(zip_content, "r") as zip_ref:- zip_ref.extractall(tmp_path)+ self._safe_extract_zip_bytes_to_dir(response.content, tmp_path)# 查找 skill 目录skill_dir = self._locate_skill_dir(tmp_path)@@ -2759,8 +2756,7 @@ class SkillManager:stage_dir = tmpdir / "zip_stage"stage_dir.mkdir(parents=True, exist_ok=True)try:- with zipfile.ZipFile(src_zip, "r") as zf:- zf.extractall(stage_dir)+ self._safe_extract_zip_to_dir(src_zip, stage_dir)except zipfile.BadZipFile as exc:raise RuntimeError(f"zip 文件损坏或格式非法: {src_zip}") from excreturn self._build_teamskills_publish_zip_from_root(stage_dir, plugin_version, tmpdir)diff --git a/tests/unit_tests/agentserver/test_skill_manager_path_safety.py b/tests/unit_tests/agentserver/test_skill_manager_path_safety.pyindex c6b48e0f43..e86f5a88d5 100644--- a/tests/unit_tests/agentserver/test_skill_manager_path_safety.py+++ b/tests/unit_tests/agentserver/test_skill_manager_path_safety.py@@ -1,4 +1,6 @@+import ioimport shutil+import zipfileimport pytest@@ -18,6 +20,40 @@ class SkillManagerHarness(SkillManager):self._refresh_agent_data_indexes()+class _FakeClawHubResponse:+ def __init__(self, content: bytes):+ self.content = content++ @staticmethod+ def raise_for_status():+ return None+++class _FakeClawHubClient:+ def __init__(self, content: bytes):+ self._content = content++ async def __aenter__(self):+ return self++ async def __aexit__(self, exc_type, exc, tb):+ return False++ async def get(self, url, *, params, headers):+ assert url == "https://clawhub.ai/api/v1/download"+ assert params == {"slug": "demo-skill"}+ assert headers["Authorization"] == "Bearer test-token"+ return _FakeClawHubResponse(self._content)+++def _zip_bytes(entries: dict[str, str | bytes]) -> bytes:+ buf = io.BytesIO()+ with zipfile.ZipFile(buf, "w", zipfile.ZIP_DEFLATED) as zf:+ for name, content in entries.items():+ zf.writestr(name, content)+ return buf.getvalue()++@pytest.mark.parametrize("name", ["../evil", "nested/skill", r"C:\tmp\skill", ".", "..", ""])def test_safe_path_name_rejects_path_like_names(name):with pytest.raises(ValueError):@@ -59,6 +95,56 @@ async def test_uninstall_rejects_skill_name_path_traversal(tmp_path):assert "invalid skill name" in result["detail"]+async def test_clawhub_download_rejects_zip_slip_archive(tmp_path, monkeypatch):+ manager = SkillManagerHarness(workspace_dir=str(tmp_path / "workspace"))+ await manager.handle_skills_clawhub_set_token({"token": "test-token"})+ zip_content = _zip_bytes(+ {+ "demo-skill/SKILL.md": "---\nname: demo-skill\nversion: 1.0.0\n---\nbody\n",+ "../evil.txt": b"x",+ }+ )++ monkeypatch.setattr(+ "jiuwenswarm.server.runtime.skill.skill_manager.httpx.AsyncClient",+ lambda timeout: _FakeClawHubClient(zip_content),+ )++ result = await manager.handle_skills_clawhub_download({"slug": "demo-skill"})++ assert result["success"] is False+ assert "ZIP" in result["detail"] or "zip" in result["detail"]+ assert not (tmp_path / "evil.txt").exists()+ assert not (tmp_path / "workspace" / "skills" / "demo-skill").exists()+++async def test_teamskills_publish_rejects_zip_slip_source_zip(tmp_path):+ manager = SkillManagerHarness(workspace_dir=str(tmp_path / "workspace"))+ src_zip = tmp_path / "bad.zip"+ src_zip.write_bytes(+ _zip_bytes(+ {+ "demo-skill/SKILL.md": "---\nname: demo-skill\nversion: 1.0.0\n---\nbody\n",+ "../evil.txt": b"x",+ }+ )+ )++ result = await manager.handle_skills_team_skills_hub_publish(+ {+ "file": str(src_zip),+ "version": "1.0.0",+ "token": "test-token",+ }+ )++ assert result["success"] is False+ assert "非法路径" in result["detail"] or "越界" in result["detail"]+ assert not (tmp_path / "evil.txt").exists()++@pytest.mark.asyncioasync def test_import_local_supports_remote_obs_zip(tmp_path):manager = SkillManagerHarness(workspace_dir=str(tmp_path / "workspace"))