Repository navigation
fix(permissions): ignore NotADirectory and strip BOM before empty check - #4658
Conversation
📊 Automated PR Analysis
SummaryFixes the Claude Code settings permission loader to treat ErrorKind::NotADirectory the same as NotFound (so a non-directory parent like a Review Checklist
Linked issues: #4651 Analyzed automatically by wshm · This is an automated analysis, not a human review. |
houtinghang
left a comment
There was a problem hiding this comment.
I tested this locally. The fix does what #4651 asks, and the two new tests fail without it and pass with it. The one thing in the way is the conflict with develop.
Conflict with develop. #4127 (62cf6ba7, merged 10-23) moved this loop into load_permission_rules_tracking() and added a three-line comment above the NotFound arm. That is the only conflicting hunk: keeping the comment and taking your matches! arm resolves it, and the rest of the patch applies unchanged. This is the resolution I tested:
Ok(content) => content,
// A file that is not there denies nothing. One that is there and
// could not be read — permissions, or bytes that are not UTF-8 —
// may hold anything.
Err(e)
if matches!(
e.kind(),
std::io::ErrorKind::NotFound | std::io::ErrorKind::NotADirectory
) =>
{
continue;
}After the rebase the change also reaches claude_hook_deny_rules(), the gate for the opt-in fish wrap from #4127. Both new skips happen before scan.every_file_read = false, so a .claude regular file or a BOM-only settings file no longer forces that gate to Unattested. That matches the SettingsScan doc ("Every settings file that exists was opened, decoded and parsed"), but since the issue is labelled area:security it may be worth a line in the description. (This part is from reading the code; I did not exercise the wrap path.)
What I ran (WSL2 Ubuntu 26.04 x86_64, rustc 1.99.0, debug builds of the PR's merge base 9a501c8d, the PR head 1c85e65c, develop 32b2ec43, and develop + this PR with the conflict resolved as above):
rtk rewrite "git status" exit codes, with an isolated HOME and CLAUDE_CONFIG_DIR, and allow: ["Bash(git status)"] in the other settings file:
| case | base | PR | develop | develop + PR |
|---|---|---|---|---|
project .claude is a regular file |
3 | 0 | 3 | 0 |
settings.local.json = EF BB BF |
3 | 0 | 3 | 0 |
settings.local.json = EF BB BF + " \r\n" |
3 | 0 | 3 | 0 |
CLAUDE_CONFIG_DIR points at a regular file |
3 | 0 | 3 | 0 |
The .claude file, BOM-only and CLAUDE_CONFIG_DIR cases under RTK_REWRITE_HOST=openclaw, with git log (no rule matches), also go from 3 to 0. In all of these the Not a directory (os error 20) / failed to parse warnings are gone after the fix.
Controls, the same on all four builds: invalid JSON → 3, BOM + invalid JSON → 3, BOM + deny: ["Bash(git status)"] → 2, settings.local.json that is a directory → 3, non-UTF-8 bytes → 3, a plain allow file → 0. The controls still print their warnings.
cargo test --lib hooks::permissions: on the base and ondevelopwith only the two new tests added, 147 passed and the 2 new tests failed; with the fix, 149/149 on both.develop+ PR:cargo test --no-fail-fast4915 passed, 0 failed (developwith only the two tests added: 4913 passed, the 2 new tests failed).cargo fmt --all --checkandcargo clippy --all-targetsare clean on both the PR head and the rebased commit.- Not tested: Windows and macOS. Per the issue, Windows already reports
NotFoundfor the regular-file layout, sotest_not_a_directory_settings_file_adds_no_rulespasses there with or without the fix.
Two small things, neither blocking:
- The first summary bullet says non-directory parents now "fail closed". After this change they are skipped like a missing file; the catch-all
Askis what #4287 calls failing closed. Rewording it would keep the description from reading as the reverse of the change. - #4680 (for #4652) reworks
read_json()for the Cursor and Droid loaders with the sameNotFound-only arm and the sametrim()check before the BOM is stripped. Whichever of the two lands second may want to carry both checks there too.
AI disclosure: I used Claude Code to build the binaries, run the tests above and draft this comment, and I checked the results and the wording before posting.
Signed-off-by: Samran Asif <[email protected]>
1c85e65 to
455ea7f
Compare
|
Hi @houtinghang, thank you so much for the thorough review and for running the extensive local test matrices across Ubuntu, WSL2, and RTK rewrite modes! Addressed in commit 455ea7f:
Verified with |
|
Thanks for the contribution! Before we can review and merge this, the CLA needs to be signed. The CLAassistant bot left the link above, it only takes a minute. Once it's signed we'll pick the review up. |
|
Hi @pszymkowiak 👋 The CLA has now been signed. Ready for review and merge whenever convenient. Thanks! |
… attested `claude_hook_deny_rules` permits the fish wrap only when the settings scan saw every file and no deny entry. A path under a regular file and a file holding only a BOM (and whitespace) now skip before the scan is touched, but no test read the scan for them: a mutation marking either one unread passed the whole suite. The test also keeps the counter-cases: a file that cannot be read or parsed still marks the scan unread, and a BOM before real rules still applies them. Co-Authored-By: Claude Opus 5.5 <[email protected]>
KuSh
left a comment
There was a problem hiding this comment.
Round 1 of 3 — APPROVED
Claim: the Claude Code settings loader skips a settings path whose parent is not a directory, and a file holding only a BOM (optionally plus whitespace), the way it skips a missing or empty file, so neither adds the fail-closed ask * nor turns off the fish-wrap gate.
Scope: accept.
Ran: rtk rewrite "git status", rtk rewrite "git log" (default and RTK_REWRITE_HOST=openclaw), rtk hook claude, rtk hook check × 22 settings layouts × develop 41e691223 / this head / develop + this PR, isolated HOME, LC_ALL=C, stdin from /dev/null; fish-wrap gate (hooks.wrap_fish_scripts = true, test -d src; and git status) × 14 layouts in a fish 4.9.1 container. Invariants: every decision (rules and wrap gate), split streams (warnings on stderr). No output filter changes, so no savings figure.
Pins: 5 changes mutated against 4594 lib tests + 19 fish_wrap_test. The new tests caught 2. 2 got past the whole suite: the fish-wrap gate staying on for a path under a regular file, and for a BOM-only file. The fixup below adds a test that catches both. The fifth mutation, a warning printed for the not-a-directory skip, also gets past the suite; it changes stderr only, not a decision.
Matrix: executed this round; pin check: executed this round
CI @aac43f6a6: all green (14 jobs, Linux, macOS and both Windows targets); 455ea7f was also all green before the fixup.
Blocks merge (0)
Follow-up (already tracked by #4651)
- The same two gaps in
read_json(Cursor, Gemini and Droid loaders, from #4680): with~/.cursoras a regular file, or a BOM-only~/.cursor/cli-config.json,rtk hook cursorstill steps aside ({}) on develop + this PR. That code is outside this PR's hunks and #4651 already tracks it (comment of 10-31). Because the description saysCloses #4651, merging would close that half too. Either fold the same two changes intoread_jsonhere, with a test for each, or change the line toRefs #4651. Both are fine.
Checked and correct — no need to re-verify
- The 11 layouts the PR targets go from exit 3 / no
permissionDecisionto exit 0 /"allow", with no warning: project.claudeas a regular file or a symlink to one,CLAUDE_CONFIG_DIRset to a regular file or a path below one,~/.claudeas a regular file, a BOM-only project or global file, BOM +" \r\n", BOM +"\n", two BOMs. - The 11 controls are unchanged on all three builds and still warn: invalid JSON → 3, BOM + invalid JSON → 3, BOM +
deny: ["Bash(git status)"]→ 2 (the hook defers,hook checkexits 1),settings.local.jsonas a directory → 3, non-UTF-8 bytes → 3, a symlink loop → 3, mode 000 → 3, a BOM after a newline → 3. BOM +{}, BOM + an allow rule and a whitespace-only file → 0 on all three builds. - Fish-wrap gate: the 7 target layouts now wrap, with an explicit
ask; the 6 controls (invalid, BOM + invalid, a deny entry, directory in place, non-UTF-8, unreadable) still don't wrap. ENOTDIRcannot hide a file: on Linux and macOS,open()returns it only when a path prefix is not a directory. On Windows the same layout reportsNotFound, which was already skipped.- develop + PR:
cargo fmt --all --check,cargo clippy --all-targets(0 warnings),cargo test --all --no-fail-fast5413 passed, 0 failed, Windowscargo check --all-targets(mingw) clean.
Your questions
None asked.
Next
Fixup aac43f6 pushed, test only, code-reviewed: test_files_holding_no_rules_leave_the_settings_scan_attested checks that the settings scan stays attested for a path under a regular file, a bare BOM and BOM + whitespace. It also checks that the scan goes unread for BOM + { and for a directory in place of the file, and that a BOM before a deny rule still applies it. Both surviving mutations turn it red, as do skipping every BOM-prefixed file and skipping IsADirectory. develop + fixup: fmt, clippy, 5414 passed, 0 failed, Windows check clean. CI is green on it, approving. The only open point is the Closes #4651 line above.
Rounds: 1/3. Threads: 0 open.
Summary
Test plan
AI Disclosure: Implementation assisted by agentic workflow; logic, bounds, and regression tests thoroughly inspected.