env/launcher
import "github.com/barevoidco/zelk/core/env/launcher"Package launcher confines the processes the local environment starts. A launcher wraps each process in a sandbox with a fixed, published profile, and reports what it enforces as the Exec row of the environment’s enforcement matrix. Requests outside the profile are rejected at construction, never silently downgraded.
None claims no confinement. Bwrap (Linux) runs each process under bubblewrap with read-only system directories, the workspace and explicitly supplied extra roots writable, a fresh /tmp, no home directory and no host sockets, new namespaces for everything, and the network off unless asked for; Seatbelt (macOS) runs it under sandbox-exec with read and write scopes and the network off or on; MXC (Windows) uses the host-supplied Microsoft executor to run a Windows process container. Sandboxed launchers validate Spec.WritableRoots and optionally protect proper descendants of any writable root through ReadOnlyLauncher. Launchers never discover Git layouts; hosts supply every extra writable root. Bwrap also has a browser profile (WrapBrowser) for zelk’s own browser. Before wrapping a process, Unix launchers mark host descriptors close-on-exec; macOS descriptor enumeration reads names without requiring file metadata. The exact profiles are in docs/design/enforcement.md.
Variables
Section titled “Variables”var ErrNoLauncher = errors.New("no launcher")ErrNoLauncher is returned by Check for a nil launcher, including a nil *Bwrap, *Seatbelt or *MXC, such as one a failed constructor returned.
var ErrUnavailable = errors.New("launcher unavailable")ErrUnavailable reports a launcher this machine can’t run: the sandbox tool is missing, or doesn’t work here.
var ErrWorkspaceUnchecked = errors.New("workspace not checked for socket files")ErrWorkspaceUnchecked reports that WrapBrowser couldn’t check the whole workspace for socket files within its bounds (200,000 entries, two seconds) or at all. The browser profile shows the workspace only with every socket file in it hidden, so it refuses; the caller may start the browser without the workspace and say what is missing.
BrowserHelper
Section titled “BrowserHelper”func BrowserHelper(ctx context.Context, args, environ []string, stdout, stderr io.Writer) (code int, handled bool)BrowserHelper runs the browser profile’s helper when args, a program’s arguments after its name, ask for it, and reports whether they did. A host whose executable is Browser.Helper calls it first thing in main and exits with code when handled.
As “–zelk-browser [-proxy SOCKET] [-local PORT=SOCKET]… – BROWSER ARGS…” it is the sandbox’s first process. It relays 127.0.0.1 ports to the given sockets: one of its own choosing to the proxy, set as the browser’s proxy for everything but the local ports, and each local port to its socket. It then starts itself as “–zelk-browser-command” with descriptors 3 and 4 passed on, closes its own, waits for the browser and stops relaying. As “–zelk-browser-command – BROWSER ARGS…” it installs the browser filter and replaces itself with the browser.
func Check(l Launcher) errorCheck reports a launcher that isn’t there, wrapping ErrNoLauncher: nil, or a nil *Bwrap, *Seatbelt or *MXC. A nil pointer of another type can’t be told from a launcher without reflection; its constructor’s error is the place to catch it.
DefaultEnv
Section titled “DefaultEnv”func DefaultEnv() []stringDefaultEnv are the environment variables a sandboxed process keeps: locale, terminal and trace context. Everything else, credentials included, is dropped unless a launcher’s options name it.
ExecNetworkCommand
Section titled “ExecNetworkCommand”func ExecNetworkCommand(ctx context.Context, path string, args, environ []string) errorExecNetworkCommand installs the command socket profile and replaces the helper process. It must only be called in a dedicated helper, which must exit on any error. The calling thread stays locked so exec cannot migrate to an unfiltered thread. Only Linux amd64/arm64 are supported; other platforms fail closed. Commands may create INET/INET6 sockets in their isolated namespace, but cannot create host-facing UNIX/VSOCK sockets, ptrace the relay, or bypass socket checks through io_uring. Socket pairs are denied too: UNIX datagram pairs can be disconnected and reconnected to a host socket. Pipes remain available.
Forward
Section titled “Forward”func Forward(ctx context.Context, ln net.Listener, dial func(context.Context) (net.Conn, error)) errorForward accepts connections on ln and joins each to a connection from dial, copying both ways, until ctx is done or ln fails. It then closes ln and every connection it carries, and returns once all of them are done. A connection over the limit, or one dial refuses, is closed at once. Forward is how a sandbox’s relay reaches the host: the egress proxy’s socket, or a host loopback port the user named for the browser.
func Relay(ctx context.Context, socket, executable string, command Spec, stdin io.Reader, stdout, stderr io.Writer) (code int, err error)Relay runs a command in a dedicated helper process with HTTP(S) proxy variables pointing at a loopback-to-UNIX broker relay. executable must dispatch –zelk-network-command through ExecNetworkCommand before executing the target. The caller must already be in an isolated network namespace. Relay protects itself from descendant ptrace, owns every connection pump and joins them before returning. It is intended for host helper dispatch, never the embedding process.
Browser
Section titled “Browser”type Browser struct { // Path is the browser's absolute path; Args the arguments after the // program name; Env the whole environment, as KEY=value pairs, of // which the launcher keeps what it keeps for a command. Path string Args []string Env []string // ReadOnly are host paths bound read-only beside the system // directories: the browser's install and any libraries it needs. ReadOnly []string // Workspace, if set, is bound read-only, so the browser can open the // workspace's files and upload them. Workspace string // Data is the browser's own directory, bound writable and its working // directory: its profile, downloads and captures. It must exist, be // canonical, and hold nothing another program trusts. Data string // Helper is the absolute path of an executable that dispatches // BrowserHelper first thing in main. It runs in the sandbox, confines // the browser and carries its traffic out. Helper string // Local are host loopback ports the browser reaches in network off and // allowlist modes. Bwrap reaches each through a host UNIX socket that // leads to it (see Forward); seatbelt, which shares the host's // network, opens the port itself and ignores Socket. Network full // needs none and refuses them. Local []LocalPort}Browser is a browser to start in bwrap’s browser profile (docs/design/browser.md, Confinement).
type Bwrap struct { // contains filtered or unexported fields}Bwrap is the bubblewrap launcher (Linux).
NewBwrap
Section titled “NewBwrap”func NewBwrap(ctx context.Context, opts BwrapOptions) (*Bwrap, error)NewBwrap returns the bubblewrap launcher, after checking that bwrap is there and can create a sandbox on this machine: inside some containers it can’t, and then NewBwrap fails with ErrUnavailable rather than run processes unconfined. On Windows it returns ErrUnavailable.
Bwrap.Enforcement
Section titled “Bwrap.Enforcement”func (b *Bwrap) Enforcement() env.EnforcementEnforcement reports bwrap’s Exec row.
Bwrap.Name
Section titled “Bwrap.Name”func (*Bwrap) Name() stringName returns “bwrap”.
Bwrap.Network
Section titled “Bwrap.Network”func (b *Bwrap) Network() NetworkNetwork returns the network mode the sandbox gives commands and the browser.
Bwrap.Terminate
Section titled “Bwrap.Terminate”func (b *Bwrap) Terminate(leader int) errorTerminate sends SIGTERM to the sandboxed command’s group. bwrap’s child, the sandbox’s first process, leads a session of its own with the command in its group, and outside the sandbox only a handler would make it take the signal, so the signal reaches the command alone. The child is found as the process whose parent is leader, and signalled through a pidfd opened before its parent is checked again, so a PID reused in between is never signalled.
Bwrap.Wrap
Section titled “Bwrap.Wrap”func (b *Bwrap) Wrap(s Spec) (Spec, error)Wrap returns bwrap running s in the published profile.
Bwrap.WrapBrowser
Section titled “Bwrap.WrapBrowser”func (b *Bwrap) WrapBrowser(ctx context.Context, br Browser) (Spec, error)WrapBrowser returns bwrap running br in the browser profile. It differs from a command’s in three ways. The filter allows UNIX sockets, socket pairs and netlink, which Chrome needs between its processes, and keeps denying ptrace, process_vm_writev, io_uring and other socket families, in every network mode. It binds less: the system directories, br.ReadOnly and the workspace read-only, br.Data writable, and none of the launcher’s own read-only and writable paths. And descriptors 3 and 4 reach the browser, for the DevTools pipe (–remote-debugging-pipe): the caller starts the returned Spec with them as its first two extra files, and must open nothing else inheritable in between, since bwrap passes every inherited descriptor on.
The browser may connect to UNIX sockets, so every socket file in the workspace is hidden under an empty file: WrapBrowser walks the workspace first, within bounds, and fails with ErrWorkspaceUnchecked when it can’t finish (browser design, D4). A socket made in the workspace after the browser starts is not hidden.
In allowlist mode the browser’s only way out is the egress proxy, and its proxy settings send even loopback there, but for br.Local’s ports.
Bwrap.WrapReadOnly
Section titled “Bwrap.WrapReadOnly”func (b *Bwrap) WrapReadOnly(s Spec, paths []string) (Spec, error)WrapReadOnly protects paths with mounts applied after the writable workspace and extra root binds, so none can shadow their protection. Each directory between its owning root and a protected path is first bound writable onto itself: a mountpoint can’t be renamed or removed.
BwrapOptions
Section titled “BwrapOptions”type BwrapOptions struct { // Egress is required for NetworkAllowlist and forbidden for off/full. Egress *Egress // Path is bwrap's absolute path; empty looks in /usr/bin and /bin. Path string // Network is off (a new network namespace with only loopback) or // full (the host's network), or allowlist (an injected enforcing broker). Network Network // Env names variables to keep beyond DefaultEnv. Env []string // ReadOnly are extra host paths to bind read-only, such as a // toolchain outside /usr. Each must be absolute and exist. Each is // bound where it leads, with symbolic links resolved, so masks meet it // at the same real paths. A path the system directories already cover // is left to them. One under /tmp shows over the sandbox's own /tmp; // /tmp itself, or a path holding it, is refused. ReadOnly []string // Writable are host directories bound writable for every command, // such as a build cache only sandboxed commands use. Each must be an // absolute, existing directory, bound where it leads as ReadOnly // paths are. Whatever a sandboxed command writes // there, an unsandboxed program must never trust. /tmp is refused as // for ReadOnly. Writable []string // Setenv are NAME=value variables every command gets, over the // process's own: where a tool finds what ReadOnly and Writable bind. // HOME and TMPDIR are the sandbox's own and can't be set. Setenv []string // Mask are files or directories hidden, like the credential files // under /etc, wherever a read-only bind would show them: a // credential inside a toolchain's directory, or under one a user // named. A path no bind exposes needs no mask and gets none. Mask []string}BwrapOptions configure the bubblewrap launcher.
Egress
Section titled “Egress”type Egress struct { Socket string Relay string}Egress supplies a trusted broker and relay for an isolated network namespace. Socket is a host UNIX socket serving an enforcing HTTP/CONNECT broker. Relay is an absolute executable implementing –zelk-network-relay and applying the documented command seccomp profile. The CLI/TUI hosts provide both.
Launcher
Section titled “Launcher”type Launcher interface { // Name names the launcher, such as "bwrap". Name() string // Wrap returns what to start instead of s: the sandbox tool, with s // inside it. Wrap(s Spec) (Spec, error) // Enforcement reports the launcher's Exec row: what a process it // started can and can't do. Enforcement() env.Enforcement}Launcher wraps how a process starts. Implementations are safe for concurrent use.
func None() LauncherNone returns the launcher that confines nothing: a process can read the user’s files, use the network and see the user’s other processes.
LocalPort
Section titled “LocalPort”type LocalPort struct { // Port is the port, on 127.0.0.1 and ::1 inside the sandbox. Port int // Socket is the absolute path of a host UNIX socket that leads to // the port on the host's loopback. Socket string}LocalPort is a host loopback port opened to the browser.
type MXC struct { // contains filtered or unexported fields}MXC starts each command in Microsoft’s Windows process container. See docs/design/enforcement.md for its profile and Microsoft’s telemetry.
NewMXC
Section titled “NewMXC”func NewMXC(_ context.Context, _ MXCOptions) (*MXC, error)NewMXC returns ErrUnavailable on systems without native Windows MXC.
Example
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.MXC.Enforcement
Section titled “MXC.Enforcement”func (m *MXC) Enforcement() env.EnforcementEnforcement reports what a command in the MXC process container can do.
MXC.Name
Section titled “MXC.Name”func (*MXC) Name() stringName returns “mxc”.
MXC.Network
Section titled “MXC.Network”func (m *MXC) Network() NetworkNetwork returns the network mode the sandbox gives commands and the browser.
MXC.Wrap
Section titled “MXC.Wrap”func (*MXC) Wrap(Spec) (Spec, error)Wrap returns ErrUnavailable on systems without native Windows MXC.
MXC.WrapBrowser
Section titled “MXC.WrapBrowser”func (*MXC) WrapBrowser(context.Context, Browser) (Spec, error)WrapBrowser returns ErrUnavailable on systems without native Windows MXC.
MXC.WrapReadOnly
Section titled “MXC.WrapReadOnly”func (*MXC) WrapReadOnly(Spec, []string) (Spec, error)WrapReadOnly returns ErrUnavailable on systems without native Windows MXC.
MXCOptions
Section titled “MXCOptions”type MXCOptions struct { // Path is the absolute path to Microsoft's wxc-exec.exe. The host // verifies the executor and its install directory before supplying it. Path string // ReadOnly names existing absolute toolchain files and directories. ReadOnly []string // Writable names sandbox-specific cache directories outside the workspace. Writable []string // Setenv supplies trusted environment entries, replacing the workload's // value case-insensitively. Use it to point tools at their sandbox caches. Setenv []string // Home is the absolute home directory. The policy denies entries outside // grants and leaves grant ancestors traversable. Home string // Network selects off, full or allowlist. Off and full deny host loopback. Network Network // EgressAddr is the trusted HTTP broker's loopback IP and port. It is // required for allowlist and must be empty in other modes. The caller // keeps the broker listening until all workloads have exited. EgressAddr string}MXCOptions supplies the trusted inputs to the native Windows sandbox.
Network
Section titled “Network”type Network stringNetwork is a sandbox’s network access. Port and domain allowlists need an egress proxy supplied through BwrapOptions.Egress.
const ( NetworkOff Network = "off" NetworkFull Network = "full" // NetworkAllowlist restricts networking through an injected egress broker. // See the launcher's enforcement report for platform-specific exceptions. NetworkAllowlist Network = "allowlist")The network settings.
PowerShellParser
Section titled “PowerShellParser”type PowerShellParser = psparse.ParserPowerShellParser owns the host’s PowerShell syntax subprocess.
NewPowerShellParser
Section titled “NewPowerShellParser”func NewPowerShellParser(ctx context.Context, executable string, environ []string) (*PowerShellParser, error)NewPowerShellParser prepares a parser using a trusted absolute executable and the supplied environment. The caller owns its lifetime: it works until Close or until ctx ends.
ReadOnlyLauncher
Section titled “ReadOnlyLauncher”type ReadOnlyLauncher interface { Launcher // WrapReadOnly returns a sandbox that refuses writes to each absolute // path, a file or a directory and everything beneath it, and refuses // renaming or removing the owning root and each directory between it and // a protected path, so protection can't be moved aside and replaced. Paths // must be existing, canonical, proper descendants of Workspace or a named // WritableRoots directory; aliases and missing paths fail closed. // Hosts keep these paths stable until every // process using the sandbox has exited. WrapReadOnly(s Spec, paths []string) (Spec, error)}ReadOnlyLauncher optionally protects paths within a workspace or writable root: environment-owned directories and git’s inputs (docs/design/enforcement.md). Materializing hosts require this capability before any process starts, including background processes predating a publication. The local environment protects git’s inputs under every launcher that implements it. Custom launchers must explicitly implement it; Wrap alone does not promise protection for generated files. None does not implement this interface.
Seatbelt
Section titled “Seatbelt”type Seatbelt struct { // contains filtered or unexported fields}Seatbelt is the sandbox-exec launcher (macOS). sandbox-exec isn’t documented as public API by Apple; zelk tests the profile against the macOS versions in its CI matrix.
NewSeatbelt
Section titled “NewSeatbelt”func NewSeatbelt(opts SeatbeltOptions) (*Seatbelt, error)NewSeatbelt returns the seatbelt launcher, after checking sandbox-exec exists. Sandboxed processes may read the active developer directory: macOS’s /usr/bin/git runs the git there, and Xcode’s lies outside the system directories. On Windows it returns ErrUnavailable.
Seatbelt.Enforcement
Section titled “Seatbelt.Enforcement”func (s *Seatbelt) Enforcement() env.EnforcementEnforcement reports seatbelt’s Exec row.
Seatbelt.Name
Section titled “Seatbelt.Name”func (*Seatbelt) Name() stringName returns “seatbelt”.
Seatbelt.Network
Section titled “Seatbelt.Network”func (s *Seatbelt) Network() NetworkNetwork returns the network mode the sandbox gives commands and the browser.
Seatbelt.Profile
Section titled “Seatbelt.Profile”func (s *Seatbelt) Profile(workspace, tmp string) stringProfile returns the sandbox profile for a workspace and a temporary directory. Paths are quoted as SBPL strings.
Seatbelt.Wrap
Section titled “Seatbelt.Wrap”func (s *Seatbelt) Wrap(sp Spec) (Spec, error)Wrap returns sandbox-exec running s under the profile.
Seatbelt.WrapBrowser
Section titled “Seatbelt.WrapBrowser”func (s *Seatbelt) WrapBrowser(ctx context.Context, br Browser) (Spec, error)WrapBrowser returns sandbox-exec running a browser in seatbelt’s browser profile (docs/design/enforcement.md, Browser profile (seatbelt)). Chrome’s own sandbox can’t start inside seatbelt, so the browser runs with –no-sandbox and the profile alone confines it: reads of the system directories, its install, ReadOnly and the workspace; writes to its data directory only; the launcher’s network mode, with the user’s local ports besides in off mode.
Seatbelt.WrapReadOnly
Section titled “Seatbelt.WrapReadOnly”func (s *Seatbelt) WrapReadOnly(sp Spec, paths []string) (Spec, error)WrapReadOnly denies writes to each path, and beneath it, in the sandbox profile, and writes to the name of each owning root and intermediate directory, so none can be renamed or removed.
SeatbeltOptions
Section titled “SeatbeltOptions”type SeatbeltOptions struct { // Path is sandbox-exec's absolute path; empty is Path string // Network is off or full. Network Network // Env names variables to keep beyond DefaultEnv. Env []string // ReadOnly are extra host paths readable inside, such as a toolchain // outside the system directories. ReadOnly []string // TempDir is the absolute path of a private directory the sandboxed // process may use as its home and temporary directory. Required: zelk // doesn't read TMPDIR on the host's behalf. TempDir string}SeatbeltOptions configure the seatbelt launcher.
type Spec struct { // Path is the executable's absolute path; Args the arguments after // the program name. Path string Args []string // CommandLine, when set, is the complete Windows command line. A // launcher must preserve its quoting when it wraps the workload. CommandLine string // Resources are owned by the caller of Wrap or WrapReadOnly. Close // them on start failure or after stopping and reaping the process // tree. A returned Spec with resources must be used for one start. Resources io.Closer // Env is the whole environment, as KEY=value pairs. Env []string // Dir is the absolute working directory. Before wrapping, the local // environment chooses a directory inside Workspace. Dir string // Workspace is the workspace's absolute path. Workspace string // WritableRoots are extra writable directories explicitly supplied by the // host or environment. Launchers never discover them (including git paths). // Each must exist and be a canonical absolute directory, with no symlink // aliases, overlaps with Workspace or another root, or redundant descendants. // Hosts keep these directory identities stable until all using processes exit. WritableRoots []string // Scratch, if set, is a directory the process may write beside the // workspace, such as the session's scratch directory: an existing // canonical absolute directory that the launcher makes writable. It // may lie inside Workspace. It isn't the process's TMPDIR, which stays // the launcher's own. The host keeps it until every process using it // has exited. Scratch string}Spec is a process about to start.
Terminator
Section titled “Terminator”type Terminator interface { // Terminate asks what the process leader, the PID the environment // started and hasn't reaped, runs in the sandbox to stop. It must // never signal a process that reused a PID. Terminate(leader int) error}Terminator is a launcher whose process asks of the host something other than SIGTERM to its group to stop gracefully, because the sandbox doesn’t pass the signal on: bwrap dies of SIGTERM, and its sandbox with it, without the command seeing it. An environment calls Terminate before it kills the group, and kills it whatever Terminate does.