Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions agentdescent/artifact_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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, drive letter 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, ()))
Expand Down Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions agentdescent/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3792,7 +3792,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:
Expand Down Expand Up @@ -3861,11 +3866,13 @@ def cmd_snapshot_apply(a: argparse.Namespace) -> int:
f"snapshot {snap.snapshot_id} is a text artifact and "
f"{dest!r} is a directory; pass --to FILE to write it "
"to a file")
src = os.path.join(tmp, "artifact")
src = os.path.join(tmp, "artifact.txt")
with open(src, "w", encoding="utf-8", newline="") as fh:
fh.write(snap.render())
else:
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(
Expand Down
8 changes: 8 additions & 0 deletions changelog.d/1150-snapshot-artifact-id.md
Original file line number Diff line number Diff line change
@@ -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).
186 changes: 186 additions & 0 deletions tests/test_snapshot_artifact_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
"""`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()


@pytest.mark.parametrize("kind,files,expected", [
("text", {"skill": "say yes"}, "artifact.txt"),
("skill_dir", {"SKILL.md": "x"}, "artifact"),
])
def test_apply_stages_under_a_fixed_name_not_the_id(
tmp_path, capsys, monkeypatch, kind, files, expected):
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)
payload = _payload("skill")
payload["kind"] = kind
payload["files"] = files
if kind != "text":
payload.pop("rendered", None)
source = tmp_path / "good.json"
source.write_text(json.dumps(payload))
code = main(["--json", "snapshot", "apply", str(source),
"--to", str(tmp_path / "victim")])
assert code == 0
assert os.path.basename(captured["src"]) == expected
assert not (tmp_path / "victim" / "skill").exists()
Loading