Skip to content

The agent that grows with you

fix(plugin-catalog): rule 17 gate no longer fails every Python entry on bytecode validate wrote

Teknium119fbcf84e3e
diff --git a/scripts/scan_plugin_payloads.py b/scripts/scan_plugin_payloads.py
index d86095ec52f..d67e8bf20e9 100755
--- a/scripts/scan_plugin_payloads.py
+++ b/scripts/scan_plugin_payloads.py
@@ -23,6 +23,8 @@ Exit 0 = clean, 1 = a payload was found (lines on stdout), 2 = bad usage.
from __future__ import annotations
import io
+import os
+import subprocess
import sys
import tarfile
import zipfile
@@ -131,9 +133,23 @@ def _file_problem(path: Path, rel: str) -> str | None:
return None
+def _shipped_files(plugin_dir: Path) -> list[Path]:
+ """Files the pinned commit ships. In a git checkout that is the tracked set: the
+ catalog gate runs `hermes plugins validate` (which imports the plugin) before this
+ scan, so untracked `__pycache__/*.pyc` it wrote must not read as committed bytecode."""
+ try:
+ out = subprocess.run(["git", "-C", str(plugin_dir), "ls-files", "-z"],
+ capture_output=True, check=True, timeout=120).stdout
+ except (OSError, subprocess.SubprocessError):
+ out = b""
+ tracked = [plugin_dir / os.fsdecode(p) for p in out.split(b"\0") if p]
+ # Not a checkout, or a tree nothing tracks (an untracked dir inside some other repo).
+ return sorted(tracked) if tracked else sorted(plugin_dir.rglob("*"))
+
+
def scan(plugin_dir: Path) -> list[str]:
findings: list[str] = []
- for path in sorted(plugin_dir.rglob("*")):
+ for path in _shipped_files(plugin_dir):
if path.is_symlink() or not path.is_file():
continue
if "/.git/" in "/" + path.as_posix():
diff --git a/tests/scripts/test_scan_plugin_payloads.py b/tests/scripts/test_scan_plugin_payloads.py
index 850d8b816d9..aabcf3fd114 100644
--- a/tests/scripts/test_scan_plugin_payloads.py
+++ b/tests/scripts/test_scan_plugin_payloads.py
@@ -109,3 +109,19 @@ def test_symlinks_are_not_followed(tmp_path):
except OSError:
pytest.skip("symlinks unsupported")
assert _scan(tmp_path) == []
+
+
+def test_git_checkout_judges_the_commit_not_bytecode_validate_wrote(tmp_path):
+ """The catalog gate imports the plugin (validate) before scanning, which writes
+ untracked __pycache__; only bytecode the pinned commit tracks fails rule 17."""
+ git = ["git", "-C", str(tmp_path), "-c", "user.name=t", "-c", "user.email=t@t"]
+ subprocess.run(["git", "init", "-q", str(tmp_path)], check=True)
+ (tmp_path / "__init__.py").write_text("x = 1\n")
+ subprocess.run([*git, "add", "-A"], check=True)
+ subprocess.run([*git, "commit", "-qm", "src"], check=True)
+ (tmp_path / "__pycache__").mkdir()
+ (tmp_path / "__pycache__" / "__init__.cpython-314.pyc").write_bytes(b"\x00" * 16)
+ assert _scan(tmp_path) == []
+ subprocess.run([*git, "add", "-f", "__pycache__"], check=True)
+ subprocess.run([*git, "commit", "-qm", "bytecode"], check=True)
+ assert any("committed Python bytecode" in f for f in _scan(tmp_path))