Skip to content

fix(hooks/opencode): parse permission configs as JSONC, comments no longer drop every rule - #4528

Open
amnesiaof wants to merge 1 commit into
rtk-ai:developfrom
amnesiaof:fix/opencode-jsonc-comments
Open

amnesiaof wants to merge 1 commit into
rtk-ai:developfrom
amnesiaof:fix/opencode-jsonc-comments

Conversation

@amnesiaof

Copy link
Copy Markdown
Contributor

Fixes #4524.

The bug

read_config parsed both opencode.json and opencode.jsonc with plain serde_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.

// opencode.jsonc
{
  // never push from this project
  "permission": { "bash": { "git push *": "deny" } }
}

Why it 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.

The deny on git push * then does not match the rewritten rtk 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_str in src/core/utils.rs removes // and /* */ comments and trailing commas before parsing, honouring string literals so "//server/share" and a/*b stay patterns. Trailing commas are included because JSONC permits them and serde_json rejects them for the same reason as comments.

Correction to the issue text, my own suggestion: src/discover/lexer.rs cannot 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, not Deserialize<'a>, because the parse target is the rebuilt string rather than a borrow of the input.

Tests

Four, in the permissions_opencode module:

  • comments and trailing commas keep every rule, line and block and inline
  • "//server/share" and a/*b survive as patterns
  • BOM plus a leading comment still parses
  • genuinely broken JSON is still rejected, including an unterminated block comment — stripping must not turn bad input into a parse that succeeds with the wrong rules

Mutation-checked: reverting the call site to from_json_str turns 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 warnings clean. cargo fmt --check clean.

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

#4458 is open and rewrites permissions_opencode.rs. This touches read_config in the same file, so expect a conflict there — small, in one function.

Opened from a fork: amnesiaof/rtk currently has read-only access to this repo, so I cannot push branches here. The branch is amnesiaof:fix/opencode-jsonc-comments, one commit on top of develop.

`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
@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟡 Risk medium

Summary

Fixes a security-relevant bug where OpenCode permission config files (opencode.json/.jsonc) were parsed with plain serde_json, causing any file with // comments or trailing commas to fail to parse entirely. This silently dropped all permission rules, which broke the pre/post-rewrite verdict comparison guard and let forbidden commands (e.g. a denied git push) slip through undetected. The fix adds a comment/trailing-comma-stripping JSONC parser (from_jsonc_str) that respects string literals…

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Linked issues: #4524


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

@Drowze

Drowze commented Oct 28, 2026

Copy link
Copy Markdown

Stumbled on this bug... For now I'm "compiling" my jsonc config into a valid strict json (and renaming it so I don't have any other unexpected issues...)

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

opencode.jsonc with comments: every permission rule is silently dropped

2 participants