sandbox

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsPolicyCompatibilityError

func IsPolicyCompatibilityError(err error) bool

IsPolicyCompatibilityError reports whether an error is a policy compatibility error.

func LogExceptionPath

func LogExceptionPath(exceptionID, component, accessType, detail string)

func LogPolicyDenied

func LogPolicyDenied(sessionID, backend, operation, resource, reason string)

func LogRelaxedMode

func LogRelaxedMode(sessionID, backend, reason string, policy Policy, warnings ...string)

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 NewSessionID

func NewSessionID() string

NewSessionID returns a unique session identifier for backend implementations.

Types

type DirEntry

type DirEntry struct {
	Name  string
	IsDir bool
	Size  int64
}

type Edit

type Edit struct {
	OldText string
	NewText string
}

type EditResult

type EditResult struct {
	AppliedEdits int
}

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

	// ReadOnlyPaths are paths mounted read-only into the sandbox.
	ReadOnlyPaths []string

	// ReadWritePaths are paths allowed for read-write access.
	// If empty, WorkingDir is used.
	ReadWritePaths []string

	// AllowEscapes permits paths outside WorkingDir when true.
	// This is an explicit relaxation that should be used with care.
	AllowEscapes bool
}

FilesystemPolicy defines filesystem constraints for a sandbox session.

type HTTPOptions

type HTTPOptions struct {
	Method  string
	URL     string
	Header  map[string]string
	Body    []byte
	Timeout time.Duration
}

type HTTPResult

type HTTPResult struct {
	StatusCode int
	Header     map[string][]string
	Body       []byte
}

type HTTPStream

type HTTPStream interface {
	Header() map[string][]string
	Reader() io.ReadCloser
	Close() error
}

type Host

type Host interface {
	ReadFile(ctx context.Context, path string, offset, limit int) (ReadResult, error)
	WriteFile(ctx context.Context, path string, content []byte) (WriteResult, error)
	EditFile(ctx context.Context, path string, edits []Edit) (EditResult, error)
	Stat(ctx context.Context, path string) (StatResult, error)
	ListDir(ctx context.Context, path string) ([]DirEntry, error)
	MkdirAll(ctx context.Context, path string, perm uint32) error
	Remove(ctx context.Context, path string, recursive bool) error
	Rename(ctx context.Context, oldPath, newPath string) error
	CreateTemp(ctx context.Context, dir, pattern string) (TempFile, error)
	Exec(ctx context.Context, command string, opts ExecOptions) (ExecResult, error)
	StartProcess(ctx context.Context, req ProcessRequest) (ProcessHandle, error)
	HTTPRequest(ctx context.Context, opts HTTPOptions) (HTTPResult, error)
	OpenHTTPStream(ctx context.Context, opts HTTPOptions) (HTTPStream, error)
	ResolvePath(path string) (string, error)
	WorkingDir() string
}

Host provides mediated access to host resources within a sandbox session.

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"
	// NetworkWhitelist allows only specified hosts/CIDRs.
	NetworkWhitelist NetworkMode = "whitelist"
)

type NetworkPolicy

type NetworkPolicy struct {
	// Mode is the network access mode.
	Mode NetworkMode

	// Allowlist contains CIDRs/hosts allowed in whitelist mode.
	Allowlist []string

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

NetworkPolicy defines network constraints for a sandbox session.

type Policy

type Policy struct {
	// Backend selects a specific backend when non-empty. Empty means auto-select.
	Backend string

	// Relaxed explicitly allows reduced enforcement on partially compatible backends.
	// It is never implied by fallback and must be explicitly set.
	Relaxed bool

	// Filesystem policy
	Filesystem FilesystemPolicy

	// Network policy
	Network NetworkPolicy

	// Process policy
	Process ProcessPolicy
}

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

func (Policy) IsRelaxed

func (p Policy) IsRelaxed() bool

IsRelaxed returns true if this policy explicitly opts into relaxed enforcement.

func (Policy) NetworkModeOrDefault

func (p Policy) NetworkModeOrDefault() NetworkMode

NetworkModeOrDefault returns the network mode with default applied.

func (Policy) RequiresWhitelist

func (p Policy) RequiresWhitelist() bool

RequiresWhitelist returns true if the policy requires whitelist network mode.

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
	RelaxedWouldHelp bool
}

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 ProcessPolicy

type ProcessPolicy struct {
	// MaxConcurrent limits concurrent processes. Zero means unlimited.
	MaxConcurrent int

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

	// Environment variables to set for processes.
	Environment map[string]string

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

ProcessPolicy defines process constraints for a sandbox session.

type ProcessRequest

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

type ReadResult

type ReadResult struct {
	Content    []byte
	Truncated  bool
	NextOffset int
}

type Registry

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

Registry manages available backend factories and creates sessions.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates an empty registry.

func (*Registry) AvailableBackends

func (r *Registry) AvailableBackends() []string

AvailableBackends returns available factory names in registration order.

func (*Registry) CreateRelaxedSession

func (r *Registry) CreateRelaxedSession(ctx context.Context, base Policy) (Session, error)

CreateRelaxedSession creates a session with relaxed policy.

func (*Registry) CreateSession

func (r *Registry) CreateSession(ctx context.Context, policy Policy) (Session, error)

CreateSession creates a session using a compatible backend.

func (*Registry) Get

func (r *Registry) Get(name string) Factory

Get returns the factory with the given name, or nil if not found.

func (*Registry) List

func (r *Registry) List() []string

List returns all registered factory names in registration order.

func (*Registry) Register

func (r *Registry) Register(factory Factory) error

Register adds a factory to the registry.

func (*Registry) Unregister

func (r *Registry) Unregister(name string)

Unregister removes a factory from the registry.

type Session

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

Session is a sandbox-managed execution boundary.

func NopSession

func NopSession() Session

NopSession returns a no-op session for testing.

type StatResult

type StatResult struct {
	Exists  bool
	IsDir   bool
	Size    int64
	Mode    uint32
	ModTime time.Time
}

type TempFile

type TempFile interface {
	Path() string
	Write([]byte) (int, error)
	Close() error
}

type WriteResult

type WriteResult struct {
	BytesWritten int
}

Jump to

Keyboard shortcuts

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