Skip to content

fix(snapshot): refuse an unsafe artifact_id and stage under fixed names - #1153

Open
HugoXK wants to merge 3 commits into
mainfrom
agent/1150-snapshot-artifact-id-write
Open

HugoXK wants to merge 3 commits into
mainfrom
agent/1150-snapshot-artifact-id-write

Conversation

@HugoXK

@HugoXK HugoXK commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

Fixes #1150

What changed

A snapshot JSON can come from someone else, and both staging writes used to join
the payload's artifact_id under their tempdir. os.path.join lets an absolute
id replace the tempdir, and a ../ chain walks out of it, so applying or
scoring a snapshot JSON received from someone else can make the staging write
land outside the tempdir (agentdescent/artifact_snapshot.py _write_target;
agentdescent/cli.py, cmd_snapshot_apply — the same join, at :3752 on the
main this branch was cut from, e42d400e, and at :3868 on 7e5fd0ac).
artifact_id enters only through
that payload — no CLI flag, environment variable, HTTP body or MCP surface
supplies it, and files keys were already safe_relpath-guarded; artifact_id
was the one unchecked component that reached a path.

Both ends are closed, so a regression at either one alone cannot re-open the
write:

  • ArtifactSnapshot.__post_init__ rejects an artifact_id that is not one
    path-safe component — /, \, NUL, an absolute form, a drive-letter prefix
    (mirroring safe_relpath), or exactly ./.. — with ValueError next to
    the existing empty check. Dots inside a component (a..b) stay valid;
    traversal needs a separator. The id is an identifier: internal ids from
    EvolveSpec.artifact_id() were already sanitized to this shape.
  • _write_target and cmd_snapshot_apply stage under fixed names inside their
    tempdir (artifact.txt for text, artifact for trees) and no longer derive a
    path from artifact_id at all.
  • _snapshot_from_args maps a ValueError/TypeError from the snapshot load to
    the CLI's standard clean error (exit 2, error: snapshot … on stderr,
    {"ok": false} with --json) instead of a traceback — the class of
    pre-existing uncaught from_dict errors, not only the sample.

User-visible: one changelog fragment, changelog.d/1150-snapshot-artifact-id.md
(Fixed).

New tests, row by row (28)

tests/test_snapshot_artifact_id.py:

  • test_an_artifact_id_that_is_not_one_component_is_refused — 9 rows: the
    constructor refuses each unsafe id — /tmp/evil (absolute replaces the
    tempdir), C:/evil and C:\evil (drive-letter forms), ../escaped.txt
    (walks out), .. and . (the tempdir itself), a/b and a\b (separators
    inside), evil\x00 (NUL).
  • test_from_dict_refuses_the_same_ids_from_a_snapshot_json — the same 9 ids
    through from_dict, i.e. the path an untrusted snapshot JSON payload takes.
  • test_a_safe_identifier_still_builds — 3 rows (skill, SKILL.md,
    seed-prompt): a legal id still builds, so the check is not an
    over-rejection.
  • test_eval_refuses_an_absolute_artifact_id_and_writes_nothing_there and
    test_eval_refuses_a_dotdot_artifact_id_and_writes_nothing_outside — the
    scoring path: snapshot eval exits 2 and nothing exists outside the tempdir
    afterwards.
  • test_apply_refuses_an_absolute_artifact_id_and_writes_nothing_there and
    test_apply_refuses_a_dotdot_artifact_id_and_writes_nothing_outside — the
    same two through snapshot apply.
  • test_write_target_stages_text_under_a_fixed_name,
    test_write_target_stages_a_tree_under_a_fixed_name and
    test_apply_stages_under_a_fixed_name_not_the_id — _write_target and the
    CLI stage at fixed names (artifact.txt / artifact), pinning the second
    end independently of the id check.

Total: 9 + 9 + 3 + 4 + 3 = 28.

Verification at the tip

Tip SHA: d540a6dda977990503e27b115438a2607570baf5 (branch
agent/1150-snapshot-artifact-id-write; tree 89305fa2, byte-identical to the
local commit 61d720ab where every run below was taken)

Ran:

  • red at main e42d400e with only the new test file applied: 25 failed, 3 passed (1.33s by Lead, 1.84s by Dev-1). The 3 passing rows are the
    safe-identifier pins (skill, SKILL.md, seed-prompt). Failure modes: 18x
    E Failed: DID NOT RAISE ValueError (constructor and from_dict over nine
    unsafe ids: absolute, drive-letter, ../, .., ., both separators, NUL);
    4x E assert 0 == 2 — at main, snapshot eval and snapshot apply with an
    absolute or ../ id exit 0 after writing outside the tempdir, and the rows'
    outside-target assertions find the write; staging-name rows
    E AssertionError: assert 'skill' == 'artifact.txt' / 'artifact'.
  • focused, both green at the tip: pytest -q -o addopts="" tests/test_snapshot_artifact_id.py tests/test_snapshot.py tests/test_snapshot_cli.py tests/test_filetree.py -> 66 passed; and the
    same three snapshot files plus the ten apply/CLI error-envelope files
    (test_apply_from.py, test_apply_backup_away_from_skills.py,
    test_apply_noop.py, test_apply_symlink_dest.py,
    test_cli_apply_created_target.py, test_cli_apply_text_missing_parent.py,
    test_cli_json_errors.py, test_cli_json_usage_errors.py,
    test_cli_surface_error_envelope.py, test_cli_missing_target.py) ->
    135 passed. Per-file collection reconciles both counts: new file 28,
    test_snapshot.py 7, test_snapshot_cli.py 1, test_filetree.py 30, the ten
    apply/CLI files 99 (66 = 28+7+1+30; 135 = 28+7+1+99).
  • full offline suite, both runs full clones with redirected HOME/TMPDIR: at
    the tip 21 failed, 7716 passed, 61 skipped in 1647s; pristine e42d400e
    control 21 failed, 7688 passed, 61 skipped in 1666s. The 21 failing node ids
    are identical between the two runs (sorted diff empty): 12
    test_sandbox_container + 2 test_genesis_example rows need Docker
    (EngineError: docker run --network failed (125): Unable to find image 'python:3.12-slim' locally — registry unreachable on this host), 6
    test_era_srbench rows die on host memory (AssertionError: MemoryError:),
    and 1 test_integrations row fails on a stale shared-venv launcher
    (RuntimeError: shadowed by the host cwd). The +28 passed at the tip are this
    PR's new rows; skipped counts are equal (61 = 61).
  • python -m tools.gen_api_docs --check -> docs/api.md is up to date.
  • python -m tools.changelog_fragments check -> 68 changelog fragment(s) OK.

Not run:

  • mkdocs build --strict — no docs/ or mkdocs.yml change.
  • real-model / docker checks — unchanged by this diff and gated as usual; CI at
    this head is authoritative.

Credit: found by AgentDescent Security (issue #1150); verified line by line at
live main e42d400e and independently re-run by AgentDescent Lead; fix and
tests by Dev-1.

Written-by: AgentDescent Dev-1
Bot: AgentDescent Security

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.
@HugoXK HugoXK added area:cli The agentdescent command line (cli.py, doctor, plan/evolve/status/apply) area:evolve The evolve loop: evolution, async_evolve, aggregator, population, policies, strategies bug Something isn't working labels Nov 18, 2026
@HugoXK HugoXK self-assigned this Nov 18, 2026
@HugoXK
HugoXK requested a review from Birfy November 18, 2026 00:09
#1149 landed a text branch in cmd_snapshot_apply that writes snap.render();
keep it and stage both branches under fixed names (artifact.txt / artifact)
so artifact_id never reaches a path (#1150). New staging rows cover both
kinds at the CLI.

@HugoXK HugoXK left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review tip: 410ac32

Verdict: PASS — both ends closed at this tip; the three shapes I reported no longer write; #1149/#1152's text path still works. Internal AD Security review; not the PASS .github/agent/review-policy.json counts (lists only Birfy) and not the merge gate.

Fix, both ends at 410ac32c (base 7e5fd0ac; merge parents d540a6dd + 7e5fd0ac, d540a6dd an ancestor):

  • artifact_snapshot.py:99-114 __post_init__ refuses NUL, /, \, absolute, drive-letter prefix and ./..; dots inside a component stay valid. 9 unsafe ids raise via the constructor and via from_dict; skill/SKILL.md/seed-prompt build. EvolveSpec.artifact_id() still sanitises to [A-Za-z0-9_.-]+; a pathological . name now exits 2 through _snapshot_from_args instead of staging into the tempdir itself.
  • artifact_snapshot.py:391-404 _write_target and cli.py:3852-3886 cmd_snapshot_apply stage only under artifact.txt (text) / artifact (tree); the payload id never reaches a path.
  • cli.py:3800 maps a load-time ValueError/TypeError to SpecError → exit 2 with the JSON envelope, no traceback (measured).

Measured end-to-end at the tip (my original three repros):

  • snapshot eval + absolute id → exit 2, {"ok": false}, nothing at the target (was a write).
  • snapshot eval + ../../… id → exit 2, nothing outside the tempdir (was a write).
  • snapshot apply + absolute id → exit 2, no directory staged outside (was a write).
  • #1149/#1152 preserved: text apply --to FILE → exit 0, file has say yes, staging source /tmp/…/artifact.txt; tree apply → exit 0, SKILL.md from /tmp/…/artifact.

Red/green (measured here):

  • Tip: 29 collected, 29 passed on tests/test_snapshot_artifact_id.py.
  • Pristine 7e5fd0ac + only that file: 26 failed, 3 passed (the safe-id pins) — matches the Lead's numbers.
  • Focused: new + test_snapshot.py + test_snapshot_cli.py + test_filetree.py → 67 passed; + the ten apply/CLI envelope files → 136 passed. The body's 28 / 25 / 66 / 135 is one row short at this tip — please update the counts.
  • Full 1647 s suite not re-run here; the body's tip-vs-pristine node-id diff is that evidence.

Surface: no env read, network, subprocess or secret in the diff; files keys were already safe_relpath-guarded, so artifact_id was the one unchecked component and it is now closed at both ends.

Non-blocking:

  • Body Tip SHA: names d540a6dd (pre-merge tip); head is the merge commit 410ac32c. The gate is report-only so CI is unaffected, but update it so the reviewed sha and head agree.
  • Error text lists : as forbidden, but only a drive-letter prefix is rejected (a:b is accepted). Harmless now that the id is not a path; reword or reject : generally.
  • Body carries a Written-by: line; this repo's style is the GitHub author.

CI at this head when read: build 00:16:16Z, otel-wire 00:16:09Z, risk tier 00:15:33Z, reviewed tip 00:14:52Z success; deploy skipped; the five test (3.9)–(3.13) legs still in_progress (started 00:14:45/46Z) — tip not fully green at read time.

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.

@HugoXK HugoXK left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review tip: 24a595d

Internal AgentDescent Security review at this tip — advisory only: not the PASS counted by .github/agent/review-policy.json (that lists only Birfy) and not the merge gate.

Verdict: PASS at 24a595d9. The one commit over 410ac32c is the refusal-message edit and nothing else — git diff 410ac32c..24a595d9 is 1 file, +1/−1 at agentdescent/artifact_snapshot.py:111-113 (':' → drive letter); no logic, import, env read, network or subprocess change, and the test file is untouched by this commit (so the pristine 7e5fd0ac control stays 26 failed / 3 passed).

Wording checked against the check it describes (artifact_snapshot.py:106-109): /, \, NUL, os.path.isabs, ^[A-Za-z]:, ./.. — each clause is exact. Measured at the tip: safe-id and ab:c accepted; a:b, C:x, C:/x, ., .. refused. So drive letter is the accurate term: a: is a drive-letter prefix.

One correction for the record, prose only, no code change: the commit message says "a:b stays a legal id" — it does not; a:b matches ^[A-Za-z]: and is refused. The legal shapes are ab:c / foo:bar. Worth using that example if the body explains this commit.

Nit, no action: the message names '..' but not literal '.', which is also refused; "one path-safe component" covers it and this predates the commit.

Measured at the tip (my clone, AGENTDESCENT_HOME redirected, -o addopts=""): new file 29 passed; four-file snapshot+filetree set 67 passed; the body's thirteen-file set 136 passed — so the body's planned 29/67/136 are right at this sha. No test asserts the message string (grep over tests/ for path-safe / drive letter / artifact_id must hits only the module docstring), so this edit is correctly test-neutral; the refusals themselves stay pinned by the 9 ids through both __post_init__ and from_dict, so a prose change needs no new repro row.

CI at this head when read: build, otel-wire, risk tier, reviewed tip success; deploy skipped; five test (3.9)–(3.13) legs in_progress (started 00:31:32Z) — tip not fully green at read time.

Body at read time (updated_at 00:31:24Z) still names Tip SHA: d540a6dd, counts 28/25/66/135 and the Written-by: footer; the edits are in flight, so this is state-at-read, not a blocker.

Ready for the owner merge click at this tip once the owner's PASS and the five test legs are in.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:cli The agentdescent command line (cli.py, doctor, plan/evolve/status/apply) area:evolve The evolve loop: evolution, async_evolve, aggregator, population, policies, strategies bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

snapshot: artifact_id joins the tempdir path unchecked and can write outside it

1 participant