Skip to content

fix(trust): keep Windows exact trust grants exact (separator folding, drive prefix) - #40110

Merged
KiroCrewCaptain merged 2 commits into
mainfrom
fix/38608-38569-trust-grant
Nov 20, 2026
Merged

KiroCrewCaptain merged 2 commits into
mainfrom
fix/38608-38569-trust-grant

Conversation

@dodgeblaster

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: On Windows, an exact "trust this command" grant approves only the command it was given for.

Two Windows-only gaps let the dashboard auto-approve a command the user never granted:

Why it matters

The exact tier is documented as "an fnmatch pattern with no wildcard power", and the channel and dashboard rely on it being exact. Many Windows programs read /x as a switch and \x as a path, and \\h\UNC\... names a different host. Since #38209 a Shared Crew teammate can press the trust buttons too.

Not a goal

  • Migrating grants stored by older versions: a stored \\?\... pattern keeps its wildcard until the user grants again.
  • The hook-gate matcher hook_runtime/tool_identity._tool_matches (config auto_approve_tools, auto_deny_tools, confirm_tools, hook matchers) and the deny-rule exception matcher in security/denied_rules.py. Both also feed deny or confirm lists, where folding makes a rule broader, so changing them needs its own decision.

What changed (motivation → approach → change)

Separator folding comes from fnmatch.fnmatch calling os.path.normcase; the matcher already lowers both strings, so it now calls fnmatch.fnmatchcase. The drive gap comes from glob.escape calling os.path.splitdrive; a shared _escape_glob helper now escapes every *, ? and [ the way glob.escape does on POSIX, and both the exact and the base tier use it. Two commits, one per issue.

What is auto-approved differently, Windows only: an exact, base or timed grant no longer covers a command that differs only in / versus \, and an exact or base grant whose text holds a device or UNC prefix no longer acts as a wildcard. One case newly matches: a grant whose prefix holds [ (\\srv\sh[1]\x.exe) now matches its own text, which it failed to before. On POSIX nothing changes: normcase is the identity there and glob.escape has no drive part.

Backwards compatibility

Compatible: on POSIX every pattern and every match result is the same; on Windows only grants that matched more than their own text match less.

Removes nothing: no key, record, function or validator is removed; the change narrows what an existing Windows grant matches.

Tests

Proof: PROVEN test/test_trust_patterns.py::TestMatchesTrustedPattern::test_exact_grant_does_not_fold_separators_under_windows_normcase[del /q foo-del \q foo], test/test_trust_patterns.py::TestMatchesTrustedPattern::test_exact_grant_does_not_fold_separators_under_windows_normcase[rm -rf build/out-rm -rf build\out], test/test_trust_patterns.py::TestMatchesTrustedPattern::test_exact_grant_does_not_fold_separators_under_windows_normcase[cat docs\a.md-cat docs/a.md], test/test_trust_patterns.py::TestMatchesTrustedPattern::test_exact_grant_escapes_windows_drive_prefix[\\?\C:\Tools\x.exe /q-others0], test/test_trust_patterns.py::TestMatchesTrustedPattern::test_exact_grant_escapes_windows_drive_prefix[\\?\UNC\srv\share\x.exe /q-others1] (head 075e653)

python3 scripts/local-gate.py: 719 passed. isort, flake8, black gate, mypy, comment-history, brand, semgrep (semgrep/, diff-scoped) and test_flake_pattern_ratchet.py pass.

Manual verification

The issues' repro scripts, plus pipelines, a redirect and a timed grant, run through matches_trusted_pattern, redirect_outside_grant and timed_grant_deadline with ntpath path rules: every alternate spelling is refused after the fix and was approved before. The dashboard route itself was not driven.

Real-machine verification

Machine: Amazon Linux 2023 host (own machine), x86_64, Python 3.12.15. Windows is simulated by patching os.path.splitdrive/os.path.normcase with the ntpath versions, which is what glob.escape and fnmatch.fnmatch call on Windows.
Before (main 06215fa): python repro.py -> 'del \\q foo' vs grant 'del /q foo': 'del /q foo', '\\\\.\\C:\\Tools\\x.exe /q': '\\\\?\\C:\\Tools\\x.exe /q', '\\\\srv\\sh[1]\\x.exe': None
After (head 075e653): same command -> None, None, '\\\\srv\\sh[[]1]\\x.exe'
Cleanup: own computer. Not run on a real Windows host.

Related Issues

Fixes #38569
Fixes #38608

Sibling sweep

Swept: git grep -nE "fnmatch.fnmatch(|glob.escape(" -- src/
Sibling: src/kiro_crew/trust_patterns.py -- changed here
Sibling: src/kiro_crew/hook_runtime/tool_identity.py -- not affected -- left as is: it also matches deny and confirm lists, where folding is broader, not narrower
Sibling: src/kiro_crew/security/denied_rules.py -- not affected -- deny-rule exceptions are a separate trust surface; needs its own decision
Sibling: src/kiro_crew/platform/governance_parts/matching.py -- not affected -- governance path rules match either separator on purpose (#24273)
Sibling: src/kiro_crew/acp/kas_skill_scope.py -- not affected -- escapes a filesystem path for glob, not a trust grant

Pattern harvest

Rule candidate: semgrep
Pattern: fnmatch.fnmatch or glob.escape in an authorization matcher; both apply Windows path rules (normcase, splitdrive) to strings that are not paths.

Checklist

  • A Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...); PR Hygiene does not count commits
  • 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

…old / into \

On Windows fnmatch.fnmatch runs ntpath.normcase, which turns every / into \,
so an exact grant for 'del /q foo' auto-approved 'del \q foo'. Match the
already-lowered strings with fnmatchcase: the same case-insensitive language
on every host.

Fixes #38569
…ust grants

glob.escape skips the ntpath.splitdrive part on Windows, so an exact grant for
'\\?\C:\Tools\x.exe /q' kept its ? as a wildcard and approved
'\\.\C:\Tools\x.exe /q'. Escape the whole string the way glob.escape does on
POSIX, in one helper used by both the exact and the base tier.

Fixes #38608
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Nov 20, 2026
@github-actions

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ⏭️ skipped

Revision 075e653a3dc2d182a29dacc21bb0e21cef8bf5c9 touches no user-facing surface (no changes under website/ or committed screenshots), so the UX review was skipped. Does not block merge.

@github-actions

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ✅ PASS

Premise-level review of 075e653a3dc2d182a29dacc21bb0e21cef8bf5c9 — 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 does not.

First-Principles-Verdict: PASS

Two mechanism-level fixes, each tied to a linked issue and a test that fails on base. The new escape helper adds no surface. The sibling matchers the PR leaves alone are declared under "Not a goal".

Not checked: real Windows fnmatch/glob.escape behaviour; the PR only simulates it with patched ntpath functions.

Notes

  • Item 1 — symptom-level sibling: the same fnmatch.fnmatch separator fold remains in hook_runtime/tool_identity.py:1081,1092 and security/denied_rules.py:556,1319 (grep fnmatch\.fnmatch\( in src/: 4 sites in authorization matchers). The PR defers them on purpose because folding widens deny and confirm rules there, so this is accepted and deferred.

What this change ships

Inventory (3 items) — 3 justified

Intent: on Windows, an exact "trust this command" grant approves only that exact command. FIX.

  1. On Windows, an exact, base or timed grant no longer treats / and \ as the same character — justified
  2. On Windows, an exact or base grant with a device or UNC prefix (\\?\, \\srv*\) no longer acts as a wildcard — justified
  3. A grant whose UNC prefix contains [ now matches its own text — justified

[FIRST-PRINCIPLES-REVIEWED] 075e653

Description read

[DESCRIPTION-READ] 532308af9a3b30a01e53a9239290a97c75920466d3a9f88f697676347c7b1ef7

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

Design Review (Opus 5.5) — ✅ PASS

Design-level review of 075e653a3dc2d182a29dacc21bb0e21cef8bf5c9 — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

Design-Verdict: PASS

Nothing blocks: the fix sits at the root cause of both gaps, and on POSIX the stored patterns come out byte-identical ([*], [?], [[]).

Notes

  • Stored Windows grants of the \\?\… form keep their wildcard. The description lists this under "Not a goal", so a follow-up issue should track re-escaping them on load.
  • The only Windows proof is a patched simulation using ntpath. Because the change only narrows matches, a missed case fails closed (the user is prompted again), so no real-Windows run is required.

[DESIGN-REVIEWED] 075e653

Description read

[DESCRIPTION-READ] d0efdc83cb3c9f22da44dec10453a21081e88a90e30e0c9c48544f0c2916b12c

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

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

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

Review details

No findings.

[OPUS-REVIEWED] 075e653

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

False positive or not applicable? A repository writer can comment:
/ai-review override fable 075e653a3dc2d182a29dacc21bb0e21cef8bf5c9 false-positive: <reason, 20 to 500 characters>

@github-actions

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

GPT 6.1 completed its review of 075e653a3dc2d182a29dacc21bb0e21cef8bf5c9 and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 075e653

False positive or not applicable? A repository writer can comment:
/ai-review override gpt 075e653a3dc2d182a29dacc21bb0e21cef8bf5c9 false-positive: <reason, 20 to 500 characters>

@dodgeblaster
dodgeblaster marked this pull request as ready for review November 20, 2026 20:47
@dodgeblaster
dodgeblaster requested review from a team and KiroCrewCaptain as code owners November 20, 2026 20:47
@dodgeblaster
dodgeblaster requested a review from Zedmor November 20, 2026 20:47
@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention readiness: passed Eligible automated validation passed for the current revision and removed readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention labels Nov 20, 2026
@KiroCrewCaptain

Copy link
Copy Markdown
Collaborator

Code change approver recommendation: A person should decide

Why: the decider estimates a 6% chance that this change introduces a defect, within the 12% limit for approving on its own, but it is held at tier pending: being reviewed.

Narrow, fail-closed fix to Windows trust grants. trust_patterns._tool_matches is only used inside trust_patterns.py (matches_trusted_pattern, _covers_write_redirect, timed_grant_deadline); the hook-gate _tool_matches in hooks.py is a separate function and is not touched. Switching to fnmatchcase on strings that are already lowered removes ntpath's '/'→'' folding, and on POSIX the result is the same. _escape_glob uses the same regex and [\1] substitution as glob.escape on POSIX, but escapes the whole string, including the drive part that glob.escape skips on Windows. Exact and base grants are only created in slot_approval.py, through the changed functions. Matches only get narrower on Windows, and on POSIX patterns and results stay the same. The new tests patch os.path.normcase and os.path.splitdrive, which fnmatch and glob.escape look up when they run, so they would fail on main. Linux and Windows CI passed.

Tier: pending: being reviewed.

An automated recommendation, not a review: a maintainer still decides. It approves on its own only when the estimated chance of a defect is at most 12% and the change has at most 400 changed lines. Decider v3c (217fe607) · commit 075e653a3d · 68 changed lines. If this recommendation is wrong, reply here: replies are how the approver is evaluated.

@KiroCrewCaptain
KiroCrewCaptain enabled auto-merge (squash) November 20, 2026 23:06

@KiroCrewCaptain KiroCrewCaptain left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approving. On head 075e653 the review read the full diff and every caller of _tool_matches in trust_patterns.py. It checked that _escape_glob gives the same output as glob.escape on POSIX over 20,000 random strings. It also ran a mutation check. Main's trust_patterns.py fails all 7 new cases. Putting back fnmatch.fnmatch fails the 3 separator cases. Putting back glob.escape fails the 4 drive-prefix cases. The full test/test_trust_patterns.py passes at head.

Done well: both fixes target the stdlib call itself, not one spelling at a time. The tests reproduce Windows by swapping in the real ntpath.normcase and ntpath.splitdrive, scoped to each test.

Security: this touches the approval matcher, but it only narrows it. On Windows an exact or base grant no longer matches a different separator, device or UNC spelling. The one new match is a grant matching its own text when it contains [. On POSIX, matching is unchanged. Grants and calls go through the same functions, so a case the change misses falls back to an approval card, never to an auto-approve.

Optional:

  • App worker-lease trusted_patterns (apps/worker_slots.py) share this matcher. On Windows they now match / and \ literally. A Reader: line for them in the Backwards compatibility section would make the take-away list complete.
  • #39990 edits the same _tool_matches docstring and return line. Whichever PR lands second should keep both the host-namespace guard and fnmatchcase.
  • The Proof: line names 5 of the 7 new test IDs. The others2 and others3 drive-prefix cases are missing.

@KiroCrewCaptain
KiroCrewCaptain merged commit 44c5af1 into main Nov 20, 2026
163 of 164 checks passed
@KiroCrewCaptain
KiroCrewCaptain deleted the fix/38608-38569-trust-grant branch November 20, 2026 23:06
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