sandbox

package
v0.74.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: AGPL-3.0 Imports: 11 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.

Documentation

Overview

Package sandbox confines a session's process tree to the work it was opened for: a private empty home, the rest of the machine read-only, and write access only inside the checkout and the provider's own state directory.

It is the counterweight to skipping permission prompts. An agent running unattended edits files and runs commands with the user's whole account behind it — every other repository on the disk, every credential in the home directory, every dotfile. Confining it costs the session nothing it needs and takes the rest of the machine off the table.

It is not a boundary against hostile code. The backends here use namespaces and mounts (Linux) or a path policy (macOS) and nothing else: no seccomp filter, no Landlock ruleset, no kernel-bug mitigation. It protects a machine from an agent working unsupervised, not from something written to break out. Running genuinely hostile code still wants a disposable VM.

The backend is selected by build tag behind Available and Wrap, the seam the PTY uses (internal/terminal): Linux runs bubblewrap, macOS runs sandbox-exec, and every other platform answers that it cannot confine anything.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AgentSocket

func AgentSocket() string

AgentSocket is the ssh agent's socket, read from the environment lich itself was launched in, or "" when there is no agent to hand over. Mounting it is what makes `git push` work in a confined session: the private home takes ~/.ssh with it, and the agent is the way to authenticate without ever putting a private key inside the sandbox — the agent signs, and the key never moves.

The cost is the honest one, and it is why this is behind a setting rather than always on: an agent holds every identity loaded into it, and a session handed the socket can sign with all of them, for any host it can reach, for as long as it runs. OpenSSH can pin a key to one destination (`ssh-add -h`), but only at the moment the key is added on the host — lich is given a socket that is already populated and can only pass it on whole.

Exported because the window shows what is in the agent beside the setting that hands it over (internal/system): a choice made without that list is a choice about "my GitHub key" that actually covers every key.

func Available

func Available() bool

Available reports whether this machine can confine a session. Linux answers for bubblewrap being installed and nothing else — whether the kernel will actually grant the namespace is a question only a spawn can ask, and the distributions that refuse (Ubuntu and Debian ship an AppArmor policy denying unprivileged user namespaces) say so in bwrap's own error, which is the one the user can act on.

func Backend

func Backend() string

Backend names what confines a session on this machine, or "" when nothing can. It is Available's answer with the name attached: a confined session behaves differently on each backend, and the name is the first thing a report about one needs.

func BinaryDirs

func BinaryDirs(name string) []string

BinaryDirs returns the directories a confined session needs mounted to run the executable named by name — a bare name resolved on PATH, or a path. Empty when nothing on PATH answers to it, which is a spawn that would have failed outside the sandbox too.

It returns *directories*, never the executable itself, and that is the whole point. Agents are installed as a symlink into a versioned store (`~/.local/bin/claude` → `~/.local/share/claude/versions/2.1.234`), and binding the symlink is what breaks: bubblewrap resolves a mount destination through symlinks, so it tries to create the mount point at a target that is not in the sandbox yet and fails the whole spawn with "Can't create file at …: No such file or directory". Binding the directories that hold each hop leaves the link intact and puts its target where the link already points.

The chain is walked hop by hop rather than resolved in one step because it can leave the home and come back — a version manager's shim into a store elsewhere — and every directory it passes through has to be there.

func Probe

func Probe() (string, error)

Probe answers whether a confined session on this machine would actually be confined, by opening one and looking. It returns the backend's name and why it confines nothing, or a nil error when it does; an empty name means this platform has no backend at all and there is nothing to report.

Available cannot answer this and does not try: it resolves the launcher and stops, because only a spawn can ask the kernel. On Ubuntu and Debian an AppArmor policy denies unprivileged user namespaces, so bubblewrap is installed, Available says yes, and every confined session dies on bwrap's own error. This is the diagnosis of that machine, not a second gate in front of it: nothing here decides whether a session is confined.

func Wrap

func Wrap(spec Spec, bin string, args []string) (string, []string)

Wrap returns the binary and arguments that run bin confined. It falls back to the unconfined spawn when bubblewrap is not installed, which is the same answer Available gives — a session must open either way, and a card that never appears is worse than one that is not confined.

Types

type Spec

type Spec struct {
	// Home is the user's home directory, replaced by an empty private one.
	Home string
	// Cwd is the checkout the session works in, writable.
	Cwd string
	// GitCommon is the repository's common git directory when Cwd is a linked
	// worktree, writable, and empty when Cwd is a repository of its own. Without
	// it every git command in a worktree session fails: the worktree's `.git` is
	// a file pointing at a directory that would not be there.
	GitCommon string
	// Write is every other path bound read-write: the provider's state, and the
	// build caches a toolchain cannot work without.
	Write []string
	// Read is every path bound read-only: configuration, installed toolchains,
	// and the binaries the session runs.
	Read []string
	// SkippedLinks names the paths under Home that were dropped for being
	// symlinks, relative to Home (".gitconfig", ".ssh/known_hosts"). It is not a
	// mount list: it is what the session will not find where it expects it, so
	// the window can say so rather than leave the absence to be discovered by a
	// command that fails inside the sandbox and works outside it.
	SkippedLinks []string
}

Spec is one confined spawn: what stays writable, what is merely readable, and what is not there at all. Everything is an absolute host path, and the paths are what the backends translate into their own vocabulary.

Home is the user's real home directory. Inside the sandbox that path exists and is empty — the point of the whole exercise — with Write and Read entries under it mounted back in one by one.

func Describe

func Describe(providerID, home, cwd, gitCommon string, extraRead []string, sshAgent bool) Spec

Describe builds the Spec for one confined session: the provider's state and the build caches writable, the toolchain and configuration readable, the checkout writable, and nothing else in the home there at all.

The display server's socket is in there too, which is the one thing mounted back that is not about running the project: without it the agent's own copy and its clipboard image paste fail, because both shell out to a tool that has to reach the compositor (displaySockets).

extraRead is what the caller knows and this package does not — where the binaries the spawn actually runs live. The provider's own executable and the lich binary (which the session calls back through, and which serves its MCP server) are installed wherever the user put them, which is regularly inside the home this call is about to empty. Pass directories, from BinaryDirs.

Paths that do not exist are dropped: a bind mount of a missing source fails the whole spawn, and every entry here is optional by nature — a machine without Rust has no ~/.cargo, and that is not an error.

Jump to

Keyboard shortcuts

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