sandbox

package
v0.56.8 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: MIT Imports: 10 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 wraps agent shell commands in an OS-level containment layer — defense-in-depth UNDER the permission classifier, not a replacement for it. The classifier decides whether a command may run; the sandbox bounds what a permitted command can touch if it (or something it spawns) misbehaves.

Modes:

ReadOnly   — read-only sessions (plan/scout shells): the filesystem is
             readable but only tmp + tool caches are writable. Always on
             for read-only sessions when the platform supports it.
Workspace  — writes confined to the repo root + tmp + tool caches.
             Opt-in for normal sessions via MEMCODE_SANDBOX=1.

Backends: macOS sandbox-exec (Seatbelt), Linux bubblewrap (bwrap) when installed. No backend → commands run unwrapped, exactly as before (fail open: the sandbox strengthens an existing gate, it must never brick a platform). MEMCODE_SANDBOX=0 force-disables wrapping everywhere.

Index

Constants

View Source
const EnvVar = "MEMCODE_SANDBOX"

EnvVar toggles sandboxing for NORMAL sessions: "1"/"true"/"workspace" wraps every bash call in Workspace mode; "0"/"off" force-disables all wrapping (including the read-only default) as the escape hatch.

Variables

This section is empty.

Functions

func NestingFailure added in v0.56.0

func NestingFailure(output string) bool

NestingFailure reports output showing a command failed only because it ran inside memcode's sandbox: it tried to start its own sandbox (Seatbelt can't nest; bubblewrap may lack namespaces), or to gain privileges with sudo (setuid programs don't run sandboxed on macOS).

func Supported

func Supported(p Policy) bool

Supported reports whether Wrap would ACTUALLY contain under p on this platform — callers that scale a permission gate to the containment (mcp_code_exec: sandboxed+no-network = Medium, unsandboxed = Dangerous) must know the difference between "asked for a sandbox" and "got one".

func Wrap

func Wrap(command string, p Policy) (string, bool)

Wrap returns a shell line that runs command under the policy's containment, and whether wrapping applies. ok=false means run the command unchanged (policy Off, or no backend on this platform).

func WrapArgv added in v0.56.0

func WrapArgv(argv []string, p Policy) (string, bool)

WrapArgv is Wrap for a program and its arguments, quoted for the shell.

Types

type Mode

type Mode int

Mode is the containment level.

const (
	Off Mode = iota
	ReadOnly
	Workspace
)

type Policy

type Policy struct {
	Mode Mode
	Root string // repo root (Workspace mode's writable tree)
	// Scratchpad is the session-scoped writable scratchpad directory for analytical modes.
	Scratchpad string
	// StrictScratchpad confines writes exclusively to Scratchpad (no broad /tmp or ~/.cache),
	// hides host /tmp in bwrap via --tmpfs /tmp, and denies network.
	StrictScratchpad bool
	// DenyNetwork additionally cuts ALL network egress (Seatbelt deny network*,
	// bwrap --unshare-net). Used by mcp_code_exec: with writes confined AND no
	// network, generated code loses its exfiltration channel and can gate at
	// Medium instead of Dangerous. Bash keeps network (builds/tests need it).
	DenyNetwork bool
	// Shield keeps memcode's own credentials and authority out of the command's
	// reach, in every mode including Off: see shield.go. Set by PolicyFor unless
	// sandboxing is force-disabled.
	Shield *Shield
}

Policy is what a wrapped command may touch.

func PolicyFor

func PolicyFor(readOnly bool, root string) Policy

PolicyFor decides the policy for a bash call: read-only sessions always get ReadOnly containment; normal sessions get Workspace only when opted in.

func ShieldOnly added in v0.56.0

func ShieldOnly(root string) Policy

ShieldOnly is the policy for a long-lived helper process memcode starts on the user's behalf (an MCP server): no write containment — it keeps its own caches and state — but the shield, unless sandboxing is force-disabled.

type Shield added in v0.56.0

type Shield struct {
	Root         string   // the working tree (may itself live under ~/.memcode, e.g. a task worktree)
	ProjectState string   // this project's state dir
	StateHome    string   // ~/.memcode (or MEMCODE_HOME)
	NoAccess     []string // directories neither readable nor writable
	NoAccessFile []string // single files neither readable nor writable
	Writable     []string // under StateHome, still writable
	Readable     []string // under StateHome/projects, still readable (and writable)
}

Shield is the part of the sandbox that is on for every agent command, not only in opt-in containment. It is an OS-enforced boundary (Seatbelt on macOS, bubblewrap on Linux), unlike the output redaction and command classification layered above it, and it protects two things a prompt injection must never reach through a shell:

  • credentials ordinary development never needs: memcode's own (~/.config/memcode) and the subscription logins memcode can ride on (Claude Code's and Codex's OAuth files) — unreadable;
  • memcode's authority: everything under ~/.memcode is read-only except site sources, skills, the current project's scratch area and the working tree itself, so a command can't approve its own future commands, add a hook or an MCP server, or rewrite memory; other projects' state (transcripts, checkpoints) is unreadable.

Everything else (the repo, ~/.ssh for git, cloud CLI configs for deploys, the network) stays as available as before: Auto mode keeps working for ordinary development.

func ForAnalytical added in v0.56.8

func ForAnalytical(root string) *Shield

ForAnalytical returns a Shield that additionally blocks common SSH/cloud/GPG credential directories for analytical scratchpad execution.

func ShieldFor added in v0.56.0

func ShieldFor(root string) *Shield

ShieldFor builds the shield for a session rooted at root.

Jump to

Keyboard shortcuts

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