Skip to content

fix(hook): fail closed on unreadable Gemini settings - #4906

Merged
KuSh merged 5 commits into
rtk-ai:developfrom
TechWizard9999:fix/hook-4878-gemini-folder-trust
Nov 29, 2026
Merged

KuSh merged 5 commits into
rtk-ai:developfrom
TechWizard9999:fix/hook-4878-gemini-folder-trust

Conversation

@TechWizard9999

@TechWizard9999 TechWizard9999 commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Preserve JsonRead::Error in Gemini global-settings loading so malformed or unreadable settings cannot load project tools.allowed rules.
  • Fail closed also when GEMINI_CLI_TRUST_WORKSPACE=true: with an unreadable or unparseable global file, no Gemini rules load at all.
  • Keep existing missing and empty global-settings behavior unchanged.
  • Add a real rtk hook gemini integration matrix for valid, malformed, missing, empty, and unreadable global settings.
  • The integration matrix is Unix-only (dirs::home_dir ignores HOME on Windows), so the trust decision is split into gemini_settings_from and unit-tested against real files on every platform, including Windows.

Fixes #4878.

Test plan

  • cargo fmt --all --check
  • cargo fmt --manifest-path proxy/Cargo.toml --all --check
  • cargo clippy --all-targets -- -D warnings
  • cargo test --test hook_config_io_test
  • cargo test --lib gemini (includes test_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_closes
  • cargo test --manifest-path proxy/Cargo.toml --all
  • Direct rtk hook gemini matrix for valid, malformed, unreadable, missing, empty, and explicit trust environment states
  • cargo test --all without skips: two rg stdin-streaming tests time out on this macOS environment. The timeout reproduces on the unchanged develop checkout and is unrelated to this hook change.

Important: All PRs must target the develop branch (not master).

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]>
@rtk-wshm-sync-bot rtk-wshm-sync-bot Bot added bug Something isn't working gemini hook security labels Nov 1, 2026
@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟡 Risk medium

Summary

Fixes 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

  • Tests present
  • Breaking change
  • Docs updated

Linked issues: #4878


Analyzed automatically by wshm · This is an automated analysis, not a human review.

TechWizard9999 and others added 2 commits November 1, 2026 11:34
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.
@TechWizard9999

Copy link
Copy Markdown
Contributor Author

Hi @KuSh @aeppling, could you please review this PR when you have a chance? Thank you!

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

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)

  1. src/hooks/permissions.rs:609: an unreadable global file also overrides GEMINI_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 allowing git push, the folderTrust-on, folderTrust-off, missing and empty global files all give allow. A trailing comma, a JSONC comment, chmod 000 or a directory gives ask_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 runs git push origin main without asking. On this head the hook answers ask_user there. #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 give allow. Without the env they give ask_user, as now. The unit loop's trusted_by_env = true case and the two *_with_trust assertions in tests/hook_config_io_test.rs (L342, L365) flip to Allow / "allow".
    Test: add a real-hook row with a valid folderTrust-on global, the env set to true, and the project allow, expecting "allow". That row also pins the env read: today, changing == Some("true") to == Some("never") in gemini_settings() (L596) passes every test. The only env rows assert ask_user, which that mutation cannot change.

Optional — will not hold merge

  • run_with_trust(.., None) calls env_remove("GEMINI_CLI_TRUST_WORKSPACE"), but common::rtk_command() already starts from an empty environment, so the None arm 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 000 or a directory: git push with a project allow goes from allow to ask_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 Error arm falling through (unit and integration), Missing returning 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.

Comment thread src/hooks/permissions.rs Resolved
TechWizard9999 and others added 2 commits November 26, 2026 10:49
…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 KuSh 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.

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)

  1. An unreadable global file overriding GEMINI_CLI_TRUST_WORKSPACE=true: fixed. With the env set, a trailing-comma, JSONC, chmod 000, directory or BOM-only global file plus a project allow now gives allow, as develop does. Without the env, the same five rows give ask_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() to Some("never") now fails the integration test.
  • Round 1's mutations still fail a test: Missing returning 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.

@KuSh
KuSh merged commit 2ffac49 into rtk-ai:develop Nov 29, 2026
16 checks passed
@rtk-release-bot rtk-release-bot Bot mentioned this pull request Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working gemini hook security

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Gemini hook: an unreadable global settings file trusts the workspace and applies its allow rules

2 participants