sandbox

package module
v0.0.0-...-e435060 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: MIT Imports: 8 Imported by: ? 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

src/cto/sandbox

The sandbox contract that ext and workload share, from the sandboxing ADR. A sandbox is three decisions: the boundary (where isolation is enforced), the policy (what code can touch) and the lifecycle (what survives between runs).

This module is data and rules only. It confines nothing and starts nothing, and it uses only the standard library and nothing else in ext, so a runner that must stay on the standard library can depend on it. Mechanisms live elsewhere:

  • src/cto/internal/confine confines one local process, through ext sandbox exec;
  • workload's drivers run containers and VMs.
import "github.com/webong/ext/src/cto/sandbox"
Piece What it is
Boundary in-process, process, confined, runtime, container or kernel. Not a ranking: a consumer states the set it accepts (Accept)
Policy Filesystem paths and their access, network (none, ports, proxy with a host allowlist, or open), child processes, devices, resource bounds, environment, lifecycle (fresh, workspace or snapshot). The same shape is a request, a policy and a grant
Grant The overlap of what a package requests and what the consumer's policy allows, plus what was refused. A request never grants anything
Capabilities What one runner can enforce, class by class, with a fixed reason for anything missing: unsupported, permission or tool_missing
Enforce Fails closed: a class the grant needs that the runner cannot enforce stops the launch with unsupported, unless the caller accepted it unenforced
Report What every launch returns: boundary, driver, grant, refusals, and the classes enforced and unenforced. Sandboxed tells whether it meets the evaluator isolation sandboxed
Plan All of the above in one call, before a launch

Errors carry a stable code (Code): invalid, denied or unsupported.

Fixtures

testdata/v1/cases.json is the contract; testdata/codes.json lists its codes. Every implementation runs every section, and where an implementation and a fixture disagree, the fixture decides. The Go runner is fixtures_test.go; EXT_SANDBOX_TESTDATA points it at a copy elsewhere. A change to an existing case makes a v2 directory, so a binding that has not caught up keeps running v1.

Documentation

Overview

Package sandbox is the portable sandbox contract that ext and github.com/webong/workload share (docs/adr-sandboxing.md): the boundary levels, the policy and lifecycle vocabulary, the rules that turn a request and a policy into a grant, the check of a grant against what a runner can enforce, and the report every launch returns.

It is data and rules only. It confines nothing, starts nothing and imports nothing but the standard library, so a runner that must stay on the standard library can depend on it. The mechanisms that enforce a grant live elsewhere: src/cto/internal/confine for one local process, workload's drivers for containers and VMs.

The behaviour is fixed by the fixtures under testdata/v1, which every implementation runs. Where this package and a fixture disagree, the fixture is right.

Index

Constants

View Source
const (
	ReasonUnsupported = "unsupported"
	ReasonPermission  = "permission"
	ReasonToolMissing = "tool_missing"
)

The reasons a class is not enforced (docs/adr-architecture.md, rule 2).

View Source
const (
	MaxPaths       = 256
	MaxPathBytes   = 4096
	MaxPorts       = 256
	MaxHosts       = 256
	MaxHostBytes   = 253
	MaxDevices     = 64
	MaxDeviceBytes = 128
	MaxEnvNames    = 256
	MaxEnvBytes    = 32768
	MaxNameBytes   = 256
	MaxRefBytes    = 1024
)

The bounds of a policy (docs/adr-architecture.md, rule 4). testdata/v1 states the same numbers under "limits".

View Source
const (
	// NetworkNone reaches nothing.
	NetworkNone = "none"
	// NetworkPorts reaches the listed TCP ports on any address, where a boundary can enforce
	// ports without hostnames.
	NetworkPorts = "ports"
	// NetworkProxy reaches only an egress proxy that allows the listed hosts.
	NetworkProxy = "proxy"
	// NetworkOpen reaches anything. A grant with an open network is never sandboxed.
	NetworkOpen = "open"
)

The network modes. An empty mode is None.

View Source
const (
	// LifecycleFresh starts from new state, deleted afterwards.
	LifecycleFresh = "fresh"
	// LifecycleWorkspace keeps a named directory or volume across runs.
	LifecycleWorkspace = "workspace"
	// LifecycleSnapshot restores a named snapshot before the run.
	LifecycleSnapshot = "snapshot"
)

The lifecycle modes. An empty mode is Fresh.

View Source
const (
	// RestoreFilesystem restores files only.
	RestoreFilesystem = "filesystem"
	// RestoreMemory restores memory and device state as well, as a VM snapshot does.
	RestoreMemory = "memory"
)

The scopes a snapshot restores.

View Source
const AdmissionApproval = "run\n"

AdmissionApproval is the complete approval the consumer writes to the runner.

View Source
const AdmissionOperation = "run-verified"

AdmissionOperation is the evaluator adapter operation that gates execution on the report from the prepared runner.

Variables

View Source
var (
	// ErrInvalid reports a malformed boundary, policy, capability set, class or report.
	ErrInvalid = errors.New("sandbox: invalid")
	// ErrDenied reports a boundary outside the set the consumer accepts.
	ErrDenied = errors.New("sandbox: boundary not accepted")
	// ErrUnsupported reports a grant that needs a class the runner cannot enforce and that the
	// caller did not accept unenforced.
	ErrUnsupported = errors.New("sandbox: class not enforced")
)

The errors of the contract. Code maps each to its stable code. Error text never carries a value it was given.

Boundaries lists every level, in the order of docs/adr-sandboxing.md.

View Source
var Classes = []string{
	"filesystem",
	"network.none",
	"network.ports",
	"network.proxy",
	"network.open",
	"process",
	"devices",
	"environment",
	"resources.memoryBytes",
	"resources.milliCpu",
	"resources.pids",
	"resources.cpuTimeMs",
	"resources.wallTimeMs",
	"resources.openFiles",
	"resources.writeBytes",
	"lifecycle.fresh",
	"lifecycle.workspace",
	"lifecycle.snapshot.filesystem",
	"lifecycle.snapshot.memory",
}

The classes a runner reports it can enforce. A grant needs a fixed set of them (Required): denying the filesystem, process creation and devices, and scrubbing the environment, always; the class of its network mode; the class of its lifecycle; and one class per resource bound.

Functions

func Accept

func Accept(accepted []Boundary, b Boundary) error

Accept checks b against the set a consumer accepts. An empty set accepts every level. It returns ErrInvalid when b or a member of the set is not a level, and ErrDenied when b is not in the set.

func AwaitAdmission

func AwaitAdmission(input io.Reader, output io.Writer, report Report) error

AwaitAdmission sends the prepared runner's report on output and waits for an explicit approval on input. Only a nil result permits guest execution.

func Code

func Code(err error) string

Code returns the stable code of err: "invalid", "denied" or "unsupported", or "" for nil or an error that is not one of this package's.

func Grant

func Grant(request, policy Policy) (Policy, []Refusal, error)

Grant returns the overlap of a request (what a package needs) and a policy (what a consumer allows), and the parts of the request that were refused. A request never grants anything by itself: everything in the grant is allowed by the policy. To grant a policy as it stands, pass it as the request too.

The rules, per class (testdata/v1 "grant"):

  • filesystem: each requested path gets the requested access that some policy path at or above it allows; the rest is refused, and a path with no access left is dropped;
  • network: a requested mode is granted when the policy has the same mode (ports and hosts are intersected) or an open network; anything else is refused and the grant reaches nothing;
  • process: children only when both allow them;
  • devices: the requested devices the policy lists;
  • resources: the lower of the two where both set a bound, else whichever is set; a request above the policy's bound is refused and the policy's bound is granted;
  • environment: the entries and secrets the policy supplies for the requested names (a request's names, and the names of its own entries and secrets, whose values are never used); a name the policy does not supply is refused;
  • lifecycle: the policy's, which must name its workspace or snapshot; a requested mode or restore scope that differs is refused.

The grant states the network and lifecycle modes explicitly. Grant returns ErrInvalid when either input fails Validate, or when the policy's workspace or snapshot has no name.

func Required

func Required(grant Policy) ([]string, error)

Required returns the classes a grant needs enforced, in the order of Classes. It returns ErrInvalid when grant fails Validate.

func Sandboxed

func Sandboxed(r Report) bool

Sandboxed reports whether a launch meets the evaluator isolation "sandboxed" (docs/evaluator-adapters.md): its boundary is confined, runtime, container or kernel, every required class was enforced, and its network is not open. A report that fails Validate is never sandboxed.

Types

type Access

type Access string

Access is one kind of access to a path.

const (
	Read    Access = "read"
	Write   Access = "write"
	Execute Access = "execute"
)

The kinds of access, in their canonical order.

type Boundary

type Boundary string

Boundary is where isolation is enforced. The levels are not a ranking: each moves the attack surface somewhere else. A consumer states the set it accepts.

const (
	// InProcess shares the host's address space and authority.
	InProcess Boundary = "in-process"
	// Process is a separate process with the user's full authority.
	Process Boundary = "process"
	// Confined is a separate process on the host kernel, restricted by OS policy.
	Confined Boundary = "confined"
	// Runtime has no syscall ABI; only the imports the host supplies.
	Runtime Boundary = "runtime"
	// Container is a process in namespaces on the host kernel, managed by a container runtime.
	Container Boundary = "container"
	// Kernel has its syscalls served by a separate kernel: a userspace kernel or a guest kernel
	// behind hardware virtualization.
	Kernel Boundary = "kernel"
)

The boundary levels.

func (Boundary) Valid

func (b Boundary) Valid() bool

Valid reports whether b is one of the levels.

type Capabilities

type Capabilities struct {
	Boundary Boundary           `json:"boundary"`
	Driver   string             `json:"driver,omitempty"`
	Classes  map[string]Support `json:"classes,omitempty"`
}

Capabilities is what a runner (one driver on this host) can enforce. A class that is not listed is not enforced, for the reason "unsupported".

func (Capabilities) Validate

func (c Capabilities) Validate() error

Validate checks that the boundary is a level, every class is known, an enforced class has no reason and a class that is not enforced has one of the reasons.

type Environment

type Environment struct {
	Names   []string `json:"names,omitempty"`
	Entries []string `json:"entries,omitempty"`
	Secrets []Secret `json:"secrets,omitempty"`
}

Environment is the environment beyond the runner's scrubbed base. A request lists the Names it needs; a policy supplies Entries ("KEY=value") and Secrets.

type Host

type Host struct {
	Host string `json:"host"`
	Port int    `json:"port"`
}

Host is one destination an egress proxy allows: an exact lowercase hostname or address and a TCP port.

type Lifecycle

type Lifecycle struct {
	Mode    string `json:"mode,omitempty"`
	Name    string `json:"name,omitempty"`
	Restore string `json:"restore,omitempty"`
}

Lifecycle is what survives between runs. Only a policy names the workspace or snapshot; the host chooses where state lives.

type Missing

type Missing struct {
	Class  string `json:"class"`
	Reason string `json:"reason"`
}

Missing is one required class a runner does not enforce, and why.

type Network

type Network struct {
	Mode  string `json:"mode,omitempty"`
	Ports []int  `json:"ports,omitempty"`
	Hosts []Host `json:"hosts,omitempty"`
}

Network is what the code can reach.

type Outcome

type Outcome struct {
	Enforced   []string  `json:"enforced"`
	Missing    []Missing `json:"missing,omitempty"`
	Unenforced []string  `json:"unenforced,omitempty"`
}

Outcome is what Enforce decided: the required classes the runner enforces, the ones it does not, and of those, the ones the caller accepted unenforced.

func Enforce

func Enforce(grant Policy, caps Capabilities, accept []string) (Outcome, error)

Enforce checks a grant against a runner's capabilities. Every class the grant needs must be enforced, or be named in accept, the classes the caller agrees to run without. It fails closed: when a required class is neither, it returns ErrUnsupported with the outcome, whose Missing lists every such class, and the launch must not happen. Classes in accept that the grant does not need are ignored. Enforce returns ErrInvalid for an invalid grant, capability set or unknown class in accept.

type Path

type Path struct {
	// Path is absolute and slash-separated: "/" and its descendants, or a drive such as "C:/"
	// and its descendants. It has no empty, "." or ".." component and no trailing slash except
	// at a root.
	Path   string   `json:"path"`
	Access []Access `json:"access"`
}

Path grants or requests access to one absolute path and everything below it.

type Policy

type Policy struct {
	Filesystem  []Path        `json:"filesystem,omitempty"`
	Network     Network       `json:"network,omitzero"`
	Process     ProcessPolicy `json:"process,omitzero"`
	Devices     []string      `json:"devices,omitempty"`
	Resources   Resources     `json:"resources,omitzero"`
	Environment Environment   `json:"environment,omitzero"`
	Lifecycle   Lifecycle     `json:"lifecycle,omitzero"`
}

Policy is what code can touch inside a boundary. The same shape serves as a request (what a package needs), a policy (what a consumer allows) and a grant (their overlap). Everything not listed is denied.

func (Policy) Validate

func (p Policy) Validate() error

Validate checks the shape and bounds of p. It does not decide whether p is acceptable: that is Grant and Enforce.

type ProcessPolicy

type ProcessPolicy struct {
	Children bool `json:"children,omitempty"`
}

ProcessPolicy says whether the code may start other processes. No new privileges is always enforced and is not a field.

type Refusal

type Refusal struct {
	Class  string   `json:"class"`
	Path   string   `json:"path,omitempty"`
	Access []Access `json:"access,omitempty"`
	Host   *Host    `json:"host,omitempty"`
	Port   int      `json:"port,omitempty"`
	Name   string   `json:"name,omitempty"`
}

Refusal is one part of a request that the policy did not allow. Class names the policy class (see Classes); the other fields say which item, where the class has items.

type Report

type Report struct {
	Boundary   Boundary  `json:"boundary"`
	Driver     string    `json:"driver,omitempty"`
	Grant      Policy    `json:"grant"`
	Refused    []Refusal `json:"refused,omitempty"`
	Enforced   []string  `json:"enforced"`
	Unenforced []string  `json:"unenforced,omitempty"`
}

Report is what every launch returns and what a consumer records: where it ran, what was requested, granted and refused, and what was actually enforced.

func Plan

func Plan(request, policy Policy, accepted []Boundary, caps Capabilities, acceptUnenforced []string) (Report, error)

Plan is the whole decision before a launch: accept the boundary, compute the grant and check it against the runner. It returns the report to hand to the consumer, or the first error: ErrInvalid, ErrDenied for a boundary outside accepted, or ErrUnsupported (with the report's grant and refusals filled in) for a class the runner cannot enforce and the caller did not accept.

func ReadAdmissionReport

func ReadAdmissionReport(input *bufio.Reader) (Report, error)

ReadAdmissionReport reads one bounded JSON line. Use the same buffered reader for subsequent output from the runner. An empty stream returns io.EOF.

func (Report) Validate

func (r Report) Validate() error

Validate checks that the report is well formed: a level, a valid grant, known classes, every required class either enforced or unenforced and none both.

type Resources

type Resources struct {
	MemoryBytes *int64 `json:"memoryBytes,omitempty"`
	MilliCPU    *int64 `json:"milliCpu,omitempty"`
	Pids        *int64 `json:"pids,omitempty"`
	CPUTimeMs   *int64 `json:"cpuTimeMs,omitempty"`
	WallTimeMs  *int64 `json:"wallTimeMs,omitempty"`
	OpenFiles   *int64 `json:"openFiles,omitempty"`
	WriteBytes  *int64 `json:"writeBytes,omitempty"`
}

Resources are bounds. A nil field sets no bound; an explicit zero or negative value is invalid.

type Secret

type Secret struct {
	Name string `json:"name"`
	Ref  string `json:"ref"`
}

Secret names an environment variable whose value the runner resolves from Ref at start. A policy never carries a secret's value.

type Support

type Support struct {
	Enforced bool   `json:"enforced"`
	Reason   string `json:"reason,omitempty"`
}

Support is whether a runner enforces one class here, and why not.

Jump to

Keyboard shortcuts

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