Sandboxing from Go
A permission rule decides whether a command may start. A sandbox limits what
it can reach once it runs, and a shell can always do something a rule’s
parser didn’t see. Sandboxing and enforcement
explains the launchers and what each enforces. This page builds on
Wiring an agent, where a workspace and a shell are passed with
WithEnvironment and WithShell: it adds the launcher, makes the agent
refuse to run without it, and covers the one place the sandbox and zelk
share files.
What is confined
Section titled “What is confined”The local environment, env/local, has two parts. Its file operations
resolve every path beneath the workspace, with openat2 on Linux and
os.Root elsewhere, and need no launcher. Its processes, the commands
the Bash and PowerShell tools run, the MCP servers started through it and the
checks Verify runs, are confined by its launcher. The default launcher,
launcher.None, confines nothing. A launcher is a value of the package
env/launcher that you build and give to local.WithLauncher:
| Constructor | Platform | Notes |
|---|---|---|
launcher.NewBwrap(ctx, launcher.BwrapOptions{…}) |
Linux | needs bwrap; network off, full or allowlist |
launcher.NewSeatbelt(launcher.SeatbeltOptions{…}) |
macOS | needs a TempDir of your own; network off or full |
launcher.NewMXC(ctx, launcher.MXCOptions{…}) |
Windows | needs Microsoft’s executor, which the host installs and verifies first |
Each takes the options for what to add to its fixed profile: extra
read-only paths for a toolchain outside the system directories, the
variables to keep, and the network mode. A request outside the profile fails
the constructor; nothing is silently weakened. The network mode allowlist
needs a broker your host provides (launcher.Egress). Hosts that don’t run
one use off or full.
Build one and require it
Section titled “Build one and require it”On Linux:
ctx := context.Background()workspace, err := os.Getwd()if err != nil { fmt.Println(err) return}sandbox, err := launcher.NewBwrap(ctx, launcher.BwrapOptions{Network: launcher.NetworkOff})if err != nil { fmt.Println(err) return}e, err := local.New(ctx, workspace, local.WithLauncher(sandbox))if err != nil { fmt.Println(err) return}defer e.Close()p, err := replay.New(provider.Caps{})if err != nil { fmt.Println(err) return}a, err := zelk.New(ctx, p, zelk.WithModel("m"), zelk.WithPermissionMode(permission.ModeAcceptEdits), zelk.WithEnvironment(e, workspace), zelk.WithShell(zelk.Shell{Path: "/bin/bash", Env: []string{"PATH=/usr/local/go/bin:/usr/bin:/bin"}}), zelk.WithRequirements( env.Requirement{Op: env.Exec, Dim: env.FSWrite, Min: env.Enforced}, env.Requirement{Op: env.Exec, Dim: env.Network, Min: env.Enforced}))if err != nil { fmt.Println(err) return}defer a.Close(ctx)Two things in it check the sandbox, and neither is optional in a service:
NewBwrapruns a trivial command in the full profile. Where bubblewrap can’t create a sandbox, such as inside some containers, it fails withlauncher.ErrUnavailable. A launcher that can’t confine never turns into one that silently doesn’t. Handle that error: decide whether the host refuses to start or runs somewhere else, but never fall back to no launcher without saying so.local.WithLauncher(nil)fails withlauncher.ErrNoLauncherfor the same reason.WithRequirementsmakeszelk.Newfail with an error wrappingenv.ErrUnenforcedunless the environment enforces what you list. It checks the environment’s published enforcement, a matrix of operation and dimension, filesystem reads and writes, network, credentials, so a stack that doesn’t deliver what the host depends on fails at construction, not after the first command. The default launcher fails it:
root, err := os.MkdirTemp("", "workspace-")if err != nil { panic(err)}defer os.RemoveAll(root)ctx := context.Background()e, err := local.New(ctx, root)if err != nil { panic(err)}defer e.Close()p, err := replay.New(provider.Caps{})if err != nil { panic(err)}_, err = zelk.New(ctx, p, zelk.WithModel("m"), zelk.WithPermissionMode(permission.ModeBypass), zelk.WithEnvironment(e, root), zelk.WithRequirements(env.Requirement{Op: env.Exec, Dim: env.Network, Min: env.Enforced}))fmt.Println("refused:", errors.Is(err, env.ErrUnenforced))// Output: refused: truelocal.WithRequirements makes the same check on the environment alone, and
Env.Enforcement() returns the matrix to read or to log. An
env.Enforcement built by hand shows how requirements compare levels:
// What a launcher might publish: file reads jailed to the workspace,// network only advisory.published := env.NewEnforcement(). With(env.FileRead, env.FSRead, env.Enforced, "path jail"). With(env.Exec, env.Network, env.Advisory, "proxy, bypassable")
err := published.Require( env.Requirement{Op: env.FileRead, Dim: env.FSRead, Min: env.Enforced}, env.Requirement{Op: env.Exec, Dim: env.Network, Min: env.Enforced},)fmt.Println(errors.Is(err, env.ErrUnenforced))fmt.Println(err)// Output:// true// required enforcement not available: exec/network needs enforced, have advisoryThe levels are None, Advisory and Enforced. Seatbelt and MXC publish
some dimensions as advisory; the launcher table
lists which. Require only the dimensions your deployment depends on.
On Windows, the same pieces take an MXC launcher:
ctx := context.Background()// The host verifies the installation and accepts Microsoft's Required// telemetry before it starts the executor.mxc, err := launcher.NewMXC(ctx, launcher.MXCOptions{ Path: `C:\zelk-tools\mxc\wxc-exec.exe`, Home: `C:\Users\example`, ReadOnly: []string{`C:\Program Files\Git`}, Network: launcher.NetworkOff,})if err != nil { return}e, err := local.New(ctx, `C:\Users\example\source`, local.WithLauncher(mxc))if err != nil { return}defer e.Close()// Use e.Start to run commands in the sandbox.The shell’s Env is the commands’ whole environment under every launcher.
Keep credentials out of it, and give the model’s commands only the
variables they need.
A private cache of published files
Section titled “A private cache of published files”zelk can publish files the model reads and commands use, such as the files
of a skill that came from outside the workspace, into a cache directory of
the workspace. A command that could rewrite a published file would be
changing what a later run trusts, so local.NewMaterializing opens the
workspace with a cache that is private to zelk and read-only to every
process. It needs a launcher that implements launcher.ReadOnlyLauncher,
as bwrap, seatbelt and MXC do. Given the default launcher it refuses, with
env.ErrUnenforced:
dir, err := os.MkdirTemp("", "materialized-workspace-")if err != nil { return}defer os.RemoveAll(dir)if err := os.Mkdir(filepath.Join(dir, "cache"), 0o700); err != nil { return}_, err = local.NewMaterializing(context.Background(), dir, "cache", materialize.Bounds{})fmt.Println(errors.Is(err, env.ErrUnenforced))// Output: trueWith a protecting launcher:
ctx := context.Background()sandbox, err := launcher.NewBwrap(ctx, launcher.BwrapOptions{Network: launcher.NetworkOff})if err != nil { fmt.Println(err) return}// The cache is a directory of the workspace that only zelk writes.e, err := local.NewMaterializing(ctx, "/srv/workspace", ".zelk-cache", materialize.Bounds{}, local.WithLauncher(sandbox))if err != nil { fmt.Println(err) return}defer e.Close()// Pass e to zelk.WithEnvironment, and to zelk.WithCatalogCleanup so// that deleting a session releases what it published.The cache must already exist as a directory inside the workspace, used for
nothing else. Searches and workspace diffs leave it out, and ReadFile and
commands can read what is published. On Windows, the cache needs a private
inheritable ACL, which NewMaterializing checks. Its reserved directories can
show staged files before a publication commits. Use the path returned by a
successful Materialize call. Share the returned environment between sessions
so one host process owns the cache.
Give the same environment to zelk.WithCatalogCleanup when you build a
catalog: deleting a session then releases what it
published.
Goals and verification covers the checks a run must pass before it can succeed; they run in this sandbox too.