sandbox

package
v0.5.11 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package sandbox 提供 AI Agent 的沙箱代码执行环境

本包提供代码执行后端,并明确区分安全隔离与宿主进程 opt-in:

  • Docker 容器沙箱
  • 进程沙箱占位模式(当前 fail-closed,不执行)
  • 显式不安全的宿主进程模式(仅可信代码)
  • WebAssembly 类型占位(当前 unsupported,fail-closed)

Docker 后端的安全配置包括:

  • 资源限制 (CPU/内存)
  • 网络隔离
  • 文件系统隔离
  • 执行超时

使用示例:

sb := sandbox.New(&sandbox.Config{
    Type:    sandbox.TypeDocker,
    Image:   "python:3.11-slim",
    Timeout: 30 * time.Second,
})
execTool := sb.ExecuteTool()

Index

Constants

This section is empty.

Variables

View Source
var ErrProcessIsolationUnavailable = errors.New("process sandbox isolation unavailable")

ErrProcessIsolationUnavailable 表示 TypeProcess 无法兑现其安全隔离契约。

Functions

func BashSandbox

func BashSandbox(useDocker bool) tool.Tool

BashSandbox 创建 Bash 沙箱工具;非 Docker 模式会 fail-closed。

func MultiLanguageSandbox

func MultiLanguageSandbox(useDocker bool) tool.Tool

MultiLanguageSandbox 创建多语言沙箱工具;非 Docker 模式会 fail-closed。

func NodeSandbox

func NodeSandbox(useDocker bool) tool.Tool

NodeSandbox 创建 Node.js 沙箱工具;非 Docker 模式会 fail-closed。

func PythonSandbox

func PythonSandbox(useDocker bool) tool.Tool

PythonSandbox 创建 Python 沙箱工具。useDocker=false 选择 fail-closed 的 TypeProcess;可信代码若确需直接运行宿主进程,必须显式使用 TypeUnsafeProcess。

Types

type Config

type Config struct {
	// Type 沙箱类型
	Type SandboxType

	// Image Docker 镜像名称
	Image string

	// Timeout 执行超时时间
	Timeout time.Duration

	// Memory 内存限制(字节)
	Memory int64

	// CPU CPU 限制(核心数)
	CPU float64

	// NetworkEnabled 是否允许网络访问
	NetworkEnabled bool

	// Mounts 挂载点
	Mounts []Mount

	// Env 环境变量
	Env []string

	// MaxOutputSize 最大输出大小
	MaxOutputSize int

	// WorkDir 容器内工作目录
	WorkDir string

	// User 运行用户
	User string
}

Config 沙箱配置

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig 返回默认配置

func DefaultDockerConfig

func DefaultDockerConfig() *Config

DefaultDockerConfig 返回默认 Docker 配置

type ExecuteInput

type ExecuteInput struct {
	Language string            `json:"language" desc:"编程语言(python, javascript, go, bash)" required:"true"`
	Code     string            `json:"code" desc:"要执行的代码" required:"true"`
	Timeout  int               `json:"timeout" desc:"超时时间(秒)"`
	Files    map[string]string `json:"files" desc:"要创建的文件(路径 -> 内容)"`
}

ExecuteInput 执行输入

type ExecuteOutput

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

ExecuteOutput 执行输出

type Mount

type Mount struct {
	Source   string `json:"source"`
	Target   string `json:"target"`
	ReadOnly bool   `json:"read_only"`
}

Mount 挂载点

type Sandbox

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

Sandbox 沙箱

func New

func New(config *Config) *Sandbox

New 创建沙箱

func (*Sandbox) Cleanup

func (s *Sandbox) Cleanup()

Cleanup 清理临时文件

func (*Sandbox) ExecuteTool

func (s *Sandbox) ExecuteTool() tool.Tool

ExecuteTool 返回沙箱执行工具

func (*Sandbox) Stop

func (s *Sandbox) Stop(id string) error

Stop 停止沙箱

type SandboxType

type SandboxType string

SandboxType 沙箱类型

const (
	// TypeDocker Docker 沙箱
	TypeDocker SandboxType = "docker"
	// TypeProcess 保留原进程沙箱配置值。当前实现无法兑现网络、文件系统与
	// 全部资源隔离,因此 Execute 会 fail-closed,不会运行宿主进程。
	TypeProcess SandboxType = "process"
	// TypeUnsafeProcess 显式选择在宿主机直接运行进程。此模式只提供超时和
	// best-effort 资源限制,不提供安全隔离,不得用于不可信代码。
	TypeUnsafeProcess SandboxType = "unsafe_process"
	// TypeWASM WebAssembly 沙箱
	TypeWASM SandboxType = "wasm"
)

Jump to

Keyboard shortcuts

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