Personal AI, On Personal Devices
Python
fix(security): reject symlinks in imported skills (#961)
Haotian Zheng5c0982c69c05
diff --git a/src/openjarvis/skills/importer.py b/src/openjarvis/skills/importer.pyindex f954aee6..bf42a33f 100644--- a/src/openjarvis/skills/importer.py+++ b/src/openjarvis/skills/importer.py@@ -3,14 +3,15 @@Steps performed by ``import_skill``:1. Parse the source SKILL.md through SkillParser (strict + tolerant).-2. Translate tool references in the markdown body via ToolTranslator.-3. Decide on scripts (default-skip; opt-in via with_scripts=True).-4. Write to disk at <target_root>/<source>/<name>/:+2. Reject symlinks in directories that would be copied into the install.+3. Translate tool references in the markdown body via ToolTranslator.+4. Decide on scripts (default-skip; opt-in via with_scripts=True).+5. Write to disk at <target_root>/<source>/<name>/:- translated SKILL.md- references/, assets/, templates/ (always copied)- scripts/ (only if approved)- .source metadata file-5. Return ImportResult with status, warnings, translated/missing tools.+6. Return ImportResult with status, warnings, translated/missing tools."""from __future__ import annotations@@ -143,7 +144,29 @@ class SkillImporter:"and/or write to the filesystem.")- # 2. Translate tool references+ # 2. Validate copied resources and translate tool references+ copied_subdirs = list(COPIED_SUBDIRS)+ if with_scripts:+ copied_subdirs.append("scripts")+ symlinks = [+ link+ for subdir in copied_subdirs+ for link in self._find_symlinks(resolved.path / subdir)+ ]+ if symlinks:+ names = ", ".join(+ str(link.relative_to(resolved.path)) for link in symlinks[:3]+ )+ if len(symlinks) > 3:+ names += f", and {len(symlinks) - 3} more"+ result.success = False+ result.warnings.append(+ "Refusing to install: skill contains symlinked entries in "+ f"copied directories ({names}). Imported skills must not "+ "read files outside their source package."+ )+ return result+translated_body, untranslated = self._translator.translate_markdown(body)result.untranslated_tools = untranslated# Compute the list of translations actually applied@@ -187,6 +210,15 @@ class SkillImporter:# Helpers# ------------------------------------------------------------------+ @staticmethod+ def _find_symlinks(root: Path) -> list[Path]:+ """Return symlinks below *root* without following them."""+ if not root.exists() and not root.is_symlink():+ return []+ if root.is_symlink():+ return [root]+ return [path for path in root.rglob("*") if path.is_symlink()]+def _read_skill_md(self, path: Path) -> tuple[dict, str]:"""Parse a SKILL.md file into (frontmatter dict, markdown body)."""raw = path.read_text(encoding="utf-8")diff --git a/tests/skills/test_importer.py b/tests/skills/test_importer.pyindex 08008d44..ae2e2fdc 100644--- a/tests/skills/test_importer.py+++ b/tests/skills/test_importer.py@@ -4,6 +4,8 @@ from __future__ import annotationsfrom pathlib import Path+import pytest+from openjarvis.skills.importer import SkillImporterfrom openjarvis.skills.parser import SkillParserfrom openjarvis.skills.sources.base import ResolvedSkill@@ -165,6 +167,53 @@ class TestImportSkill:assert (target / "references" / "REFERENCE.md").exists()assert (target / "assets" / "template.txt").exists()+ @pytest.mark.parametrize(+ ("subdir", "with_scripts"),+ [("references", False), ("scripts", True)],+ )+ def test_rejects_symlinks_before_copying_external_targets(+ self, tmp_path: Path, subdir: str, with_scripts: bool+ ):+ target_root = tmp_path / "skills"+ src_dir = tmp_path / "source" / "my-skill"+ src_dir.mkdir(parents=True)+ (src_dir / "SKILL.md").write_text(+ "---\nname: my-skill\ndescription: A test skill\n---\n"+ )++ outside = tmp_path / "outside.txt"+ outside.write_text("SENSITIVE-SENTINEL")+ copied_root = src_dir / subdir+ copied_root.mkdir()+ try:+ (copied_root / "linked.txt").symlink_to(outside)+ except OSError:+ pytest.skip("filesystem does not permit creating symlinks")++ resolved = ResolvedSkill(+ name="my-skill",+ source="hermes",+ path=src_dir,+ category="x",+ description="A test skill",+ commit="a",+ )+ importer = SkillImporter(+ parser=SkillParser(),+ tool_translator=ToolTranslator(),+ target_root=target_root,+ )+ target = target_root / "hermes" / "my-skill"+ target.mkdir(parents=True)+ (target / "keep.txt").write_text("existing install")++ result = importer.import_skill(resolved, with_scripts=with_scripts, force=True)++ assert not result.success+ assert any("symlink" in warning.lower() for warning in result.warnings)+ assert (target / "keep.txt").read_text() == "existing install"+ assert not (target / subdir / "linked.txt").exists()+def test_force_overwrites_existing_install(self, tmp_path: Path):target_root = tmp_path / "skills"importer = SkillImporter(