Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

OpenCode Permissions Configuration

OpenCode Permissions Configuration

OpenCode's permission system controls which operations can run automatically, which require your manual approval, and which are blocked outright. Proper permission configuration both lets the AI complete tasks efficiently and prevents accidental operations from damaging code or leaking sensitive files.

Starting from v1.1.1, the legacy tools boolean configuration is deprecated and has been merged into permission . Legacy config is still supported for backward compatibility, but migrating to the new permission syntax is recommended.


Three permission actions

Each permission rule ultimately resolves to one of the following three actions:

Action effect Applicable scenarios
"allow" No approval needed, runs automatically Low-risk, high-frequency operations such as reading files and running tests
"ask" Pops up an approval prompt, and you decide whether to allow it Somewhat risky operations such as writing files and executing scripts
"deny" Blocked outright, neither executed nor prompted Explicitly disallowed dangerous operations such as deleting files and pushing code

Basic configuration

Permission config is written in the file under the repository root (or user config directory) opencode.json and configured using the permission field.

1. Globally set all permissions

The simplest approach: use a single string to set permissions for all operations at once. Suitable for quick starts or temporary debugging:

Example

{

  "$schema": "https://opencode.ai/config.json",

  "permission": "allow" // All operations run automatically with no prompts (suitable for local development and when you fully trust the AI)

}

2. Configure by tool name

Using object syntax, you can specify permissions for different tools individually."*" is a wildcard that matches all operations, usually used as a fallback default:

Example

{

  "$schema": "https://opencode.ai/config.json",

  "permission": {

    "*": "ask", // Fallback rule: all tools not individually configured will prompt by default

    "bash": "allow", // bash (executes shell commands): allowed directly, no prompt

    "edit": "deny" // edit (modifies files): blocked outright

  }

}


Fine-grained rules (object syntax)

For most permissions, besides setting a uniform action, you can also useObject syntaxto apply different rules based on the tool's specific input. For example, for the bash tool, you can distinguish which commands are allowed, which need approval, and which are rejected outright:

Example

{

  "$schema": "https://opencode.ai/config.json",

  "permission": {

    "bash": {

      "*": "ask", // Fallback: all bash commands require approval by default

      "git *": "allow", // Commands starting with git (e.g., git status, git log) are allowed directly

      "npm *": "allow", // Commands starting with npm (e.g., npm install, npm run build) are allowed directly

      "grep *": "allow", // grep search commands are allowed directly

      "rm *": "deny" // rm delete commands are blocked outright (prevents accidental file deletion)

    },

    "edit": {

      "*": "deny", // Block all file edits by default

      "packages/web/src/content/docs/*.mdx": "allow" // Only allow editing .mdx files in the docs directory

    }

  }

}

Rule matching order: the last matching rule takes priority. It's recommended to put the wildcard "*" rule first as the default, and more specific rules later to override it. This way, more specific rules have higher priority, with clear logic that's less error-prone.


Wildcard rules

Permission patterns support simple wildcard matching, with the following rules:

Wildcard Meaning Example
* Matches zero or more arbitrary characters (not across directories) git * Matches git status、git log --oneline etc.
** Matches any path across directories ~/projects/** Matches all files and subdirectories under projects
? Matches exactly one of any character file?.txt Matches file1.txt、fileA.txt
Other Characters Matches exactly by literal value git status Matches only git status, does not match variants with arguments

Note:For commands with arguments, always add at the end *. For example "grep *" can match grep pattern file.txt, while writing alone "grep" only matches the bare command without any arguments, which rarely hits in actual execution.

Home directory expansion

At the start of a pattern, you can use ~ or $HOME to reference the current user's home directory, and OpenCode will automatically expand it to the full path:

Example

// The following three forms are equivalent, all expanding to /Users/username/projects/* (path varies by system)

"~/projects/*"

"$HOME/projects/*"

"/Users/username/projects/*" // You can also write an absolute path directly, but it's not recommended (not general enough)


External directory permissions (external_directory)

By default, OpenCode only allows tools to accessthe working directory it was launched inand its subdirectories. If you need to access paths outside the project directory (e.g., another project or a shared config directory), you must grant external_directory explicit authorization.

Home directory expansion (~/...) is merely a pathshorthand, and does not automatically authorize access to that path. Paths outside the working directory still require external_directory explicit permission.

1. Allow access to external directories

Example

{

  "$schema": "https://opencode.ai/config.json",

  "permission": {

    "external_directory": {

      "~/projects/personal/**": "allow" // Allow access to all files and subdirectories under ~/projects/personal/

                                           // ** matches subpaths at any level

    }

  }

}

2. Allow reading but forbid editing

Directories authorized by external_directory inherit the current workspace's default permissions. Since read Defaults to "allow", after authorization, files in that directory can be read. If you need to further restrict an operation (e.g., allow reading but forbid editing), you can stack additional rules:

Example

{

  "$schema": "https://opencode.ai/config.json",

  "permission": {

    "external_directory": {

      "~/projects/personal/**": "allow" // Step 1: authorize access to this external directory

    },

    "edit": {

      "~/projects/personal/**": "deny" // Step 2: stack a rule on this directory, allowing reads but forbidding edits

    }

  }

}


All available permission items

OpenCode's permissions are keyed by tool name, covering all types such as file operations, command execution, and network access:

Permission item Operation controlled Pattern match content Default value
read Read file contents File paths (e.g. src/index.js) allow(.env files excluded)
edit All file modifications, covering edit, write, patch, multiedit File path allow
glob File glob search (e.g., finding all .ts file) Glob patterns (e.g. **/*.ts) allow
grep Search for text within file contents Regular expression patterns allow
list List files in a directory Directory paths allow
bash Run shell commands The full resolved command (e.g. git status --porcelain) allow
task Launch subagents Subagent type names allow
skill Load skills Skill names allow
lsp Run LSP language service queries Fine-grained configuration is not currently supported allow
webfetch Fetch network URL content Full URL (e.g. https://example.com/api) allow
websearch Web Search Search query strings allow
codesearch Code Search Search query strings allow
external_directory Access paths outside the working directory External directory paths ask
doom_loop Triggered when the same tool is called 3 times with identical input (prevents the AI from getting stuck in a loop) — ask

Default permission notes

If you haven't configured any permissions, OpenCode uses the following built-in defaults:

Example

// OpenCode's built-in default read rules (no manual config needed, for reference only)

{

  "permission": {

    "read": {

      "*": "allow", // Allow reading all files by default

      "*.env": "deny", // Block reading .env files (prevents leaking database passwords, API keys, etc.)

      ".env.": "deny", // Block reading variants like .env.local, .env.production

      "*.env.example": "allow" // Allow reading .env.example (sample file, no real secrets)

    }

  }

}


Three options in the approval prompt

When an operation's permission is "ask" , OpenCode pops up an approval prompt offering the following three choices:

Option effect Applicable scenarios
once (this time only) Approves only this request; the same operation will prompt again next time Temporarily allow an operation without permanently opening it up
always (always allow) Adds this operation's matching pattern to the whitelist; no more prompts for the rest of the current session You've confirmed a class of operations is safe and want it auto-approved this session
reject (deny) Rejects this request; the operation will not execute You find the operation risky and explicitly don't want it executed

Choice always afterwards, OpenCode has the tool automatically provide a suggested whitelist pattern (e.g., after approving git status , it usually adds git status* to the whitelist).The whitelist is only valid for the current session and resets after restarting OpenCode.


Configure permissions separately for agents

If your workflow uses multiple agents, you can override permission config for each one individually. Agent permissions merge with the global config, andagent rules take priority over global rules。

1. Configure agent permissions in opencode.json

Example

{

  "$schema": "https://opencode.ai/config.json",

  "permission": {

    // Global permissions: git operations allowed, but commit and push forbidden

    "bash": {

      "*": "ask",

      "git *": "allow",

      "git commit *": "deny",

      "git push *": "deny",

      "grep *": "allow"

    }

  },

  "agent": {

    "build": { // An agent named build

      "permission": {

        "bash": {

          "*": "ask",

          "git *": "allow",

          "git commit *": "ask", // Overrides global: the build agent allows commit, but with approval

          "git push *": "deny", // Inherits global: push is still forbidden

          "grep *": "allow"

        }

      }

    }

  }

}

2. Configure agent permissions in Markdown files

Agents can also be configured via Markdown files, with permissions written in the YAML Front Matter at the top of the file (--- section):

Example

File path: ~/.config/opencode/agents/review.md


description

:

Code review agent (only analyzes code, makes no modifications)

mode

:

subagent

permission

:

  edit

:

deny        

Deny all file edits

  bash

:

ask        

bash commands require approval

  webfetch

:

deny    

Deny access to external URLs


Only analyze code and suggest changes.

The following is the agent's system prompt, describing its responsibilities and behavior guidelines


Practical configuration examples

Example 1: Conservative mode (all operations require approval)

Suitable for first-time OpenCode users or when working on important projects; all operations require manual confirmation:

Example

{

  "$schema": "https://opencode.ai/config.json",

  "permission": {

    "*": "ask" // All operations of all tools pop up an approval prompt

  }

}

Example 2: Common development config (read operations open, write operations require approval)

Suitable for daily development: allows the AI to freely read and search code, but confirms when modifying files and executing commands:

Example

{

  "$schema": "https://opencode.ai/config.json",

  "permission": {

    "*": "ask", // Fallback: tools not individually configured require approval by default

    "read": "allow", // Reading files: allowed directly

    "glob": "allow", // File glob search: allowed directly

    "grep": "allow", // Content search: allowed directly

    "list": "allow", // List directories: allowed directly

    "bash": {

      "*": "ask", // bash commands require approval by default

      "git status *": "allow", // View git status: allowed

      "git log *": "allow", // View git log: allowed

      "git diff *": "allow", // View git diff: allowed

      "npm run *": "allow", // Run npm scripts: allowed

      "npm test *": "allow", // Run tests: allowed

      "rm *": "deny" // Delete files: blocked outright

    }

  }

}

Example 3: Allow access to multiple project directories

When the AI needs to work across multiple project directories, authorize external directory access:

Example

{

  "$schema": "https://opencode.ai/config.json",

  "permission": {

    "external_directory": {

      "~/projects/personal/**": "allow", // Allow access to personal project directories

      "~/projects/work/**": "allow", // Allow access to work project directories

      "~/dotfiles/**": "allow" // Allow access to config file directories

    },

    "edit": {

      "~/dotfiles/**": "deny" // Even if access to dotfiles is allowed, editing is forbidden (read-only)

    }

  }

}