Documentation
¶
Overview ¶
Package guard is the `execute` boundary in non-interactive deployments.
AgentKit authorizes tool calls with a per-call interceptor rather than a command allowlist, on the grounds that a static allowlist is both trivially escaped and too narrow to run a build. That reasoning assumes an embedder that can answer a permission question. A daemon triaging issues overnight cannot, and "allow" by default is strictly worse than the allowlist it replaced. So:
- agentkit.New refuses LOUDLY an agent whose tool set reaches a shell tool with no Guard configured (core.ErrUnguardedExecute), and
- Check is the pure decision — may this argv run? — and Restricted ships as an importable, REPLACEABLE interceptor over it, kept out of the SDK's enforcement path so it can be swapped rather than only narrowed.
Restricted is a floor for unattended safety, not a sandbox: an allowlist wide enough to run a build is escapable through the allowed programs' own configuration and subprocess surfaces. It remains that floor whatever runs the tools.
A sandbox is the boundary the guard never claimed to be. AllowAll is a reasonable choice only when every shell tool the agent can reach is a proxy from a sandbox whose provider reports sandbox.IsolationContainer or sandbox.IsolationMicroVM (sandbox/podman), under sandbox.NetworkNone (or with egress the embedder accepts), with Spec.Resources set, and with the files exported from it treated as untrusted. It is never reasonable with the local provider (sandbox/local), which isolates nothing, or with any local shell tool in the set. agentkit.New still requires a Guard either way.
Index ¶
- Variables
- func Allow(programs ...string) core.BeforeToolCall
- func AllowAll(context.Context, core.BeforeToolCallContext) core.BeforeToolCallDecision
- func IsShellTool(name string) bool
- func Restricted(o Options) core.BeforeToolCall
- type Command
- type Decision
- type Options
- type Redirect
- type SyntaxError
Constants ¶
This section is empty.
Variables ¶
var ShellToolNames = []string{"execute", "run_command"}
ShellToolNames are the tools the guard treats as a shell. A caller-supplied tool of the same name counts: the name is what the model calls, and a custom `execute` is no less a shell for being custom.
Functions ¶
func Allow ¶
func Allow(programs ...string) core.BeforeToolCall
Allow is Restricted with only AllowedPrograms set: it admits the shell calls whose program is in programs and blocks the rest, with Restricted's decisions and its "guard.Restricted: " reasons. Restricted is the way to set the other options (AllowShellOperators, AllowEnvPrefixes, BlockedTools, TerminateOnBlock). With no programs every shell call is blocked.
Like Restricted, the guard is a floor, not a sandbox.
Allow copies programs, so a caller who later changes or appends to the slice it passed does not widen the allowlist.
func AllowAll ¶
func AllowAll(context.Context, core.BeforeToolCallContext) core.BeforeToolCallDecision
AllowAll is the explicit opt-out from the guard: an interceptor that never blocks. Passing it is an affirmative act — the embedder has said, in code, that this agent runs an unrestricted shell.
It is reasonable only when every shell tool the agent can reach is a proxy from a sandbox whose provider reports IsolationContainer or IsolationMicroVM, under NetworkNone (or egress the embedder accepts), with Resources set and exported files treated as untrusted; never with the local provider or with a local shell tool in the set.
func IsShellTool ¶
IsShellTool reports whether name is one of ShellToolNames.
func Restricted ¶
func Restricted(o Options) core.BeforeToolCall
Restricted is the reference interceptor for headless embedders: an allowlist of programs plus shell-operator rejection.
It is a FLOOR, not a sandbox. The argument against a command allowlist still holds — an allowlist wide enough to run a build is escapable through the allowed programs' own configuration and subprocess surfaces — and this policy does nothing about that. What it does is make the unattended default "no" instead of "yes", which is the difference the guard exists to close. Embedders with real context should replace it, not extend it.
What it does check, it checks against what will actually run: the program of every command on the line (see Commands) is matched as spelled, not by basename, and an assignment that would change which binary a listed name resolves to, or load code into it, is refused.
Types ¶
type Command ¶
type Command struct {
// Text is the command as written, trimmed: its words and redirections,
// without the operator that ends it or a heredoc's body.
Text string
// Assign are the names of the NAME=value assignments in front of the
// program, in order. A command that is only assignments has an empty Argv.
Assign []string
// Argv is the program and its arguments with quotes and escapes removed.
// Expansions are left as written ($HOME, ${x}, $(date), `date`), so a word
// the shell would compute is never spelled like a plain program name.
Argv []string
// Redirects are the command's redirections, in order.
Redirects []Redirect
}
Command is one simple command of a shell line, as Commands reads it.
func Commands ¶
Commands splits a POSIX-sh / bash command line into every simple command it runs: the ones separated by `;`, `&`, `&&`, `||`, `|`, `|&` and newlines, the ones inside subshells, and the ones inside command substitutions ($(...) and backticks), process substitutions (<(...), >(...)), parameter expansions (${x:-$(...)}), arithmetic ($((...))) and the bodies of unquoted heredocs. Commands are returned in the order they start in the line; a substituted command follows the command it appears in.
What the shell treats as data stays data: quoted text, comments, and the body of a heredoc (apart from the substitutions an unquoted one expands).
It reads the grammar, it does not run it: expansions are not expanded, aliases and functions are not resolved, and the keywords of compound commands (if, for, while, {, [[, !, time) are words like any other, so a guard that matches Argv[0] against an allowlist refuses them unless they are listed. A case statement, whose patterns end in an unbalanced `)`, is refused with a *SyntaxError rather than misread.
It returns a *SyntaxError for a line it cannot read with certainty; see SyntaxError.
type Decision ¶
Decision is Check's verdict on one command.
func Check ¶
Check decides whether the command argv may run under o: whether argv[0] is on o.AllowedPrograms. It is a pure function — no environment, no filesystem, no PATH lookup — so a host can ask it about a command before running it as well as through Restricted.
A program with a path separator matches only a listed path, compared after cleaning: `./go` names a file in the workspace, not the go on PATH. A bare name matches a listed bare name only. A block carries o.TerminateOnBlock.
type Options ¶
type Options struct {
// AllowedPrograms are the programs that may run: argv[0], or the first
// word after any assignments of each command an `execute` line runs (see
// Commands). A bare name
// ("go") admits only that bare name, resolved through PATH. A path
// ("/usr/bin/git") admits that exact path only — not a bare git found
// through PATH, which may be another binary. A
// program word containing a path separator is refused unless that path
// itself is listed: `./go` names a file in the workspace, not the go on
// PATH. Empty means every shell call is blocked, which is the safe
// default for a policy whose only purpose is to say no.
AllowedPrograms []string
// AllowShellOperators permits pipes, `;`, `&&`, `||`, redirection,
// subshells, command substitution and variable expansion in `execute`
// commands. Off by default. The grammar this filter understands is POSIX
// sh / bash quoting — single quotes literal, double quotes expanding —
// and it is declared here because a filter must say which grammar it
// filters.
//
// When on, every command the line runs is judged, not only the first:
// each one Commands finds, the ones inside substitutions and unquoted
// heredoc bodies included. A line Commands cannot read with certainty
// (a *SyntaxError) is refused.
AllowShellOperators bool
// AllowEnvPrefixes permits `NAME=value` assignments in `execute` commands:
// in front of a program (`GOFLAGS=-mod=mod go build`) or, under
// AllowShellOperators, on their own (`d=$(go list .)`), which changes the
// environment of the commands after it when NAME is already exported. Off
// by default: an assignment configures the programs it reaches, and an
// allowlist of names says nothing about how those programs are
// configured. Even when on, a name that changes which binary runs or
// what is loaded into it — PATH, LD_*, DYLD_*, BASH_ENV, interpreter
// injection variables, see envDenied — is refused.
AllowEnvPrefixes bool
// BlockedTools are refused by name, shell or not.
BlockedTools []string
// TerminateOnBlock sets Terminate beside every Block, so that a refusal
// ends the run instead of looping the model into retrying.
TerminateOnBlock bool
}
Options configures Restricted.
type Redirect ¶
type Redirect struct {
// FD is the file descriptor written in front of the operator ("2" in
// 2>&1), or "" when there is none.
FD string
// Op is the operator: <, >, >>, >|, <>, <&, >&, &>, &>>, <<, <<- or <<<.
Op string
// Target is the word after the operator, unquoted: a file, a descriptor,
// a here-string or, for << and <<-, the heredoc's delimiter.
Target string
}
Redirect is one redirection of a Command.
type SyntaxError ¶
type SyntaxError struct {
// Offset is the byte offset in the line where reading stopped.
Offset int
// Reason says what was found there.
Reason string
}
SyntaxError is Commands' refusal of a line it cannot read with certainty: one the shell would reject (an unterminated quote, an unmatched parenthesis), or a construct Commands does not follow (a case statement, a heredoc with no terminating line). A guard refuses such a line: the commands Commands would return for it are not the ones the shell runs.
func (*SyntaxError) Error ¶
func (e *SyntaxError) Error() string