sandbox

package module
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: GPL-3.0 Imports: 3 Imported by: 6

README

libsandbox

libsandbox is a small Go library that provides a declarative API for running programs inside a sandbox environment.

It is a thin wrapper over the sandbox command-line tool and is responsible only for:

  • describing how a process should be executed inside a sandbox
  • assembling filesystem mappings, environment, and resource constraints
  • invoking the sandbox executable via os/exec

All execution semantics, isolation guarantees, and validation rules are defined by the sandbox tool itself.


Sandbox tool

This library relies on the external sandbox executable.

Canonical documentation, usage, and guarantees are defined in the sandbox repository:

👉 https://github.com/Highload-fun/sandbox

If something is unclear or unspecified in this library, always refer to the sandbox tool documentation.


Design goals

  • Declarative API — describe what should happen, not how
  • No duplication of CLI semantics — the sandbox tool is the single source of truth
  • Low-level and predictable — arguments are built explicitly and passed as-is
  • Minimal abstraction — no hidden behavior, no magic defaults

This makes libsandbox suitable for infrastructure code, CI systems, judges, and high-load execution environments.


Usage example

sb := sandbox.New("/tmp/sandbox")

cmd := sb.
    AddFile("/usr/bin/go", "/usr/bin/go", true).
    MountDir("/workspace", "/workspace").
    AddEnv("PATH=/usr/bin").
    SetNoNewNet(true).
    SetMemLimit(512 * 1024 * 1024).
    ExecDir("/workspace").
    Command("go", "test", "./...")

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

if err := cmd.Run(); err != nil {
    log.Fatal(err)
}

PassListener("127.0.0.1:3128", "/run/app/listener.sock") makes the sandbox listen on the address in its network namespace and pass the listener to the unix socket (not with SetNoNewNet(true)).


Package philosophy

  • The Go API expresses intent, not command-line syntax
  • The library does not attempt to validate sandbox configuration
  • Any future changes in sandbox flags should not require changes in libsandbox

If you need behavior that is not exposed here, it should be added to the sandbox tool first.


Concurrency

Sandbox is a mutable builder and is not safe for concurrent use.

If you need to reuse a base configuration across goroutines, create separate instances per execution.

Documentation

Overview

Package sandbox provides a Go API for executing programs inside a sandbox environment.

It is a thin, declarative wrapper over the sandbox tool, responsible only for configuration and process invocation. The sandbox executable defines all execution semantics and guarantees.

Sandbox tool documentation and usage:

https://github.com/Highload-fun/sandbox

Index

Constants

This section is empty.

Variables

View Source
var Path = "/usr/bin/sandbox"

Path points to the sandbox executable. It is used as the program invoked by exec.Command.

Functions

This section is empty.

Types

type Sandbox

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

Sandbox is a mutable builder that describes how a program should be executed inside a sandbox.

It accumulates filesystem mappings, environment configuration, resource limits, and execution parameters, which are later translated into sandbox tool arguments.

func New

func New(path string) *Sandbox

New creates a new sandbox configuration for the given sandbox root path.

func (*Sandbox) AddEnv

func (s *Sandbox) AddEnv(value string) *Sandbox

AddEnv adds an environment variable that will be visible to the sandboxed process.

func (*Sandbox) AddFile

func (s *Sandbox) AddFile(src, dst string, withLibs bool) *Sandbox

AddFile declares that a file from the host must be available inside the sandbox at the given location.

func (*Sandbox) BuildExecArgs

func (s *Sandbox) BuildExecArgs(path string, args []string) []string

BuildExecArgs converts the sandbox configuration into a complete argument list for the sandbox executable.

func (*Sandbox) Command

func (s *Sandbox) Command(path string, args ...string) *exec.Cmd

Command constructs an exec.Cmd that runs a command inside the configured sandbox.

func (*Sandbox) CommandContext

func (s *Sandbox) CommandContext(ctx context.Context, path string, args ...string) *exec.Cmd

CommandContext is identical to Command, but allows the execution to be bound to a context.

func (*Sandbox) ExecDir

func (s *Sandbox) ExecDir(dir string) *Sandbox

ExecDir sets the working directory inside the sandbox where the command will be executed.

func (*Sandbox) MountDir

func (s *Sandbox) MountDir(src, dst string) *Sandbox

MountDir declares that a directory from the host filesystem must be accessible inside the sandbox.

func (*Sandbox) PassListener

func (s *Sandbox) PassListener(addr, socketPath string) *Sandbox

PassListener makes the sandbox listen on addr in its network namespace and pass the listener to the unix socket at socketPath.

func (*Sandbox) SaveUsageStat

func (s *Sandbox) SaveUsageStat(filename string) *Sandbox

SaveUsageStat enables persisting execution statistics after the process exits.

func (*Sandbox) SetCGroup

func (s *Sandbox) SetCGroup(name string) *Sandbox

SetCGroup assigns the sandboxed process to a control group.

func (*Sandbox) SetCpuSet

func (s *Sandbox) SetCpuSet(set string) *Sandbox

SetCpuSet restricts which CPUs the sandboxed process may use.

func (*Sandbox) SetMemLimit

func (s *Sandbox) SetMemLimit(limit uint64) *Sandbox

SetMemLimit limits memory usage of the sandboxed process.

func (*Sandbox) SetNoNewNet

func (s *Sandbox) SetNoNewNet(v bool) *Sandbox

SetNoNewNet configures whether the sandboxed process is isolated from the network.

Jump to

Keyboard shortcuts

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