Skip to content

The agent that grows with you

fix(skills): keep inline shell off for nested, cross-profile and unreadable-lock community skills

Teknium587f638e5256
diff --git a/agent/skill_preprocessing.py b/agent/skill_preprocessing.py
index e07aa87b3f7..5464a68f51b 100644
--- a/agent/skill_preprocessing.py
+++ b/agent/skill_preprocessing.py
@@ -1,6 +1,7 @@
"""Shared SKILL.md preprocessing helpers: ``${HERMES_*}`` template tokens and
inline ``!`cmd``` shell expansion."""
+import json
import logging
import re
import subprocess
@@ -103,29 +104,39 @@ def _is_community_hub_skill(skill_dir: Path | None) -> bool:
DSL scans as high severity, so auto-executing it on view would re-arm exactly what the
gate refused (#63307). Provenance is the hub lock entry (trusted/builtin entries expand;
anything without one — bundled-synced, user-created, project/external — keeps the flag's
- contract). A lock read failure skips the gate, like every other provenance consumer.
+ contract). It covers every SKILL.md nested inside a community install and is read from
+ the lock that owns the skills tree the dir lives in, not only the active profile's
+ (``external_dirs`` can expose another profile's installs). A lock that exists but cannot
+ be read fails closed for skills in its tree; no lock means no hub installs.
"""
if skill_dir is None:
return False
try:
- from tools.skills_tool import _skills_dir
from tools.skills_hub import HubLockFile
- installed = HubLockFile().load().get("installed") or {}
- for entry in installed.values():
- if not (isinstance(entry, dict) and entry.get("trust_level") == "community"):
- continue
- rel = str(entry.get("install_path") or "")
- if not rel:
+ from tools.skills_tool import _skills_dir
+
+ target = skill_dir.resolve()
+ locks = {HubLockFile().path.resolve(): _skills_dir().resolve()}
+ for root in target.parents:
+ if root.name == "skills" and (root / ".hub" / "lock.json").is_file():
+ locks.setdefault((root / ".hub" / "lock.json").resolve(), root)
+ for lock_path, root in locks.items():
+ if not (target.is_relative_to(root) and lock_path.is_file()):
continue
try:
- if skill_dir.resolve() == (_skills_dir() / rel).resolve():
+ installed = json.loads(lock_path.read_text(encoding="utf-8-sig"))["installed"]
+ entries = [e for e in installed.values() if isinstance(e, dict)]
+ except (OSError, ValueError, KeyError, TypeError, AttributeError):
+ logger.debug("Unreadable hub lock %s; inline shell stays off for its tree", lock_path, exc_info=True)
+ return True
+ for entry in entries:
+ rel = str(entry.get("install_path") or "")
+ if entry.get("trust_level") == "community" and rel and target.is_relative_to((root / rel).resolve()):
return True
- except (OSError, ValueError):
- continue
return False
except Exception:
- logger.debug("Could not read hub lock for inline-shell trust scoping", exc_info=True)
- return False
+ logger.debug("Could not resolve hub provenance for inline-shell trust scoping", exc_info=True)
+ return True
def preprocess_skill_content(
diff --git a/tests/agent/test_skill_inline_shell_trust.py b/tests/agent/test_skill_inline_shell_trust.py
index c84381db586..99a763c151b 100644
--- a/tests/agent/test_skill_inline_shell_trust.py
+++ b/tests/agent/test_skill_inline_shell_trust.py
@@ -4,6 +4,8 @@ gate — a ``--force`` or pre-scanner community install must not re-arm them."""
import json
from unittest.mock import patch
+import pytest
+
from agent.skill_preprocessing import preprocess_skill_content
CFG = {"inline_shell": True, "inline_shell_timeout": 5}
@@ -77,3 +79,35 @@ def test_slash_invocation_leaves_community_snippet_raw(tmp_path):
# Executed output would be "Dynamic: SENTINEL"; the gate leaves the raw DSL.
assert "Dynamic: SENTINEL" not in msg
assert "!`printf SENTINEL`" in msg
+
+
+def _community_bundle_home(home, lock_text=None):
+ """A profile skills tree holding a --force community bundle with a nested SKILL.md,
+ plus a user-created skill; *lock_text* overrides the lock (corrupt/wrong shape)."""
+ skills = home / "skills"
+ _make_skill(skills, "outer")
+ _make_skill(skills, "outer/agents/inner")
+ _make_skill(skills, "mine")
+ lock = skills / ".hub" / "lock.json"
+ lock.parent.mkdir(parents=True)
+ lock.write_text(lock_text if lock_text is not None else json.dumps({"version": 1, "installed": {
+ "outer": {"trust_level": "community", "install_path": "outer"}}}), encoding="utf-8")
+ return skills
+
+
[email protected]("lock_text, viewer, rel, expands", [
+ (None, "owner", "outer/agents/inner", False), # nested SKILL.md inside a community bundle
+ (None, "other", "outer", False), # another profile reaching it through external_dirs
+ (None, "other", "outer/agents/inner", False),
+ ('{"version": 1, "inst', "owner", "outer", False), # truncated lock fails closed
+ ('{"version": 1, "installed": []}', "other", "outer", False), # wrong shape fails closed
+ (None, "owner", "mine", True), # user-created skill beside the install keeps the opt-in
+ (None, "other", "mine", True),
+])
+def test_community_provenance_follows_the_owning_lock(tmp_path, lock_text, viewer, rel, expands):
+ owner = _community_bundle_home(tmp_path / "owner", lock_text)
+ active = owner if viewer == "owner" else (tmp_path / "other" / "skills")
+ active.mkdir(parents=True, exist_ok=True)
+ with patch("tools.skills_tool.SKILLS_DIR", active):
+ rendered = preprocess_skill_content(SNIPPET, owner / rel, skills_cfg=dict(CFG))
+ assert rendered == ("Dynamic: SENTINEL" if expands else SNIPPET)