opencode icon indicating copy to clipboard operation
opencode copied to clipboard

[FEATURE]: Segment-based permission matching for compound shell commands (allowed-prefix + `;`/`&&` chaining executes trailing segments unchecked)

Open WyattChristeson opened this issue 2 weeks ago • 4 comments

Feature hasn't been suggested before.

  • [x] I have verified this feature I'm about to request hasn't been suggested before. (searched the issue tracker for permission/shell/chaining/compound/segment matching — no prior match found)

Describe the enhancement you want to request

The gap. Shell permission rules are matched against the whole command string (prefix patterns). When a rule allows a prefix such as ls *, a compound command like ls .ai ; echo CHAIN_MARKER matches that allow rule and executes BOTH segments without a prompt — the trailing segment is never evaluated against any rule. Observed live on v2.2.6: opencode run --agent build with the prompt ls .ai ; echo CHAIN_MARKER_XYZ_42 printed the marker with no permission prompt. The same holds for &&/|| chains: an allow on the first segment admits everything after the separator.

Security consequence. Any allowed prefix becomes an execution primitive for arbitrary trailing commands (ls ; curl … | sh, git status ; cat ~/.ssh/id_ed25519, …). Deny rules do not recover this: they only win when the whole command string matches the deny pattern, and a deny pattern broad enough to catch chaining (a pattern matching any command containing a separator) would also block every legitimate compound command (cd dir && make), so it is not a viable config-level workaround.

The request. Evaluate compound shell commands segment-wise: split the command on ;, &&, || (quote/escape-aware — a separator inside quotes is not a split point), then evaluate EVERY segment against the permission rules. Allow the command iff every segment is allowed; deny as soon as any segment matches a deny (or prompt on the first non-matching segment). This closes the chained-command bypass structurally, without breaking compound commands.

Scope note: command substitution ($(…), backticks) and pipe segments are adjacent surfaces — segment-wise evaluation of ;/&&/|| is the focused fix requested here.

Environment observed: opencode v2.2.6, permission rules via config permissions and policies blocks.

WyattChristeson avatar Nov 13 '26 06:11 WyattChristeson

Thanks for the detailed write-up, @WyattChristeson.

According to the permissions docs, V2 is meant to do what you describe already. A compound shell command may produce several shell resources, and the rules "apply to every extracted subcommand". If any of them is denied, the command is denied; otherwise, if any asks, you get a prompt; it runs without a prompt only if all of them are allowed.

Two things could explain what you saw without a bypass:

  • Every agent, build included, starts with an allow-all base policy ({ "action": "*", "resource": "*", "effect": "allow" }; see Defaults). Without a { "action": "shell", "resource": "*", "effect": "ask" } rule, the echo … segment is allowed on its own and needs no prompt.
  • opencode run approves permission requests automatically with --auto, --yolo or --dangerously-skip-permissions.

To tell whether this is a real bug, could you share:

  1. The exact permissions and policies blocks you used, both global and per-agent, including any shell * → ask rule.
  2. The exact opencode run command line with all flags, and whether you had approved anything with "always" before in that project.
  3. Your OS and how you installed OpenCode.

If you can reproduce it with a config that has a shell * → ask rule, that is a security bug. In that case, please send the full reproduction through a private GitHub Security Advisory rather than posting it here.

You can reply here with the details, or edit the issue. If you edit it, please also leave a comment saying you've updated it, so we know when you're done.

Related issue:

  • #52083: a different gap in how compound shell commands were checked (redirection targets were left out of the permission check). It was reproduced and closed.

opencodejr[bot] avatar Nov 13 '26 06:11 opencodejr[bot]

Hi I want to work in this issue

meutex avatar Nov 13 '26 07:11 meutex

Here is my followup question Segment-wise permission matching already exists in meutex/opencode, so which of these three should the PRD scope? (a) TEST-ONLY — packages/core/src/shell/scan.ts already splits commands quote/escape-aware on ;/&&/||/|&, packages/core/src/shell/parse.ts already emits one permission resource per segment (git status && npm run test -- --watch → two resources, per packages/core/test/shell-parse.test.ts), and packages/core/src/permission.ts already denies if any segment denies, asks if any asks, and allows only if all allow; the fork is also on 2.2.7, past the v2.2.6 you observed — so the honest change is a regression test proving a shell * → ask policy prompts for the echo segment of ls .ai ; echo X, with no behaviour change. (b) REAL BYPASS — you have a repro that still runs unprompted with a shell * → ask rule configured; post the exact permissions/policies blocks (global + per-agent) and the exact opencode run flags, and I'll scope the actual fix instead — but note upstream asked for that through a private GitHub Security Advisory, and SECURITY.md states AI-generated security reports are rejected. (c) NOTHING TO BUILD — the request is answered by the default allow-all base policy plus the existing segment matching; I write up the finding and stop, with no PR and no handoff. Also: without a shell * → ask rule, echo CHAIN_MARKER is allowed on its own by the default {action:"*",resource:"*",effect:"allow"} policy, which fully explains the missing prompt.

meutex avatar Nov 13 '26 07:11 meutex

Hi @WyattChristeson, we haven't heard back, so I'm closing this issue for now.

We were waiting for your permissions and policies config and the exact opencode run command and flags. With those we can tell whether the echo segment ran because of the default allow-all policy or auto-approval, or because the per-segment check was really bypassed. As noted in the earlier comment, V2 is meant to check each segment of a compound command already.

If you can still reproduce it with a shell * → ask rule, please send the reproduction through a private GitHub Security Advisory. Replying here with the details reopens this issue.

opencodejr[bot] avatar Nov 13 '26 12:11 opencodejr[bot]