Files
graphify/tests/test_update_manifest_preservation.py
safishamsiandClaude Opus 4.8 c3d5305eb2 security(skill): read the scan root from .graphify_root, never substitute it (#4762)
The generated /graphify runbook substituted the agent-supplied scan path straight
into Python string literals across every code block: detect(Path('INPUT_PATH')),
extract(cache_root=Path('INPUT_PATH'), root=Path('INPUT_PATH')), the semantic
cache check/save, build_from_json/generate/diagnose, the manifest write, and the
--update detect_incremental + merge. A scan root whose NAME carried a single
quote or a $(...) / backtick broke out of the literal and ran as arbitrary code
the moment the block ran (e.g. a cloned repo with a subdirectory named
$(...) ). The shell transport was already hardened (#3642/#3844 write the path
to graphify-out/.graphify_root through a quoted heredoc); this extends the same
trust boundary to the Python blocks.

Each block now reads the resolved root from graphify-out/.graphify_root (the file
Step 1 writes) into an INPUT_PATH variable and passes the value, so the path is
always inert data, never code. The prose no longer tells the agent to substitute
a path. Applied to the assembled hosts (core.md, references/shared/update.md) and
to the Aider and Devin monoliths; a new _is_input_path_sidecar_fix_line sanctions
the monolith diff and --monolith-roundtrip / --check / --audit-coverage stay
green. A fast regression guard asserts no generated skill body carries a quoted
'INPUT_PATH' code literal.

SPEC_PATH (the skill's own extraction-spec install path, not attacker-controlled)
is left substituted and tracked as a follow-up.

Reported and first proposed by @danikdanik in #4762.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-07 23:13:09 +00:00

53 lines
2.7 KiB
Python

"""Execute the generated update manifest block against real detector state."""
import json
from pathlib import Path
import pytest
from graphify.detect import detect_incremental, load_manifest, save_manifest
from tools.skillgen import gen
SPLIT_HOSTS = [key for key, platform in gen.load_platforms().items()
if any(str(a.path).replace("\\", "/").endswith("/references/update.md") for a in gen.render(platform))]
@pytest.mark.parametrize("host", SPLIT_HOSTS)
@pytest.mark.parametrize("failed_change", [False, True])
def test_update_preserves_undispatched_semantic_stamps(tmp_path, monkeypatch, host, failed_change):
monkeypatch.chdir(tmp_path)
(tmp_path / "graphify-out").mkdir()
docs = [tmp_path / f"doc{i}.md" for i in range(5)]
for p in docs:
p.write_text("# A real document\nUnchanged semantic content.\n", encoding="utf-8")
save_manifest({"document": [str(p) for p in docs]}, root=tmp_path)
before = load_manifest(root=tmp_path)
docs[0].unlink()
if failed_change:
docs[1].write_text("# Changed\nThis document's extraction failed.\n", encoding="utf-8")
incremental = detect_incremental(tmp_path)
assert incremental["new_total"] == int(failed_change)
assert len(incremental["deleted_files"]) == 1
extraction_path = Path("graphify-out/.graphify_extract.json")
extraction_path.write_text(json.dumps({"nodes": [], "edges": []}), encoding="utf-8")
artifact = next(a for a in gen.render(gen.load_platforms()[host])
if str(a.path).replace("\\", "/").endswith("/references/update.md"))
# Execute exactly the generated manifest block, including clear_semantic
# and the raw scan corpus. INPUT_PATH is a constant fixture root here.
block = artifact.content.split("from graphify.cli import _stamped_manifest_files", 1)[1]
block = "from graphify.cli import _stamped_manifest_files" + block.split("print('[graphify update] Manifest saved.')", 1)[0]
# Since #4762 the block reads the scan root from .graphify_root into INPUT_PATH
# above this slice; provide that resolved value directly in the exec scope.
scope = {"incremental": incremental, "new_extraction": json.loads(extraction_path.read_text(encoding="utf-8")),
"Path": Path, "save_manifest": save_manifest, "INPUT_PATH": "."}
exec(compile(block, str(artifact.path), "exec"), scope)
after = load_manifest(root=tmp_path)
assert str(docs[0]) not in after
for p in docs[2:] if failed_change else docs[1:]:
assert after[str(p)] == before[str(p)]
following = detect_incremental(tmp_path)
assert following["new_total"] == int(failed_change)
assert following["deleted_files"] == []
if failed_change:
assert not after[str(docs[1])]["semantic_hash"]