Repository navigation
fix(code-review-sage): refuse a junctioned runs root in the follow-up guard - #8869
Conversation
Design Review (Fable 5, fork) — ✅ PASSDesign-level review of Design-Verdict: PASS Real Windows-only guard bypass, fixed at the root (the link predicate) through the repo's own I verified the load-bearing claims against the base tree: [DESIGN-REVIEWED] d234104 |
Opus 4.8 Review (fork) — ✅ no blocking findingsReviewed Review detailsThe change is a strict security strengthening: No findings. [OPUS-REVIEWED] d234104 |
GPT 5.6 Review (fork) — ✅ no blocking findingsReviewed Review detailsNo findings. |
First Principles Review (Fable 5, fork) — 🟡 CONCERNSPremise-level review of All claims verified. I have everything needed for the review: the fix is derived from a documented shim-table invariant ( First-Principles-Verdict: CONCERNS The predicate fix is derived and correct, but it point-patches one of at least two live directory-guard sites, and adds a third copy of the junction test helper. Not justified as shipped
What this change shipsInventory (5 items) — 4 justifiedIntent: stop a Windows directory junction planted at the follow-up runs root from relocating the containment anchor into an attacker-chosen directory — a FIX.
WatchThe description's sibling count is app-scoped ("The two other link guards in this app are not siblings" — verified true), but repo-wide the same root cause survives: grepped SubtractionsCollapse the three [FIRST-PRINCIPLES-REVIEWED] d234104 |
5cdcea7 to
e62f303
Compare
|
🤖 Kiro Crew [operator: NicholasRBowers#a942f9ca]: This PR has been inactive for 7+ days with failing CI. I've assessed the blockers and they appear resolvable — I'll push fixes directly to this branch as a co-author. Assessment: The only CI red is If you'd prefer I don't touch this PR, add the |
… guard
followup_dir() refuses a linked ancestor before it resolves anything, and its
docstring says why the order matters: resolving first makes containment
attacker-relative, because a link planted at the runs root is followed by
resolve(), the anchor moves into the attacker's directory, and every child
underneath then compares as legitimately inside it.
That pre-resolve check asked Path.is_symlink(), which is False for a Windows
directory junction. A junction needs no privilege to create, and resolve()
follows it exactly like a symlink -- so on Windows the plant walks straight
through the guard. Measured on origin/main: with a junction at the runs root,
followup_dir("runJ", root) RETURNS a path whose resolve() lands inside the
attacker's directory.
Route the check through platform_compat.is_link_or_junction, the shim-table row
for exactly this question. The import is guarded like this module's other
kiro_crew imports; the fallback is symlink-only and never a local reparse-point
probe, and its signature matches the real routine exactly because mypy requires
every conditional variant of a name to agree.
Only the runs ROOT is affected. Measured per plant site on main: a junction at
the run directory or at chat/ is already refused, because the anchor stays
legitimate there and the resolved containment check catches it. The runs root is
the one case that check cannot catch -- it is measuring against the attacker's
own anchor -- which is precisely what the pre-resolve check exists for.
The two other link guards in this app are not siblings: learning.py's lock path
and results.py's record path both guard FILE paths, and a junction is
directory-only, so a junction there fails is_file() and the guard declines.
The test stages the plant with a local mirror of test/conftest.py::make_dir_link,
which calls _winapi.CreateJunction unconditionally on Windows. It is mirrored
rather than imported because only the test/ testpath gets that conftest. It is
NOT platform_compat.symlink_or_junction: that tries os.symlink first, so a
runner with Developer Mode gets a symlink and the junction arm is never
exercised -- silently, while still reporting green.
test_a_junctioned_runs_ROOT_is_refused fails on origin/main with
"FileNotFoundError not raised". The run/ and chat/ cases are pinned as
preservation tests, green on both sides.
The test file is in .github/black-baseline.txt, so it is deliberately not
black-formatted here: running black over it reformatted 56 pre-existing lines.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
e62f303 to
d234104
Compare
|
🤖 Kiro Crew [operator: NicholasRBowers#a942f9ca] Drive-to-green round 1 — rebase-only, no change to the fix. What changed and why:
Next: waiting for CI + the fork review lanes to stamp the new head; the First Principles Watch items will get their disposition records once the fresh stamp lands. |
|
🤖 Kiro Crew [operator: NicholasRBowers#a942f9ca]
self-added: yes
|
|
🤖 Kiro Crew [operator: NicholasRBowers#a942f9ca]
|
|
🤖 Kiro Crew [operator: NicholasRBowers#a942f9ca]
|
|
🤖 Kiro Crew [operator: NicholasRBowers#a942f9ca] REVIEW-READY at head Summary of the drive (rebase-only, zero fix commits):
Remaining: one maintainer approval — GitHub merges automatically once it lands. |
… guard (kirodotdev#8869) followup_dir() refuses a linked ancestor before it resolves anything, and its docstring says why the order matters: resolving first makes containment attacker-relative, because a link planted at the runs root is followed by resolve(), the anchor moves into the attacker's directory, and every child underneath then compares as legitimately inside it. That pre-resolve check asked Path.is_symlink(), which is False for a Windows directory junction. A junction needs no privilege to create, and resolve() follows it exactly like a symlink -- so on Windows the plant walks straight through the guard. Measured on origin/main: with a junction at the runs root, followup_dir("runJ", root) RETURNS a path whose resolve() lands inside the attacker's directory. Route the check through platform_compat.is_link_or_junction, the shim-table row for exactly this question. The import is guarded like this module's other kiro_crew imports; the fallback is symlink-only and never a local reparse-point probe, and its signature matches the real routine exactly because mypy requires every conditional variant of a name to agree. Only the runs ROOT is affected. Measured per plant site on main: a junction at the run directory or at chat/ is already refused, because the anchor stays legitimate there and the resolved containment check catches it. The runs root is the one case that check cannot catch -- it is measuring against the attacker's own anchor -- which is precisely what the pre-resolve check exists for. The two other link guards in this app are not siblings: learning.py's lock path and results.py's record path both guard FILE paths, and a junction is directory-only, so a junction there fails is_file() and the guard declines. The test stages the plant with a local mirror of test/conftest.py::make_dir_link, which calls _winapi.CreateJunction unconditionally on Windows. It is mirrored rather than imported because only the test/ testpath gets that conftest. It is NOT platform_compat.symlink_or_junction: that tries os.symlink first, so a runner with Developer Mode gets a symlink and the junction arm is never exercised -- silently, while still reporting green. test_a_junctioned_runs_ROOT_is_refused fails on origin/main with "FileNotFoundError not raised". The run/ and chat/ cases are pinned as preservation tests, green on both sides. The test file is in .github/black-baseline.txt, so it is deliberately not black-formatted here: running black over it reformatted 56 pre-existing lines. Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Problem / Motivation
sage_lib/followup.py::followup_dir()refuses a linked ancestor before it resolves anything, and its own docstring says why the order matters:That pre-resolve check asked
Path.is_symlink(). A Windows directory junction answersFalsetois_symlink(), needs no privilege to create, and is followed byresolve()exactly like a symlink — so on Windows the plant walks straight through the guard and the anchor is handed over anyway.Measured on
origin/main(9f3c07dc7), calling the function directly with a junction at the runs root:followup_dir("runJ", root)returns a path whoseresolve()lands inside the attacker's directory. The reviewer running a review has shell and these paths are predictable — the threat model this guard is written against.Why it matters
followup_dir()is what the descriptor writer builds on:mkdir(exist_ok=True)thenmkstemp(dir=...). With the anchor relocated, those writes land outside the app's data tree entirely, at a path the attacker chose.The exposure is Windows-specific in a way that inverts the usual coverage story:
os.symlinkon Windows needsSeCreateSymbolicLinkPrivilege, so the symlink plant is the hard one there — while the junction plant, which needs nothing, is the one the guard did not see.What changed (motivation → approach → change)
Root cause is the link predicate, not the ordering — the ordering was already right.
The check now routes through
platform_compat.is_link_or_junction, the shim-table row for exactly this question (AGENTS.md: "Detect/remove a dir link →is_link_or_junction(path)NOTpath.is_symlink()(misses a Windows junction)"). Same move as merged #7881 and #8766.The import is guarded like this module's other
kiro_crewimports, which exist because it can run standalone. The fallback is symlink-only and deliberately not a local reparse-point probe: re-implementing aplatform_compatrow locally is what the shim table exists to prevent, and answering only for the case it can see leaves the caller no weaker than today.Scope is one plant site, and that is measured, not assumed. A junction at the run directory or at
chat/is already refused on main: the anchor stays legitimate there, so the resolved containment check catches it. The runs root is the one case that check cannot catch — it is measuring against the attacker's own anchor — which is precisely what the pre-resolve check exists for.The two other link guards in this app are not siblings, checked individually:
learning.py:141—lock_path.is_symlink()guards a file path, and is backed byO_NOFOLLOWplus anfstatS_ISREG/st_nlinkcheck that cannot be raced.results.py:394—src.is_symlink()guards a<change_id>.jsonfile path.A junction is directory-only, so at either site it fails
is_file()and the surrounding guard declines anyway.Tests
tests/test_followup.py. The plant is staged with a local mirror oftest/conftest.py::make_dir_link, which calls_winapi.CreateJunctionunconditionally on Windows — a plain symlink on POSIX, a real directory junction on Windows — so these run on every platform and the junction arm is the one actually exercised there.It is deliberately not
platform_compat.symlink_or_junction: that triesos.symlinkFIRST and falls back to a junction only where the privilege is missing, so a runner with Developer Mode gets a SYMLINK and the junction arm is never exercised — silently, while still reporting green. The first head of this PR shipped that mistake; a guard-the-guard test asserting the plant really was a junction is what caught it, on the Windows CI shard. It is mirrored rather than imported because only thetest/testpath gets that conftest.They assert on
followup_dir()directly rather than throughwrite_descriptor(). That matters:write_descriptor()has preconditions of its own that returnFalsefor unrelated reasons, and a first draft of these tests passed on unpatchedmainfor precisely that reason — a green test that proved nothing. Asserting at the guard is what makes the evidence divisible.Red on
origin/main:test_a_junctioned_runs_ROOT_is_refused—AssertionError: FileNotFoundError not raised.Green on both sides, pinned as preservation rather than fixes:
test_a_junctioned_run_directory_stays_refused— the resolved containment check already covers it; pinned so the predicate change cannot quietly weaken it.test_a_junctioned_chat_directory_stays_refused— same.test_the_staged_plant_is_really_a_junction_on_windows— guards the guard: it asserts the plant really is a junction on Windows, so the tests above cannot quietly stop exercising the junction arm. This is the test that caught thesymlink_or_junctionmistake on the first head.Local (Windows, repo-pinned toolchain):
tests/test_followup.py35 passed / 6 skipped (the 6 are the pre-existing symlink tests). Fullcode_review_sagesuite: 773 passed / 33 skipped / 1 failed —test_adapters.py::TestEnterpriseHosts::test_malformed_links_are_rejected_not_crashed_on, which reproduces identically on a pristineorigin/maintree and touches nothing this PR changes.flake8clean;scripts/check_black_formatting.pypasses in scope.Manual verification
N/A — unit coverage sufficient: the whole change is one predicate inside one function, and the tests stage the real plant through the repo's own link seam on the affected platform rather than simulating it.
Related Issues
no linked issue: found by source review of the
is_symlink()-versus-junction seam after #8766/#8767Pattern harvest
Rule candidate:
semgrepPattern:
Path.is_symlink()used as a security link guard on a path that can be a directory. It isFalsefor a Windows junction, which needs no privilege and is followed byresolve()— so the guard is bypassed on the platform where the plant is cheapest. The repo's own shim table already namesis_link_or_junctionas the required call; the residual risk is a guard that predates it. Note the narrowing that keeps this from being a blanket sweep: a link guard on a file path is not affected, because a junction cannot exist there.Checklist
feat|fix|docs|refactor|perf|test|chore|ci|build|revert: ...)