Skip to content

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

Open

Description

@amnesiaof

Summary

rtk cannot read opencode.jsonc. It parses the file with a strict JSON parser, which rejects comments, so the whole file fails to load and rtk sees zero permission rules from it.

The consequence is that a single // comment line silently disables every permission rule in that file for rtk's rewrite path. The user still has rules configured; rtk just cannot see them, and it says nothing at the point where it matters.

What happens

A user writes the file the way OpenCode's docs show it:

// opencode.jsonc
{
  // never push from this project
  "permission": { "bash": { "git push *": "deny" } }
}
  1. read_config calls utils::from_json_str, which is plain serde_json::from_str.
  2. serde_json stops at the first //. read_config prints [rtk] warning: failed to parse permissions from <path> to stderr and returns None.
  3. load_opencode_rules ends up with no rules. check_command_with_opencode_rules returns Default, not Deny.
  4. opencode_answer computes before = Default, rewrites git push origin main to rtk git push origin main, computes after = Default, sees them equal, and returns the rewrite.

OpenCode then evaluates permissions on the rewritten string. The rule git push * does not match rtk git push origin main, so the deny never fires and the command runs.

Why this matters

rtk's guard exists precisely to stop a rewrite from changing what the user's rules decide. It compares the verdict before and after — but both verdicts come from the rules rtk read. When the file fails to parse, both say "no rule matched", the comparison succeeds, and the guard reports that nothing changed. The property holds only while the rules load, and the failure mode of loading is the one case where it cannot notice.

djbclark reproduced the underlying mechanism on 2.0.26 in #4187: execute.before runs before permission evaluation, so the engine judges the string the plugin handed back.

Scope

permissions_opencode.rs is the only reader of .jsonc in the tree, and there is no comment-stripping helper anywhere in src/. JSONC also permits trailing commas, which serde_json rejects for the same reason.

Suggested fix

Strip comments and trailing commas before parsing, honouring string literals so a path like "//server/share" survives. src/discover/lexer.rs already walks bytes with in-string and in-quote state, so the tokenizer exists.

Worth doing alongside:

  • Treat an unparsable file as "rules present, refuse to rewrite" rather than "no rules". An unreadable policy file should fail closed, the same way an unreadable agent markdown file does after feat(hook/opencode): support OpenCode V2, Desktop, and Windows (#3463, #3326) #4187. Silently continuing is what turns a typo into lost enforcement.
  • Decide what the stderr warning is for. It is currently the only signal, and it lands on the same stream as ordinary output.

Verification

Confirmed on develop at 27a659f5: a test writing opencode.jsonc with one // comment and a git push * deny rule reports rules seen = [].

Not verified: the behaviour on a real OpenCode session, and whether OpenCode's own matcher would catch the rewritten command. The bypass follows from the pattern no longer matching the string, not from a reproduced OpenCode run.

Activity

  1. rtk-wshm-sync-bot commented on Oct 12, 2026

    @rtk-wshm-sync-bot

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟠 Priority high
    🎯 Confidence 93%

    Summary

    JSONC comments/trailing-commas in opencode.jsonc cause silent parse failure, dropping all permission rules and letting rtk rewrite past a deny rule unnoticed.

    📁 Relevant files
    • src/permissions_opencode.rs
    • src/discover/lexer.rs
    • src/utils.rs

    Suggested Actions

    1. Add a JSONC-stripping step (reuse lexer.rs's string-aware byte walker) before calling utils::from_json_str in permissio…
    2. Make parse failures fail closed (treat as rules present, refuse rewrite) instead of returning None/Default, per feat(hook/opencode): support OpenCode V2, Desktop, and Windows (#3463, #3326) #4187's…
    3. Cross-check against OpenCode permission rules: rtk's config loader diverges from OpenCode's config resolution #4422 (config loader divergence) and fix(hooks): judge OpenCode against its own permission rules #4349 (judge OpenCode against its own rules) for overlap
    4. Clarify/escalate the stderr-only warning so a dropped policy file isn't silent

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

  2. amnesiaof commented on Oct 12, 2026

    @amnesiaof
    ContributorAuthor

    File paths in the automated triage are wrong on two of three. The real ones:

    • src/hooks/permissions_opencode.rs — read_config at :204, CONFIG_NAMES at :196, opencode_configs at :167
    • src/core/utils.rs — from_json_str at :98, which is plain serde_json::from_str
    • src/discover/lexer.rs — the string-aware byte walker, correct as listed

    Also #4349 is a pull request, not an issue: it is the delegation change that introduced this path, and the test it added is in tests/hook_decision_protocol_test.rs.

    The diagnosis in the triage is right and needs no correction.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions