翻篇收录说明
翻篇网页快照

Permissions | OpenCode

https://opencode.ai/docs/permissions/

最近抓取:2026-12-02 17:43:34(北京时间)

最近内容变化:2026-11-30 03:15:01(北京时间)

这是最近一次有效抓取的文本快照,不含网页脚本、图片和动态交互内容。实际网页可能已更新。

访问原网页

页面简介

Control what OpenCode agents can do with ordered allow, deny, and ask rules that match actions and resources such as paths and commands.

正文摘录

PermissionsPermissions control whether an agent may perform an action on a resource. Configure A common setup is to ask before running shell commands, allow routine Git inspection, and always block pushes. Add these ordered rules to opencode.jsonc: JSONC{ "$schema": "https://opencode.ai/config.json", "permissions": [ { "action": "shell", "resource": "*", "effect": "ask" }, { "action": "shell", "resource": "git status *", "effect": "allow" }, { "action": "shell", "resource": "git diff *", "effect": "allow" }, { "action": "shell", "resource": "git push *", "effect": "deny" }, ], } The last matching rule wins, so the specific exceptions follow the broad rule. Rules Each rule requires three string fields: FieldMeaningactionTool permission actionresourceValue being used, such as a path, command, URL, query, skill ID, or agent IDeffectallow, deny, or ask JSONC{ "permissions": [ { "action": "read", "resource": "*", "effect": "allow" }, { "action": "read", "resource": "*.env", "effect": "deny" }, ], } EffectResultallowContinue without promptingdenyBlock the operationaskWait for a decision from the client If no rule matches, OpenCode uses ask. Matching Actions and resources use simple whole-value wildcards: PatternMatch*Zero or more characters, including /?Exactly one characterOtherThe literal character JSONC{ "permissions": [ { "action": "edit", "resource": "packages/docs/*.mdx", "effect": "allow" }, ], } This pattern matches the entire normalized path. Backslashes are normalized to slashes, and matching is case-insensitive on Windows. A shell pattern ending in * also matches the command without arguments: JSONC{ "action": "shell", "resource": "git status *", "effect": "allow" } This matches both git status and git status --short. OpenCode combines rules in order and uses the last match. Lower-priority configuration is loaded first, global rules are appended next, and agent rules are appended last. JSONC{ "permissions": [ { "action": "read", "resource": "*", "effect": "allow" }, { "action": "read", "resource": "secrets/*", "effect": "deny" }, ], } Operations may check several resources, such as a patch that touches multiple files. Any deny denies the operation; otherwise any ask asks; otherwise the operation is allowed. Actions V2 action names are strings, so plugins may define more actions. Built-in tools currently use these actions and resources: ActionResourcereadLocation-relative internal path or canonical absolute external patheditTarget path for edit, write, and patchglobRequested glob patterngrepRequested regular expression, not the search pathshellScanner-produced command string; compound commands may produce severalsubagentTarget agent IDskillSkill IDquestion*webfetchRequested URLwebsearchSearch querygenerate_imageSelected image model as providerID/modelID, plus #variant when setexternal_directoryCanonical external directory boundary, normally ending in /*<server>_<tool>* for an MCP tool; both names keep letters, digits, _ and -, and any other character becomes _opencode_list_mcp_resourcesMCP server nameopencode_read_mcp_resource<server>:<uri>execute*; a deny removes Code Mode and lists its tools directly, while nested tools enforce their rules MCP actions keep hyphens, so tools from a server named mcp-atlassian match mcp-atlassian_*, not mcp_atlassian_*. For example, allow one skill and deny all other skills: JSONC{ "permissions": [ { "action": "skill", "resource": "*", "effect": "deny" }, { "action": "skill", "resource": "effect", "effect": "allow" }, ], } doom_loop and lsp are not current V2 Core permission actions. Directories A path outside both the active Location and its non-root project worktree is checked against external_directory before its read or edit rules. The default policy allows external_directory, so external paths are not prompted unless you add a rule. To be asked before any access outside the project: JSONC{ "$schema": "https://opencode.ai/config.json", "permissions": [ { "action": "external_directory", "resource": "*", "effect": "ask" }, ], } Narrower rules after it can allow or deny specific directories: JSONC{ "$schema": "https://opencode.ai/config.json", "permissions": [ { "action": "external_directory", "resource": "~/projects/reference/*", "effect": "allow" }, { "action": "read", "resource": "~/projects/reference/*", "effect": "allow" }, { "action": "edit", "resource": "~/projects/reference/*", "effect": "deny" }, ], } This applies to external paths used by read, edit, write, and patch. Shell checks its external working directory and directories inferred by its scanner before checking shell resources. For external_directory, read, and edit, a leading ~, ~/, $HOME, or $HOME/ is expanded when configuration loads: JSONC{ "action": "read", "resource": "$HOME/reference/*", "effect": "allow" } Shell resources remain raw command text and are not home-expanded. Relative mutation paths may leave the active Location while remaining inside its project worktree. Explicit external paths are canonicalized before matching, so authorize only trusted directory boundaries. Scanner Shell permission checks use OpenCode’s built-in shell scanner across Bash, Zsh, POSIX sh, and PowerShell. Some commands can’t be safely analyzed, such as malformed syntax or dynamic evaluation like eval. If any shell deny rule applies, such a command fails with a scanner error, because its subcommands can’t be checked against the deny rule. Otherwise it needs approval for the whole command as one shell resource and for external_directory *. Only a rule allowing every directory, or the user, can grant the second approval, and neither approval is saved as a pattern. JSONC{ "$schema": "https://opencode.ai/config.json", "policies": [ { "permissions": [ { "action": "shell", "resource": "git status *", "effect": "allow" }, { "action": "shell", "resource": "rm *", "effect": "deny" }, ], }, ], } Permission rules, saved approval patterns, and best-effort directory inference apply to every extracted subcommand and cd target. There is no extra approval mode or blanket restriction for unknown directories. Defaults Every agent, including custom agents, starts with this ordered base policy: JSONC[ { "action": "*", "resource": "*", "effect": "allow" }, { "action": "read", "resource": "*.env", "effect": "ask" }, { "action": "read", "resource": "*.env.*", "effect": "ask" }, { "action": "read", "resource": "*.env.example", "effect": "allow" }, ] Shipped agents append these policies: AgentAdditional policybuildAllows questionsplanAllows questions; denies edits except files under ~/.opencode/plangeneralDenies questions and launching subagentsexploreDenies everything except shell, reads, globs, grep, web fetches, web searches, and external directories; asks for .env readstitleDenies all actionssummaryDenies all actionscompactionKeeps the base policy Because no default rule asks for external_directory, reads and edits outside the Location and worktree proceed without a prompt. Add an external_directory ask or deny rule, as shown in Directories, to restore a prompt or block outside access. Later global and agent rules override these defaults. Agents Put shared rules at the top level and narrower rules under agents.<id>.permissions: JSONC{ "$schema": "https://opencode.ai/config.json", "permissions": [ { "action": "shell", "resource": "*", "effect": "ask" }, { "action": "shell", "resource": "git status *", "effect": "allow" }, ], "agents": { "reviewer": { "description": "Review code without changing it", "mode": "subagent", "permissions": [ { "action": "edit", "resource": "*", "effect": "deny" }, ], }, }, } Agent rules are appended after global rules; they do not replace the global array. A custom subagent uses its own permissions, not a subset of its parent’s permissions. Approvals When a rule resolves to ask, clients can reply with: ChoiceReplyResultAllow onceonceApprove only the pending requestAllow alwaysalwaysApprove it and save the tool’s proposed patterns for the projectRejectrejectReject it and every other pending permission request in that session For example, choosing Allow always for git status --short may save a shell prefix that covers later git status commands: shell: git status * → allow Saved approvals are durable, project-scoped allow rules. They never override a configured deny. Tools choose the proposed saved pattern: some propose *, shell proposes command prefixes, web fetches propose the URL’s site (such as https://opencode.ai/*), and skills and subagents propose their IDs. Review broad approvals and remove those no longer needed. Clients may attach feedback when rejecting. Non-interactive clients must decide how to handle approval requests; configured deny rules always remain enforced. Policies A policy can hard-deny a permission check after these rules and saved approvals run. It turns allow or ask into deny and never grants access. JSONC{ "policies": [{ "action": "tool.use", "resource": "shell:sudo *", "effect": "deny" }], } Global and Console-managed policies override project configuration, which is how an organization blocks a command that a repository would allow.