Skip to content

fix(diagnostics): reject a junctioned log dir, not just a symlinked one - #8165

Merged
bolichen97 merged 1 commit into
kirodotdev:mainfrom
leonlaiyc:fix/diagnostics-dir-junction-guard
Sep 3, 2026
Merged

bolichen97 merged 1 commit into
kirodotdev:mainfrom
leonlaiyc:fix/diagnostics-dir-junction-guard

Conversation

@leonlaiyc

Copy link
Copy Markdown
Contributor

Problem / Motivation

diagnostics._usable_dir is the support-bundle collector's one guard against
enumerating through a linked log directory. Its own docstring states the
reason precisely:

Rejecting only symlinked files is not enough: Path.is_dir() follows links,
so a symlinked crash-dumps (or DiagnosticReports) directory is walked
through to its target, and the real files found there are not themselves
symlinks — they sail past the per-file guard and get packaged.

The guard is in the right place. Its predicate is not: it tested
p.is_symlink(), which does not report a Windows junction.

This is worse than an even trade on Windows, because the two link types are not
equally available to an attacker there. A directory symlink needs
SeCreateSymbolicLinkPrivilege — the repo's own test fixtures document this and
skip on it (requires_symlinks). A junction needs no privilege at all, and is
followed by the same reparse machinery: is_dir(), glob and resolve all
traverse it identically. So on Windows the only directory link an unprivileged
process can plant is precisely the one the guard did not see.

Why it matters

Everything _usable_dir protects is then enumerated and packaged. The three
directories this module globs all route through it:

  • the kiro-log directories (_kiro_log_dirs → _usable_dir at the dedup step),
  • macOS DiagnosticReports,
  • <data home>/logs/crash-dumps.

A junction planted at any of them makes the collector read the target's files
and write them into kirocrew-diagnostics-*.zip. That bundle is not a private
artifact: the dashboard flow hands it to the user to attach to a public issue,
and the module already treats this as the threat model it is defending —
_kiro_cli_chat_log refuses a KIRO_CHAT_LOG_FILE pointing at a sensitive path
with the comment "would copy plaintext logins into a bundle destined for a
public issue. Redaction is not a backstop for that."
A junction reaches the
same outcome without touching that env var.

What changed (motivation → approach → change)

Symptom: a link guard that a Windows junction walks straight past. Root cause:
Path.is_symlink() where AGENTS.md's cross-platform table names a
platform_compat predicate —

Need Use (platform_compat) NOT
Detect/remove a dir link is_link_or_junction(path) / unlink_link_or_junction(path) path.is_symlink() (misses a Windows junction)

Change: _usable_dir returns p.is_dir() and not platform_compat.is_link_or_junction(p). One predicate, one line — and because
every globbed directory in this module already funnels through this single
function, the gap closes at all three sites at once rather than needing a guard
added per call site.

Deliberately not widened here: platform_compat.first_linked_ancestor covers
the neighbouring case where the path itself is a real directory but an
ancestor is a link. That is a different defect with a different blast radius
(an ancestor link to \\host\share turns an is_dir() into an authenticated
outbound SMB connection), it needs its own reasoning about which of the
collector's paths are attacker-influenced, and folding it in would make this
diff a survey rather than a fix. It is named under Pattern harvest instead.

The per-file checks (_usable_log, the .ips filter, the crash-dump filter)
are left on is_symlink(): a file symlink IS reported on Windows, and a
junction is a directory-only reparse point, so there is nothing for the wider
predicate to add there.

Tests

TestUsableDirRejectsEveryDirLink in test/test_diagnostics.py, three tests:

  • test_a_real_directory_link_is_not_usable — the end-to-end one, and it uses a
    real link, not a mock: conftest.make_dir_link creates an actual junction
    on Windows (_winapi.CreateJunction) and a real directory symlink on POSIX,
    so the behaviour is exercised on Windows instead of skipped there. It first
    asserts the two facts that make the link dangerous — link.is_dir() is True
    and link.glob("*") reaches the target's files — and then that _usable_dir
    refuses it. Red on 997d59421 with a live junction on Windows 11:
    AssertionError: assert True is False.
  • test_a_plain_directory_stays_usable — the guard does not reject the ordinary
    case it exists to allow (passes before and after; it is what stops the fix
    from being "return False").
  • test_guard_routes_through_the_shared_link_predicate — pins the seam as well
    as the behaviour, so this guard cannot drift from the rest of the tree on
    which reparse types count as a link. Red on 997d59421.

Green: test_diagnostics.py 46 passed, 3 skipped. Directly relevant siblings
test_strict_identity_diagnostics.py + test_portability.py: 61 passed, 3
skipped. black measured against a regenerated control at this PR's own base —
both files carry a pre-existing 224-line baseline and this diff adds no new
finding; flake8, isort, git diff --check, module mypy and the
diff-scoped brand-name gate are clean.

Manual verification

N/A — unit coverage sufficient, and it is not the usual "unit tests are enough"
claim: the Windows leg is verified against a real junction created by
_winapi.CreateJunction on a Windows 11 host, which is the exact artifact the
defect turns on. A mocked isjunction would have proved nothing about whether
is_dir() and glob actually traverse it.

Related Issues

None — found by auditing AGENTS.md's platform_compat "use this / NOT that"
table against current main.

Pattern harvest

Rule candidate: semgrep

Pattern: path.is_symlink() (or os.path.islink) used as a directory link
guard — the shape is an is_dir()/iterdir()/glob() gated on not is_symlink(). It is only ever correct for files; for directories it is blind to
the one reparse type an unprivileged Windows user can create. Two adjacent
residuals this diff deliberately leaves alone, both worth their own look:

  • the ancestor case — platform_compat.first_linked_ancestor, for a real
    directory reached through a linked parent;
  • other is_dir() and not is_symlink() sites still on main, e.g.
    dashboard/theme_validate.py:1159 and
    apps/builtins/pptx_maker/backend/engine_source.py:413.

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|refactor|perf|test|chore|ci|build|revert: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

Contribution License Agreement


Merge note: open PR #7692 also touches src/kiro_crew/diagnostics.py and
test/test_diagnostics.py. The two are semantically independent — #7692 rewrites
_kiro_cli_version (line ~276) and appends tests at the end of the file, and
does not touch _usable_dir or any of its call sites. The only textual contact
is the from kiro_crew import ... line, two lines above where #7692 inserts its
new imports; whichever lands second may need that one-line import merge.

🤖 Generated with Claude Code

`_usable_dir` is the collector's one guard against enumerating THROUGH a linked
log directory: `Path.is_dir()` follows the link, and the real files on the other
side are not themselves links, so they pass the per-file check and get packaged
into the support bundle.

It tested `p.is_symlink()`, which does not report a Windows junction — and a
junction is the only directory link an unprivileged Windows user can create,
since a symlink needs SeCreateSymbolicLinkPrivilege. So the guard was open on
exactly the platform where planting one is easiest, and what leaks through is a
bundle the user attaches to a public issue.

Route it through `platform_compat.is_link_or_junction`, per the AGENTS.md
cross-platform table. All three directories this module globs — the kiro-log
dirs, macOS DiagnosticReports, and `logs/crash-dumps` — go through this one
predicate, so the guard closes at every site at once.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@leonlaiyc
leonlaiyc requested review from a team and chenmingwei23 September 3, 2026 13:20
@github-actions github-actions Bot added fork Pull request from a fork (external contributor) readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Sep 3, 2026
@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

Design Review (Fable 5, fork) — ✅ PASS

Design-level review of fc048fc10302ca7fe3135a85c52414e2cec5497e via the fork AI-review pipeline — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Design-Verdict: PASS

A real Windows-privilege asymmetry closed at the single funnel all three globbed directories already route through — root cause, right layer, correctly scoped.

[DESIGN-REVIEWED] fc048fc

@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

Opus 4.8 Review (fork) — ✅ no blocking findings

Reviewed fc048fc10302ca7fe3135a85c52414e2cec5497e via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] fc048fc

@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

GPT 5.6 Review (fork) — ✅ no blocking findings

Reviewed fc048fc10302ca7fe3135a85c52414e2cec5497e via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] fc048fc

@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

First Principles Review (Fable 5, fork) — ✅ PASS

Premise-level review of fc048fc10302ca7fe3135a85c52414e2cec5497e via the fork AI-review pipeline — 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.

All claims verified against the base: the guard is the single funnel for all three globbed directories (diagnostics.py:196, 264, 516), platform_compat.is_link_or_junction exists and is the AGENTS.md-mandated predicate, make_dir_link is an existing conftest helper being reused, and the same is_dir() and not is_symlink() pattern survives at five other sites on main — the description names two of them.

First-Principles-Verdict: PASS

A derived one-predicate fix at the single funnel all three globbed directories route through, using the mandated existing helper — nothing rides along.

What this change ships

Intent: stop a Windows junction planted at a log directory from leaking its target's files into a support bundle destined for a public issue — a FIX.

  1. _usable_dir now rejects a junctioned directory, not just a symlinked one — justified (AGENTS.md table row mandates is_link_or_junction over is_symlink; external-content boundary).
  2. Docstring gains the junction rationale — justified (states the constraint the code can't show).
  3. New platform_compat import in diagnostics — the fix's mechanism.
  4. Three tests, one against a real junction, reusing the existing make_dir_link helper — justified; no new helper added.

No new surface: no config key, flag, or public symbol; _usable_dir is an existing private function. The requirement is derived twice over — the documented platform_compat invariant plus a named boundary (untrusted local content reaching a public artifact). The delete option was already taken: reuse of the shared predicate rather than a local junction check.

Watch

Sibling count is understated: grepping is_dir() and not is_symlink() under src/kiro_crew/ finds 3 unfixed sites beyond the 2 the harvest names — dashboard/handlers/themes.py:116, pod/cli.py:560, pod/runtime.py:1471. The deferral reasoning (per-site threat analysis) is declared and holds, but the follow-up list should carry all 5.

[FIRST-PRINCIPLES-REVIEWED] fc048fc

@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 3, 2026
@bolichen97
bolichen97 enabled auto-merge (squash) September 3, 2026 18:51

@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.

Approving on the strength of a full readiness audit of every open PR against main, not a
line-by-line reading of this diff — recording that plainly so the next reader knows what this
stamp does and does not cover.

Verified against this exact head SHA:

  • readiness: passed present, and PR Readiness — the one required status context on main
    (ruleset protected-branches) — is success on this head.
  • No check run on this head is failure, cancelled, timed_out or still in flight. Skipped
    jobs are path-filtered conditionals, none of them required.
  • mergeable: true, and the head is not far enough behind main for its green CI to describe a
    base that no longer exists.
  • No surviving reviewer CHANGES_REQUESTED: any such review is on an older commit and therefore
    already dismissed by dismiss_stale_reviews_on_push.
  • Every issue comment, inline review comment and review thread was read and classified. Nothing
    left is an unresolved human change request — the remainder is bot review-lane output, resolved
    or outdated threads, explicitly non-blocking suggestions, and author status notes.

Auto-merge (squash) is armed, so this lands once every other ruleset requirement is met.

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

Labels

fork Pull request from a fork (external contributor)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants