Skip to content

rtk init --gemini --uninstall silently skips an unparsable settings.json and leaves a dangling hook entry #3400

Open

Description

@HetCreep

A JSON parse failure while installing the Gemini hook is swallowed and treated as "the file was empty", so the user's whole configuration is overwritten with a 5-line file. There is no backup on this path.

Provenance: AI-assisted source audit, human-directed, at v0.44.2 (700bdde). Read from source; rtk init was not executed (it writes to a real home directory).

The defect

src/hooks/init.rs:4269-4275:

let mut settings: serde_json::Value = if settings_path.exists() {
    let content = fs::read_to_string(&settings_path)
        .with_context(|| format!("Failed to read {}", settings_path.display()))?;
    serde_json::from_str(&content).unwrap_or(serde_json::json!({}))   // <-- parse error becomes {}
} else {
    serde_json::json!({})
};

settings then has rtk's hook inserted and is written back over the file (:4362-4365). Whatever the user had — auth, model, mcpServers, coreTools / excludeTools tool restrictions — is gone, and nothing in this function takes a .bak.

The same file gets it right three times over

The Claude installer bails instead of guessing (init.rs:968-975):

serde_json::from_str(&content)
    .with_context(|| format!("Failed to parse {} as JSON", settings_path.display()))?

Cursor (:3628) and Droid (:2965) also propagate the error. Only the Gemini path uses unwrap_or.

What triggers a parse failure in practice

This is not hypothetical for a hand-edited config:

  • a UTF-8 BOM — Windows Notepad and PowerShell 5.1's Out-File both write one by default, and serde_json rejects it
  • a trailing comma, or a // comment (common in editors that tolerate JSONC)
  • a file left half-written by an editor crash or a full disk

In each case the user's config is replaced rather than the error being reported.

Suggested fix (untested)

Use the same ? propagation the Claude path uses, so the install fails loudly with the path and the parse error instead of overwriting. If a lenient path is wanted, back up to settings.json.bak first — the Claude, Cursor and Droid installers already fs::copy a backup before writing (:1029, :3661, :3128); the Gemini one does not.

Related, same area, filed separately

  • init.rs:4421-4425 — the Gemini uninstaller removes every BeforeTool entry whose command merely .contains("rtk") (substring, not the precise match Claude uses), and its write at :4434 is non-atomic with no backup.
  • init.rs:1876-1884 — the Hermes config path also writes with no backup, while Claude/Cursor/Droid all take one.

I am reporting those two here as context rather than as separate issues since they are the same "this installer is missing the safety its siblings have" pattern; happy to split them out if you would rather track them individually.

Activity

  1. added
    bug
    good first issue
    on Aug 4, 2026
  2. pszymkowiak commented on Aug 4, 2026

    @pszymkowiak
    Collaborator

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟠 Priority high
    🎯 Confidence 90%

    Summary

    rtk init --gemini uses unwrap_or({}) on JSON parse failure, silently overwriting the user's ~/.gemini/settings.json with no backup; fix by propagating the parse error (or adding a .bak) like the Claude/Cursor/Droid installers already do.

    This looks like a simple fix that could be auto-resolved. Repo maintainers can use /wshm fix to attempt it.

    📁 Relevant files
    • src/hooks/init.rs

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

  3. HetCreep commented on Aug 30, 2026

    @HetCreep
    Author

    Re-verified at develop @ 0de9610. The defect in the title is fixed — and one item from the report's "related, same area" section is not, so I would suggest narrowing this issue rather than closing it.

    Provenance: AI-assisted source audit, human-directed, at develop @ 0de9610. Source-read and grep-verified; not executed.

    Fixed

    unwrap_or(json!({})) is gone. A parse failure now warns and returns without writing, and the caller prints an explicit "settings.json: NOT patched" line. Two extras landed on the same path that are worth noting because they remove the most likely real-world trigger: a leading BOM is stripped before parsing, and the write is now NamedTempFile + persist rather than a truncating write. The test asserts byte equality, so it genuinely pins the behaviour.

    Still open, and not tracked anywhere else

    uninstall_gemini still:

    • removes every BeforeTool entry whose command merely contains "rtk", so an unrelated hook command containing those three letters is dropped on rtk init --uninstall --gemini;
    • writes with a bare fs::write — not atomic, no backup;
    • silently swallows the same parse failure the install side now warns about. That asymmetry is the thing this issue is really about, and it survives one function lower.

    I checked whether this is covered by a sibling issue and it is not — #3404 is about the uninstall dispatcher routing --agent kilocode to the Claude uninstaller, and #3406 is about the project-rules .clinerules substring. Neither mentions uninstall_gemini. If this issue is closed, that item is not written down anywhere.

    Correction to my own report

    The Hermes item in my "related" list is weaker than I made it sound: that path is atomic and additive, so the only surviving part is the absence of a .bak, and that is already the subject of #3405. Please disregard the Hermes bullet here.

  4. HetCreep commented on Sep 16, 2026

    @HetCreep
    Author

    Re-verified against develop @ 5e0f92c (read the code at this commit; didn't build or run it).

    Fixed: the headline issue. patch_gemini_settings (src/hooks/init.rs:5602-5618) no longer falls back to an empty object when settings.json fails to parse. It strips the BOM first (:5597), then on a JSON parse error it warns and returns without touching the file:

    match from_json_str(content) {
        Ok(v) => v,
        Err(e) => {
            eprintln!("Warning: failed to parse {} as JSON: {}", settings_path.display(), e);
            print_gemini_manual_setup(&settings_path);
            return Ok(true);
        }
    }

    The write is atomic (NamedTempFile::new_in + tmp.persist, :5704-5707), and the caller is wired correctly — run_gemini captures the settings_parse_failed flag (:5544) and prints "settings.json: NOT patched (existing file could not be parsed; see warning above)" (:5554-5558) instead of implying success. The fix is general, not limited to the BOM case from the original report: test_patch_gemini_settings_malformed_json_warns_without_overwriting (:9152-9186) uses a trailing-comma malformed JSON, not a BOM, and asserts the file comes back byte-for-byte unchanged (:9181-9185). Landed via @kylehgc's fixes on 2026-08-29 (b6f354ca, ed8b0eb5). Thanks for this — nothing more needed on this half.

    Still open, narrower than the original report: the Gemini uninstaller. uninstall_gemini (src/hooks/init.rs:5717) has the same three problems the install side used to have, and the install-side fix didn't touch it:

    • Substring match, not exact — arr.retain (:5765-5769) drops any hook whose command merely .contains("rtk"), which can remove a hook that isn't rtk's.
    • Non-atomic write — fs::write(&settings_path, new_content)? (:5778) writes in place; no temp-file-and-rename like the install path now has.
    • Silent parse failure — if let Ok(mut settings) = from_json_str::<serde_json::Value>(&content) { (:5758) has no else arm, so a settings.json that fails to parse makes the uninstaller silently no-op instead of warning the way the install side now does.

    No test exercises uninstall_gemini either way — it has exactly one call site, the production dispatcher (:964).

    This is a local config-correctness issue, not attacker-exploitable, so nothing urgent — but please leave #3400 open until the uninstaller gets the same treatment as the install path.

  5. HetCreep commented on Oct 14, 2026

    @HetCreep
    Author

    Re-checked on v1.0.0 (stable, 2026-10-13): the official release binary plus the v1.0.0 source. Provenance: AI-assisted check, human-directed. "ran" = executed against rtk 1.0.0 with a control; "read" = verified from source only.

    Fixed (read, static): the titled defect. patch_gemini_settings (src/hooks/init/gemini.rs) now strips a BOM, and on a parse error it warns, returns Ok(true) and leaves the file untouched; the write goes through NamedTempFile::persist. Thanks.

    Still live at v1.0.0 (read):

    • uninstall_gemini (gemini.rs:251-320) still matches c.contains("rtk") on /hooks/0/command only, so a command such as smartkit matches and the whole entry (including hooks[1..]) is removed. The install-side dedupe at :164 uses the same substring and can wrongly skip patching.
    • The uninstall write is a plain fs::write (:314) with no .bak (the Claude path keeps a json.bak, claude.rs:388/870).
    • if let Ok silently skips an unparsable settings file after the hook script was already deleted, leaving a dangling reference with no warning.
    • Hermes: write_if_changed(config.yaml) (hermes.rs:58) writes without a backup.

    Static read only: I did not run init/--uninstall because they write real agent config.

  6. KuSh commented on Oct 31, 2026

    @KuSh
    Collaborator

    Re-checked on v1.1.0 by running it (fresh HOME, LC_ALL=C; src/hooks/init/gemini.rs is unchanged on develop), to support the narrowing the reporter asked for:

    What only this issue tracks is the Gemini uninstaller's handling of a file it cannot parse. With a trailing comma in ~/.gemini/settings.json after a normal install:

    $ rtk init -g --gemini --uninstall
    RTK uninstalled (Gemini):
      - Gemini hook: ~/.gemini/hooks/rtk-hook-gemini.sh
      ...
    exit 0
    

    The hook script is deleted, settings.json is left as it was, so its BeforeTool entry now points at the deleted file, and nothing warns. When the file does parse, the uninstall writes it with a plain fs::write and no .bak.

    A title like "rtk init --gemini --uninstall silently skips an unparsable settings.json and leaves a dangling hook entry" would match the remaining scope.

  7. changed the title [-]`rtk init --gemini` silently replaces the user's entire `~/.gemini/settings.json` if it fails to parse, with no backup[/-] [+]`rtk init --gemini --uninstall` silently skips an unparsable settings.json and leaves a dangling hook entry[/+] on Nov 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:cliarea:configbuggood first issuepriority:high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions