Repository navigation
Description
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-Fileboth write one by default, andserde_jsonrejects 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 everyBeforeToolentry whose command merely.contains("rtk")(substring, not the precise match Claude uses), and its write at:4434is 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
pszymkowiak commentedon Aug 4, 2026
🔍 Automated Triage
bughighSummary
rtk init --geminiusesunwrap_or({})on JSON parse failure, silently overwriting the user's~/.gemini/settings.jsonwith no backup; fix by propagating the parse error (or adding a.bak) like the Claude/Cursor/Droid installers already do.📁 Relevant files
src/hooks/init.rsTriaged automatically by wshm · This is an automated analysis, not a human review.
HetCreep commentedon Aug 30, 2026
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 nowNamedTempFile+ 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_geministill:BeforeToolentry whose command merely contains"rtk", so an unrelated hook command containing those three letters is dropped onrtk init --uninstall --gemini;fs::write— not atomic, no backup;I checked whether this is covered by a sibling issue and it is not — #3404 is about the uninstall dispatcher routing
--agent kilocodeto the Claude uninstaller, and #3406 is about the project-rules.clinerulessubstring. Neither mentionsuninstall_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.HetCreep commentedon Sep 16, 2026
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:The write is atomic (
NamedTempFile::new_in+tmp.persist,:5704-5707), and the caller is wired correctly —run_geminicaptures thesettings_parse_failedflag (: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:arr.retain(:5765-5769) drops any hook whose command merely.contains("rtk"), which can remove a hook that isn't rtk's.fs::write(&settings_path, new_content)?(:5778) writes in place; no temp-file-and-rename like the install path now has.if let Ok(mut settings) = from_json_str::<serde_json::Value>(&content) {(:5758) has noelsearm, 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_geminieither 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.
HetCreep commentedon Oct 14, 2026
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.0with 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, returnsOk(true)and leaves the file untouched; the write goes throughNamedTempFile::persist. Thanks.Still live at v1.0.0 (read):
uninstall_gemini(gemini.rs:251-320) still matchesc.contains("rtk")on/hooks/0/commandonly, so a command such assmartkitmatches and the whole entry (includinghooks[1..]) is removed. The install-side dedupe at:164uses the same substring and can wrongly skip patching.fs::write(:314) with no.bak(the Claude path keeps ajson.bak,claude.rs:388/870).if let Oksilently skips an unparsable settings file after the hook script was already deleted, leaving a dangling reference with no warning.write_if_changed(config.yaml)(hermes.rs:58) writes without a backup.Static read only: I did not run
init/--uninstallbecause they write real agent config.KuSh commentedon Oct 31, 2026
Re-checked on v1.1.0 by running it (fresh
HOME,LC_ALL=C;src/hooks/init/gemini.rsis unchanged on develop), to support the narrowing the reporter asked for:/hooks/0/command(install skips a foreign hook such as/usr/local/bin/smartkit-guard, uninstall then deletes it) still reproduces. It is tracked with reproductions inrtk init --geminitreats any BeforeTool group whose first hook contains "rtk" as RTK's: uninstall deletes user hooks, install skips #4227, which also covers a user hook sharing RTK's group.rtk init --agent hermescan write invalid YAML into~/.hermes/config.yaml, or patch text inside a block scalar — with no backup #3405.What only this issue tracks is the Gemini uninstaller's handling of a file it cannot parse. With a trailing comma in
~/.gemini/settings.jsonafter a normal install:The hook script is deleted,
settings.jsonis left as it was, so itsBeforeToolentry now points at the deleted file, and nothing warns. When the file does parse, the uninstall writes it with a plainfs::writeand no.bak.A title like "
rtk init --gemini --uninstallsilently skips an unparsable settings.json and leaves a dangling hook entry" would match the remaining scope.[-]`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[/+]