permission

package
v0.98.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 21, 2026 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package permission evaluates tool invocation permissions with OpenCode-compatible rules.

Index

Constants

View Source
const (
	ToolRunTerminal         = "run_terminal"
	ToolListDir             = "list_dir"
	ToolGlobFiles           = "glob_files"
	ToolGrepFiles           = "grep_files"
	ToolReadFile            = "read_file"
	ToolWriteFile           = "write_file"
	ToolApplyPatch          = "apply_patch"
	ToolWebSearch           = "web_search"
	ToolWebFetch            = "web_fetch"
	ToolRunCode             = "run_code"
	ToolReadSkill           = "read_skill"
	ToolDelegateSubagent    = "delegate_subagent"
	ToolScheduleTask        = "schedule_task"
	ToolUpdateScheduledTask = "update_scheduled_task"
	ToolListScheduledTasks  = "list_scheduled_tasks"
	ToolCancelScheduledTask = "cancel_scheduled_task"
	ToolUpdateMemory        = "update_memory"
	ToolTodoWrite           = "todo_write"
	ToolListTodos           = "list_todos"
)

Tool names used by the chat agent coding toolkit.

View Source
const FormActionInherit = "inherit"

FormActionInherit means the user keeps the system default for one permission key.

View Source
const KeyDelegate = "delegate"

KeyDelegate is the permission key for subagent delegation via the delegate_subagent tool.

View Source
const KeyDoomLoop = "doom_loop"

KeyDoomLoop is the permission key for repeated identical tool calls.

View Source
const KeyExternalDirectory = "external_directory"

KeyExternalDirectory is the permission key for workspace-external path access.

View Source
const KeyMemory = "memory"

KeyMemory is the permission key for the update_memory tool.

View Source
const KeySchedule = "schedule"

KeySchedule is the permission key for scheduled task write operations.

View Source
const KeyScheduleRead = "schedule_read"

KeyScheduleRead is the permission key for listing scheduled tasks.

View Source
const KeyTodo = "todo"

KeyTodo is the permission key for session todo checklist tools.

View Source
const KeyWildcard = "*"

KeyWildcard applies to all permission keys when no specific rule matches.

Variables

This section is empty.

Functions

func IsOverlyBroadPattern

func IsOverlyBroadPattern(pattern string) bool

IsOverlyBroadPattern reports patterns that would grant unrestricted access.

func MatchGlob

func MatchGlob(pattern, value string) bool

MatchGlob reports whether value matches pattern using * and ? wildcards.

func ParentDirPattern

func ParentDirPattern(path string) string

ParentDirPattern returns a directory wildcard pattern for file paths.

func PermissionKeyForTool

func PermissionKeyForTool(tool string) string

PermissionKeyForTool maps a tool name to its OpenCode permission key.

func SuggestedPattern

func SuggestedPattern(key, primary string, bash ParseBashCommand) (string, bool)

SuggestedPattern builds a safe always-allow pattern for one permission decision. The second return value is false when always should not be offered.

func ValidateUserConfig added in v0.94.0

func ValidateUserConfig(cfg Config) error

ValidateUserConfig rejects overly broad or unsafe user permission overrides.

Types

type Action

type Action string

Action is the permission decision for one tool invocation.

const (
	// ActionAllow runs the tool without prompting.
	ActionAllow Action = "allow"
	// ActionAsk prompts the user before running the tool.
	ActionAsk Action = "ask"
	// ActionDeny blocks the tool from running.
	ActionDeny Action = "deny"
)

func ParseAction

func ParseAction(raw string) (Action, bool)

ParseAction converts raw text into an Action when valid.

func Stricter

func Stricter(a, b Action) Action

Stricter returns the more restrictive of two actions (deny > ask > allow).

func (Action) Valid

func (a Action) Valid() bool

Valid reports whether a is a known permission action.

type Config

type Config map[string]RuleSet

Config maps permission keys to rule sets (OpenCode-compatible shape).

func BuildUserConfigFromForm added in v0.95.0

func BuildUserConfigFromForm(defaults Config, form FormValues) (Config, map[string]string, error)

BuildUserConfigFromForm builds a user override config from form values and defaults. Matching defaults are omitted. Field errors map form keys to messages.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns OpenCode-style baseline rules used when the user has no overrides.

func EffectiveConfig

func EffectiveConfig(user Config) Config

EffectiveConfig merges defaults with user overrides for API responses.

func Merge

func Merge(base, overlay Config) Config

Merge overlays user rules on top of defaults; user keys replace defaults entirely.

func ParseConfig

func ParseConfig(raw []byte) (Config, error)

ParseConfig unmarshals permission JSON (string or pattern map per key).

func ScheduledRunOverlay added in v0.94.0

func ScheduledRunOverlay() Config

ScheduledRunOverlay returns stricter permission rules for autonomous scheduled task runs.

func (Config) MarshalJSON

func (c Config) MarshalJSON() ([]byte, error)

MarshalJSON serializes the config for API responses.

type Evaluator

type Evaluator struct {
	// contains filtered or unexported fields
}

Evaluator resolves tool permissions from merged config and session state.

func NewEvaluator

func NewEvaluator(config Config) *Evaluator

NewEvaluator builds an evaluator from merged permission config.

func (*Evaluator) Config

func (e *Evaluator) Config() Config

Config returns the evaluator's rule set.

func (*Evaluator) Evaluate

func (e *Evaluator) Evaluate(req Request, session *SessionState) Result

Evaluate resolves the action for one tool invocation.

type ExtractedInputs

type ExtractedInputs struct {
	PermissionKey string
	Primary       string
	Bash          ParseBashCommand
	ExternalPaths []string
}

ExtractedInputs holds match strings derived from a tool call.

func ExtractInputs

func ExtractInputs(req Request) ExtractedInputs

ExtractInputs derives permission match inputs from a tool call.

type FormPatternRow added in v0.95.0

type FormPatternRow struct {
	Pattern string
	Action  string
}

FormPatternRow is one pattern rule submitted from the permissions web form.

type FormValues added in v0.95.0

type FormValues struct {
	Simple   map[string]string
	Patterns map[string][]FormPatternRow
}

FormValues holds parsed permission form submissions.

func ParseFormPostArgs added in v0.95.0

func ParseFormPostArgs(args map[string]string) FormValues

ParseFormPostArgs converts flat HTML form keys into structured permission form values.

type ParseBashCommand

type ParseBashCommand struct {
	// Prefix is the arity-derived command prefix used for rule matching.
	Prefix string
	// HasChain is true when pipes or command connectors are present.
	HasChain bool
	// Complex is true when parsing could not produce a reliable prefix.
	Complex bool
}

ParseBashCommand analyzes a shell command for permission matching.

func AnalyzeBashCommand

func AnalyzeBashCommand(command string) ParseBashCommand

AnalyzeBashCommand tokenizes command and extracts the permission prefix.

type PatternRule

type PatternRule struct {
	Pattern string
	Action  Action
}

PatternRule binds one glob pattern to an action; last match wins during evaluation.

type Request

type Request struct {
	Tool          string
	Args          map[string]any
	WorkspaceRoot string
	ExternalPath  bool
}

Request is the input for permission evaluation.

type Result

type Result struct {
	Action            Action
	PermissionKey     string
	Pattern           string
	SuggestedPattern  string
	SuggestAlways     bool
	DoomLoopTriggered bool
	ExternalChecked   bool
}

Result is the outcome of one permission evaluation.

type RuleSet

type RuleSet struct {
	Default  Action
	Patterns []PatternRule
}

RuleSet is either one global action or an ordered list of pattern rules.

type SessionState

type SessionState struct {
	// contains filtered or unexported fields
}

SessionState holds per-session grants and doom-loop counters across runs.

func NewSessionState

func NewSessionState() *SessionState

NewSessionState creates empty session permission state.

func (*SessionState) AddGrant

func (s *SessionState) AddGrant(key, pattern string) error

AddGrant records an always-allow pattern for one permission key.

func (*SessionState) Clear

func (s *SessionState) Clear()

Clear removes all session grants and doom-loop counters.

func (*SessionState) Grants

func (s *SessionState) Grants() map[string][]string

Grants returns a copy of always-allow patterns grouped by permission key.

func (*SessionState) MatchesGrant

func (s *SessionState) MatchesGrant(key, input string) bool

MatchesGrant reports whether input is covered by a session always grant.

func (*SessionState) RecordDoomLoop

func (s *SessionState) RecordDoomLoop(tool string, args map[string]any) (int, bool)

RecordDoomLoop increments the counter for one tool invocation fingerprint. It returns the new count and whether the doom-loop threshold was reached.

func (*SessionState) RestoreGrants added in v0.94.0

func (s *SessionState) RestoreGrants(grants map[string][]string)

RestoreGrants replaces always-allow patterns from persisted storage.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL