From d540a6dda977990503e27b115438a2607570baf5 Mon Sep 17 00:00:00 2001 From: Day Dreamer <87640074+HugoXK@users.noreply.github.com> Date: Tue, 17 Nov 2026 13:18:25 +0800 Subject: [PATCH 1/2] fix(snapshot): artifact_id is an identifier, not a path (#1150) A snapshot JSON can come from someone else; both staging writes joined its artifact_id under their tempdir, so an absolute id replaced the tempdir and a ../ chain walked out of it. The id is now validated at construction as one path-safe component, staging uses fixed names inside the tempdir, and a malformed snapshot reports as a clean CLI error instead of a traceback. --- agentdescent/artifact_snapshot.py | 21 ++- agentdescent/cli.py | 11 +- changelog.d/1150-snapshot-artifact-id.md | 8 ++ tests/test_snapshot_artifact_id.py | 174 +++++++++++++++++++++++ 4 files changed, 210 insertions(+), 4 deletions(-) create mode 100644 changelog.d/1150-snapshot-artifact-id.md create mode 100644 tests/test_snapshot_artifact_id.py diff --git a/agentdescent/artifact_snapshot.py b/agentdescent/artifact_snapshot.py index 56296efe..0e2d800f 100644 --- a/agentdescent/artifact_snapshot.py +++ b/agentdescent/artifact_snapshot.py @@ -24,6 +24,7 @@ import hashlib import json import os +import re import tempfile from dataclasses import dataclass, field, replace from typing import Any, Callable, Dict, Mapping, Optional, Sequence, Tuple @@ -98,6 +99,19 @@ class ArtifactSnapshot: def __post_init__(self) -> None: if not isinstance(self.artifact_id, str) or not self.artifact_id.strip(): raise ValueError("artifact_id must be a non-empty string") + # The id can come from a snapshot JSON someone else wrote, and both + # staging writes used to join it under their tempdir: it is an + # identifier, so it must be one path-safe component, like the id + # `EvolveSpec.artifact_id()` already sanitizes to (#1150). + if ("\x00" in self.artifact_id + or "/" in self.artifact_id or "\\" in self.artifact_id + or os.path.isabs(self.artifact_id) + or re.match(r"^[A-Za-z]:", self.artifact_id) + or self.artifact_id.strip() in (".", "..")): + raise ValueError( + "artifact_id must be one path-safe component (no '/', '\\', " + "':', NUL, absolute path or '..'), got: " + f"{self.artifact_id!r}") object.__setattr__(self, "files", _files(self.files)) object.__setattr__(self, "editable", _as_patterns(self.editable, ("**",))) object.__setattr__(self, "frozen", _as_patterns(self.frozen, ())) @@ -375,13 +389,16 @@ def evaluate_snapshot( def _write_target(snapshot: ArtifactSnapshot, kind: str, tmp: str) -> str: + # Staging never derives a path from `artifact_id`: the name is fixed, so + # even a construction regression elsewhere cannot steer the write + # outside `tmp` (#1150). if kind == "text": - path = os.path.join(tmp, snapshot.artifact_id) + path = os.path.join(tmp, "artifact.txt") text = snapshot.render() with open(path, "w", encoding="utf-8") as fh: fh.write(text) return path - dest = os.path.join(tmp, snapshot.artifact_id) + dest = os.path.join(tmp, "artifact") os.makedirs(dest, exist_ok=True) snapshot.materialize(dest) return dest diff --git a/agentdescent/cli.py b/agentdescent/cli.py index 6a7bc022..76177f95 100644 --- a/agentdescent/cli.py +++ b/agentdescent/cli.py @@ -3691,7 +3691,12 @@ def _snapshot_from_args(a: argparse.Namespace): path = os.path.abspath(os.path.expanduser(source)) if not os.path.isfile(path): source = resolve_run_id(source, store=a.store) or source - return load_snapshot_source(source, store=a.store) + try: + return load_snapshot_source(source, store=a.store) + except (TypeError, ValueError) as e: + # A malformed snapshot is bad input, like a malformed spec: the CLI + # reports it as exit 2, a traceback is not a verdict (#1150). + raise SpecError(f"snapshot {source}: {e}") from None def _print_snapshot(snap, *, as_json: bool) -> None: @@ -3749,7 +3754,9 @@ def cmd_snapshot_apply(a: argparse.Namespace) -> int: if not a.to: raise runstore.RunStoreError("snapshot apply needs --to PATH") with tempfile.TemporaryDirectory(prefix="ad-snapshot-apply-") as tmp: - src = os.path.join(tmp, snap.artifact_id or "artifact") + # Fixed staging name: the payload's `artifact_id` never reaches a + # path, so the staging write cannot leave `tmp` (#1150). + src = os.path.join(tmp, "artifact") os.makedirs(src, exist_ok=True) snap.materialize(src) payload = apply_from_payload( diff --git a/changelog.d/1150-snapshot-artifact-id.md b/changelog.d/1150-snapshot-artifact-id.md new file mode 100644 index 00000000..bc1c966b --- /dev/null +++ b/changelog.d/1150-snapshot-artifact-id.md @@ -0,0 +1,8 @@ +Fixed +A snapshot's `artifact_id` is validated as one path-safe component when the +snapshot is built, and both staging writes (`snapshot eval`, `snapshot apply`) +stage under a fixed name inside their temp directory instead of deriving a +path from the id: applying or scoring a snapshot JSON received from someone +else can no longer make the staging write land outside that temp directory. +A malformed id is reported as a clean CLI error (exit 2), not a traceback +(#1150). diff --git a/tests/test_snapshot_artifact_id.py b/tests/test_snapshot_artifact_id.py new file mode 100644 index 00000000..99255d41 --- /dev/null +++ b/tests/test_snapshot_artifact_id.py @@ -0,0 +1,174 @@ +"""`artifact_id` is an identifier, never a path component (#1150). + +A snapshot JSON can come from someone else, and both staging writes used to +derive their path from it: `os.path.join` lets an absolute id replace the +tempdir and a ``../`` chain walk out of it, so a victim applying or scoring +a snapshot JSON received from someone else could make the staging write +land outside the tempdir. The id is now validated as one path-safe +component at construction, and staging uses a fixed name so a regression +at either end alone cannot re-open the write. +""" +from __future__ import annotations + +import json +import os +import tempfile +from pathlib import Path + +import pytest + +from agentdescent.artifact_snapshot import ArtifactSnapshot, _write_target +from agentdescent.cli import main + +UNSAFE_IDS = [ + "/tmp/evil", # absolute replaces the tempdir + "C:/evil", "C:\\evil", # drive-letter forms + "../escaped.txt", # walks out of the tempdir + "..", ".", # the tempdir itself + "a/b", "a\\b", # separators inside + "evil\x00", # NUL +] + + +def _payload(artifact_id: str) -> dict: + return { + "format": "agentdescent.artifact_snapshot", + "format_version": 1, + "artifact_id": artifact_id, + "kind": "text", + "files": {"skill": "say yes"}, + "rendered": "say yes", + } + + +def _snapshot_file(path: Path, artifact_id: str) -> Path: + path.write_text(json.dumps(_payload(artifact_id))) + return path + + +def _dest_spec(path: Path) -> Path: + path.write_text(json.dumps({ + "kind": "text", "target": "ignored", + "agent": "echo", "score": "contains", + "data": {"inline": [ + {"id": "a", "prompt": "say yes", "gold": "yes"}, + ]}, + })) + return path + + +@pytest.mark.parametrize("artifact_id", UNSAFE_IDS) +def test_an_artifact_id_that_is_not_one_component_is_refused(artifact_id): + with pytest.raises(ValueError, match="artifact_id"): + ArtifactSnapshot(artifact_id=artifact_id, files={"skill": "x"}) + + +@pytest.mark.parametrize("artifact_id", UNSAFE_IDS) +def test_from_dict_refuses_the_same_ids_from_a_snapshot_json(artifact_id): + with pytest.raises(ValueError, match="artifact_id"): + ArtifactSnapshot.from_dict(_payload(artifact_id)) + + +@pytest.mark.parametrize("artifact_id", ["skill", "SKILL.md", "seed-prompt"]) +def test_a_safe_identifier_still_builds(artifact_id): + snap = ArtifactSnapshot(artifact_id=artifact_id, files={"skill": "x"}) + assert snap.snapshot_id == artifact_id + + +def test_eval_refuses_an_absolute_artifact_id_and_writes_nothing_there( + tmp_path, capsys, monkeypatch): + outside = tmp_path / "outside" + outside.mkdir() + evil = outside / "evil.txt" + source = _snapshot_file(tmp_path / "evil.json", str(evil)) + dest = _dest_spec(tmp_path / "dest.json") + + def boom(*args, **kwargs): + raise AssertionError("snapshot eval must not call evolve") + + monkeypatch.setattr("agentdescent.evolution.evolve", boom) + code = main(["--json", "snapshot", "eval", str(source), "--spec", str(dest)]) + assert code == 2 + assert not evil.exists() + shown = json.loads(capsys.readouterr().out) + assert shown["ok"] is False + assert "artifact_id" in shown["error"] + + +def test_eval_refuses_a_dotdot_artifact_id_and_writes_nothing_outside( + tmp_path, capsys, monkeypatch): + monkeypatch.setattr(tempfile, "tempdir", str(tmp_path)) + source = _snapshot_file(tmp_path / "evil.json", "../escaped.txt") + dest = _dest_spec(tmp_path / "dest.json") + + def boom(*args, **kwargs): + raise AssertionError("snapshot eval must not call evolve") + + monkeypatch.setattr("agentdescent.evolution.evolve", boom) + code = main(["--json", "snapshot", "eval", str(source), "--spec", str(dest)]) + assert code == 2 + assert not (tmp_path / "escaped.txt").exists() + shown = json.loads(capsys.readouterr().out) + assert shown["ok"] is False + + +def test_apply_refuses_an_absolute_artifact_id_and_writes_nothing_there( + tmp_path, capsys): + outside = tmp_path / "outside" + outside.mkdir() + source = _snapshot_file(tmp_path / "evil.json", str(outside)) + to = tmp_path / "victim" + + code = main(["--json", "snapshot", "apply", str(source), "--to", str(to), + "--dry-run"]) + assert code == 2 + assert not any(outside.iterdir()) + shown = json.loads(capsys.readouterr().out) + assert shown["ok"] is False + assert "artifact_id" in shown["error"] + + +def test_apply_refuses_a_dotdot_artifact_id_and_writes_nothing_outside( + tmp_path, capsys, monkeypatch): + monkeypatch.setattr(tempfile, "tempdir", str(tmp_path)) + source = _snapshot_file(tmp_path / "evil.json", "../escaped") + to = tmp_path / "victim" + + code = main(["--json", "snapshot", "apply", str(source), "--to", str(to), + "--dry-run"]) + assert code == 2 + assert not (tmp_path / "escaped").exists() + shown = json.loads(capsys.readouterr().out) + assert shown["ok"] is False + + +def test_write_target_stages_text_under_a_fixed_name(tmp_path): + snap = ArtifactSnapshot(artifact_id="skill", files={"skill": "say yes"}, + rendered="say yes") + path = _write_target(snap, "text", str(tmp_path)) + assert os.path.basename(path) == "artifact.txt" + assert Path(path).read_text() == "say yes" + assert not (tmp_path / "skill").exists() + + +def test_write_target_stages_a_tree_under_a_fixed_name(tmp_path): + snap = ArtifactSnapshot(artifact_id="skill", files={"SKILL.md": "x"}) + dest = _write_target(snap, "skill_dir", str(tmp_path)) + assert os.path.basename(dest) == "artifact" + assert (Path(dest) / "SKILL.md").read_text() == "x" + assert not (tmp_path / "skill").exists() + + +def test_apply_stages_under_a_fixed_name_not_the_id(tmp_path, capsys, monkeypatch): + captured = {} + + def fake_apply(src, *, to, dry_run, backup, check_write): + captured["src"] = src + return {"ok": True, "planned": 1} + + monkeypatch.setattr("agentdescent.cli.apply_from_payload", fake_apply) + source = _snapshot_file(tmp_path / "good.json", "skill") + code = main(["--json", "snapshot", "apply", str(source), + "--to", str(tmp_path / "victim")]) + assert code == 0 + assert os.path.basename(captured["src"]) == "artifact" From 24a595d9e44f8f1ab8f4153cd794839a4c453401 Mon Sep 17 00:00:00 2001 From: Day Dreamer <87640074+HugoXK@users.noreply.github.com> Date: Wed, 18 Nov 2026 08:31:14 +0800 Subject: [PATCH 2/2] fix(snapshot): the refusal message names only what is refused It said ":" was rejected, but only a drive-letter prefix is; `a:b` stays a legal id (harmless now that the id is never a path). No behaviour change, no test asserts on this string. --- agentdescent/artifact_snapshot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/agentdescent/artifact_snapshot.py b/agentdescent/artifact_snapshot.py index 0e2d800f..fb21e1dc 100644 --- a/agentdescent/artifact_snapshot.py +++ b/agentdescent/artifact_snapshot.py @@ -110,7 +110,7 @@ def __post_init__(self) -> None: or self.artifact_id.strip() in (".", "..")): raise ValueError( "artifact_id must be one path-safe component (no '/', '\\', " - "':', NUL, absolute path or '..'), got: " + "NUL, absolute path, drive letter or '..'), got: " f"{self.artifact_id!r}") object.__setattr__(self, "files", _files(self.files)) object.__setattr__(self, "editable", _as_patterns(self.editable, ("**",)))