permissions.mdx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. ## Configuration
  15. You can set permissions globally (with `*`), and override specific tools.
  16. ```json title="opencode.json"
  17. {
  18. "$schema": "https://opencode.ai/config.json",
  19. "permission": {
  20. "*": "ask",
  21. "bash": "allow",
  22. "edit": "deny"
  23. }
  24. }
  25. ```
  26. You can also set all permissions at once:
  27. ```json title="opencode.json"
  28. {
  29. "$schema": "https://opencode.ai/config.json",
  30. "permission": "allow"
  31. }
  32. ```
  33. ---
  34. ## Granular Rules (Object Syntax)
  35. For most permissions, you can use an object to apply different actions based on the tool input.
  36. ```json title="opencode.json"
  37. {
  38. "$schema": "https://opencode.ai/config.json",
  39. "permission": {
  40. "bash": {
  41. "*": "ask",
  42. "git *": "allow",
  43. "npm *": "allow",
  44. "rm *": "deny",
  45. "grep *": "allow"
  46. },
  47. "edit": {
  48. "*": "deny",
  49. "packages/web/src/content/docs/*.mdx": "allow"
  50. }
  51. }
  52. }
  53. ```
  54. 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.
  55. ### Wildcards
  56. Permission patterns use simple wildcard matching:
  57. - `*` matches zero or more of any character
  58. - `?` matches exactly one character
  59. - All other characters match literally
  60. ---
  61. ## Available Permissions
  62. OpenCode permissions are keyed by tool name, plus a couple of safety guards:
  63. - `read` — reading a file (matches the file path)
  64. - `edit` — all file modifications (covers `edit`, `write`, `patch`, `multiedit`)
  65. - `glob` — file globbing (matches the glob pattern)
  66. - `grep` — content search (matches the regex pattern)
  67. - `list` — listing files in a directory (matches the directory path)
  68. - `bash` — running shell commands (matches parsed commands like `git status --porcelain`)
  69. - `task` — launching subagents (matches the subagent type)
  70. - `skill` — loading a skill (matches the skill name)
  71. - `lsp` — running LSP queries (currently non-granular)
  72. - `todoread`, `todowrite` — reading/updating the todo list
  73. - `webfetch` — fetching a URL (matches the URL)
  74. - `websearch`, `codesearch` — web/code search (matches the query)
  75. - `external_directory` — triggered when a tool touches paths outside the project working directory
  76. - `doom_loop` — triggered when the same tool call repeats 3 times with identical input
  77. ---
  78. ## Defaults
  79. If you don’t specify anything, OpenCode starts from permissive defaults:
  80. - Most permissions default to `"allow"`.
  81. - `doom_loop` and `external_directory` default to `"ask"`.
  82. - `read` is `"allow"`, but `.env` files are denied by default:
  83. ```json title="opencode.json"
  84. {
  85. "permission": {
  86. "read": {
  87. "*": "allow",
  88. "*.env": "deny",
  89. "*.env.*": "deny",
  90. "*.env.example": "allow"
  91. }
  92. }
  93. }
  94. ```
  95. ---
  96. ## What “Ask” Does
  97. When OpenCode prompts for approval, the UI offers three outcomes:
  98. - `once` — approve just this request
  99. - `always` — approve future requests matching the suggested patterns (for the rest of the current OpenCode session)
  100. - `reject` — deny the request
  101. 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*`).
  102. ---
  103. ## Agents
  104. 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.
  105. :::note
  106. Refer to the [Granular Rules (Object Syntax)](#granular-rules-object-syntax) section above for more detailed pattern matching examples.
  107. :::
  108. ```json title="opencode.json"
  109. {
  110. "$schema": "https://opencode.ai/config.json",
  111. "permission": {
  112. "bash": {
  113. "*": "ask",
  114. "git *": "allow",
  115. "git commit *": "deny",
  116. "git push *": "deny",
  117. "grep *": "allow"
  118. }
  119. },
  120. "agent": {
  121. "build": {
  122. "permission": {
  123. "bash": {
  124. "*": "ask",
  125. "git *": "allow",
  126. "git commit *": "ask",
  127. "git push *": "deny",
  128. "grep *": "allow"
  129. }
  130. }
  131. }
  132. }
  133. }
  134. ```
  135. You can also configure agent permissions in Markdown:
  136. ```markdown title="~/.config/opencode/agent/review.md"
  137. ---
  138. description: Code review without edits
  139. mode: subagent
  140. permission:
  141. edit: deny
  142. bash: ask
  143. webfetch: deny
  144. ---
  145. Only analyze code and suggest changes.
  146. ```
  147. :::tip
  148. 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.
  149. :::