diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7cb5cd4..d94a327 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,7 +17,7 @@ concurrency: jobs: validator-regression: - name: Validator regression (Python ${{ matrix.python-version }}) + name: Validator and packaging regression (Python ${{ matrix.python-version }}) runs-on: ubuntu-latest strategy: fail-fast: false @@ -36,8 +36,8 @@ jobs: - name: Install test dependencies run: python -m pip install --upgrade pip pytest PyYAML - - name: Run UTF-8 validator regression test - run: python -m pytest -q skill-creator/tests/test_quick_validate_encoding.py + - name: Run skill-creator regression tests + run: python -m pytest -q skill-creator/tests - name: Compile validator and tests run: python -m compileall -q skill-creator/scripts skill-creator/tests diff --git a/skill-creator/scripts/package_skill.py b/skill-creator/scripts/package_skill.py old mode 100755 new mode 100644 index f48eac4..1bde99a --- a/skill-creator/scripts/package_skill.py +++ b/skill-creator/scripts/package_skill.py @@ -61,11 +61,14 @@ def package_skill(skill_path, output_dir=None): print(f"❌ Error: Path is not a directory: {skill_path}") return None - # Validate SKILL.md exists + # Validate SKILL.md exists and is a real file inside the skill tree. skill_md = skill_path / "SKILL.md" if not skill_md.exists(): print(f"❌ Error: SKILL.md not found in {skill_path}") return None + if skill_md.is_symlink(): + print(f"❌ Error: SKILL.md must not be a symbolic link: {skill_md}") + return None # Run validation before packaging print("🔍 Validating skill...") @@ -88,11 +91,22 @@ def package_skill(skill_path, output_dir=None): # Create the .skill file (zip format) try: - with zipfile.ZipFile(skill_filename, 'w', zipfile.ZIP_DEFLATED) as zipf: - # Walk through the skill directory, excluding build artifacts - for file_path in skill_path.rglob('*'): + with zipfile.ZipFile(skill_filename, "w", zipfile.ZIP_DEFLATED) as zipf: + # Walk through the skill directory, excluding build artifacts. + # Never follow file symlinks: they can pull unrelated files outside + # the skill tree into the distributable archive. + for file_path in skill_path.rglob("*"): + if file_path.is_symlink(): + print(f" Skipped symbolic link: {file_path.relative_to(skill_path)}") + continue if not file_path.is_file(): continue + # The default output directory can be the skill directory itself. + # In that case the archive is visible to rglob while it is being + # written, so explicitly keep the output archive out of itself. + if file_path.resolve() == skill_filename.resolve(): + print(f" Skipped output archive: {file_path.relative_to(skill_path)}") + continue arcname = file_path.relative_to(skill_path.parent) if should_exclude(arcname): print(f" Skipped: {arcname}") diff --git a/skill-creator/tests/test_package_skill.py b/skill-creator/tests/test_package_skill.py new file mode 100644 index 0000000..08e8fb9 --- /dev/null +++ b/skill-creator/tests/test_package_skill.py @@ -0,0 +1,101 @@ +from __future__ import annotations + +import sys +import zipfile +from pathlib import Path + +import pytest + +# package_skill imports quick_validate through the scripts package. +sys.path.insert(0, str(Path(__file__).parents[1])) +from scripts.package_skill import package_skill # noqa: E402 + + +def make_valid_skill(root: Path) -> Path: + skill_dir = root / "sample-skill" + skill_dir.mkdir() + (skill_dir / "SKILL.md").write_text( + "---\nname: sample-skill\ndescription: A test skill.\n---\n\n# Sample\n", + encoding="utf-8", + ) + return skill_dir + + +def test_package_excludes_build_artifacts_but_keeps_nested_evals(tmp_path: Path) -> None: + skill_dir = make_valid_skill(tmp_path) + (skill_dir / "guide.md").write_text("Guide", encoding="utf-8") + (skill_dir / "evals").mkdir() + (skill_dir / "evals" / "private.json").write_text("{}", encoding="utf-8") + (skill_dir / "references").mkdir() + (skill_dir / "references" / "evals").mkdir() + (skill_dir / "references" / "evals" / "example.json").write_text("{}", encoding="utf-8") + (skill_dir / "__pycache__").mkdir() + (skill_dir / "__pycache__" / "module.pyc").write_bytes(b"bytecode") + (skill_dir / "node_modules").mkdir() + (skill_dir / "node_modules" / "dependency.js").write_text("ignored", encoding="utf-8") + (skill_dir / ".DS_Store").write_text("ignored", encoding="utf-8") + (skill_dir / "module.pyc").write_bytes(b"bytecode") + output_dir = tmp_path / "dist" + + archive = package_skill(skill_dir, output_dir) + + assert archive is not None + with zipfile.ZipFile(archive) as zipf: + names = set(zipf.namelist()) + assert "sample-skill/SKILL.md" in names + assert "sample-skill/guide.md" in names + assert "sample-skill/references/evals/example.json" in names + assert "sample-skill/evals/private.json" not in names + assert not any("__pycache__" in name or "node_modules" in name for name in names) + assert not any(name.endswith(".pyc") or name.endswith(".DS_Store") for name in names) + + +def test_package_skips_symlinked_files_outside_skill_tree(tmp_path: Path) -> None: + skill_dir = make_valid_skill(tmp_path) + outside_secret = tmp_path / "outside-secret.txt" + outside_secret.write_text("do not package", encoding="utf-8") + link = skill_dir / "external.txt" + try: + link.symlink_to(outside_secret) + except (OSError, NotImplementedError) as exc: + pytest.skip(f"symlinks are not available: {exc}") + + archive = package_skill(skill_dir, tmp_path / "dist") + + assert archive is not None + with zipfile.ZipFile(archive) as zipf: + names = set(zipf.namelist()) + assert "sample-skill/external.txt" not in names + assert "sample-skill/SKILL.md" in names + + +def test_package_rejects_symlinked_skill_manifest(tmp_path: Path) -> None: + skill_dir = tmp_path / "sample-skill" + skill_dir.mkdir() + external_manifest = tmp_path / "external-SKILL.md" + external_manifest.write_text( + "---\nname: sample-skill\ndescription: A test skill.\n---\n", + encoding="utf-8", + ) + (skill_dir / "SKILL.md").symlink_to(external_manifest) + + assert package_skill(skill_dir, tmp_path / "dist") is None + + +def test_package_does_not_include_archive_when_output_is_skill_directory( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + skill_dir = make_valid_skill(tmp_path) + (skill_dir / "guide.md").write_text("Guide", encoding="utf-8") + monkeypatch.chdir(skill_dir) + + archive = package_skill(skill_dir) + + assert archive == skill_dir / "sample-skill.skill" + assert archive.is_file() + with zipfile.ZipFile(archive) as zipf: + names = set(zipf.namelist()) + assert zipf.testzip() is None + assert "sample-skill/SKILL.md" in names + assert "sample-skill/guide.md" in names + assert "sample-skill/sample-skill.skill" not in names