fix(skills): reject an archive whose SKILL.md is a directory (#8648)

A SKILL.md directory in a skill archive raised IsADirectoryError in
frontmatter validation. The install routes reported it as a 500 that
echoed the host extraction temp path. Validation now returns
"SKILL.md must be a file", so install raises ValueError and both
routes return 400.

Co-authored-by: Willem Jiang <willem.jiang@gmail.com>
This commit is contained in:
Hyeonsang Cho 2026-11-26 16:43:11 +09:00 committed by GitHub
parent 53a03624d5
commit d3aad45fc6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 71 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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] = []

View File

@ -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:

View File

@ -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)