diff --git a/backend/docs/API.md b/backend/docs/API.md index b32fe91c6b..e7454a1dd8 100644 --- a/backend/docs/API.md +++ b/backend/docs/API.md @@ -1240,6 +1240,10 @@ 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`. + #### Upload and Install Skill Upload a local `.skill` archive and install it for the current user. Requires an diff --git a/backend/packages/harness/deerflow/skills/AGENTS.md b/backend/packages/harness/deerflow/skills/AGENTS.md index 3339aeb3ff..18f9d3f683 100644 --- a/backend/packages/harness/deerflow/skills/AGENTS.md +++ b/backend/packages/harness/deerflow/skills/AGENTS.md @@ -56,6 +56,9 @@ their credentials and enabled state stay per user. - **A skill name is a directory name on every host.** Creation and archive install reject Windows reserved device names on all platforms. Reads and deletes stay permissive, so that skills stored by an older release can still be removed. +- **Archive install rejects a bad package with `ValueError`.** The route maps it + to `400`. Any other exception becomes a `500` that echoes its message, which can + name the extraction temp path, so check the extracted tree before reading it. - **History is JSONL split on LF only.** Unicode line separators inside a saved field are data. Every history writer appends a whole record and its LF under `locked_history_file`: callers do not share a mutation lock, and a split or diff --git a/backend/packages/harness/deerflow/skills/validation.py b/backend/packages/harness/deerflow/skills/validation.py index 6ec241fbfd..7c9cf24d5d 100644 --- a/backend/packages/harness/deerflow/skills/validation.py +++ b/backend/packages/harness/deerflow/skills/validation.py @@ -23,6 +23,10 @@ def _validate_skill_frontmatter(skill_dir: Path) -> tuple[bool, str, str | None] skill_md = skill_dir / SKILL_MD_FILE if not skill_md.exists(): return False, f"{SKILL_MD_FILE} not found", None + # An archive can carry a SKILL.md directory; reading it would raise an + # OSError that the install route reports as a 500 with the temp path. + if not skill_md.is_file(): + return False, f"{SKILL_MD_FILE} must be a file", None content = skill_md.read_text(encoding="utf-8") return validate_skill_frontmatter_text(content) diff --git a/backend/tests/test_skills_custom_router.py b/backend/tests/test_skills_custom_router.py index 3729a58d6b..4109d579b9 100644 --- a/backend/tests/test_skills_custom_router.py +++ b/backend/tests/test_skills_custom_router.py @@ -158,6 +158,33 @@ 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): + 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") + + 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)) + 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"}) + + # Before the fix this was a 500 whose detail carried the host temp path. + assert response.status_code == 400 + assert response.json()["detail"] == "Invalid skill: SKILL.md must be a file" + assert not _user_custom_dir(tmp_path, "default").joinpath("dir-skill").exists() + + def test_upload_skill_archive_installs_without_thread_workspace(monkeypatch, tmp_path): installed_paths: list[Path] = [] refresh_calls: list[str] = [] diff --git a/backend/tests/test_skills_installer.py b/backend/tests/test_skills_installer.py index 6b007b3eae..f96ce7109c 100644 --- a/backend/tests/test_skills_installer.py +++ b/backend/tests/test_skills_installer.py @@ -875,6 +875,32 @@ class TestInstallSkillFromArchive: assert not (skills_root / "custom" / "test-skill").exists() + @pytest.mark.parametrize( + "members", + [ + {"test-skill/SKILL.md/": None}, + {"test-skill/SKILL.md/notes.md": "# Notes\n"}, + {"SKILL.md/": None, "notes.md": "# Notes\n"}, + ], + ids=["directory-entry", "implied-directory", "archive-root"], + ) + @pytest.mark.parametrize("skill_scan_enabled", [True, False], ids=["scan", "no-scan"]) + def test_skill_markdown_directory_is_an_invalid_skill(self, tmp_path, members, skill_scan_enabled): + zip_path = tmp_path / "test-skill.skill" + with zipfile.ZipFile(zip_path, "w") as zf: + for name, content in members.items(): + if content is None: + zf.writestr(zipfile.ZipInfo(name), "") + else: + zf.writestr(name, content) + skills_root = tmp_path / "skills" + skills_root.mkdir() + + with pytest.raises(ValueError, match=r"^Invalid skill: SKILL\.md must be a file$"): + _make_storage(skills_root, skill_scan_enabled=skill_scan_enabled).install_skill_from_archive(zip_path) + + assert not any((skills_root / "custom").iterdir()) + 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: diff --git a/backend/tests/test_skills_validation.py b/backend/tests/test_skills_validation.py index a759f0e427..a4f419c984 100644 --- a/backend/tests/test_skills_validation.py +++ b/backend/tests/test_skills_validation.py @@ -120,6 +120,13 @@ class TestValidateSkillFrontmatter: assert "not found" in msg assert name is None + def test_skill_md_directory_is_rejected(self, tmp_path): + (tmp_path / "SKILL.md").mkdir() + valid, msg, name = _validate_skill_frontmatter(tmp_path) + assert valid is False + assert msg == "SKILL.md must be a file" + assert name is None + def test_no_frontmatter(self, tmp_path): skill_dir = _write_skill(tmp_path, "# Just markdown\n\nNo front matter.\n") valid, msg, _ = _validate_skill_frontmatter(skill_dir)