permissions.mdx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. ---
  2. title: Permissions
  3. description: Control which actions require approval to run.
  4. ---
  5. OpenCode uses the `permission` config to decide whether a given action should run automatically, prompt you, or be blocked.
  6. As of `v1.1.1`, the legacy `tools` boolean config is deprecated and has been merged into `permission`. The old `tools` config is still supported for backwards compatibility.
  7. ---
  8. ## Actions
  9. Each permission rule resolves to one of:
  10. - `"allow"` — run without approval
  11. - `"ask"` — prompt for approval
  12. - `"deny"` — block the action
  13. ---
  14. ## Auto mode
  15. Start OpenCode with `--auto` to automatically approve permission requests that are not explicitly denied.
  16. ```bash
  17. opencode --auto
  18. ```
  19. You can also use auto mode with [`opencode run`](/docs/cli#run).
  20. ```bash
  21. opencode run --auto "Refactor this module"
  22. ```
  23. Explicit `"deny"` rules are still enforced. Auto mode only changes requests that would otherwise ask for approval.
  24. In the TUI, open the command palette and select **Enable auto-approve permissions** or **Disable auto-approve permissions** to change modes. When auto mode is active, the prompt displays a muted `auto` indicator next to the current agent.
  25. ---
  26. ## Configuration
  27. You can set permissions globally (with `*`), and override specific tools.
  28. ```json title="opencode.json"
  29. {
  30. "$schema": "https://opencode.ai/config.json",
  31. "permission": {
  32. "*": "ask",
  33. "bash": "allow",
  34. "edit": "deny"
  35. }
  36. }
  37. ```
  38. You can also set all permissions at once:
  39. ```json title="opencode.json"
  40. {
  41. "$schema": "https://opencode.ai/config.json",
  42. "permission": "allow"
  43. }
  44. ```
  45. ---
  46. ## Granular Rules (Object Syntax)
  47. For most permissions, you can use an object to apply different actions based on the tool input.
  48. ```json title="opencode.json"
  49. {
  50. "$schema": "https://opencode.ai/config.json",
  51. "permission": {
  52. "bash": {
  53. "*": "ask",
  54. "git *": "allow",
  55. "npm *": "allow",
  56. "rm *": "deny",
  57. "grep *": "allow"
  58. },
  59. "edit": {
  60. "*": "deny",
  61. "packages/web/src/content/docs/*.mdx": "allow"
  62. }
  63. }
  64. }
  65. ```
  66. Rules are evaluated by pattern match, with the **last matching rule winning**. A common pattern is to put the catch-all `"*"` rule first, and more specific rules after it.
  67. ### Wildcards
  68. Permission patterns use simple wildcard matching:
  69. - `*` matches zero or more of any character
  70. - `?` matches exactly one character
  71. - All other characters match literally
  72. ### Home Directory Expansion
  73. You can use `~` or `$HOME` at the start of a pattern to reference your home directory. This is particularly useful for [`external_directory`](#external-directories) rules.
  74. - `~/projects/*` -> `/Users/username/projects/*`
  75. - `$HOME/projects/*` -> `/Users/username/projects/*`
  76. - `~` -> `/Users/username`
  77. ### External Directories
  78. Use `external_directory` to allow tool calls that touch paths outside the working directory where OpenCode was started. This applies to any tool that takes a path as input (for example `read`, `edit`, `glob`, `grep`, and many `bash` commands).
  79. Home expansion (like `~/...`) only affects how a pattern is written. It does not make an external path part of the current workspace, so paths outside the working directory must still be allowed via `external_directory`.
  80. For example, this allows access to everything under `~/projects/personal/`:
  81. ```json title="opencode.json"
  82. {
  83. "$schema": "https://opencode.ai/config.json",
  84. "permission": {
  85. "external_directory": {
  86. "~/projects/personal/**": "allow"
  87. }
  88. }
  89. }
  90. ```
  91. Any directory allowed here inherits the same defaults as the current workspace. Since [`read` defaults to `allow`](#defaults), reads are also allowed for entries under `external_directory` unless overridden. Add explicit rules when a tool should be restricted in these paths, such as blocking edits while keeping reads:
  92. ```json title="opencode.json"
  93. {
  94. "$schema": "https://opencode.ai/config.json",
  95. "permission": {
  96. "external_directory": {
  97. "~/projects/personal/**": "allow"
  98. },
  99. "edit": {
  100. "~/projects/personal/**": "deny"
  101. }
  102. }
  103. }
  104. ```
  105. Keep the list focused on trusted paths, and layer extra allow or deny rules as needed for other tools (for example `bash`).
  106. ---
  107. ## Available Permissions
  108. OpenCode permissions are keyed by tool name, plus a couple of safety guards:
  109. - `read` — reading a file (matches the file path)
  110. - `edit` — all file modifications (covers `edit`, `write`, `patch`)
  111. - `glob` — file globbing (matches the glob pattern)
  112. - `grep` — content search (matches the regex pattern)
  113. - `bash` — running shell commands (matches parsed commands like `git status --porcelain`)
  114. - `task` — launching subagents (matches the subagent type)
  115. - `skill` — loading a skill (matches the skill name)
  116. - `lsp` — running LSP queries (currently non-granular)
  117. - `question` — asking the user questions during execution
  118. - `webfetch` — fetching a URL (matches the URL)
  119. - `websearch` — web search (matches the query)
  120. - `external_directory` — triggered when a tool touches paths outside the project working directory
  121. - `doom_loop` — triggered when the same tool call repeats 3 times with identical input
  122. ---
  123. ## Defaults
  124. If you don’t specify anything, OpenCode starts from permissive defaults:
  125. - Most permissions default to `"allow"`.
  126. - `doom_loop` and `external_directory` default to `"ask"`.
  127. - `read` is `"allow"`, but `.env` files are denied by default:
  128. ```json title="opencode.json"
  129. {
  130. "permission": {
  131. "read": {
  132. "*": "allow",
  133. "*.env": "deny",
  134. "*.env.*": "deny",
  135. "*.env.example": "allow"
  136. }
  137. }
  138. }
  139. ```
  140. ---
  141. ## What “Ask” Does
  142. When OpenCode prompts for approval, the UI offers three outcomes:
  143. - `once` — approve just this request
  144. - `always` — approve future requests matching the suggested patterns (for the rest of the current OpenCode session)
  145. - `reject` — deny the request
  146. The set of patterns that `always` would approve is provided by the tool (for example, bash approvals typically whitelist a safe command prefix like `git status*`).
  147. ---
  148. ## Agents
  149. You can override permissions per agent. Agent permissions are merged with the global config, and agent rules take precedence. [Learn more](/docs/agents#permissions) about agent permissions.
  150. :::note
  151. Refer to the [Granular Rules (Object Syntax)](#granular-rules-object-syntax) section above for more detailed pattern matching examples.
  152. :::
  153. ```json title="opencode.json"
  154. {
  155. "$schema": "https://opencode.ai/config.json",
  156. "permission": {
  157. "bash": {
  158. "*": "ask",
  159. "git *": "allow",
  160. "git commit *": "deny",
  161. "git push *": "deny",
  162. "grep *": "allow"
  163. }
  164. },
  165. "agent": {
  166. "build": {
  167. "permission": {
  168. "bash": {
  169. "*": "ask",
  170. "git *": "allow",
  171. "git commit *": "ask",
  172. "git push *": "deny",
  173. "grep *": "allow"
  174. }
  175. }
  176. }
  177. }
  178. }
  179. ```
  180. You can also configure agent permissions in Markdown:
  181. ```markdown title="~/.config/opencode/agents/review.md"
  182. ---
  183. description: Code review without edits
  184. mode: subagent
  185. permission:
  186. edit: deny
  187. bash: ask
  188. webfetch: deny
  189. ---
  190. Only analyze code and suggest changes.
  191. ```
  192. :::tip
  193. Use pattern matching for commands with arguments. `"grep *"` allows `grep pattern file.txt`, while `"grep"` alone would block it. Commands like `git status` work for default behavior but require explicit permission (like `"git status *"`) when arguments are passed.
  194. :::