Repository navigation
Conversation
`read_config` parsed both `opencode.json` and `opencode.jsonc` with plain `serde_json`, which stops at the first `//`. The whole file then failed to load and rtk saw zero permission rules from it — silently, with only a stderr line nobody sees during a hook call. That inverts the guard. `opencode_answer` compares the verdict before and after the rewrite to prove the rewrite did not change what the user's rules decide. With no rules loaded both verdicts are `Default`, the comparison succeeds, and the guard reports that nothing changed. A `deny` on `git push *` then does not match the rewritten `rtk git push origin main`, so OpenCode evaluates a command the user forbade. `from_jsonc_str` removes `//` and `/* */` comments and trailing commas before parsing, honouring string literals so `"//server/share"` and `a/*b` stay patterns. JSON escape handling means `src/discover/lexer.rs` cannot be reused: it walks shell quoting, which has no `\"`. Fails closed on nothing — genuinely broken JSON is still rejected, covered by a test. Making an unreadable policy file refuse the rewrite outright is a separate semantic change and is deliberately left out. Refs rtk-ai#4524
📊 Automated PR Analysis
SummaryFixes a security-relevant bug where OpenCode permission config files (opencode.json/.jsonc) were parsed with plain serde_json, causing any file with Review Checklist
Linked issues: #4524 Analyzed automatically by wshm · This is an automated analysis, not a human review. |
|
Stumbled on this bug... For now I'm "compiling" my cd ~/.config/opencode
mv opencode.jsonc opencode.jsonc.bak
cat opencode.jsonc.bak |
ruby -rjson -e 'puts JSON[ARGF.read, allow_comments: true, allow_trailing_comma: true].to_json' > opencode.json |
Fixes #4524.
The bug
read_configparsed bothopencode.jsonandopencode.jsoncwith plainserde_json, which stops at the first//. The whole file failed to load, rtk saw zero permission rules from it, and the only signal was a stderr line that nobody sees during a hook call.Why it inverts the guard
opencode_answercompares the verdict before and after the rewrite, to prove the rewrite did not change what the user's rules decide. With no rules loaded both verdicts areDefault, the comparison succeeds, and the guard reports that nothing changed.The
denyongit push *then does not match the rewrittenrtk git push origin main, so OpenCode evaluates a command the user forbade. The safety property holds only while the rules load, and a parse failure is exactly the case it cannot notice.The fix
from_jsonc_strinsrc/core/utils.rsremoves//and/* */comments and trailing commas before parsing, honouring string literals so"//server/share"anda/*bstay patterns. Trailing commas are included because JSONC permits them andserde_jsonrejects them for the same reason as comments.Correction to the issue text, my own suggestion:
src/discover/lexer.rscannot be reused here. It walks shell quoting, which has no\"escape, so it cannot tell an escaped quote from a closing one. This needs JSON string semantics.DeserializeOwned, notDeserialize<'a>, because the parse target is the rebuilt string rather than a borrow of the input.Tests
Four, in the
permissions_opencodemodule:"//server/share"anda/*bsurvive as patternsMutation-checked: reverting the call site to
from_json_strturns the first and third red. The second stays green either way, since those patterns are valid plain JSON — it is a guard against a future stripper that is not string-aware, not a proof.cargo test --bin rtk hooks::669 passed.cargo clippy --all-targets -- -D warningsclean.cargo fmt --checkclean.Left out on purpose
The issue also suggested treating an unreadable policy file as "rules present, refuse the rewrite". That changes the meaning of the guard rather than fixing the bug, and it is a design call for the maintainers rather than a consequence of this one. Worth its own PR if you want it.
Merge order
#4458is open and rewritespermissions_opencode.rs. This touchesread_configin the same file, so expect a conflict there — small, in one function.Opened from a fork:
amnesiaof/rtkcurrently has read-only access to this repo, so I cannot push branches here. The branch isamnesiaof:fix/opencode-jsonc-comments, one commit on top ofdevelop.