All guides

Set shell command approval rules in OpenCode

Use ordered permissions rules so shell commands ask by default, read-only Git checks pass, pushes are denied, and learn what approval replies and saved rules do.

AINavWritten / Updated Sources checked
On this page

At a glance

Configure shell prompts and specific Git exceptions, then inspect the effective permission decision and approval choices. The example outcomes assume no later configured override, no saved approval changing ask to allow, no applicable hard-deny policy, and no directory check changing the overall operation result.

Workflow

  1. Write rules broad to narrow

    Each rule has action, resource and effect. The last matching rule wins, so the broad rule goes first and exceptions follow.

  2. Walk sample commands through

    Check configured rules, saved approvals, hard-deny policies and directory checks for each command before recording the overall decision.

  3. Check approval outcomes

    once, always and reject differ. always saves a project-scoped allow rule but never overrides a deny.

Each stage cites official sources.

Before you start

  • You have a project and can edit or create its opencode.jsonc.
  • You know this guide uses V2 names: permissions, shell and subagent, not V1's permission, bash and task.
  • You have a few sample commands to test and know which commands must never run automatically.

1. Confirm field names and open the config

Edit the project's opencode.jsonc, add "$schema", and use the top-level permissions array. The page warns that V1 and V2 use different field and action names, so use permissions and shell here. Rules written with the old names are outside this guide.

2. Start with one broad ask rule

Write { "action": "shell", "resource": "*", "effect": "ask" } first. The official base policy allows all actions, so this rule is what makes shell commands wait for your decision. It must come first so that later rules can override it as exceptions.

3. Allow read-only Git checks

Add two allow rules: git status * and git diff *. A shell pattern ending in * also matches the command without arguments, so both git status and git status --short match. Allow only specific commands you are sure do not change the repository, and avoid broad prefixes.

4. Deny pushes and verify rule order

Add a deny for git push * last. Because the last match wins, the deny must come after the permissive rules. When one operation checks several resources, any deny denies it; otherwise any ask asks; otherwise it is allowed. Compound commands may be split into several subcommands, and each takes part in the decision. Global and active-agent rules can be appended after the project permissions array. Inspect them before calling the boundary complete, because later matches affect these outcomes. The example outcomes assume no later configured override, no saved approval changing ask to allow, no applicable hard-deny policy, and no directory check changing the overall operation result. After configured rules, inspect saved approvals, applicable hard-deny policies and directory checks such as external_directory. An initial rule match is not the effective decision.

5. Optionally restrict directories outside the project

The default policy allows external_directory, so reads and edits outside the project and worktree do not prompt. If you want a prompt, add { "action": "external_directory", "resource": "*", "effect": "ask" } and follow it with narrower rules for specific directories. This step is optional and does not change the shell rules above.

6. Try one command and choose a reply

After those checks, select a command whose effective result remains ask and have the agent request it. Have the agent trigger a command that needs asking. once approves only the pending request. always approves it and saves the tool's proposed patterns for the project (shell proposes command prefixes). reject rejects it and every other pending permission request in that session. When unsure, choose once or reject.

7. How to stop and tighten

reject denies the current and other pending requests in the session; it does not prove that an already-running command stops immediately. Saved always rules are durable project-scoped allow rules, so review them regularly and remove any no longer needed. They never override a configured deny. For a limit that even an allow cannot bypass, use policies, for example { "action": "tool.use", "resource": "shell:sudo *", "effect": "deny" }, which only turns allow or ask into deny and never grants access.

Original example

The example outcomes assume no later configured override, no saved approval changing ask to allow, no applicable hard-deny policy, and no directory check changing the overall operation result. Invented project /home/user/projects/acme with this opencode.jsonc:

json
{
  "$schema": "https://opencode.ai/config.json",
  "permissions": [
    { "action": "shell", "resource": "*", "effect": "ask" },
    { "action": "shell", "resource": "git status *", "effect": "allow" },
    { "action": "shell", "resource": "git diff *", "effect": "allow" },
    { "action": "shell", "resource": "git push *", "effect": "deny" }
  ]
}

Assuming no later global or active-agent rule overrides these project rules, the documentation-based walk-through is: git status --short → allow; git diff HEAD~1 → allow; git push origin main → deny; npm test → matches only the first rule, so ask. If you reply always to npm test, a prefix rule such as shell: npm * → allow might be saved (the tool proposes the pattern; the page shows a similar example only for git status), and you should go back and check whether it is too broad. This is a deduction from the documentation, not an observed run.

Completion checks

  • The config uses permissions and shell, not the older field names.
  • Every specific exception sits after the broad rule, and a denied command in your walk-through is not overridden by a later allow.
  • The example outcomes assume no later configured override, no saved approval changing ask to allow, no applicable hard-deny policy, and no directory check changing the overall operation result. Later global and active-agent rules, saved approvals, hard-deny policies and directory checks have been inspected. The approval exercise uses a command whose final result remains ask.
  • On the first real prompt you know what once, always and reject each do.
  • Saved always rules are reviewed regularly and no overly broad pattern remains.

Limits

  • shell runs with the host user's filesystem, process and network authority. Directory inference from command text is best effort, and the page recommends a narrow shell allowlist instead of trying to recognize every dangerous command.
  • A command with malformed syntax or dynamic evaluation that the scanner cannot safely analyze returns a scanner error instead of executing unverified subcommands.
  • Non-interactive clients must decide how to handle approval requests. For a prohibition that an allow cannot override, use the documented hard-deny policies and inspect their actual scope.
  • This guide does not cover the full rules for skill, MCP, webfetch and other actions; consult the official permissions page for those.

Verification scope

Only the official permissions documentation was read. Login, installation of OpenCode, product API execution, file upload and generated output were not tested. The sample commands, directory and outcomes are invented deductions from the documentation and were not verified in a real session.

Article sources

Original editorial content; official descriptions and checking suggestions are distinguished.

  1. OpenCode · Official sources
    View details