Skip to content

fix(windows): make four is_symlink guards see a junction (#12379, #10492) - #15310

Merged
bolichen97 merged 1 commit into
mainfrom
fix/junction-blind-symlink-sweep-12379
Sep 30, 2026
Merged

bolichen97 merged 1 commit into
mainfrom
fix/junction-blind-symlink-sweep-12379

Conversation

@chenmingwei23

@chenmingwei23 chenmingwei23 commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: Stop four link guards in src/kiro_crew from following a Windows directory junction, so an unprivileged writer cannot use one to delete or read outside the guarded tree.

A directory junction is a reparse point that Path.is_symlink() and os.path.islink() both answer False for while is_dir() answers True, and it is the only directory link an unprivileged Windows user can create. Four guards were still written against is_symlink() (or a bare is_dir()), so on Windows each failed OPEN for exactly that link:

  • portability.apply_import_zip (replace mode): the skills/auto strip guarded shutil.rmtree with a bare auto_dir.is_dir(). rmtree follows a junction into its target, so a junction planted at skills/auto in the extraction tree aimed the delete OUTSIDE the archive.
  • connections.mint._is_reapable_spec: a junction at a mint-spec name read as our own spec and was unlinked — removing a directory link into a target this module does not own.
  • connections.warm._warm_spec_is_foreign: a junction at a warm-spec name read as a free path (is_symlink() and, when dangling, exists() both False), so the writer replaced it and the sweep unlinked it.
  • mcp_gateway.spill (cleanup_old_spill_files + the write guard in maybe_spill_response): a junction at the spill dir was followed, so the 24h sweep deleted files under its target — a confused-deputy delete outside the data home.

Why it matters

Each site is a delete or a read/write that a same-UID agent process can redirect with a link needing no elevation. On Windows the junction reaches the guard, passes it, and the operation lands on the link's target: files deleted outside the artifact store / data home, a user's hand-written agent spec unlinked, or a spill write redirected out of the data home. On POSIX the same plant is a symlink the old predicate already caught, which is why the gap stayed green until now.

Not a goal

What changed (motivation → approach → change)

Root cause at every site is the same: a link check that a junction answers False to (is_symlink), or a directory probe that follows one (is_dir → rmtree). The fix is the move the prior sweeps made (#11136, #14327) — replace the predicate with platform_compat.is_link_or_junction, the oracle docs/system-specs/common/platform-compat.md mandates for detecting a directory link.

  • mint / warm / spill: straight predicate swap — the guard already refused a link and returned; it now refuses a junction too.
  • portability: swapping the predicate alone is not enough for an rmtree, so the link case is removed with platform_compat.unlink_link_or_junction (which removes the LINK, never its target) and only a real directory reaches shutil.rmtree.

The four now match test_link_screen_hold_pin's discovery condition (an is_link_or_junction screen followed by a by-name resolve), so test/link_screen_sites.py is regenerated with test/gen_link_screen_sites.py. All four are the NO-HOLD kind — each refuses every link and never traverses one — so none joins HELD_SITES.

Also folds in #10492 (the spill.py line-61 guard), an overdue deferred finding that is the same predicate swap and was confirmed not yet on main.

Backwards compatibility

Compatible: the change only widens what each guard REFUSES (a junction now counts as a link). No accepted input stops being accepted — a real directory is still swept/read exactly as before; only a reparse point at the guarded name, which was silently followed, is now refused. is_link_or_junction is is_symlink-equivalent on POSIX.

Tests

Each site gets a test that stages a real junction through conftest.make_dir_link (a junction on Windows, a directory symlink on POSIX, so every shard exercises it) and asserts the guard refuses it without touching the link's target:

  • test_connections_mint.py::test_a_junction_at_a_mint_spec_name_is_never_unlinked
  • test_connections_warm.py::test_a_junction_at_the_spec_path_reads_as_foreign
  • test_mcp_gateway_oversize.py::TestSpillCleanup::test_cleanup_refuses_a_junction_at_the_spill_dir
  • test_portability.py::TestImportReplace::test_a_junction_at_skills_auto_is_not_rmtreed_through (plants the junction at skills/auto via the _strip_host_local_store_state seam, the only in-flight point since a zip cannot carry a reparse point)

Locally: the four new tests pass; the full affected files pass (473 passed, 5 skipped); test_link_screen_hold_pin passes with exactly the four expected baseline additions; black (baselined) / isort / flake8 / mypy clean on the changed files.

Manual verification

N/A — unit coverage is sufficient; the junction arm is exercised on Windows CI via make_dir_link and the POSIX arm via a directory symlink.

Related Issues

Fixes #12379
Fixes #10492

Pattern harvest

Not generalizable: the test_link_screen_hold_pin gate already enumerates this exact class (is_symlink/is_dir screen + by-name resolve) from source and now carries these four sites, so the class is policed by an existing ratchet rather than needing a new lint.

Checklist

  • At most two commits (one here), Conventional Commits title
  • Existing tests pass and new tests added
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable) — N/A, no doc/spec change (already-mandated predicate)
  • No secrets, credentials, or internal references in the diff

)

A Windows directory JUNCTION is a reparse point that Path.is_symlink()
and os.path.islink() both answer False for while is_dir() answers True,
and it is the only directory link an unprivileged Windows writer can
plant. Four link guards still tested is_symlink() (or a bare is_dir()),
so each failed OPEN for a junction while reading green on POSIX, where
the same plant is a symlink the old predicate catches. All four move to
platform_compat.is_link_or_junction, the repository's mandated predicate.

- portability.apply_import_zip: the replace-mode skills/auto strip
  guarded shutil.rmtree with a bare is_dir(), so a junction planted at
  skills/auto in the extraction tree aimed the delete OUTSIDE the
  archive. The LINK is now unlinked with unlink_link_or_junction (never
  its target); only a real directory reaches rmtree.
- connections.mint._is_reapable_spec: a junction at a mint-spec name was
  read as our own spec and unlinked, removing a directory link into a
  target this module does not own.
- connections.warm._warm_spec_is_foreign: a junction at a warm-spec name
  read as a free path (is_symlink() and, when dangling, exists() both
  False), so the writer replaced it and the sweep unlinked it.
- mcp_gateway.spill cleanup_old_spill_files / maybe_spill_response: a
  junction at the spill dir was followed, so the sweep deleted 24h-old
  files under its target (confused-deputy delete outside the data home).

Tests stage a real junction through conftest.make_dir_link (a junction
on Windows, a directory symlink on POSIX) and assert the guard refuses
it without touching the link's target, matching #11136 / #14327.

The four now match test_link_screen_hold_pin's discovery condition
(is_link_or_junction screen + a by-name resolve), so link_screen_sites.py
is regenerated. All four are the NO-HOLD kind: each refuses every link
and never traverses one.

The issue's fourth named site -- the deploy staging walk -- already
carries the fix on main (#14327, _stage_tree_safe), so it is not
re-touched here.

Fixes #12379
Fixes #10492
@chenmingwei23
chenmingwei23 requested a review from a team as a code owner September 30, 2026 06:01
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Sep 30, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Design Review (Fable 5) — ✅ PASS

Design-level review of d13e5147b54efd05efbd9cc328b0d6e1cc68af83 — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Design-Verdict: PASS

Confirm the Windows backend-test-windows shards ran green at this head — the junction arm of all four new tests only executes there.

[DESIGN-REVIEWED] d13e514

Description read

[DESCRIPTION-READ] 48bf8209da81821ed5ff46793a8ab8139350560dc5e94b11a1c78c3505ffa808

That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

GPT 5.6 Review — ✅ no blocking findings

GPT 5.6 completed its review of d13e5147b54efd05efbd9cc328b0d6e1cc68af83 and found no blocking issues.

This comment is updated in place on each push.

Review details

FINDING -- src/kiro_crew/portability.py:1086 -- “shutil.rmtree follows a junction” contradicts supported Python behavior: it refuses junction roots, so the old path aborted the import rather than deleting the target -> Fix: describe the refusal/import failure accurately here and in the changed test docstring. (origin: validation)
[GPT-REVIEWED] d13e514

False positive or not applicable? A repository writer can comment:
/ai-review override gpt d13e5147b54efd05efbd9cc328b0d6e1cc68af83: <one-sentence reason>

@github-actions

Copy link
Copy Markdown
Contributor

First Principles Review (Fable 5) — ✅ PASS

Premise-level review of d13e5147b54efd05efbd9cc328b0d6e1cc68af83 — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

First-Principles-Verdict: PASS

Confirm against #12379's audit list that these four sites are its complete residue — the issue text is the only part of the provenance not checkable from this repo.

What this change ships

Inventory (7 items) — 6 justified

Intent: FIX — stop four link guards from following a Windows directory junction so a same-UID unprivileged writer cannot aim a delete, read, or write outside the guarded tree (#12379, #10492).

  1. Import replace mode unlinks a junction planted at skills/auto instead of rmtree-ing through it — justified
  2. The mint reap sweep refuses a junction at a mint-spec name instead of unlinking it — justified
  3. The warm sweep reads a junction at a warm-spec name as occupied and leaves it in place — justified
  4. The 24h spill cleanup refuses a junction at the spill dir instead of sweeping its target — justified
  5. A spill write is refused when the spill dir is a junction — justified
  6. The refusal log now says "link" instead of "symlink" — rides along, accurate after the widened predicate, harm-free
  7. The link-screen baseline gains the four sites — justified (regeneration is mandated by test_every_site_is_declared)

All five guard changes route through the existing mandated platform_compat.is_link_or_junction / unlink_link_or_junction (platform_compat.py:7136, :7182) — no new mechanism, no new public surface, no deleted pins. Candidate siblings of the is_symlink() or exists() shape (grepped \.is_symlink\(\), 6 occupancy-probe hits outside these sites) checked at agent.py:1290 and member_essential_context.py:470 are different classes (deliberate-follow launcher probe; read-inclusion check), so no unfixed-sibling finding stands.

[FIRST-PRINCIPLES-REVIEWED] d13e514

Description read

[DESCRIPTION-READ] 1b368c10fb3d192e6dbd115ad8e85742a2ddd308ce6b5786ad9b409f56915de1

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

Copy link
Copy Markdown
Contributor

Opus 5 Review — ✅ no blocking findings

Reviewed d13e5147b54efd05efbd9cc328b0d6e1cc68af83 — this comment is updated in place on each push.

Review details

No findings that block.

FINDING — src/kiro_crew/portability.py:1079 — for fpath in snap.rglob("*") runs 12 lines before the new junction guard and rglob descends a junction on Windows (entry.is_symlink() is False for IO_REPARSE_TAG_MOUNT_POINT, as this file's own _entry_is_link docstring states), so a same-UID writer that plants a junction at <snap>/skills/auto in the owner-ACL'd tempfile.TemporaryDirectory during a mode="replace" import gets fpath = <snap>/skills/auto/id_rsa, which is_sensitive_path resolves through the junction to ~/.ssh/id_rsa, matches, and fpath.unlink() deletes the real credential file outside the archive — the exact confused-deputy delete the added comment says is now closed → Fix: hoist the is_link_or_junction/unlink_link_or_junction handling above the EXPORT_EXCLUDE and rglob loops, and walk the tree with the module's existing reparse-aware _entry_is_link walk instead of snap.rglob("*") (needs untouched code, hence advisory).

FINDING — test/test_connections_warm.py:1078 — make_dir_link(planted, outside) is planted over an existing outside, so path.exists() follows the link and answers True on both shards (POSIX also answers is_symlink() True), meaning the pre-change return path.is_symlink() or path.exists() already returns True and this is the only new test for the warm.py half — reverting that line reds nothing → Fix: delete outside after make_dir_link so the link dangles, the one shape where exists() is False and is_link_or_junction is load-bearing.

[OPUS-REVIEWED] d13e514

Verdict parsed from the review's SHA-scoped output markers for commit d13e5147b54efd05efbd9cc328b0d6e1cc68af83.

False positive or not applicable? A repository writer can comment:
/ai-review override fable d13e5147b54efd05efbd9cc328b0d6e1cc68af83: <one-sentence reason>

@github-actions github-actions Bot added readiness: passed Eligible automated validation passed for the current revision and removed readiness: checking Automated validation is still running labels Sep 30, 2026

@bolichen97 bolichen97 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed on behalf of the maintainers (sweep 2026-09-30): readiness passed, required checks green on d13e514, no blocking review threads, security posture unchanged. Approving and merging.

@bolichen97
bolichen97 merged commit 6d54165 into main Sep 30, 2026
103 of 106 checks passed
@bolichen97
bolichen97 deleted the fix/junction-blind-symlink-sweep-12379 branch September 30, 2026 07:39
bolichen97 added a commit that referenced this pull request Oct 1, 2026
…8.0 (#15666)

Backport of #14327, #14962, #15310 to release/0.8.0, cherry-picked in main's merge order.

* fix(windows): anchor six directory-link fences to pinned descriptors (#14327)
  (cherry picked from commit 6b0a6d5)
* fix(config): refuse a link or junction at the workspace dir name (#14962)
  (cherry picked from commit 4bb50ef)
* fix(windows): make four is_symlink guards see a junction (#12379, #10492) (#15310)
  (cherry picked from commit 6d54165)
bolichen97 added a commit that referenced this pull request Oct 1, 2026
… release/0.8.0 (#15619)

* fix(windows): backport the directory-junction hardening to release/0.8.0 (#15666)

Backport of #14327, #14962, #15310 to release/0.8.0, cherry-picked in main's merge order.

* fix(windows): anchor six directory-link fences to pinned descriptors (#14327)
  (cherry picked from commit 6b0a6d5)
* fix(config): refuse a link or junction at the workspace dir name (#14962)
  (cherry picked from commit 4bb50ef)
* fix(windows): make four is_symlink guards see a junction (#12379, #10492) (#15310)
  (cherry picked from commit 6d54165)

* style(test): black-format the failed-start reap test and drop its history wording on release/0.8.0 (#15665)

test/test_acp_runtime_failed_start_reap.py arrived with #15625 not
black-formatted (fails scripts/check_black_formatting.py on the release
tip), and the _gone docstring said "no longer runs", which
scripts/check_comment_history.py flags as history narration. Black 26.3.1
(--target-version py310) reflow, and the docstring now reads "has stopped
running", the wording the #15664 forward-port uses. Test file only.

* fix(adaptive): keep a same-window gate step from eating idle recovery (#15595)

Idle recovery (#15541) took its exec step only when nothing else had
changed on the sample. A spawn-gate increase earned on the same sample
set that shared flag, so the idle exec step due on it was skipped, and
the gate step also restarted the one-increase-per-window clock: the
exec cap then waited a further window for a step it had already been
due.

Judge the idle step on the exec track alone. Both tracks keep sharing
one increase-per-window clock across samples, as the earn rules do.

An increase's reason now names each track it moved: the exec note only
when the exec cap moved, and every gate step as "spawn gate target
X -> Y" (the policy's target; the daemon confirms or clamps it). A
gate-only step used to read "clean window earned +1", and a gate step
beside an exec step was not mentioned at all.

(cherry picked from commit f23a39f)

* fix(acp): treat only JSON whitespace as an empty mcp.json (#15673)

#15627 read an empty or whitespace-only MCP settings file as absent, using
bytes.strip() with no argument. That also strips \x0b and \x0c, which JSON
does not allow, so a file holding only those bytes read as "no restrictions"
instead of failing closed like any other malformed document. The emptiness
check now strips only space, tab, CR and LF (RFC 8259); anything else goes
to the parser and fails closed as before #15627.

Refs #15616

(cherry picked from commit 157fd51)

* fix(sandbox): backport the scope probe PATH and bus-path fixes to release/0.8.0 (#15638)

* fix(sandbox): keep the scope probe refresh off PATH, decode bus paths

Two defects the post-logout probe fix (#15622) introduced, from its
review:

- The probe now refreshes on a TTL, and every refresh resolved
  systemd-run with shutil.which on the event loop. A PATH entry on a
  stalled mount would hang the gateway's loop. The probe now uses
  platform_compat.trusted_system_bin, the same fixed-directory lookup
  cgroup_scope_argv already pins its wrapper with.
- DBUS_SESSION_BUS_ADDRESS unix:path= values may percent-escape any
  byte. Probing the escaped spelling literally misses a live bus that
  lives outside XDG_RUNTIME_DIR and drops the cgroup ceiling. The value
  is now decoded before it is dialed.

(cherry picked from commit e5a68df)
(cherry picked from commit 623f2b4)

* fix(sandbox): resolve the probe's systemd-run without any PATH walk

Second-round review of this PR: trusted_system_bin's miss diagnostic
still calls shutil.which, so a host with no trusted systemd-run walked
PATH on the event loop at the first probe refresh. The probe now uses
platform_compat.trusted_system_bin_quiet, the same fixed-directory
lookup without the diagnostic, and reports the miss in its reason.

A D-Bus unix:path= value that decodes to an empty path or one holding
a NUL cannot name a socket and made os.stat raise ValueError on the
spawn path; such values are now skipped.

(cherry picked from commit 5843231)

* fix(chat): restore the interrupted turn when Resume lands on a natively loaded session (#15643) (#15644)

* fix(chat): restore the interrupted turn when Resume lands on a natively loaded session (#15643)

kiro-cli appends a prompt to <sid>.jsonl only once the model has answered
it. When the process serving a turn dies mid-answer (a gateway restart, a
crash, a recycled runtime) that request is never written, so session/load
brings the conversation back without it. A natively resumed session gets
no Kiro Crew replay, so the Resume press on that turn sent a bare "finish
the user's most recent request" with the request missing: the model
answered the turn before it, or said there was nothing to continue.

Reproduced against kiro-cli 2.26.1: after a completed turn and a second
turn killed mid-stream, session/load restores only the first. The path is
any native resume: a dashboard chat with agent.tool_search off, a
dashboard chat linked to a channel, and the automatic connection-lost
recovery on a respawned runtime.

When the Resume message or the connection-lost recovery runs on a session
that this turn natively resumed, _run_chat now prepends the interrupted
turn from the slot's own transcript (the opener the turn-in-flight marker
restores, plus the partial answer), bracketed [INTERRUPTED TURN] so the
context scanner attributes it. A cold start that replays already carries
the turn and is unchanged. A session whose only turn was interrupted was
already covered: SessionMap.get prunes a sid whose log holds no turn.

Backport of #15643 to release/0.8.0. Conflict: context.py still carries
compress_thread_history here (removed on main by #14952); the new helper
is placed after it.

* fix(chat): open the interrupted-turn restore at the right row, and mint it after the scrub (#15643)

A cron, app or synthesis delivery opens its turn as an inject row. The
interrupted-turn walk only stopped at user, nudge and sub-agent rows, so
for such a turn it reached the already-answered request before it and
restored that as the user's most recent request. Openers now include the
inject kinds dashboard.state.TURN_OPENING_INJECT_KINDS names (the
dispatching kinds minus recovery and user_replay, which continue a turn).

The session-busy recovery (_BUSY_RECOVER_MSG) also resets the session and
session/loads the same native conversation, so it gets the restore too.

The frame tells the model the request inside it is the one to carry on
with, so its markers join _STRUCTURAL_MARKER_RES and the runner prepends
it after the egress scrub, like the task checklist; the builder scrubs
the restored payload itself.

The turn-in-flight marker now copies a sub-agent completion opener too
(_LOCAL_TURN_OPENER_ROLES / _LOCAL_TURN_PROMPT_ROLES): without it a crash
before the flush loses that row, and the restore would walk past it to
the previous, already-answered request.

* fix(mcp): repair Windows command casing at every resolver, not only the rewriter

On Windows `shutil.which` spells the extension it appends exactly as
PATHEXT spells it, so a bare `demo-mcp` resolves to `demo-mcp.EXE` while
the file on disk is `demo-mcp.exe`. A launcher that dispatches on its own
argv[0] basename case-sensitively (a tool manager's multiplexer shim)
refuses to run under that spelling and the MCP server never starts.

The rewriter already restored the on-disk basename, but the agent-config
resolver (`agent._resolve_command`) and the dashboard probe
(`mcp_discovery`) consumed raw `which` output, and the resolver's result is
PERSISTED as the spec's absolute `command`. An absolute command is accepted
verbatim on every later pass, so the uppercase spelling written once looked
operator-authored to the rewriter from then on and its repair never ran.

Move the helper to `kiro_crew.env.resolved_command_casing`, beside the
`mcp_search_path` the three resolvers already share, and apply it on both
return paths of `_resolve_command` and in the probe. Repairing at the
resolver also keeps the provenance record's `emitted` value repaired, so
`command_is_ours` still recognises the entry and re-derivation stays on.

Fixes #11627

(cherry picked from commit aa7c0bd, #15071;
 applied as its pre-#14919 revision 457210d plus the round-2 test
 and docstring delta, since release/0.8.0 predates the agent.py split)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

readiness: passed Eligible automated validation passed for the current revision

Projects

None yet

2 participants