permissions.mdx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. ---
  2. title: "Permissions"
  3. description: ""
  4. ---
  5. Permissions control whether an agent may perform an action on a resource. V2
  6. configuration uses the `permissions` field and an ordered array of rules.
  7. <Warning>
  8. The V1 object syntax uses different field and action names. Do not use
  9. `permission`, `bash`, or `task` in V2 configuration; use `permissions`,
  10. `shell`, and `subagent`.
  11. </Warning>
  12. ## Rule schema
  13. Each rule has three required string fields:
  14. ```jsonc
  15. {
  16. "$schema": "https://opencode.ai/config.json",
  17. "permissions": [
  18. { "action": "*", "resource": "*", "effect": "ask" },
  19. { "action": "read", "resource": "*", "effect": "allow" },
  20. { "action": "read", "resource": "*.env", "effect": "deny" },
  21. { "action": "shell", "resource": "git status *", "effect": "allow" },
  22. { "action": "shell", "resource": "git push *", "effect": "deny" },
  23. { "action": "edit", "resource": "packages/docs/*.mdx", "effect": "allow" }
  24. ]
  25. }
  26. ```
  27. - `action` matches a tool permission action.
  28. - `resource` matches the value the tool is trying to use, such as a path,
  29. command, URL, query, or agent ID.
  30. - `effect` is `"allow"`, `"deny"`, or `"ask"`.
  31. `allow` proceeds without prompting, `deny` blocks the operation, and `ask`
  32. waits for a user decision. If no rule matches, the result is `ask`.
  33. ## Matching and order
  34. Both `action` and `resource` support simple wildcards:
  35. - `*` matches zero or more characters, including `/`.
  36. - `?` matches exactly one character.
  37. - All other characters are literal.
  38. Matches cover the entire value. Slashes are normalized, and matching is
  39. case-insensitive on Windows. For shell convenience, a pattern ending in
  40. `" *"` also matches the command without arguments: `"git status *"` matches
  41. both `git status` and `git status --short`.
  42. The **last matching rule wins**. Put broad rules first and exceptions later.
  43. Rules from lower-priority configuration files are loaded first. OpenCode then
  44. appends all global rules before agent-specific rules, so a matching agent rule
  45. overrides a global rule.
  46. Some operations check several resources at once, such as a patch touching
  47. multiple files. OpenCode denies the operation if any resource resolves to
  48. `deny`; otherwise it asks if any resolves to `ask`; otherwise it allows it.
  49. ## Actions and resources
  50. V2 action names are strings, so plugins may introduce additional actions. The
  51. current built-in actions use these resources:
  52. | Action | Resource matched |
  53. | --- | --- |
  54. | `read` | Location-relative path for an internal file or directory; canonical absolute path for an external target |
  55. | `edit` | Target path for `edit`, `write`, and `patch`; all three tools share this action |
  56. | `glob` | The requested glob pattern |
  57. | `grep` | The requested regular expression, not the search path |
  58. | `shell` | The complete raw shell command string |
  59. | `subagent` | The target agent ID |
  60. | `skill` | The skill ID |
  61. | `question` | `*` |
  62. | `webfetch` | The requested URL |
  63. | `websearch` | The search query |
  64. | `external_directory` | A canonical external directory boundary, normally ending in `/*` |
  65. | `<server>_<tool>` | `*` for an MCP tool; unsupported characters in both names become `_` |
  66. | `execute` | `*`; controls availability of the Code Mode dispatcher, while each nested tool still enforces its own permission |
  67. Built-in agent policy also reserves `plan_enter` and `plan_exit` for plan-mode
  68. transitions. `doom_loop` and `lsp` are not current V2 Core permission actions.
  69. ## External directories
  70. An external path requires a separate `external_directory` decision before the
  71. tool's own `read` or `edit` decision. This applies to external paths used by
  72. `read`, `edit`, `write`, and `patch`, and to an external `shell` working
  73. directory.
  74. ```jsonc
  75. {
  76. "$schema": "https://opencode.ai/config.json",
  77. "permissions": [
  78. {
  79. "action": "external_directory",
  80. "resource": "~/projects/reference/*",
  81. "effect": "allow"
  82. },
  83. {
  84. "action": "read",
  85. "resource": "~/projects/reference/*",
  86. "effect": "allow"
  87. },
  88. {
  89. "action": "edit",
  90. "resource": "~/projects/reference/*",
  91. "effect": "deny"
  92. }
  93. ]
  94. }
  95. ```
  96. For `external_directory`, `read`, and `edit` resources, a leading `~`, `~/`,
  97. `$HOME`, or `$HOME/` is expanded when configuration loads. Shell resources are
  98. raw command text and are **not** home-expanded.
  99. <Warning>
  100. `shell` runs with the host user's filesystem, process, and network authority.
  101. Its resource is raw text, not a parsed command. External command arguments
  102. produce only best-effort warnings; `external_directory` is enforced for the
  103. working directory, not every path embedded in a command. Prefer a narrow
  104. shell allowlist over patterns intended to identify every dangerous command.
  105. </Warning>
  106. Relative mutation paths cannot escape the active Location, and symlink escapes
  107. from inside it are rejected. Explicit external paths are canonicalized before
  108. matching, so authorize only trusted directory boundaries.
  109. ## Defaults
  110. The evaluator's fallback is `ask`, but shipped agents include ordered defaults:
  111. | Agent | Effective default policy |
  112. | --- | --- |
  113. | `build` | Allows most actions; asks for external directories and `.env` reads; allows questions and entering plan mode; denies exiting plan mode |
  114. | `plan` | Uses the same base, allows questions and exiting plan mode, and denies edits except OpenCode plan files |
  115. | `general` | Uses the base policy but cannot launch another subagent; questions and plan transitions remain denied |
  116. | `explore` | Denies everything except `read`, `glob`, `grep`, `webfetch`, and `websearch`; cannot launch subagents and asks for external directories |
  117. | Hidden maintenance agents | Deny all actions |
  118. The base read rules are ordered as follows:
  119. ```jsonc
  120. [
  121. { "action": "read", "resource": "*", "effect": "allow" },
  122. { "action": "read", "resource": "*.env", "effect": "ask" },
  123. { "action": "read", "resource": "*.env.*", "effect": "ask" },
  124. { "action": "read", "resource": "*.env.example", "effect": "allow" }
  125. ]
  126. ```
  127. OpenCode also permits its managed tool-output and temporary directories where
  128. needed. These exceptions do not grant general external-directory access.
  129. ## Agent overrides
  130. Configure shared policy at the top level and append narrower rules to a named
  131. agent under `agents.<id>.permissions`:
  132. ```jsonc
  133. {
  134. "$schema": "https://opencode.ai/config.json",
  135. "permissions": [
  136. { "action": "shell", "resource": "*", "effect": "ask" },
  137. { "action": "shell", "resource": "git diff *", "effect": "allow" },
  138. { "action": "shell", "resource": "git status *", "effect": "allow" }
  139. ],
  140. "agents": {
  141. "reviewer": {
  142. "description": "Review code without changing it",
  143. "mode": "subagent",
  144. "permissions": [
  145. { "action": "edit", "resource": "*", "effect": "deny" },
  146. { "action": "shell", "resource": "git diff *", "effect": "allow" },
  147. { "action": "shell", "resource": "git status *", "effect": "allow" }
  148. ]
  149. }
  150. }
  151. }
  152. ```
  153. Agent rules do not replace the global array; they are appended after it. A
  154. custom subagent executes with its own permissions, not a permission subset
  155. derived from the parent agent.
  156. ## Approval choices
  157. When an `ask` rule matches, clients can reply with:
  158. - **Allow once** (`once`): approve only the pending request.
  159. - **Allow always** (`always`): approve this request and save the patterns
  160. proposed by the tool for the current project.
  161. - **Reject** (`reject`): reject the request. Rejecting also rejects other
  162. pending permission requests in the same session; clients may attach feedback.
  163. Saved approvals are durable and project-scoped. They are additional `allow`
  164. rules, but they can never override a configured `deny`. The proposed saved
  165. pattern may be broader than the displayed resource: several tools propose `*`,
  166. shell proposes the exact command text, and skills and subagents propose their
  167. IDs. Review the confirmation carefully and remove saved approvals that are no
  168. longer needed.
  169. For non-interactive runs, `opencode2 run --auto` replies `once` to permission
  170. requests. It does not save approvals, and explicit `deny` rules remain enforced.
  171. Without `--auto`, a non-interactive run rejects permission requests.