Files
Cyrene/backend/ai-core/internal/host/sandbox.go
T
AskaEth 91c9ee4b2d fix: 修复 AI 回复无法送达发送者 + 重复消息 + action角色泄露 + OS环境支持
广播逻辑重构:
- AI 回复 (stream_start/response/stream_segments/multi_message/stream_end) 改用 broadcastToUser 发送给所有客户端
- 用户消息回显保持 broadcastToUserExcept 排除发送者

消息去重与角色修复:
- CacheMessage(user) 移至回复生成后,避免本轮 LLM 调用出现重复用户消息
- action 角色消息在 DB 存储时映射为 assistant,DeepSeek 等模型不支持自定义角色
- stream_end defer 机制确保错误路径也会终止客户端思考指示器

OS 完整环境支持:
- host 包重构为 HostBackend 接口 + Direct/WSL/Docker 三种后端
- 新增 os_exec/os_file/os_system 工具供 AI 在完整 Linux 环境中自由操作

其他:
- 视觉模型注入 + 图片预处理后清空 Images 避免传给 Chat 模型
- 图片 URL 相对路径→绝对 URL 转换
- DevTools 链路追踪页面 + 重启修复
- 记忆搜索模糊匹配增强
- 后台思考定时调度支持
- 管理后台页面 (模型配置/用户管理等)
- docs/api 更新广播机制说明

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-29 12:46:17 +08:00

228 lines
5.4 KiB
Go

package host
import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
)
// SandboxConfig configures the sandbox execution environment.
type SandboxConfig struct {
AllowedCommands []string
AllowedDirs []string
MaxOutputBytes int
DefaultTimeout time.Duration
MaxTimeout time.Duration
}
// DefaultSandboxConfig returns a safe default configuration.
func DefaultSandboxConfig() SandboxConfig {
return SandboxConfig{
AllowedCommands: []string{
"echo", "cat", "ls", "dir", "pwd", "date", "time",
"wc", "head", "tail", "sort", "uniq", "grep", "find",
"python", "python3", "node", "go", "rustc", "cargo",
"git", "curl", "wget", "ping", "nslookup", "tracert",
"dotnet", "java", "javac", "gcc", "g++", "make", "cmake",
"npm", "npx", "yarn", "pnpm", "pip", "pip3",
"docker", "kubectl", "helm",
"ffmpeg", "ffprobe", "imagemagick", "convert",
"systeminfo", "tasklist", "taskkill", "netstat",
},
MaxOutputBytes: 512 * 1024,
DefaultTimeout: 30 * time.Second,
MaxTimeout: 300 * time.Second,
}
}
// Sandbox provides a safe execution environment for host commands.
type Sandbox struct {
cfg SandboxConfig
}
// NewSandbox creates a new sandbox.
func NewSandbox(cfg SandboxConfig) *Sandbox {
return &Sandbox{cfg: cfg}
}
// DirEntry represents a filesystem directory entry.
type DirEntry struct {
Name string `json:"name"`
IsDir bool `json:"is_dir"`
Size int64 `json:"size"`
ModTime string `json:"mod_time,omitempty"`
}
// ExecResult holds the result of a sandboxed command execution.
type ExecResult struct {
Stdout string `json:"stdout"`
Stderr string `json:"stderr"`
ExitCode int `json:"exit_code"`
Duration string `json:"duration"`
TimedOut bool `json:"timed_out"`
}
// Exec runs a command inside the sandbox. The command string is parsed into
// the executable name and arguments. Returns the combined output.
func (s *Sandbox) Exec(ctx context.Context, command string, workDir string, timeout time.Duration) (*ExecResult, error) {
if command == "" {
return nil, fmt.Errorf("empty command")
}
parts := strings.Fields(command)
if len(parts) == 0 {
return nil, fmt.Errorf("empty command")
}
cmdName := parts[0]
var args []string
if len(parts) > 1 {
args = parts[1:]
}
if !s.isCommandAllowed(cmdName) {
return nil, fmt.Errorf("command not allowed: %s", cmdName)
}
if workDir == "" {
workDir = s.defaultWorkDir()
}
if err := s.validateWorkDir(workDir); err != nil {
return nil, err
}
if timeout <= 0 {
timeout = s.cfg.DefaultTimeout
}
if timeout > s.cfg.MaxTimeout {
timeout = s.cfg.MaxTimeout
}
execCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
cmd := exec.CommandContext(execCtx, cmdName, args...)
cmd.Dir = workDir
cmd.Env = s.filteredEnv()
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
start := time.Now()
err := cmd.Run()
elapsed := time.Since(start)
result := &ExecResult{
Duration: elapsed.Round(time.Millisecond).String(),
}
if stdout.Len() > s.cfg.MaxOutputBytes {
result.Stdout = stdout.String()[:s.cfg.MaxOutputBytes] + "\n... [output truncated]"
} else {
result.Stdout = stdout.String()
}
if stderr.Len() > s.cfg.MaxOutputBytes {
result.Stderr = stderr.String()[:s.cfg.MaxOutputBytes] + "\n... [output truncated]"
} else {
result.Stderr = stderr.String()
}
if execCtx.Err() == context.DeadlineExceeded {
result.TimedOut = true
result.ExitCode = -1
return result, fmt.Errorf("command timed out after %s", timeout)
}
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
result.ExitCode = exitErr.ExitCode()
} else {
result.ExitCode = -1
}
} else {
result.ExitCode = 0
}
return result, err
}
func (s *Sandbox) isCommandAllowed(cmd string) bool {
if len(s.cfg.AllowedCommands) == 0 {
return true
}
base := filepath.Base(cmd)
base = strings.TrimSuffix(base, ".exe")
for _, allowed := range s.cfg.AllowedCommands {
if strings.EqualFold(base, allowed) {
return true
}
}
return false
}
func (s *Sandbox) validateWorkDir(dir string) error {
info, err := os.Stat(dir)
if err != nil {
return fmt.Errorf("work directory not accessible: %s: %w", dir, err)
}
if !info.IsDir() {
return fmt.Errorf("not a directory: %s", dir)
}
if len(s.cfg.AllowedDirs) == 0 {
return nil
}
absDir, err := filepath.Abs(dir)
if err != nil {
return fmt.Errorf("cannot resolve path: %w", err)
}
for _, allowed := range s.cfg.AllowedDirs {
absAllowed, err := filepath.Abs(allowed)
if err != nil {
continue
}
if strings.HasPrefix(absDir, absAllowed) {
return nil
}
}
return fmt.Errorf("directory not in allowed list: %s", dir)
}
func (s *Sandbox) defaultWorkDir() string {
if len(s.cfg.AllowedDirs) > 0 {
return s.cfg.AllowedDirs[0]
}
wd, _ := os.Getwd()
return wd
}
func (s *Sandbox) filteredEnv() []string {
allowed := map[string]bool{
"PATH": true, "HOME": true, "USER": true, "USERNAME": true,
"TMP": true, "TEMP": true, "TMPDIR": true,
"LANG": true, "LC_ALL": true, "SHELL": true,
"SYSTEMROOT": true, "WINDIR": true, "ProgramFiles": true,
"GOPATH": true, "GOROOT": true, "GOPROXY": true,
"NODE_PATH": true, "PYTHONPATH": true,
"JAVA_HOME": true, "DOTNET_ROOT": true,
"CARGO_HOME": true, "RUSTUP_HOME": true,
}
var filtered []string
for _, e := range os.Environ() {
kv := strings.SplitN(e, "=", 2)
if len(kv) == 2 && allowed[kv[0]] {
filtered = append(filtered, e)
}
}
filtered = append(filtered, "CYRENE_SANDBOX=1")
return filtered
}