permissions

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 18 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package permissions is the safety layer of the local/coding profile: rules (allow / ask / deny) and modes decide, before a tool call runs, whether it is executed, needs the user's approval or is refused. It plugs into the agent as a BeforeToolHook (approvals reuse the AG-UI interrupt and resume flow) and knows how to read shell commands well enough to judge every simple command of a line.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeniedMessage

func DeniedMessage(d Decision) string

DeniedMessage is the tool result of a refused call.

func ToolClass

func ToolClass(tool string) string

ToolClass is "command" for execute_command, "edit" for write_file/edit_file and "other" for every other tool.

Types

type Action

type Action int

Action is a decision.

const (
	Allow Action = iota
	Ask
	Deny
)

func (Action) String

func (a Action) String() string

type Decision

type Decision struct {
	Action Action
	// Reason says why, for the user and the model.
	Reason string
	// Rule is the user rule that decided, if any.
	Rule string
	// Hard decisions (built-in catastrophic denials) cannot be allowed by a rule.
	Hard bool
	// Once: approving the call approves the tool for the rest of the run.
	Once bool
	// ReadOnly: the call was judged read-only (what readonly mode allows).
	ReadOnly bool
}

Decision is the outcome for a tool call.

func (Decision) String

func (d Decision) String() string

type File

type File struct {
	Mode  string   `yaml:"mode"`
	Allow []string `yaml:"allow"`
	Ask   []string `yaml:"ask"`
	Deny  []string `yaml:"deny"`
}

File is the YAML form of a permissions file (.har/permissions.yml).

type Hook

type Hook struct {
	Policy *Policy
	Inner  *approvals.Gate
}

Hook is the agent's BeforeToolHook for the local/coding profile. Calls the policy allows run at once; calls it asks about pause the run with a normal approval interrupt (resumed by the approval decision); calls it denies are refused with a clear tool error (it implements agent.ToolDenier). Tools the policy has no opinion on go to Inner, the interactive-mode approval gate.

func (*Hook) Denial

func (h *Hook) Denial(ctx context.Context, run *store.Run, toolName, arguments string) string

Denial implements agent.ToolDenier: a non-empty text refuses the call.

func (*Hook) NeedsApproval

func (h *Hook) NeedsApproval(ctx context.Context, run *store.Run, toolName, arguments string) bool

NeedsApproval implements agent.BeforeToolHook.

func (*Hook) Pause

func (h *Hook) Pause(ctx context.Context, run *store.Run, tc ai.ToolCall) (*tools.Interrupt, error)

Pause implements agent.BeforeToolHook: it records the approval like the interactive gate does.

func (*Hook) Risky

func (h *Hook) Risky(ctx context.Context, run *store.Run, toolName, arguments string) bool

Risky implements agent.RiskJudge: whether the call would be asked, whatever the run's mode, so calls behind a pending approval keep waiting.

type Mode

type Mode string

Mode is how strictly calls are gated (AGENT_PERMISSIONS).

const (
	// ModeAsk applies the rules and defaults: what is not known safe is asked.
	ModeAsk Mode = "ask"
	// ModeAutoEdit is ask, but edits are free everywhere (still not .git internals or secrets).
	ModeAutoEdit Mode = "auto-edit"
	// ModeYolo never asks; deny rules still apply.
	ModeYolo Mode = "yolo"
	// ModeYoloUnsafe never asks and ignores deny rules too.
	ModeYoloUnsafe Mode = "yolo-unsafe"
	// ModeReadOnly refuses edits and every command that is not read-only.
	ModeReadOnly Mode = "readonly"
)

func ParseMode

func ParseMode(s string) (Mode, error)

ParseMode reads a mode name ("" is ask).

type Options

type Options struct {
	// Workspace is the host directory the agent works in (its /workspace).
	Workspace string
	// Mode is AGENT_PERMISSIONS ("" lets the files decide, then ask).
	Mode string
	// Allow, Ask and Deny are extra rules from the environment
	// (AGENT_PERMISSIONS_ALLOW|ASK|DENY: entries separated by newlines or ";;").
	Allow, Ask, Deny string
	// AskTools are globs on tool names that are asked about, once per run, unless a rule says otherwise
	// (the tools of MCP servers that start local programs: "name__*").
	AskTools []string
	// Files are read in order when they exist; earlier files have priority for
	// the mode. Default: <workspace>/.har/permissions.yml, then
	// ~/.har/permissions.yml.
	Files []string
	// Home is the user's home directory (default os.UserHomeDir).
	Home string
	// Trusted says the project is trusted (config.TrustFile): its permissions file may then add allow rules
	// and loosen the mode. Otherwise only the default workspace file is affected: its allow rules are ignored
	// and its mode only counts when it is as strict as the one already in force. Files given explicitly in
	// Files are the caller's own and are always honoured.
	Trusted bool
}

Options builds a Policy.

type Parsed

type Parsed struct {
	// Commands are all simple commands, including those inside $(...), backticks,
	// subshells, bash -c strings and pipelines.
	Commands []Simple
	// Pipelines are the texts of the multi-command pipelines ("curl x | sh"),
	// by Pipeline index of their commands (single commands have none).
	Pipelines map[int]string
	// Incomplete: the line could not be parsed with confidence (unbalanced quote,
	// too deep nesting). Callers must treat it as unknown.
	Incomplete bool
}

Parsed is the result of splitting a shell line.

func Parse

func Parse(line string) Parsed

Parse splits a shell command line into the simple commands it would run. It is a conservative approximation of bash, enough to judge each command: it understands quotes, escapes, ; && || | & newlines, subshells and groups, $(...) and backticks (also inside double quotes), heredocs, redirections, env prefixes and the wrappers sudo, env, nohup, time, nice, timeout, xargs, command, exec, eval and `sh -c '…'`.

type Policy

type Policy struct {
	Mode      Mode
	Rules     []Rule
	Workspace string // host directory of /workspace ("" if unknown)
	Home      string
	Files     []string // permission files that were read
	// Ignored says what an untrusted project's permissions file asked for and was not honoured.
	Ignored []string
	// AskTools: tools (name globs) that ask, once per run, when no rule matches them.
	AskTools []string
}

Policy decides tool calls.

func Load

func Load(o Options) (*Policy, error)

Load builds the policy: the environment's mode and rules, then the workspace file, then the user's file. Rules of all sources are merged (a deny anywhere wins over an ask, an ask over an allow); the mode comes from the first source that sets one.

func (*Policy) Decide

func (p *Policy) Decide(tool, arguments string) (d Decision, judged bool)

Decide judges one tool call. judged is false for a tool the policy has no opinion on (no rule names it): the caller applies its own gate to it.

type Redirect

type Redirect struct {
	Op     string
	Target string
}

Redirect is a file redirection of a command (> >> >|, &>); input redirections and descriptor duplications (2>&1) are not kept.

type Rule

type Rule struct {
	Action  Action
	Kind    string
	Tool    string // kindTool: glob on the tool name
	Pattern string
	Source  string // where it came from, for messages
}

Rule is one allow, ask or deny rule.

func ParseFile

func ParseFile(data []byte, source string) (mode string, rules []Rule, err error)

ParseFile reads a permissions file.

func ParseRule

func ParseRule(a Action, entry, source string) (Rule, error)

ParseRule reads a rule entry of the config file:

execute_command(git push*)   Bash(git push*) and a bare "git push*" mean the same
write_file(**/.env)          Edit(...), Write(...), edit_file(...): paths of write_file and edit_file
write_file                   the whole tool (also execute_command / Bash)
mcp__github__create*(*)      any other tool: its name, and a glob on its JSON arguments

func (Rule) String

func (r Rule) String() string

type Simple

type Simple struct {
	// Words are the program and its arguments, after env prefixes and wrappers
	// (sudo, env, nohup, time, timeout, xargs, bash -c …) are removed.
	Words []string
	// Raw is the original text of the words (before wrappers were removed).
	Raw string
	// Env are the leading NAME=value assignments.
	Env []string
	// Elevated: run through sudo/doas.
	Elevated bool
	// Dynamic: some arguments come from a pipe (xargs), so their values are unknown.
	Dynamic bool
	// Dyn marks words (by index in Words) that contain a variable or command
	// substitution, whose value is unknown.
	Dyn map[int]bool
	// Redirects are the output redirections.
	Redirects []Redirect
	// Pipeline identifies the pipeline of the line this command is part of.
	Pipeline int
}

Simple is one simple command of a shell line, as far as it can be judged without running the shell.

func (Simple) Prog

func (s Simple) Prog() string

Prog is the program name without its directory.

func (Simple) Text

func (s Simple) Text() string

Text is the command as one string (the unit user rules are matched against).

Jump to

Keyboard shortcuts

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