mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-08-01 10:56:02 +00:00
* fix(skills): close install-path scan coverage gap Skill installs only sent scripts/* and document files under references/ and templates/ to the LLM security scanner. Code at the skill root or under lib/, bin/, src/, etc., and binary files could be installed without any scan. - Scan code files anywhere in the skill tree (by extension, plus shebang detection for extensionless files) with the executable policy: only an explicit allow admits them. - Reject ELF/PE/Mach-O executable binaries by magic bytes during safe archive extraction; non-executable binary assets remain allowed. Interim hardening ahead of the SkillScan framework (RFC #2634); the deterministic full-tree scanner from PR #3033 supersedes the per-file LLM coverage when it lands. * Apply suggestions from code review Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * test(skills): pin magic coverage and shebang offload boundary Follow-up to the applied review suggestions: cover every Mach-O magic variant with tests (plus the fat64 pair and a partial-prefix asset that must stay installable), name the pure classification helper so the call-site logic reads as policy, drop the now-unused sync _is_code_file, and pin that only extensionless files get the shebang sniff. --------- Co-authored-by: Willem Jiang <willem.jiang@gmail.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
575 lines
25 KiB
Python
575 lines
25 KiB
Python
"""Tests for deerflow.skills.installer — shared skill installation logic."""
|
|
|
|
import shutil
|
|
import stat
|
|
import zipfile
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from deerflow.skills.installer import (
|
|
SkillSecurityScanError,
|
|
is_symlink_member,
|
|
is_unsafe_zip_member,
|
|
resolve_skill_dir_from_archive,
|
|
safe_extract_skill_archive,
|
|
should_ignore_archive_entry,
|
|
)
|
|
from deerflow.skills.security_scanner import ScanResult
|
|
from deerflow.skills.storage import get_or_new_skill_storage
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# is_unsafe_zip_member
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestIsUnsafeZipMember:
|
|
def test_absolute_path(self):
|
|
info = zipfile.ZipInfo("/etc/passwd")
|
|
assert is_unsafe_zip_member(info) is True
|
|
|
|
def test_windows_absolute_path(self):
|
|
info = zipfile.ZipInfo("C:\\Windows\\system32\\drivers\\etc\\hosts")
|
|
assert is_unsafe_zip_member(info) is True
|
|
|
|
def test_dotdot_traversal(self):
|
|
info = zipfile.ZipInfo("foo/../../../etc/passwd")
|
|
assert is_unsafe_zip_member(info) is True
|
|
|
|
def test_safe_member(self):
|
|
info = zipfile.ZipInfo("my-skill/SKILL.md")
|
|
assert is_unsafe_zip_member(info) is False
|
|
|
|
def test_empty_filename(self):
|
|
info = zipfile.ZipInfo("")
|
|
assert is_unsafe_zip_member(info) is False
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# is_symlink_member
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestIsSymlinkMember:
|
|
def test_detects_symlink(self):
|
|
info = zipfile.ZipInfo("link.txt")
|
|
info.external_attr = (stat.S_IFLNK | 0o777) << 16
|
|
assert is_symlink_member(info) is True
|
|
|
|
def test_regular_file(self):
|
|
info = zipfile.ZipInfo("file.txt")
|
|
info.external_attr = (stat.S_IFREG | 0o644) << 16
|
|
assert is_symlink_member(info) is False
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# should_ignore_archive_entry
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestShouldIgnoreArchiveEntry:
|
|
def test_macosx_ignored(self):
|
|
assert should_ignore_archive_entry(Path("__MACOSX")) is True
|
|
|
|
def test_dotfile_ignored(self):
|
|
assert should_ignore_archive_entry(Path(".DS_Store")) is True
|
|
|
|
def test_normal_dir_not_ignored(self):
|
|
assert should_ignore_archive_entry(Path("my-skill")) is False
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# resolve_skill_dir_from_archive
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestResolveSkillDir:
|
|
def test_single_dir(self, tmp_path):
|
|
(tmp_path / "my-skill").mkdir()
|
|
(tmp_path / "my-skill" / "SKILL.md").write_text("content")
|
|
assert resolve_skill_dir_from_archive(tmp_path) == tmp_path / "my-skill"
|
|
|
|
def test_with_macosx(self, tmp_path):
|
|
(tmp_path / "my-skill").mkdir()
|
|
(tmp_path / "my-skill" / "SKILL.md").write_text("content")
|
|
(tmp_path / "__MACOSX").mkdir()
|
|
assert resolve_skill_dir_from_archive(tmp_path) == tmp_path / "my-skill"
|
|
|
|
def test_empty_after_filter(self, tmp_path):
|
|
(tmp_path / "__MACOSX").mkdir()
|
|
(tmp_path / ".DS_Store").write_text("meta")
|
|
with pytest.raises(ValueError, match="empty"):
|
|
resolve_skill_dir_from_archive(tmp_path)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# safe_extract_skill_archive
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestSafeExtract:
|
|
def _make_zip(self, tmp_path, members: dict[str, str | bytes]) -> Path:
|
|
"""Create a zip with given filename->content entries."""
|
|
zip_path = tmp_path / "test.zip"
|
|
with zipfile.ZipFile(zip_path, "w") as zf:
|
|
for name, content in members.items():
|
|
if isinstance(content, str):
|
|
content = content.encode()
|
|
zf.writestr(name, content)
|
|
return zip_path
|
|
|
|
def test_rejects_zip_bomb(self, tmp_path):
|
|
zip_path = self._make_zip(tmp_path, {"big.txt": "x" * 1000})
|
|
dest = tmp_path / "out"
|
|
dest.mkdir()
|
|
with zipfile.ZipFile(zip_path) as zf:
|
|
with pytest.raises(ValueError, match="too large"):
|
|
safe_extract_skill_archive(zf, dest, max_total_size=100)
|
|
|
|
def test_rejects_absolute_path(self, tmp_path):
|
|
zip_path = tmp_path / "abs.zip"
|
|
with zipfile.ZipFile(zip_path, "w") as zf:
|
|
zf.writestr("/etc/passwd", "root:x:0:0")
|
|
dest = tmp_path / "out"
|
|
dest.mkdir()
|
|
with zipfile.ZipFile(zip_path) as zf:
|
|
with pytest.raises(ValueError, match="unsafe"):
|
|
safe_extract_skill_archive(zf, dest)
|
|
|
|
def test_skips_symlinks(self, tmp_path):
|
|
zip_path = tmp_path / "sym.zip"
|
|
with zipfile.ZipFile(zip_path, "w") as zf:
|
|
info = zipfile.ZipInfo("link.txt")
|
|
info.external_attr = (stat.S_IFLNK | 0o777) << 16
|
|
zf.writestr(info, "/etc/passwd")
|
|
zf.writestr("normal.txt", "hello")
|
|
dest = tmp_path / "out"
|
|
dest.mkdir()
|
|
with zipfile.ZipFile(zip_path) as zf:
|
|
safe_extract_skill_archive(zf, dest)
|
|
assert (dest / "normal.txt").exists()
|
|
assert not (dest / "link.txt").exists()
|
|
|
|
def test_normal_archive(self, tmp_path):
|
|
zip_path = self._make_zip(
|
|
tmp_path,
|
|
{
|
|
"my-skill/SKILL.md": "---\nname: test\ndescription: x\n---\n# Test",
|
|
"my-skill/README.md": "readme",
|
|
},
|
|
)
|
|
dest = tmp_path / "out"
|
|
dest.mkdir()
|
|
with zipfile.ZipFile(zip_path) as zf:
|
|
safe_extract_skill_archive(zf, dest)
|
|
assert (dest / "my-skill" / "SKILL.md").exists()
|
|
assert (dest / "my-skill" / "README.md").exists()
|
|
|
|
@pytest.mark.parametrize(
|
|
"magic",
|
|
[
|
|
pytest.param(b"\x7fELF\x02\x01\x01\x00", id="elf"),
|
|
pytest.param(b"MZ\x90\x00\x03\x00\x00\x00", id="pe"),
|
|
pytest.param(b"\xfe\xed\xfa\xce\x00\x00\x00\x0c", id="mach-o-32-be"),
|
|
pytest.param(b"\xfe\xed\xfa\xcf\x00\x00\x00\x0c", id="mach-o-64-be"),
|
|
pytest.param(b"\xce\xfa\xed\xfe\x0c\x00\x00\x00", id="mach-o-32-le"),
|
|
pytest.param(b"\xcf\xfa\xed\xfe\x07\x00\x00\x01", id="mach-o-64-le"),
|
|
pytest.param(b"\xca\xfe\xba\xbe\x00\x00\x00\x02", id="mach-o-fat-be"),
|
|
pytest.param(b"\xbe\xba\xfe\xca\x02\x00\x00\x00", id="mach-o-fat-le"),
|
|
pytest.param(b"\xca\xfe\xba\xbf\x00\x00\x00\x02", id="mach-o-fat64-be"),
|
|
pytest.param(b"\xbf\xba\xfe\xca\x02\x00\x00\x00", id="mach-o-fat64-le"),
|
|
],
|
|
)
|
|
def test_rejects_executable_binary(self, tmp_path, magic):
|
|
zip_path = self._make_zip(
|
|
tmp_path,
|
|
{
|
|
"my-skill/SKILL.md": "---\nname: test\ndescription: x\n---\n# Test",
|
|
"my-skill/bin/tool": magic + b"\x00" * 64,
|
|
},
|
|
)
|
|
dest = tmp_path / "out"
|
|
dest.mkdir()
|
|
with zipfile.ZipFile(zip_path) as zf:
|
|
with pytest.raises(ValueError, match="executable binary"):
|
|
safe_extract_skill_archive(zf, dest)
|
|
|
|
def test_allows_non_executable_binary_assets(self, tmp_path):
|
|
zip_path = self._make_zip(
|
|
tmp_path,
|
|
{
|
|
"my-skill/SKILL.md": "---\nname: test\ndescription: x\n---\n# Test",
|
|
"my-skill/assets/logo.png": b"\x89PNG\r\n\x1a\n" + b"\x00" * 32,
|
|
},
|
|
)
|
|
dest = tmp_path / "out"
|
|
dest.mkdir()
|
|
with zipfile.ZipFile(zip_path) as zf:
|
|
safe_extract_skill_archive(zf, dest)
|
|
assert (dest / "my-skill" / "assets" / "logo.png").exists()
|
|
|
|
def test_allows_asset_sharing_a_partial_magic_prefix(self, tmp_path):
|
|
"""Only full 4-byte magics are executable; \\xfe\\xed\\xfa + other byte is data."""
|
|
zip_path = self._make_zip(
|
|
tmp_path,
|
|
{
|
|
"my-skill/SKILL.md": "---\nname: test\ndescription: x\n---\n# Test",
|
|
"my-skill/assets/blob.bin": b"\xfe\xed\xfa\x00" + b"\x00" * 32,
|
|
},
|
|
)
|
|
dest = tmp_path / "out"
|
|
dest.mkdir()
|
|
with zipfile.ZipFile(zip_path) as zf:
|
|
safe_extract_skill_archive(zf, dest)
|
|
assert (dest / "my-skill" / "assets" / "blob.bin").exists()
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# install_skill_from_archive (full integration)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestInstallSkillFromArchive:
|
|
@pytest.fixture(autouse=True)
|
|
def _allow_security_scan(self, monkeypatch):
|
|
async def _scan(*args, **kwargs):
|
|
return ScanResult(decision="allow", reason="ok")
|
|
|
|
monkeypatch.setattr("deerflow.skills.installer.scan_skill_content", _scan)
|
|
|
|
def _make_skill_zip(self, tmp_path: Path, skill_name: str = "test-skill") -> Path:
|
|
"""Create a valid .skill archive."""
|
|
zip_path = tmp_path / f"{skill_name}.skill"
|
|
with zipfile.ZipFile(zip_path, "w") as zf:
|
|
zf.writestr(
|
|
f"{skill_name}/SKILL.md",
|
|
f"---\nname: {skill_name}\ndescription: A test skill\n---\n\n# {skill_name}\n",
|
|
)
|
|
return zip_path
|
|
|
|
def test_success(self, tmp_path):
|
|
zip_path = self._make_skill_zip(tmp_path)
|
|
skills_root = tmp_path / "skills"
|
|
skills_root.mkdir()
|
|
result = get_or_new_skill_storage(skills_path=skills_root).install_skill_from_archive(zip_path)
|
|
assert result["success"] is True
|
|
assert result["skill_name"] == "test-skill"
|
|
assert (skills_root / "custom" / "test-skill" / "SKILL.md").exists()
|
|
|
|
def test_installed_skill_tree_is_readable_by_sandbox_mount(self, tmp_path):
|
|
zip_path = tmp_path / "test-skill.skill"
|
|
with zipfile.ZipFile(zip_path, "w") as zf:
|
|
zf.writestr("test-skill/SKILL.md", "---\nname: test-skill\ndescription: A test skill\n---\n\n# test-skill\n")
|
|
zf.writestr("test-skill/references/guide.md", "# Guide\n")
|
|
skills_root = tmp_path / "skills"
|
|
skills_root.mkdir()
|
|
|
|
get_or_new_skill_storage(skills_path=skills_root).install_skill_from_archive(zip_path)
|
|
|
|
installed_dir = skills_root / "custom" / "test-skill"
|
|
nested_dir = installed_dir / "references"
|
|
skill_file = installed_dir / "SKILL.md"
|
|
guide_file = nested_dir / "guide.md"
|
|
|
|
assert stat.S_IMODE(installed_dir.stat().st_mode) & 0o055 == 0o055
|
|
assert stat.S_IMODE(nested_dir.stat().st_mode) & 0o055 == 0o055
|
|
assert stat.S_IMODE(skill_file.stat().st_mode) & 0o044 == 0o044
|
|
assert stat.S_IMODE(guide_file.stat().st_mode) & 0o044 == 0o044
|
|
|
|
def test_scans_skill_markdown_before_install(self, tmp_path, monkeypatch):
|
|
zip_path = self._make_skill_zip(tmp_path)
|
|
skills_root = tmp_path / "skills"
|
|
skills_root.mkdir()
|
|
calls = []
|
|
|
|
async def _scan(content, *, executable, location):
|
|
calls.append({"content": content, "executable": executable, "location": location})
|
|
return ScanResult(decision="allow", reason="ok")
|
|
|
|
monkeypatch.setattr("deerflow.skills.installer.scan_skill_content", _scan)
|
|
|
|
get_or_new_skill_storage(skills_path=skills_root).install_skill_from_archive(zip_path)
|
|
|
|
assert calls == [
|
|
{
|
|
"content": "---\nname: test-skill\ndescription: A test skill\n---\n\n# test-skill\n",
|
|
"executable": False,
|
|
"location": "test-skill/SKILL.md",
|
|
}
|
|
]
|
|
|
|
def test_scans_support_files_and_scripts_before_install(self, tmp_path, monkeypatch):
|
|
zip_path = tmp_path / "test-skill.skill"
|
|
with zipfile.ZipFile(zip_path, "w") as zf:
|
|
zf.writestr("test-skill/SKILL.md", "---\nname: test-skill\ndescription: A test skill\n---\n\n# test-skill\n")
|
|
zf.writestr("test-skill/references/guide.md", "# Guide\n")
|
|
zf.writestr("test-skill/templates/prompt.txt", "Use care.\n")
|
|
zf.writestr("test-skill/scripts/run.sh", "#!/bin/sh\necho ok\n")
|
|
zf.writestr("test-skill/assets/logo.png", b"\x89PNG\r\n\x1a\n")
|
|
zf.writestr("test-skill/references/.env", "TOKEN=secret\n")
|
|
zf.writestr("test-skill/templates/config.cfg", "TOKEN=secret\n")
|
|
skills_root = tmp_path / "skills"
|
|
skills_root.mkdir()
|
|
calls = []
|
|
|
|
async def _scan(content, *, executable, location):
|
|
calls.append({"content": content, "executable": executable, "location": location})
|
|
return ScanResult(decision="allow", reason="ok")
|
|
|
|
monkeypatch.setattr("deerflow.skills.installer.scan_skill_content", _scan)
|
|
|
|
get_or_new_skill_storage(skills_path=skills_root).install_skill_from_archive(zip_path)
|
|
|
|
assert calls == [
|
|
{
|
|
"content": "---\nname: test-skill\ndescription: A test skill\n---\n\n# test-skill\n",
|
|
"executable": False,
|
|
"location": "test-skill/SKILL.md",
|
|
},
|
|
{
|
|
"content": "# Guide\n",
|
|
"executable": False,
|
|
"location": "test-skill/references/guide.md",
|
|
},
|
|
{
|
|
"content": "#!/bin/sh\necho ok\n",
|
|
"executable": True,
|
|
"location": "test-skill/scripts/run.sh",
|
|
},
|
|
{
|
|
"content": "Use care.\n",
|
|
"executable": False,
|
|
"location": "test-skill/templates/prompt.txt",
|
|
},
|
|
]
|
|
assert all("secret" not in call["content"] for call in calls)
|
|
|
|
def test_scans_code_files_anywhere_in_tree(self, tmp_path, monkeypatch):
|
|
zip_path = tmp_path / "test-skill.skill"
|
|
with zipfile.ZipFile(zip_path, "w") as zf:
|
|
zf.writestr("test-skill/SKILL.md", "---\nname: test-skill\ndescription: A test skill\n---\n\n# test-skill\n")
|
|
zf.writestr("test-skill/helper.py", "import os\nprint('root code')\n")
|
|
zf.writestr("test-skill/lib/util.sh", "echo lib\n")
|
|
zf.writestr("test-skill/bin/tool", "#!/usr/bin/env python3\nprint('extensionless')\n")
|
|
zf.writestr("test-skill/assets/logo.png", b"\x89PNG\r\n\x1a\n")
|
|
zf.writestr("test-skill/assets/data.txt", "just data\n")
|
|
skills_root = tmp_path / "skills"
|
|
skills_root.mkdir()
|
|
calls = []
|
|
|
|
async def _scan(content, *, executable, location):
|
|
calls.append({"executable": executable, "location": location})
|
|
return ScanResult(decision="allow", reason="ok")
|
|
|
|
monkeypatch.setattr("deerflow.skills.installer.scan_skill_content", _scan)
|
|
|
|
get_or_new_skill_storage(skills_path=skills_root).install_skill_from_archive(zip_path)
|
|
|
|
assert {"executable": True, "location": "test-skill/helper.py"} in calls
|
|
assert {"executable": True, "location": "test-skill/lib/util.sh"} in calls
|
|
assert {"executable": True, "location": "test-skill/bin/tool"} in calls
|
|
scanned_locations = {call["location"] for call in calls}
|
|
assert "test-skill/assets/logo.png" not in scanned_locations
|
|
assert "test-skill/assets/data.txt" not in scanned_locations
|
|
|
|
def test_shebang_sniff_only_reads_extensionless_files(self, tmp_path, monkeypatch):
|
|
"""Suffix/scripts classification is name-based; only extensionless files are opened."""
|
|
import deerflow.skills.installer as installer_module
|
|
|
|
zip_path = tmp_path / "test-skill.skill"
|
|
with zipfile.ZipFile(zip_path, "w") as zf:
|
|
zf.writestr("test-skill/SKILL.md", "---\nname: test-skill\ndescription: A test skill\n---\n\n# test-skill\n")
|
|
zf.writestr("test-skill/helper.py", "print('code')\n")
|
|
zf.writestr("test-skill/scripts/run.sh", "#!/bin/sh\necho ok\n")
|
|
zf.writestr("test-skill/bin/tool", "#!/usr/bin/env python3\nprint('extensionless')\n")
|
|
zf.writestr("test-skill/assets/data.txt", "just data\n")
|
|
skills_root = tmp_path / "skills"
|
|
skills_root.mkdir()
|
|
sniffed = []
|
|
original_has_shebang = installer_module._has_shebang
|
|
|
|
def _tracking_has_shebang(path):
|
|
sniffed.append(path.name)
|
|
return original_has_shebang(path)
|
|
|
|
monkeypatch.setattr(installer_module, "_has_shebang", _tracking_has_shebang)
|
|
|
|
get_or_new_skill_storage(skills_path=skills_root).install_skill_from_archive(zip_path)
|
|
|
|
assert sniffed == ["tool"]
|
|
|
|
def test_code_file_outside_scripts_warn_prevents_install(self, tmp_path, monkeypatch):
|
|
zip_path = tmp_path / "test-skill.skill"
|
|
with zipfile.ZipFile(zip_path, "w") as zf:
|
|
zf.writestr("test-skill/SKILL.md", "---\nname: test-skill\ndescription: A test skill\n---\n\n# test-skill\n")
|
|
zf.writestr("test-skill/lib/run.py", "import os\nos.system('whoami')\n")
|
|
skills_root = tmp_path / "skills"
|
|
skills_root.mkdir()
|
|
|
|
async def _scan(*args, executable, **kwargs):
|
|
if executable:
|
|
return ScanResult(decision="warn", reason="code needs review")
|
|
return ScanResult(decision="allow", reason="ok")
|
|
|
|
monkeypatch.setattr("deerflow.skills.installer.scan_skill_content", _scan)
|
|
|
|
with pytest.raises(SkillSecurityScanError, match="rejected executable.*code needs review"):
|
|
get_or_new_skill_storage(skills_path=skills_root).install_skill_from_archive(zip_path)
|
|
|
|
assert not (skills_root / "custom" / "test-skill").exists()
|
|
|
|
def test_executable_binary_prevents_install(self, tmp_path):
|
|
zip_path = tmp_path / "test-skill.skill"
|
|
with zipfile.ZipFile(zip_path, "w") as zf:
|
|
zf.writestr("test-skill/SKILL.md", "---\nname: test-skill\ndescription: A test skill\n---\n\n# test-skill\n")
|
|
zf.writestr("test-skill/bin/tool", b"\x7fELF\x02\x01\x01\x00" + b"\x00" * 64)
|
|
skills_root = tmp_path / "skills"
|
|
skills_root.mkdir()
|
|
|
|
with pytest.raises(ValueError, match="executable binary"):
|
|
get_or_new_skill_storage(skills_path=skills_root).install_skill_from_archive(zip_path)
|
|
|
|
assert not (skills_root / "custom" / "test-skill").exists()
|
|
|
|
def test_nested_skill_markdown_prevents_install(self, tmp_path):
|
|
zip_path = tmp_path / "test-skill.skill"
|
|
with zipfile.ZipFile(zip_path, "w") as zf:
|
|
zf.writestr("test-skill/SKILL.md", "---\nname: test-skill\ndescription: A test skill\n---\n\n# test-skill\n")
|
|
zf.writestr("test-skill/references/other/SKILL.md", "# Nested skill\n")
|
|
skills_root = tmp_path / "skills"
|
|
skills_root.mkdir()
|
|
|
|
with pytest.raises(SkillSecurityScanError, match="nested SKILL.md"):
|
|
get_or_new_skill_storage(skills_path=skills_root).install_skill_from_archive(zip_path)
|
|
|
|
assert not (skills_root / "custom" / "test-skill").exists()
|
|
|
|
def test_script_warn_prevents_install(self, tmp_path, monkeypatch):
|
|
zip_path = tmp_path / "test-skill.skill"
|
|
with zipfile.ZipFile(zip_path, "w") as zf:
|
|
zf.writestr("test-skill/SKILL.md", "---\nname: test-skill\ndescription: A test skill\n---\n\n# test-skill\n")
|
|
zf.writestr("test-skill/scripts/run.sh", "#!/bin/sh\necho ok\n")
|
|
skills_root = tmp_path / "skills"
|
|
skills_root.mkdir()
|
|
|
|
async def _scan(*args, executable, **kwargs):
|
|
if executable:
|
|
return ScanResult(decision="warn", reason="script needs review")
|
|
return ScanResult(decision="allow", reason="ok")
|
|
|
|
monkeypatch.setattr("deerflow.skills.installer.scan_skill_content", _scan)
|
|
|
|
with pytest.raises(SkillSecurityScanError, match="rejected executable.*script needs review"):
|
|
get_or_new_skill_storage(skills_path=skills_root).install_skill_from_archive(zip_path)
|
|
|
|
assert not (skills_root / "custom" / "test-skill").exists()
|
|
|
|
def test_security_scan_block_prevents_install(self, tmp_path, monkeypatch):
|
|
zip_path = self._make_skill_zip(tmp_path, skill_name="blocked-skill")
|
|
skills_root = tmp_path / "skills"
|
|
skills_root.mkdir()
|
|
|
|
async def _scan(*args, **kwargs):
|
|
return ScanResult(decision="block", reason="prompt injection")
|
|
|
|
monkeypatch.setattr("deerflow.skills.installer.scan_skill_content", _scan)
|
|
|
|
with pytest.raises(SkillSecurityScanError, match="Security scan blocked.*prompt injection"):
|
|
get_or_new_skill_storage(skills_path=skills_root).install_skill_from_archive(zip_path)
|
|
|
|
assert not (skills_root / "custom" / "blocked-skill").exists()
|
|
|
|
def test_copy_failure_does_not_leave_partial_install(self, tmp_path, monkeypatch):
|
|
zip_path = self._make_skill_zip(tmp_path)
|
|
skills_root = tmp_path / "skills"
|
|
skills_root.mkdir()
|
|
|
|
def _copytree(src, dst):
|
|
partial = Path(dst)
|
|
partial.mkdir(parents=True)
|
|
(partial / "partial.txt").write_text("partial", encoding="utf-8")
|
|
raise OSError("copy failed")
|
|
|
|
monkeypatch.setattr("deerflow.skills.installer.shutil.copytree", _copytree)
|
|
|
|
with pytest.raises(OSError, match="copy failed"):
|
|
get_or_new_skill_storage(skills_path=skills_root).install_skill_from_archive(zip_path)
|
|
|
|
custom_dir = skills_root / "custom"
|
|
assert not (custom_dir / "test-skill").exists()
|
|
assert not [path for path in custom_dir.iterdir() if path.name.startswith(".installing-test-skill-")]
|
|
|
|
def test_concurrent_target_creation_does_not_get_clobbered(self, tmp_path, monkeypatch):
|
|
zip_path = self._make_skill_zip(tmp_path)
|
|
skills_root = tmp_path / "skills"
|
|
skills_root.mkdir()
|
|
target = skills_root / "custom" / "test-skill"
|
|
original_copytree = shutil.copytree
|
|
|
|
def _copytree(src, dst):
|
|
target.mkdir(parents=True)
|
|
(target / "marker.txt").write_text("external", encoding="utf-8")
|
|
return original_copytree(src, dst)
|
|
|
|
monkeypatch.setattr("deerflow.skills.installer.shutil.copytree", _copytree)
|
|
|
|
with pytest.raises(ValueError, match="already exists"):
|
|
get_or_new_skill_storage(skills_path=skills_root).install_skill_from_archive(zip_path)
|
|
|
|
assert (target / "marker.txt").read_text(encoding="utf-8") == "external"
|
|
assert not (target / "SKILL.md").exists()
|
|
|
|
def test_move_failure_cleans_reserved_target(self, tmp_path, monkeypatch):
|
|
zip_path = self._make_skill_zip(tmp_path)
|
|
skills_root = tmp_path / "skills"
|
|
skills_root.mkdir()
|
|
|
|
def _move(src, dst):
|
|
Path(dst).write_text("partial", encoding="utf-8")
|
|
raise OSError("move failed")
|
|
|
|
monkeypatch.setattr("deerflow.skills.installer.shutil.move", _move)
|
|
|
|
with pytest.raises(OSError, match="move failed"):
|
|
get_or_new_skill_storage(skills_path=skills_root).install_skill_from_archive(zip_path)
|
|
|
|
assert not (skills_root / "custom" / "test-skill").exists()
|
|
|
|
def test_duplicate_raises(self, tmp_path):
|
|
zip_path = self._make_skill_zip(tmp_path)
|
|
skills_root = tmp_path / "skills"
|
|
(skills_root / "custom" / "test-skill").mkdir(parents=True)
|
|
with pytest.raises(ValueError, match="already exists"):
|
|
get_or_new_skill_storage(skills_path=skills_root).install_skill_from_archive(zip_path)
|
|
|
|
def test_invalid_extension(self, tmp_path):
|
|
bad_path = tmp_path / "bad.zip"
|
|
bad_path.write_text("not a skill")
|
|
with pytest.raises(ValueError, match=".skill"):
|
|
get_or_new_skill_storage(skills_path=tmp_path).install_skill_from_archive(bad_path)
|
|
|
|
def test_bad_frontmatter(self, tmp_path):
|
|
zip_path = tmp_path / "bad.skill"
|
|
with zipfile.ZipFile(zip_path, "w") as zf:
|
|
zf.writestr("bad/SKILL.md", "no frontmatter here")
|
|
skills_root = tmp_path / "skills"
|
|
skills_root.mkdir()
|
|
with pytest.raises(ValueError, match="Invalid skill"):
|
|
get_or_new_skill_storage(skills_path=skills_root).install_skill_from_archive(zip_path)
|
|
|
|
def test_nonexistent_file(self, tmp_path):
|
|
with pytest.raises(FileNotFoundError):
|
|
get_or_new_skill_storage(skills_path=tmp_path).install_skill_from_archive(Path("/nonexistent/path.skill"))
|
|
|
|
def test_macosx_filtered_during_resolve(self, tmp_path):
|
|
"""Archive with __MACOSX dir still installs correctly."""
|
|
zip_path = tmp_path / "mac.skill"
|
|
with zipfile.ZipFile(zip_path, "w") as zf:
|
|
zf.writestr("my-skill/SKILL.md", "---\nname: my-skill\ndescription: desc\n---\n# My Skill\n")
|
|
zf.writestr("__MACOSX/._my-skill", "meta")
|
|
skills_root = tmp_path / "skills"
|
|
skills_root.mkdir()
|
|
result = get_or_new_skill_storage(skills_path=skills_root).install_skill_from_archive(zip_path)
|
|
assert result["success"] is True
|
|
assert result["skill_name"] == "my-skill"
|