Repository navigation
fix(hook): fail closed on unreadable Gemini settings - #4906
Conversation
Preserve JsonRead::Error in gemini_settings so malformed or unreadable global settings cannot cause project tools.allowed rules to be applied. Fixes rtk-ai#4878 Co-authored-by: Copilot <[email protected]>
📊 Automated PR Analysis
SummaryFixes a Gemini hook security gap where unreadable or malformed global settings.json previously fell back to None, effectively allowing project-level tools.allowed rules to load as if folder trust was untrusted. Now JsonRead::Error causes the function to return None immediately (fail closed), preserving the existing behavior for missing/empty settings. Adds an integration test matrix covering valid, malformed, missing, empty, and unreadable global settings states. Review Checklist
Linked issues: #4878 Analyzed automatically by wshm · This is an automated analysis, not a human review. |
Keep Gemini integration helpers behind the Unix-only test because Windows uses a different home-directory resolution path. Co-authored-by: Copilot <[email protected]>
The integration matrix in tests/hook_config_io_test.rs is Unix-only because dirs::home_dir ignores HOME on Windows, so the rtk-ai#4878 fix had no coverage there. Split the trust decision into gemini_settings_from (global read result, trust env, lazy project-root lookup) and unit-test it against real files: malformed and unreadable global settings never let the project's tools.allowed apply, with or without GEMINI_CLI_TRUST_WORKSPACE; missing/empty keep today's behavior.
KuSh
left a comment
There was a problem hiding this comment.
Round 1 — CHANGES REQUESTED
Claim: when the global ~/.gemini/settings.json exists but rtk cannot read or parse it, no Gemini rules load, so a project file can no longer grant allow that folder trust would have withheld (#4878).
Scope: accept if narrowed to "when GEMINI_CLI_TRUST_WORKSPACE does not trust the workspace" (item 1).
Ran: rtk hook gemini on develop and on this PR merged into develop, 72 rows each: 9 global-file shapes (folderTrust on, folderTrust off, trailing comma, JSONC comment, chmod 000, a directory, missing, empty, BOM only) × project file allowing git push or absent × trust env unset or true × git push origin main / git status. Invariants: every decision, run through the real hook. Savings: n/a, this is a permission decision with no filtered output.
Pins: 5 decisions, 5 mutated, 1 survived: the trust env read in gemini_settings() (item 1).
CI @9dda069: all green. Locally, merged into develop 762708c: fmt clean, clippy 0 warnings, cargo test --all 5822 passed, 0 failed.
Blocks merge (1)
src/hooks/permissions.rs:609: an unreadable global file also overridesGEMINI_CLI_TRUST_WORKSPACE=true. When the env trusts the workspace, the global file cannot change the trust outcome, and develop ignores it whatever it holds. On this head, with the env set and a project file allowinggit push, the folderTrust-on, folderTrust-off, missing and empty global files all giveallow. A trailing comma, a JSONC comment,chmod 000or a directory givesask_user. So the only global file that withdraws the project's allow is one rtk cannot read. Gemini CLI 0.70.0 itself, run headless against a mock API with the env set and the same project file, refuses to start on a trailing comma,chmod 000, a directory or a BOM-only global file. So in those cases the hook never runs. With a JSONC global file, which Gemini accepts and rtk cannot parse, Gemini runsgit push origin mainwithout asking. On this head the hook answersask_userthere. #4878 was measured without the env, and #4458 makes the same call: fail closed only when the env does not trust.
The narrowed fix must pass these rows. With the env set, those four unreadable shapes plus a project allow giveallow. Without the env they giveask_user, as now. The unit loop'strusted_by_env = truecase and the two*_with_trustassertions intests/hook_config_io_test.rs(L342, L365) flip toAllow/"allow".
Test: add a real-hook row with a valid folderTrust-on global, the env set totrue, and the project allow, expecting"allow". That row also pins the env read: today, changing== Some("true")to== Some("never")ingemini_settings()(L596) passes every test. The only env rows assertask_user, which that mutation cannot change.
Optional — will not hold merge
run_with_trust(.., None)callsenv_remove("GEMINI_CLI_TRUST_WORKSPACE"), butcommon::rtk_command()already starts from an empty environment, so theNonearm can simply not set it.
Checked and correct — no need to re-verify
- No env, folderTrust-on global with a trailing comma, a JSONC comment,
chmod 000or a directory:git pushwith a project allow goes fromallowtoask_user. That closes #4878 as reported. - Missing, empty, folderTrust-on and folderTrust-off global files: all 36 rows are identical to develop.
- Mutations killed by the PR's tests: the global
Errorarm falling through (unit and integration),Missingreturning early (both), the env winning over an unreadable file (both), and the injected project root replaced by the ambient lookup (unit).
Next
Narrow the env case as in item 1, with its rows as tests.
Threads: 0 open.
…ut the trust env With GEMINI_CLI_TRUST_WORKSPACE=true the workspace is trusted whatever the global settings say, so an unreadable or unparseable global file no longer drops the project file's rules there. Without the env it still yields no rules, as before.
…the trust env With GEMINI_CLI_TRUST_WORKSPACE=true a parseable project file still applies and replaces the global one. A global file rtk cannot read or parse only matters when no parseable project file is found: a parseable global file would supply its rules there, one rtk cannot read or parse supplies none. The comment said it changed nothing. Co-Authored-By: Claude Opus 5.5 <[email protected]>
KuSh
left a comment
There was a problem hiding this comment.
Round 2 — APPROVED
Claim: when the global ~/.gemini/settings.json exists but rtk cannot read or parse it, and GEMINI_CLI_TRUST_WORKSPACE does not trust the workspace, no Gemini rules load, so a project file can no longer grant allow that folder trust would have withheld (#4878).
Scope: accept if narrowed to "when GEMINI_CLI_TRUST_WORKSPACE does not trust the workspace" (round 1, frozen), done in 68f2adc.
Ran: rtk hook gemini on develop, on the round-1 head and on 68f2adc, each merged into develop 387b7f0, 72 rows each: 9 global-file shapes × project allow or none × trust env unset or true × git push origin main / git status. Invariants: every decision, run through the real hook. Savings: n/a, this is a permission decision with no filtered output.
Pins: 6 decisions, 6 mutated, 0 survived. The env read that survived in round 1 is now caught by the new valid_with_trust row.
CI @bbbdc887 (with the fixup below): all green. Locally, merged into develop 387b7f0: fmt clean (root and proxy/), clippy 0 warnings, cargo test --all 5955 passed, 0 failed.
Blocks merge (0 of 1, frozen at round 1)
An unreadable global file overriding: fixed. With the env set, a trailing-comma, JSONC,GEMINI_CLI_TRUST_WORKSPACE=truechmod 000, directory or BOM-only global file plus a project allow now givesallow, as develop does. Without the env, the same five rows giveask_user. The other 62 rows are identical to develop.
Checked and correct — no need to re-verify
- Removing the new guard, making it unconditional, or inverting it each fails both the unit loop and
gemini_global_settings_fail_closed_on_read_and_parse_errors. - Changing the env comparison in
gemini_settings()toSome("never")now fails the integration test. - Round 1's mutations still fail a test:
Missingreturning early, and the injected project root replaced by the ambient lookup.
Next
I pushed fixup bbbdc88, which fixes the new comment above gemini_settings. It said that with the env set an unreadable global file "changes nothing". But when no parseable project file is found, a parseable global file still supplies its rules, and one rtk cannot read or parse leaves none. The comment now says that; no behaviour changes. Approving.
Threads: 0 open.
Summary
JsonRead::Errorin Gemini global-settings loading so malformed or unreadable settings cannot load projecttools.allowedrules.GEMINI_CLI_TRUST_WORKSPACE=true: with an unreadable or unparseable global file, no Gemini rules load at all.rtk hook geminiintegration matrix for valid, malformed, missing, empty, and unreadable global settings.dirs::home_dirignoresHOMEon Windows), so the trust decision is split intogemini_settings_fromand unit-tested against real files on every platform, including Windows.Fixes #4878.
Test plan
cargo fmt --all --checkcargo fmt --manifest-path proxy/Cargo.toml --all --checkcargo clippy --all-targets -- -D warningscargo test --test hook_config_io_testcargo test --lib gemini(includestest_gemini_unusable_global_settings_never_trust_the_project, which fails with the fix reverted)cargo test --all -- --skip rg_passthrough_streams_before_stdin_closes --skip rg_streams_matches_before_stdin_closescargo test --manifest-path proxy/Cargo.toml --allrtk hook geminimatrix for valid, malformed, unreadable, missing, empty, and explicit trust environment statescargo test --allwithout skips: tworgstdin-streaming tests time out on this macOS environment. The timeout reproduces on the unchangeddevelopcheckout and is unrelated to this hook change.