Repository navigation
fix(trust): keep Windows exact trust grants exact (separator folding, drive prefix) - #40110
Conversation
…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
UX Review (Opus 5.5) — ⏭️ skippedRevision |
First Principles Review (Opus 5.5) — ✅ PASSPremise-level review of 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 Notes
What this change shipsInventory (3 items) — 3 justifiedIntent: on Windows, an exact "trust this command" grant approves only that exact command. FIX.
[FIRST-PRINCIPLES-REVIEWED] 075e653 Description read[DESCRIPTION-READ] 532308af9a3b30a01e53a9239290a97c75920466d3a9f88f697676347c7b1ef7 That sha256 names the PR description this verdict read. Recompute it with |
Design Review (Opus 5.5) — ✅ PASSDesign-level review of 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
[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 |
Opus 5.5 Review — ✅ no blocking findingsReviewed Verdict parsed from the review's SHA-scoped output markers for commit False positive or not applicable? A repository writer can comment: |
GPT 6.1 Review — ✅ no blocking findingsGPT 6.1 completed its review of This comment is updated in place on each push. Review detailsNo findings. False positive or not applicable? A repository writer can comment: |
Code change approver recommendation: A person should decideWhy: 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. 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 |
KiroCrewCaptain
left a comment
There was a problem hiding this comment.
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. AReader:line for them in the Backwards compatibility section would make the take-away list complete. - #39990 edits the same
_tool_matchesdocstring and return line. Whichever PR lands second should keep both the host-namespace guard andfnmatchcase. - The Proof: line names 5 of the 7 new test IDs. The
others2andothers3drive-prefix cases are missing.
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:
_tool_matchescallsfnmatch.fnmatch, which runsntpath.normcaseon Windows and folds/into\. A grant fordel /q fooapproveddel \q foo(Windows: an exact trust grant also auto-approves the same command with/and\swapped #38569).exact_trust_patternandbase_trust_patternscallglob.escape, which on Windows leaves thentpath.splitdrivepart unescaped. A grant for\\?\C:\Tools\x.exe /qkept its?as a wildcard and approved\\.\C:\Tools\x.exe /qand\\h\C:\Tools\x.exe /q; a grant for\\srv\sh[1]\x.exedid not match its own text (Exact trust grant is not wildcard-free for a command that starts with a Windows device or UNC prefix, becauseglob.escapeleaves the drive part unescaped #38608).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
/xas a switch and\xas a path, and\\h\UNC\...names a different host. Since #38209 a Shared Crew teammate can press the trust buttons too.Not a goal
\\?\...pattern keeps its wildcard until the user grants again.hook_runtime/tool_identity._tool_matches(configauto_approve_tools,auto_deny_tools,confirm_tools, hook matchers) and the deny-rule exception matcher insecurity/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.fnmatchcallingos.path.normcase; the matcher already lowers both strings, so it now callsfnmatch.fnmatchcase. The drive gap comes fromglob.escapecallingos.path.splitdrive; a shared_escape_globhelper now escapes every*,?and[the wayglob.escapedoes 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:normcaseis the identity there andglob.escapehas 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
test_exact_grant_does_not_fold_separators_under_windows_normcase: withos.path.normcaseset tontpath.normcase, an exact grant matches itself and its upper-case form, not the separator-swapped command (both directions), and adel *-style base grant still matches.test_exact_grant_escapes_windows_drive_prefix: withntpath.splitdriveandntpath.normcase, each device or UNC grant matches itself and none of the other spellings from Exact trust grant is not wildcard-free for a command that starts with a Windows device or UNC prefix, becauseglob.escapeleaves the drive part unescaped #38608;C:\Tools\[x] ystill becomesC:\Tools\[[]x] y;base_trust_patternsescapes a UNC prefix.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) andtest_flake_pattern_ratchet.pypass.Manual verification
The issues' repro scripts, plus pipelines, a redirect and a timed grant, run through
matches_trusted_pattern,redirect_outside_grantandtimed_grant_deadlinewithntpathpath 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.normcasewith thentpathversions, which is whatglob.escapeandfnmatch.fnmatchcall 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': NoneAfter (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.fnmatchorglob.escapein an authorization matcher; both apply Windows path rules (normcase,splitdrive) to strings that are not paths.Checklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...); PR Hygiene does not count commits