advanced45 minLesson 5 of 5

Permissions, Security Rules and Multi-Agent Collaboration

Implement permission rules, access control, file restrictions, agent delegation, audit logging, and multi-agent workflows.

Permissions, Security Rules and Multi-Agent Collaboration

Permission Rules in opencode.json

Permissions define what actions agents can perform. They are structured as allow/deny rules applied to specific tools or MCP servers.

json
{ "permissions": [ { "tool": "bash", "allow": ["npm *", "git *", "pip *", "cargo *"], "deny": ["rm -rf *", "sudo *", "chmod *", "> *", "| *"] }, { "tool": "write", "allow": ["src/**", "docs/**", "tests/**"], "deny": [".env", "*.key", "node_modules/**"] } ] }
📌Important

Permission rules are evaluated at runtime for every tool invocation. Deny rules are checked first — if a command matches any deny pattern, it is rejected immediately regardless of whether it also matches an allow pattern. This fail-safe design prevents accidental bypasses.

Permission Evaluation Logic

Understanding the exact evaluation order is critical for writing secure permission rules.

100%
ℹ️Note

The default behavior for tools without permission rules depends on the tool type. Read-only tools (read, grep, glob) are allowed by default. Write and execution tools (bash, write, edit, task) are denied by default unless explicitly permitted.


Allow/Deny Patterns

Patterns support glob-style wildcards for flexible rule matching:

PatternMatchesExample Match
src/**All files and directories under src/src/components/button.tsx
*.envAny .env file at any level/project/.env
**/secrets/*Any file inside a secrets/ directoryconfig/secrets/keys.json
npm *Any command starting with npmnpm install express
git *Any command starting with gitgit push origin main
rm -rf *Recursive force deleterm -rf node_modules
⚠️Warning

Glob patterns are case-sensitive on Linux and case-insensitive on macOS by default. Be careful with file extensions — *.KEY will NOT match secret.key on Linux. Use lowercase patterns for cross-platform compatibility.


Tool Access Control

Each tool can have fine-grained access rules:

json
{ "permissions": [ { "tool": "read", "allow": ["*"], "description": "Read is allowed everywhere" }, { "tool": "edit", "allow": ["src/**/*.ts", "src/**/*.tsx"], "deny": ["src/generated/**"], "requireApproval": true }, { "tool": "bash", "deny": ["curl *", "wget *", "ssh *"], "requireApproval": "always" } ] }
💡Tip

Use requireApproval: true for destructive or sensitive operations. This creates a human-in-the-loop gate that prevents automated agents from performing irreversible actions like deployments, data deletion, or configuration changes without explicit user confirmation.


File Path Restrictions

Path restrictions limit which files agents can access:

json
{ "permissions": [ { "tool": "bash", "allow": [ "/home/user/projects/*", "/tmp/*" ], "deny": [ "/etc/**", "/home/user/.ssh/**", "/home/user/projects/secret-repo/**" ] } ] }
⚠️Warning

File path restrictions only apply when the tool is invoked through OpenCode's tool registry. Direct shell access bypasses these restrictions — always combine with bash command allow/deny rules. An agent with permission to run bash but no path restrictions could access any file by running shell commands directly.


Multi-Agent Workflows

Multi-agent workflows enable complex task decomposition:

100%
💡Tip

In multi-agent workflows, start with a simple orchestrator-plus-specialists pattern. Each specialist should have a narrowly scoped description and constrained permissions. The orchestrator decomposes high-level requests into subtasks and delegates to the appropriate specialist.

json
{ "agents": { "default": { "model": "gpt-4o", "description": "Orchestrator — decomposes tasks and delegates to specialists" }, "code-agent": { "model": "gpt-4o", "description": "Implements feature code following project patterns", "constraints": { "allowedTools": ["read", "write", "edit", "glob", "bash"] } }, "review-agent": { "model": "claude-sonnet-4-20250514", "description": "Reviews code for security, performance, and style issues", "constraints": { "allowedTools": ["read", "grep", "glob"], "deniedTools": ["write", "edit", "bash"] } }, "test-agent": { "model": "gpt-4o", "description": "Writes unit and integration tests", "constraints": { "maxTokens": 4096 } }, "deploy-agent": { "model": "gpt-4o-mini", "description": "Handles deployment pipelines with approval gates", "constraints": { "allowedTools": ["bash", "read", "glob"] } } } }

Agent-to-Agent Delegation

Agents can delegate subtasks to other agents. Delegation respects the target agent's permissions and constraints.

📌Important

When an orchestrator delegates to a specialist, the specialist operates under its own permission scope. This means a specialist can have tighter restrictions than the orchestrator, providing defense in depth. Always design delegation chains so that each agent has the minimum permissions needed for its role.

100%
json
{ "agentRouting": { "mode": "delegation", "delegationRules": [ { "sourceAgent": "default", "targetAgent": "review-agent", "trigger": "after code changes", "conditions": { "filePattern": "src/**/*.ts" } }, { "sourceAgent": "default", "targetAgent": "test-agent", "trigger": "after implementation", "conditions": { "required": true } } ] } }

Audit Logging

Audit logging tracks all agent actions for security and debugging:

json
{ "audit": { "enabled": true, "logPath": ".opencode/audit.log", "events": [ "tool.call", "tool.call.result", "agent.delegation", "permission.denied", "permission.approved" ], "retention": "30d" } }
📌Important

Audit logs are critical for incident response and compliance. If a security breach occurs, the audit log is your primary source of truth for reconstructing what happened. Set appropriate retention periods based on your compliance requirements (SOX, HIPAA, SOC2 typically require 90 days to 7 years).

bash
# Analyze audit logs for security insights # Count denied permissions by tool grep "permission.denied" .opencode/audit.log | \ jq -r '.data.tool' | sort | uniq -c | sort -rn # Find all delegation events with timestamps grep "agent.delegation" .opencode/audit.log | \ jq -r '[.timestamp, .data.source, .data.target] | @tsv' # Track approval gate activity grep "permission.approved\|permission.denied" .opencode/audit.log | \ jq -r '[.timestamp, .event, .data.tool, .data.command] | @tsv'

Comparison: Permission Rule Types

Rule TypeScopeExampleUse Case
Tool allow/denyTool-level"allow": ["npm *"]Safe command restrictions
Path allow/denyFile access"allow": ["src/**"]Restrict file modifications
MCP server ruleServer-level"mcpServer": "github"External service access control
Require approvalAction-level"requireApproval": trueSensitive operations gate
Agent constraintAgent-level"deniedTools": ["bash"]Per-agent capability limits
Subagent scopeInheritanceInherited from parent by defaultHierarchical permission boundaries
Audit event filterLogging-level"events": ["tool.call", "permission.denied"]Selective audit log capture
💡Tip
| Follow the principle of least privilege: start with no permissions and grant only what each agent needs. Use agent-level constraints for broad capability limits, tool-level allow/deny for specific command control, and MCP server rules for external service access. Layer these for defense in depth.

Practice Questions

Practice Question

A security engineer is writing a permission rule. What three components must every permission rule specify?

Practice Question

What is the practical difference between a tool-level `deny: ['rm -rf *']` rule and an agent-level `deniedTools: ['bash']` constraint?

Practice Question

In a multi-agent workflow with an orchestrator and specialist agents, how does the orchestrator decide which agent should handle a subtask?

Practice Question

A security team wants to audit every tool invocation, delegation, and permission decision in OpenCode. Which set of audit events should they enable?

Practice Question

An administrator configured file path restrictions to block access to `/etc/` but did not add any bash allow/deny rules. Why is this configuration incomplete?


**Key Takeaways**
  • Permission rules use allow/deny patterns with glob-style wildcards for flexible access control
  • Deny rules are evaluated first and take precedence over allow rules
  • Tool-level rules control what commands and file operations agents can execute
  • File path restrictions must be combined with bash command rules to prevent bypasses
  • Multi-agent workflows decompose complex tasks through orchestrator-to-specialist delegation
  • Agent-to-agent delegation respects each target agent's independent permission scope
  • Audit logging captures tool calls, delegations, and permission events for security review
  • Approval gates add a human-in-the-loop for sensitive operations like deployments
  • The permission evaluation logic follows a structured flow: tool check, deny check, allow check, approval gate
  • Principle of least privilege: start with no permissions and grant only what each agent needs
Progress100%