Repository navigation
fix(cli): refuse a symlink planted in apply's destination - #988
Conversation
|
Naming the tip: Red at base Green at the tip: 204 focused tests passed, including Ready for review. |
`apply` link-checked the source and then handed `--to` to `shutil.copy2` and `open`, both of which follow a link. Probe `skills/SKILL.md -> id_rsa` against live main overwrote the target and left `skills/SKILL.md.bak-0` holding the target's bytes inside a directory host scanners read. The destination is now checked before any backup is taken: `os.path.islink` on the leaf and on every existing component under `--to`, plus containment on the leaf and its parent. The rule lives in `filetree.dest_problem`, next to `materialize`'s boundary, and both writers use it -- `apply <run_id>`'s tree path goes through `materialize`, `apply --from` through its own copy loop. `_backup_slot` also uses `lexists`, so a dangling link at `.bak-0` is skipped rather than written through. Four offline tests: planted directory link, planted file link at the leaf, a tree file link pointing outside `--to`, and the run-id tree writer. Each asserts the refusal, that the outside file is byte-identical, and that no `.bak-N` holds its bytes.
The tree backup slot scanned `bak-N` with `exists`, so a dangling link there looked free and `copytree` died on it. Scan with `lexists` (#977). `apply` now refuses a destination link before the write-allowlist check, so `test_existing_directory_link_cannot_escape[apply]` asserts the new refusal; the escape is still refused, only the message moved. The run-id test sets `AGENTDESCENT_HOME` like its siblings so it cannot write into the developer's own home.
|
Verdict: PASS Reviewed: Ran: Usability note (non-blocking): a |
…out (#1282) (#1285) Closes #1282. ## What changed **Before:** `tests/test_docs_citations.py` decided containment lexically (`..` in the path, or an absolute path) and then called `is_file()` / `_line_count()` on `root / path`. A committed symlink at that leaf is neither `..` nor absolute, so the guard followed it and line-counted a file this repository does not contain. An in-range citation reported nothing (the page read as verified); a past-the-end one reported the outside file's length. **After:** after the lexical checks, the guard resolves the cited path and requires it to stay under `root.resolve()` before any `is_file()` or line count. An escaping link is reported as `(citation path escapes this repository)`, the same message as `#988` / the `..` and absolute cases. The shipped docs are untouched. Tests-only plus the guard in the test module — no `docs/` or runtime change, so no changelog fragment (`changelog_fragments check` passes without one). ## Verification at the tip Tip SHA: 4208b59 Red at `48fdc67889` with only the test delta (`6629fddb97`): ``` 2 failed, 5 passed in 0.12s tests/test_docs_citations.py:184: AssertionError: [] (assert 0 == 1) — in-range citation through the link reported nothing tests/test_docs_citations.py:200: AssertionError: ['docs/page.md:1 -> link.txt:9 (file has 3 lines)'] — past-the-end reported the outside file's length ``` Ran: - `python -m pytest tests/test_docs_citations.py -q` → `7 passed in 0.10s` - `python -m pytest tests/test_docs_citations.py tests/test_docs_links.py tests/test_site_navigation.py tests/test_docs_examples.py -q` → `38 passed in 7.92s` - `python -m tools.changelog_fragments check` → `47 changelog fragment(s) OK` - `python -m tools.gen_api_docs --check` → `docs/api.md is up to date` Not run: - full `pytest -q` — the delta is one test file, the docs family above is the affected surface. - `mkdocs build --strict` — no page, nav or mkdocs.yml change.
Fixes #977.
What changed
applyrefused a symlink in the source and then handed the destination toshutil.copy2/open, both of which follow a link. Probeskills/SKILL.md -> id_rsaagainst live main overwrote the target and leftskills/SKILL.md.bak-0holding the target's bytes, inside a directory host scanners read.os.path.islinkon the leaf and on every existing component under--to, then containment on the leaf as well as its parent. The rule isfiletree.dest_problem(), extracted besidematerialize()'s realpath boundary instead of a second copy of it -- soapply <run_id>'s tree path (which writes throughmaterialize) andapply --from's copy loop refuse the same way.--toitself, when it names a directory, is resolved; links under it, and a link at a file destination, are refused. A host may legitimately point at a symlinked skills dir, so the root is the one case that is followed on purpose._backup_slotnow usesos.path.lexists, so a dangling link at.bak-0is skipped rather than written through by the backup copy._file_tree_backup_pathinevolution.pyhad the sameexistsscan for the directory backup slot: a dangling link there made the slot look free andcopytreedied on it with an unhandledFileExistsError, so it useslexiststoo and picksbak-1.refuse to apply to '/…/dest/skills/SKILL.md': 'skills/SKILL.md' is a symlink under --to '/…/dest'.tests/test_apply_symlink_dest.py: a planted directory link, a planted file link at the leaf, a tree file link pointing outside--to, the run-id tree writer, and a dangling link atbak-0(skipped,bak-1used). Each destination-link test asserts the refusal, that the outside file is byte-identical, and that no.bak-N(or a link inside one) holds its bytes.test_existing_directory_link_cannot_escape[apply]intests/test_mcp_write_confinement.pymatches the new earlier refusal:applyrefuses the link before the write-allowlist check. The escape is still refused, the message moved; theopsparameterisation keeps assertingwrite allowlist.Two files outside the issue's literal "cli.py + test + fragment" list:
agentdescent/filetree.py, where the shared boundary had to live to be reused rather than re-implemented (the extracted helper plus its call sites), andagentdescent/evolution.pyfor the one-linelexistsfix and its comment.docs/api.mdis unchanged (_file_tree_backup_pathis private).T3, not T2, once measured: 256 changed lines (+248/-8) over 6 files, and
agentdescent/evolution*.pyis a T3 path in.github/agent/risk-rules.json. The size is over the T2 cap because of the two extra tests and thatlexistsfix; the code change itself is the guard plus the extracted helper.Verification at the tip
Tip SHA: 1033f78
Ran:
cbf20e14(the base):tests/test_apply_symlink_dest.pycollects there (it imports onlycli,EvolutionResult,TreeError,canonical, all present at base) and all five tests fail there --apply --fromwriting through the link,write_tonot raising, andapply --fromdying withFileExistsErroron the danglingbak-0. The failures are assertion/FileExistsErrorfailures at base, not import errors.test_apply_symlink_dest.py,test_apply_from.py,test_apply_noop.py,test_apply_backup_away_from_skills.py,test_runstore_cli.py,test_filetree.py,test_skill_library_apply_install.py,test_mcp_write_confinement.py,test_mcp.py,test_mcp_meta.py,test_latest_run_alias.py): 204 passed.python -m tools.gen_api_docs --checkup to date;python -m tools.changelog_fragments check94 OK.pytest -qat this head: 6982 passed, 11 failed, 46 skipped, 10 errors (27m45s). The 11 failures and 10 errors are the host-only baseline, identical at a clean9d991bfe90worktree: 6xtest_era_srbenchMemoryErrorin this sandbox, 2xtest_genesis_example(no hostclaude-code), 2xtest_sandbox_containerplus 10 docker-gated errors (no usable docker), 1xtest_wheel_smokeTMPDIR. The previous tipd46d5062had 12 failed -- the extra one wastest_existing_directory_link_cannot_escape[apply], fixed in this tip.Not run:
mkdocs build --strict— nodocs/ormkdocs.ymlchange.tests/test_sandbox_container.pycases — no usable docker here.