sandbox

package
v0.40.0 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2026 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnsurePrivateDir added in v0.38.0

func EnsurePrivateDir(path string) error

EnsurePrivateDir creates path if needed and enforces owner-only permissions. MkdirAll does not chmod existing directories, so callers must use this for security-sensitive shared host directories.

func HostEnvBuildHome added in v0.33.0

func HostEnvBuildHome(workDir string) string

HostEnvBuildHome returns the HOME value for host-execution sandbox backends. On Linux (with bwrap), HOME is remapped to /workspace; elsewhere it mirrors the working directory.

func HostEnvBuildPath added in v0.33.0

func HostEnvBuildPath(stellaHome string) string

HostEnvBuildPath returns a sanitized PATH suitable for host-execution sandbox backends (local, none). It prepends the mise shims and stella bin directories and filters host PATH entries to a safe allowlist on Linux.

func HostEnvCopy added in v0.33.0

func HostEnvCopy(env map[string]string)

HostEnvCopy copies a fixed allowlist of host environment variables into env. Only locale, terminal, and proxy variables are included.

func HostEnvPathAllowed added in v0.33.0

func HostEnvPathAllowed(entry, stellaBin string) bool

HostEnvPathAllowed reports whether a PATH entry is in the safe allowlist.

func IsPolicyCompatibilityError

func IsPolicyCompatibilityError(err error) bool

IsPolicyCompatibilityError reports whether an error is a policy compatibility error.

func LogSessionClosed

func LogSessionClosed(sessionID, backend, reason string)

func LogSessionCreated

func LogSessionCreated(sessionID, backend string, policy Policy)

func LogUnsupportedBackend

func LogUnsupportedBackend(policy Policy, attempted []string, reason string)

func MiseShimsDir added in v0.38.0

func MiseShimsDir(stellaHome string) string

MiseShimsDir returns the mise shims directory for host-execution sandbox backends. Tools installed by the manifest/org reconcilers are exposed here as shims (not copied into bin), so it must be on PATH for them to resolve.

func MiseToolsDir added in v0.38.0

func MiseToolsDir(stellaHome string) string

MiseToolsDir returns the root MISE_DATA_DIR for Stella-managed mise installs. This is the single source of truth for the on-disk layout: the manifest/org reconcilers install into it and the sandbox PATH is built from it, so both sides must derive their paths from here to stay in lockstep.

func NewSessionID

func NewSessionID() string

NewSessionID returns a cryptographically random session identifier. Using random IDs avoids Docker container name collisions across test runs.

func StellaHomeSandboxDirs added in v0.38.1

func StellaHomeSandboxDirs() []string

StellaHomeSandboxDirs returns the subdirectory names (relative to STELLA_HOME) that sandbox backends must expose. Every backend — bwrap, Docker, none — should derive its mount list from this slice so adding a new directory is a one-line change. The returned names use filepath.Separator and are safe to join with any absolute root.

Types

type DirEntry

type DirEntry struct {
	Name  string
	IsDir bool
	Size  int64
}

DirEntry is retained for use by prompt_host.go which reads directories via os.ReadDir and needs a uniform entry type shared with sandbox callers.

type ExecOptions

type ExecOptions struct {
	Cwd     string
	Env     map[string]string
	Timeout time.Duration
}

type ExecResult

type ExecResult struct {
	Stdout   string
	Stderr   string
	ExitCode int
}

type Factory

type Factory interface {
	CreateSession(ctx context.Context, policy Policy) (Session, error)
	Supported(policy Policy) error
	Name() string
	Available() bool
}

Factory creates sessions from policies.

type FilesystemPolicy

type FilesystemPolicy struct {
	// WorkspaceRoot is the host path mounted as the sandbox root. When empty,
	// WorkingDir is used for backwards compatibility.
	WorkspaceRoot string

	// WorkingDir is the logical working directory inside the sandbox root.
	WorkingDir string

	// ExtraReadOnlyMounts is a list of host paths to mount read-only inside the
	// sandbox at their exact host path (same-path strategy). Used for skill dirs
	// that live outside the workspace root but must be accessible for script execution.
	ExtraReadOnlyMounts []string

	// TempDirHost is the host directory mounted as /tmp inside the sandbox.
	// Empty means the backend chooses a session-local temp directory. Non-empty
	// directories are guaranteed to exist with 0700 permissions before the
	// backend sees the policy; they may be shared across sessions.
	TempDirHost string
}

FilesystemPolicy defines filesystem constraints for a sandbox session.

type Host

type Host = Session

Host is an alias for Session kept for internal use by the runner and core tools. New code should use Session directly.

type NetworkMode

type NetworkMode string

NetworkMode defines the network access mode for a sandbox session.

const (
	// NetworkDisabled blocks all network access.
	NetworkDisabled NetworkMode = "disabled"
	// NetworkAllowAll allows unrestricted network access.
	NetworkAllowAll NetworkMode = "allow_all"
)

type NetworkPolicy

type NetworkPolicy struct {
	// Mode is the network access mode: disabled | allow_all.
	Mode NetworkMode

	// Timeout for network operations. Zero means no timeout.
	Timeout time.Duration
}

NetworkPolicy defines network constraints for a sandbox session.

type Policy

type Policy struct {
	// Filesystem policy
	Filesystem FilesystemPolicy

	// Network policy
	Network NetworkPolicy

	// Env holds environment variables injected into sandboxed processes.
	Env map[string]string

	// InheritEnv includes system environment variables when true.
	InheritEnv bool

	// Timeout for process execution. Zero means no timeout.
	Timeout time.Duration
}

Policy is an immutable, backend-agnostic session policy describing requested limits for filesystem, network, and process constraints.

func (Policy) NetworkModeOrDefault

func (p Policy) NetworkModeOrDefault() NetworkMode

NetworkModeOrDefault returns the network mode with default applied.

func (Policy) Validate

func (p Policy) Validate() error

Validate returns an error if the policy contains invalid configurations. This validates policy structure, not backend compatibility.

func (Policy) WorkspaceRootOrDefault

func (p Policy) WorkspaceRootOrDefault() string

WorkspaceRootOrDefault returns the mounted sandbox root on the host.

type PolicyCompatibilityError

type PolicyCompatibilityError struct {
	Backend string
	Policy  Policy
	Reason  string
}

PolicyCompatibilityError indicates a policy is not compatible with a backend.

func (*PolicyCompatibilityError) Error

func (e *PolicyCompatibilityError) Error() string

type ProcessHandle

type ProcessHandle interface {
	PID() int
	Wait(ctx context.Context) (ExecResult, error)
	Stdin() io.WriteCloser
	Stdout() io.ReadCloser
	Stderr() io.ReadCloser
	Close() error
}

type ProcessRequest

type ProcessRequest struct {
	Path    string
	Args    []string
	Cwd     string
	Env     map[string]string
	Timeout time.Duration
}

type ResilientSession added in v0.38.0

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

ResilientSession wraps a Session and transparently recreates it when the underlying session has been closed unexpectedly (e.g. container exited or sandbox process crashed). Explicit Close calls are permanent — no recreation after that.

func NewResilientSession added in v0.38.0

func NewResilientSession(session Session, create SessionCreator) *ResilientSession

NewResilientSession wraps an existing session with auto-recreation support.

func (*ResilientSession) Alive added in v0.38.0

func (r *ResilientSession) Alive() bool

func (*ResilientSession) Close added in v0.38.0

func (r *ResilientSession) Close() error

func (*ResilientSession) Done added in v0.38.0

func (r *ResilientSession) Done() <-chan struct{}

func (*ResilientSession) Exec added in v0.38.0

func (r *ResilientSession) Exec(ctx context.Context, command string, opts ExecOptions) (ExecResult, error)

func (*ResilientSession) Policy added in v0.38.0

func (r *ResilientSession) Policy() Policy

func (*ResilientSession) ResolvePath added in v0.38.0

func (r *ResilientSession) ResolvePath(path string) (string, error)

func (*ResilientSession) ResolveWritePath added in v0.38.0

func (r *ResilientSession) ResolveWritePath(path string) (string, error)

func (*ResilientSession) StartProcess added in v0.38.0

func (r *ResilientSession) StartProcess(ctx context.Context, req ProcessRequest) (ProcessHandle, error)

func (*ResilientSession) WorkingDir added in v0.38.0

func (r *ResilientSession) WorkingDir() string

type Session

type Session interface {
	// Lifecycle
	Policy() Policy
	Close() error
	Alive() bool
	Done() <-chan struct{}

	// Host process surface
	Exec(ctx context.Context, command string, opts ExecOptions) (ExecResult, error)
	StartProcess(ctx context.Context, req ProcessRequest) (ProcessHandle, error)

	// Path resolution — use os.* with the resolved path for file I/O.
	// ResolvePath validates read access; use ResolveWritePath for write operations.
	ResolvePath(path string) (string, error)
	// ResolveWritePath is like ResolvePath but additionally rejects paths in
	// read-only mounts (e.g. ExtraReadOnlyMounts / skill directories).
	ResolveWritePath(path string) (string, error)
	WorkingDir() string
}

Session is the plugin-facing sandbox surface: lifecycle + mediated host access. It combines what was previously the Session lifecycle interface and the Host file/process interface into a single type so plugins receive one coherent value.

func NopSession

func NopSession() Session

NopSession returns a no-op session for testing.

type SessionCreator added in v0.38.0

type SessionCreator func(ctx context.Context) (Session, error)

SessionCreator creates a new session. Used by ResilientSession to recreate the underlying session when it is found closed.

Jump to

Keyboard shortcuts

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