permissions.mdx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. ---
  2. title: Permissions
  3. description: Control what AI agents can do in your codebase.
  4. ---
  5. The opencode permissions system provides granular control over what actions AI agents can perform in your codebase. It allows you to configure explicit approval requirements for sensitive operations like file editing, bash commands, and more.
  6. ## How it works
  7. The permissions system works by intercepting tool calls and checking if user approval is required before executing potentially sensitive operations. When a tool requests permission, it creates a permission request that must be approved by the user.
  8. ```typescript
  9. // Example of how a tool requests permission
  10. await Permission.ask({
  11. type: "edit",
  12. sessionID: ctx.sessionID,
  13. messageID: ctx.messageID,
  14. callID: ctx.callID,
  15. title: "Edit this file: " + filePath,
  16. metadata: {
  17. filePath,
  18. diff,
  19. },
  20. })
  21. ```
  22. When a permission is requested, the system checks the configuration to determine if approval is needed. If approval is required, the user is prompted to allow or deny the action.
  23. ## Configuration
  24. Permissions are configured in your `opencode.json` file under the `permission` key. Here are the available options:
  25. ### permission.edit
  26. Controls whether file editing operations require user approval.
  27. ```json title="opencode.json"
  28. {
  29. "permission": {
  30. "edit": "ask"
  31. }
  32. }
  33. ```
  34. - `"ask"` - Prompt user for approval before editing files
  35. - `"allow"` - Allow all file editing operations without approval
  36. ### permission.bash
  37. Controls whether bash commands require user approval. This can be configured globally or with specific patterns.
  38. ```json title="opencode.json"
  39. {
  40. "permission": {
  41. "bash": "ask"
  42. }
  43. }
  44. ```
  45. Or with specific patterns:
  46. ```json title="opencode.json"
  47. {
  48. "permission": {
  49. "bash": {
  50. "git *": "allow",
  51. "npm install": "ask",
  52. "*": "ask"
  53. }
  54. }
  55. }
  56. ```
  57. ## Configuration examples
  58. ### Basic permission configuration
  59. ```json title="opencode.json"
  60. {
  61. "$schema": "https://opencode.ai/config.json",
  62. "permission": {
  63. "edit": "ask",
  64. "bash": "ask"
  65. }
  66. }
  67. ```
  68. ### Advanced bash permission configuration
  69. ```json title="opencode.json"
  70. {
  71. "$schema": "https://opencode.ai/config.json",
  72. "permission": {
  73. "edit": "ask",
  74. "bash": {
  75. "git status": "allow",
  76. "git diff": "allow",
  77. "git add *": "ask",
  78. "git commit*": "ask",
  79. "npm install": "ask",
  80. "npm run build": "allow",
  81. "ls": "allow",
  82. "pwd": "allow",
  83. "*": "ask"
  84. }
  85. }
  86. }
  87. ```
  88. ### Permissive configuration (development only)
  89. ```json title="opencode.json"
  90. {
  91. "$schema": "https://opencode.ai/config.json",
  92. "permission": {
  93. "edit": "allow",
  94. "bash": "allow"
  95. }
  96. }
  97. ```
  98. ### Strict configuration
  99. ```json title="opencode.json"
  100. {
  101. "$schema": "https://opencode.ai/config.json",
  102. "permission": {
  103. "edit": "ask",
  104. "bash": {
  105. "*": "ask"
  106. }
  107. }
  108. }
  109. ```
  110. ## Best practices
  111. 1. **Start with "ask"**: Begin with asking for permissions and adjust based on your workflow
  112. 2. **Use patterns wisely**: Create specific patterns for commands you trust
  113. 3. **Review regularly**: Periodically review your permission settings
  114. 4. **Be specific**: Use specific patterns rather than broad wildcards when possible
  115. 5. **Document exceptions**: Comment your configuration to explain why certain permissions are set
  116. This permissions system ensures that you maintain control over what AI agents can do in your codebase while providing flexibility for trusted operations.