fix(skills): reject damaged archive members as invalid packages (#8685)

This commit is contained in:
ZephyrWang 2026-11-26 21:16:46 +08:00 committed by GitHub
parent 08694c6378
commit f8cde989d9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 32 additions and 11 deletions

View File

@ -1241,8 +1241,9 @@ Content-Type: application/json
```
An archive that is not a valid skill returns `400`, for example one with unsafe
or conflicting member paths, invalid frontmatter, or a `SKILL.md` that is missing
or is not a file. A skill name that already exists returns `409`.
or conflicting member paths, invalid frontmatter, a member CRC-32 mismatch found
during extraction, or a `SKILL.md` that is missing or is not a file. A skill name
that already exists returns `409`.
#### Upload and Install Skill

View File

@ -204,9 +204,13 @@ class LocalSkillStorage(SkillStorage):
except (zipfile.BadZipFile, IsADirectoryError):
raise ValueError("File is not a valid ZIP archive") from None
with zf:
scan_archive_preflight_or_raise(path, app_config=self._app_config)
safe_extract_skill_archive(zf, tmp_path)
try:
with zf:
scan_archive_preflight_or_raise(path, app_config=self._app_config)
safe_extract_skill_archive(zf, tmp_path)
except zipfile.BadZipFile:
# Member CRC/header errors can surface only when extraction reads the data.
raise ValueError("File is not a valid ZIP archive") from None
skill_dir = resolve_skill_dir_from_archive(tmp_path)

View File

@ -158,30 +158,46 @@ def test_install_skill_archive_runs_security_scan(monkeypatch, tmp_path):
assert refresh_calls == [("refresh", "default")]
def test_install_skill_archive_with_a_skill_md_directory_returns_400(monkeypatch, tmp_path):
@pytest.mark.parametrize("endpoint", ["thread", "upload"])
@pytest.mark.parametrize("static_enabled", [True, False])
@pytest.mark.parametrize("invalid_member", ["directory", "crc"])
def test_install_skill_archive_with_invalid_member_returns_400(monkeypatch, tmp_path, endpoint, static_enabled, invalid_member):
skills_root = tmp_path / "skills"
(skills_root / "custom").mkdir(parents=True)
archive = tmp_path / "dir-skill.skill"
with zipfile.ZipFile(archive, "w") as zf:
zf.writestr("dir-skill/SKILL.md/notes.md", "# Notes\n")
if invalid_member == "directory":
zf.writestr("dir-skill/SKILL.md/notes.md", "# Notes\n")
else:
zf.writestr("dir-skill/SKILL.md", _skill_content("dir-skill"))
zf.writestr("dir-skill/references/guide.md", b"a" * 8192 + b"corruption-marker")
if invalid_member == "crc":
data = archive.read_bytes()
assert data.count(b"corruption-marker") == 1
archive.write_bytes(data.replace(b"corruption-marker", b"corruption-MARKER", 1))
with zipfile.ZipFile(archive) as zf:
assert zf.testzip() == "dir-skill/references/guide.md"
from deerflow.config.paths import Paths
paths = Paths(base_dir=tmp_path)
monkeypatch.setattr("deerflow.config.paths.get_paths", lambda: paths)
monkeypatch.setattr("deerflow.config.paths._paths", None)
storage = UserScopedSkillStorage("default", host_path=str(skills_root))
storage = UserScopedSkillStorage("default", host_path=str(skills_root), app_config=SimpleNamespace(skill_scan=SimpleNamespace(enabled=static_enabled)))
monkeypatch.setattr(skills_router, "resolve_thread_virtual_path", lambda thread_id, path: archive)
monkeypatch.setattr(skills_router, "_get_user_skill_storage", lambda cfg: storage)
monkeypatch.setattr(skills_router, "get_effective_user_id", lambda: "default")
app = _make_test_app(SimpleNamespace())
with TestClient(app) as client:
response = client.post("/api/skills/install", json={"thread_id": "thread-1", "path": "mnt/user-data/outputs/dir-skill.skill"})
if endpoint == "thread":
response = client.post("/api/skills/install", json={"thread_id": "thread-1", "path": "mnt/user-data/outputs/dir-skill.skill"})
else:
response = client.post("/api/skills/install/upload", files={"archive": (archive.name, archive.read_bytes(), "application/zip")})
# Before the fix this was a 500 whose detail carried the host temp path.
# Invalid packages stay client errors for either endpoint and scan setting.
assert response.status_code == 400
assert response.json()["detail"] == "Invalid skill: SKILL.md must be a file"
assert response.json()["detail"] == ("Invalid skill: SKILL.md must be a file" if invalid_member == "directory" else "File is not a valid ZIP archive")
assert not _user_custom_dir(tmp_path, "default").joinpath("dir-skill").exists()