sandbox

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package sandbox provides isolation for untrusted tool/skill execution.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backend added in v0.3.0

type Backend string

Backend identifies a sandbox backend type.

const (
	BackendProcess   Backend = "process"
	BackendContainer Backend = "container"
	BackendWASM      Backend = "wasm"
	BackendK8sJob    Backend = "k8s"
)

func ParseBackend added in v0.3.0

func ParseBackend(s string) Backend

ParseBackend parses a backend string.

type Config added in v0.3.0

type Config struct {
	Backend Backend `json:"backend"`
	WorkDir string  `json:"work_dir,omitempty"`
	// Container-specific
	Image   string `json:"image,omitempty"`
	Network string `json:"network,omitempty"`
	// Runtime selects a hardened OCI runtime for the container backend
	// (e.g. "runsc" for gVisor, "kata-runtime"). Empty uses the daemon default.
	Runtime string `json:"runtime,omitempty"`
	// K8s-specific
	Namespace  string `json:"namespace,omitempty"`
	ServiceAcc string `json:"service_account,omitempty"`
	// WASM-specific
	WASMPath string `json:"wasm_path,omitempty"`
}

Config holds configuration for sandbox creation.

type ContainerConfig

type ContainerConfig struct {
	Image       string
	SocketPath  string
	MemoryBytes int64
	CPUQuota    int64
	NetworkMode string
	// Hardening (P2-001). Zero values fall back to hardened defaults.
	User           string
	PidsLimit      int64
	NofileLimit    int64
	NprocLimit     int64
	CapAdd         []string
	SeccompProfile string
	// Runtime selects a hardened OCI runtime such as "runsc" (gVisor) or
	// "kata-runtime". Empty uses the daemon default (runc). It is never required.
	Runtime string
	// Tmpfs overrides the default writable /tmp mount. Nil uses the default.
	Tmpfs map[string]string
	// WritableRootfs disables the read-only rootfs hardening when true.
	WritableRootfs bool
}

ContainerConfig holds container sandbox configuration.

type ContainerPool added in v0.3.0

type ContainerPool struct {
	// contains filtered or unexported fields
}

ContainerPool maintains a pool of pre-warmed containers for reduced cold-start latency.

func NewPool added in v0.3.0

func NewPool(cfg PoolConfig) (*ContainerPool, error)

NewPool creates a ContainerPool with the given configuration.

func (*ContainerPool) Acquire added in v0.3.0

func (p *ContainerPool) Acquire() (Sandbox, error)

Acquire returns a ready sandbox from the pool. If the pool is empty, a new sandbox is created. Returns immediately if a warm container is available.

func (*ContainerPool) Close added in v0.3.0

func (p *ContainerPool) Close() error

Close shuts down the pool and all containers.

func (*ContainerPool) InUse added in v0.3.0

func (p *ContainerPool) InUse() int

InUse returns the number of containers currently in use.

func (*ContainerPool) Release added in v0.3.0

func (p *ContainerPool) Release(sb Sandbox) error

Release returns a sandbox to the pool. If the pool is full, the sandbox is closed.

func (*ContainerPool) Size added in v0.3.0

func (p *ContainerPool) Size() int

Size returns the current number of available containers.

func (*ContainerPool) Warmup added in v0.3.0

func (p *ContainerPool) Warmup(ctx context.Context, n int) error

Warmup pre-creates n containers in the pool.

type ContainerSandbox

type ContainerSandbox struct {
	Image string

	// Resource limits
	MemoryBytes int64
	CPUQuota    int64
	NetworkMode string
	// Hardening (P2-001)
	User           string            // non-root user/uid:gid the workload runs as
	PidsLimit      int64             // max processes inside the container
	NofileLimit    int64             // RLIMIT_NOFILE (open files)
	NprocLimit     int64             // RLIMIT_NPROC (processes/threads)
	ReadonlyRootfs bool              // mount the container root filesystem read-only
	CapAdd         []string          // capabilities to re-add on top of CapDrop ALL
	SeccompProfile string            // custom seccomp profile JSON; empty keeps Docker's default
	Runtime        string            // OCI runtime (e.g. "runsc" for gVisor, "kata-runtime")
	Tmpfs          map[string]string // writable tmpfs mounts (path -> mount options)
	// contains filtered or unexported fields
}

ContainerSandbox implements Sandbox using Docker Engine API for production-grade isolation.

func NewContainerSandbox

func NewContainerSandbox(cfg ContainerConfig) *ContainerSandbox

NewContainerSandbox creates a Docker-based sandbox with a hardened default profile: non-root user, all capabilities dropped, no-new-privileges, the default seccomp profile, a pids limit, file/process ulimits, a read-only rootfs backed by a small tmpfs /tmp, and memory/CPU limits.

func (*ContainerSandbox) Close

func (c *ContainerSandbox) Close() error

func (*ContainerSandbox) Execute

func (c *ContainerSandbox) Execute(ctx context.Context, command string, args []string, timeout time.Duration) (*Result, error)

type K8sJobConfig added in v0.3.0

type K8sJobConfig struct {
	Image          string
	Namespace      string
	ServiceAccount string
}

K8sJobConfig holds Kubernetes Job sandbox configuration.

type K8sJobSandbox added in v0.3.0

type K8sJobSandbox struct {
	// contains filtered or unexported fields
}

K8sJobSandbox is a placeholder for a future Kubernetes Job isolation backend. It is not yet implemented: no Kubernetes client (client-go) is wired in.

func NewK8sJobSandbox added in v0.3.0

func NewK8sJobSandbox(cfg K8sJobConfig) (*K8sJobSandbox, error)

NewK8sJobSandbox reports that the Kubernetes Job backend is not implemented.

Per P2-001 the stub backends fail at construction rather than deferring the error to execution time, so callers discover the missing capability immediately. It always returns a nil sandbox and a non-nil error.

func (*K8sJobSandbox) Close added in v0.3.0

func (s *K8sJobSandbox) Close() error

func (*K8sJobSandbox) Execute added in v0.3.0

func (s *K8sJobSandbox) Execute(_ context.Context, command string, _ []string, _ time.Duration) (*Result, error)

type PoolConfig added in v0.3.0

type PoolConfig struct {
	// MaxSize is the maximum number of containers in the pool (default 5).
	MaxSize int
	// MaxIdleTime is how long an idle container is kept before being destroyed (default 5m).
	MaxIdleTime time.Duration
	// Factory creates new sandbox instances.
	Factory func() (Sandbox, error)
}

PoolConfig configures a container pool.

type ProcessSandbox

type ProcessSandbox struct {
	WorkDir string
}

ProcessSandbox runs commands in a subprocess with timeouts. For production, replace with container-based isolation (gVisor, Firecracker, etc.).

func NewProcessSandbox

func NewProcessSandbox(workDir string) *ProcessSandbox

func (*ProcessSandbox) Close

func (s *ProcessSandbox) Close() error

func (*ProcessSandbox) Execute

func (s *ProcessSandbox) Execute(ctx context.Context, command string, args []string, timeout time.Duration) (*Result, error)

type Result

type Result struct {
	Stdout   string `json:"stdout"`
	Stderr   string `json:"stderr"`
	ExitCode int    `json:"exit_code"`
}

Result captures the output of a sandboxed execution.

type Sandbox

type Sandbox interface {
	Execute(ctx context.Context, command string, args []string, timeout time.Duration) (*Result, error)
	Close() error
}

Sandbox defines the interface for executing untrusted code.

func NewFromConfig added in v0.3.0

func NewFromConfig(cfg Config) (Sandbox, error)

NewFromConfig creates a sandbox based on the configuration. This is the factory function that selects the backend by config string.

type WASMSandbox added in v0.3.0

type WASMSandbox struct {
	// contains filtered or unexported fields
}

WASMSandbox is a placeholder for a future WebAssembly (WASI) isolation backend. It is not yet implemented: no WASM runtime (e.g. Wazero or Wasmtime) is wired in.

func NewWASMSandbox added in v0.3.0

func NewWASMSandbox(wasmPath string) (*WASMSandbox, error)

NewWASMSandbox reports that the WASM backend is not implemented.

Per P2-001 the stub backends fail at construction rather than deferring the error to execution time, so callers discover the missing capability immediately. It always returns a nil sandbox and a non-nil error.

func (*WASMSandbox) Close added in v0.3.0

func (s *WASMSandbox) Close() error

func (*WASMSandbox) Execute added in v0.3.0

func (s *WASMSandbox) Execute(_ context.Context, command string, _ []string, _ time.Duration) (*Result, error)

Jump to

Keyboard shortcuts

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