sandbox

package
v0.264.1 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: Apache-2.0 Imports: 15 Imported by: 0 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.

README

sandbox

What - the sandbox bounded context: the policy that decides which paths the file tools may read and write, which bash commands run without a prompt, where a bash child may write once the kernel confines it, and how a denied path turns into a grant the user approves. Why - a security decision needs one chokepoint. Keeping it out of config leaves that package as schema and path constants, and keeping runtime grants apart from the configured policy keeps the system prompt byte-stable so the provider prompt cache stays warm. How - domain/ holds the policy value objects (Allowed with an Access, Denied with a Violation), the denial contract (DeniedError, ParseDenial) and the SandboxAccess approval name. The root package holds the process-wide Granted set and GrantFor, and applies the filesystem section of sandbox.yaml plus the carve-outs (skills, plugins, runtime dirs, memory, Go and Rust library dirs) through ValidateRead and ValidateWrite, and resolves the per-mode bash allow-list through IsBashCommandAllowed, and lists the paths a bash child may write through WritePaths. infrastructure/ persists an "always" grant into ~/.infer/sandbox.yaml, the only policy file, and builds the confined bash child (Command, started through Child.Start): Landlock on Linux, applied on the thread that forks bash so only the child inherits it, Seatbelt on macOS through /usr/bin/sandbox-exec, plain bash elsewhere.

How it plugs in

  • Read, Tree, Grep, Wait, ImageDecode, TextToSpeech and the file writer call ValidateRead. Write, Edit, MultiEdit and Delete call ValidateWrite, which also rejects custom-tool directories and the read-only library carve-out (Go and Rust).
  • Bash, Wait, the approval policy and command hooks call IsBashCommandAllowed and surface BashCommandRejectionHint when a command is denied. The system prompt lists BashAllowedCommands.
  • Bash spawns a command the allow-list runs unattended through sandboxinfra.Command. With filesystem.enforce (default on) the kernel lets the child execute anything and write only under WritePaths: the writable allowed entries and grants, the writable carve-outs, the temp dir, the Go, cargo and npm caches, and /dev. In plan mode the project gives way to the scratchpad. On macOS the kernel also refuses to read, rename or delete the files the blocking deny entries name (auth.yaml, *.key, ...), so a spelling the argv walker cannot predict still fails, through a symlink, a copy or a clone alike. Directory entries (.git/) and globs the profile cannot say exactly stay with the path check, since git must keep reading its own tree, and Linux has no read denial, since Landlock only grants. A tool that needs a denied file unattended, ssh with a denied id_* or a dev server loading .env, fails and the agent asks for approval. A refused write or read surfaces DeniedHint in the tool result. A command somebody approved runs as approved, unconfined, so the approval flow for a path outside the sandbox keeps its meaning.
  • infer debug sandbox prints the effective policy, the carve-outs and the kernel write paths from the current directory, and infer tools validate <command> answers for one bash command.
  • Decision order: denied entries first (a blocking one fails whatever its position, otherwise the first approval entry asks), then the built-in carve-outs, then the first matching allowed entry, then the user is asked. Denied entries match case-insensitively, since a case-insensitive filesystem opens .ENV as .env. An empty allowed list is no boundary. A grant only unlocks what approval could have, so a granted directory still respects denied.
  • The default policy denies .infer/ with on_violation: approval, so the config dirs, whose files can hold tokens, ask before every read and write. The carve-outs skip that entry, so skills, plans, plugins, memory and runtime output stay open.
  • sandbox.yaml loads over the defaults, so a list the file leaves out keeps its default, and a file that fails to parse or validate stops the CLI from starting rather than falling back to a wider policy.
  • The agent's tool loop recovers a DeniedError from the flattened tool result with ParseDenial, raises a synthetic SandboxAccess approval for GrantFor (the exact path when a denied entry matched, otherwise the directory around it, except that a handback inside a config dir grants the exact file), and on approval calls Granted.Add. An auto-accept answer also calls PersistGrant unless a denied entry matched, since denied wins over any allowed entry it could write. PersistGrant refuses grants inside the config dirs, puts a write grant first and a read grant last, so a read grant never shadows a write entry.
  • ValidateWrite refuses ~/.infer/sandbox.yaml whatever denied says, a project copy is never loaded, and infer config set cannot reach the policy keys, so the agent cannot widen its own sandbox through the tools it runs.
  • Concurrency: tools run in parallel goroutines, so Granted is the only mutable sandbox state and sits behind an RWMutex. List returns a copy, so no check holds the lock while it walks the filesystem. cfg.Tools.Sandbox is never written after load and is read without a lock. SaveSandbox replaces the file through a rename, so a worker that starts while another persists a grant never reads a torn policy.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllowedDirectories

func AllowedDirectories(cfg *config.Config) []string

AllowedDirectories are the anchored allowed paths, in order, for callers that want somewhere sensible to look.

func BashAllowedCommands

func BashAllowedCommands(cfg *config.Config, mode agentdomain.AgentMode) []string

BashAllowedCommands returns the effective bash allow-list for mode: the shared mode.all baseline unioned with that mode's own bucket. This is the ONE place an AgentMode maps to a tools.bash.mode.<bucket> list: Plan -> plan, AutoAccept -> auto, Standard/AutoWithJudge/unknown -> standard, ReadOnly -> baseline only. The system prompt lists it so the agent knows what runs.

func BashCommandDeniedPath added in v0.262.0

func BashCommandDeniedPath(cfg *config.Config, command string) (string, bool)

BashCommandDeniedPath reports the first path in command's statically nameable arguments that a blocking deny entry matches, for the Bash tool to refuse in every mode: auto mode's unrestricted allow-list skips the path check, and no approval reopens what the policy blocks.

func BashCommandRejectionHint

func BashCommandRejectionHint(cfg *config.Config, command string) string

BashCommandRejectionHint explains why the guard rejected command, or a path outside the sandbox, so the model can correct course instead of retrying. It returns "" when the command is simply not in the allow-list. A nil config skips the sandbox reason.

func BlockingDenials added in v0.262.0

func BlockingDenials(cfg *config.Config) []sandboxdomain.Denied

BlockingDenials lists the deny entries that refuse rather than ask, so every layer honors them the same way: the file tools' exclusion, the Bash tool's walker, and the kernel's read denial where the OS can express one.

func Denies added in v0.239.0

func Denies(cfg *config.Config, projectDir, absPath string) bool

Denies reports whether a denied entry of the policy matches a file of a project, given its absolute path and its path relative to the project dir. A session checkpoint uses it to keep denied files, such as *.env and *.key, out of the repository it packs, wherever the daemon itself runs.

func GrantFor

GrantFor is the entry worth asking the user for after a denial: the exact path when a denied entry matched or it sits in a config dir, otherwise the directory around it so the agent can keep working there without a prompt per file.

func IsAnchored

func IsAnchored(rulePath string) bool

func IsBashCommandAllowed

func IsBashCommandAllowed(cfg *config.Config, command string, mode agentdomain.AgentMode) bool

IsBashCommandAllowed reports whether command is auto-approved in mode, for the Bash tool, the approval policy and agent auto-approval alike. A ".*" entry allows anything. Otherwise the command must pass cleanSingleCommand, match an allow entry whole, and name only paths inside the sandbox. What is not allowed asks in chat and is rejected in headless mode.

func IsUnrestricted

func IsUnrestricted(allow []string) bool

IsUnrestricted reports whether the allow-list contains the "allow any command" sentinel, which makes the mode unrestricted (and skips the clean-command guard). ".*", "^.*$", ".+", and a few trivially-equivalent forms qualify.

func ReadOnlyCarveOuts added in v0.262.0

func ReadOnlyCarveOuts() []string

ReadOnlyCarveOuts lists the language library directories the file tools may read but never write: the Go module cache and standard library source, the Cargo registry and git sources, and the rustup toolchain sources.

func ValidateRead

func ValidateRead(cfg *config.Config, path string) error

ValidateRead checks that a path and the file it resolves to through symlinks may both be read. Checking the target stops a link inside the sandbox from reaching a file outside it.

func ValidateWrite

func ValidateWrite(ctx context.Context, cfg *config.Config, path string) error

ValidateWrite is ValidateRead for writes. The userspace policy files and the custom-tool directories are never writable, whatever the rules say. In plan mode only the project scratchpad may be written: the gate runs before grants and the configured allowed list, since no grant can widen plan mode.

func WritableCarveOuts added in v0.262.0

func WritableCarveOuts(cfg *config.Config) []string

WritableCarveOuts lists the built-in directories the file tools may write although the config dirs ask: skills, plugins, runtime output, plans, insights and memory.

func WritePaths added in v0.262.0

func WritePaths(cfg *config.Config, mode agentdomain.AgentMode) []string

WritePaths lists the directories a bash child may write while the kernel confines it: the writable allowed entries and grants, the carve-outs the file tools may write, the temp and tool cache dirs a build needs, and /dev. In plan mode the project gives way to the scratchpad, as ValidateWrite does.

Types

type Grants

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

Grants are the entries the user approved after a denial. They extend the configured policy for the rest of the process and never feed the system prompt, which must stay byte-stable within a session.

var Granted Grants

Granted holds the process-wide grants. ponytail: process-wide like the port registry, fold into an injected guard if one process ever runs multiple configs.

func (*Grants) Add

func (g *Grants) Add(entry sandboxdomain.Allowed)

Add grants entry for the rest of the process. Idempotent.

func (*Grants) List

func (g *Grants) List() []sandboxdomain.Allowed

List returns a copy of the granted entries.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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