This document describes the integration of sandbox terminal functionality from the AI-Agent project into the Agent.Api. The implementation provides a secure, local sandbox environment for command execution.
本文档描述了将AI-Agent项目的沙盒终端功能集成到Agent.Api中的实现。该实现提供了一个安全的本地沙盒环境用于命令执行。
- ISandboxTerminalService - Service interface - 服务接口
- SandboxTerminalService - Service implementation - 服务实现
- SandboxTerminalController - REST API controller - REST API控制器
- SandboxTerminalExtensions - Dependency injection extensions - 依赖注入扩展
| Feature | AI-Agent | Agent.Api Implementation |
|---|---|---|
| Environment | Docker containers | Local processes - 本地进程 |
| Language | Python | C# .NET |
| Session Management | Complex socket-based | Simplified process-based - 简化的基于进程的 |
| Security | Container isolation | Command filtering + sanitization - 命令过滤和清理 |
POST /api/sandboxterminal/execute
Content-Type: application/json
{
"command": "ls -la",
"workingDirectory": "/tmp",
"timeout": 30
}Response - 响应:
{
"exitCode": 0,
"standardOutput": "total 8\ndrwxrwxrwt 2 root root 4096 ...",
"standardError": "",
"executionTimeMs": 125,
"isSuccess": true,
"combinedOutput": "total 8\ndrwxrwxrwt 2 root root 4096 ..."
}POST /api/sandboxterminal/execute/stream
Content-Type: application/json
{
"command": "ping -c 5 google.com",
"timeout": 30
}Response - 响应: (Streaming text/plain)
[STDOUT] PING google.com (142.250.191.14) 56(84) bytes of data.
[STDOUT] 64 bytes from lga25s62-in-f14.1e100.net (142.250.191.14): icmp_seq=1 ttl=118 time=12.3 ms
...
Get Working Directory - 获取工作目录:
GET /api/sandboxterminal/workdirSet Working Directory - 设置工作目录:
POST /api/sandboxterminal/workdir
Content-Type: application/json
{
"path": "/home/user/projects"
}POST /api/sandboxterminal/check-command
Content-Type: application/json
{
"command": "rm -rf /"
}Response - 响应:
{
"isSafe": false,
"command": "rm -rf /",
"message": "Command contains potentially dangerous operations"
}GET /api/sandboxterminal/system-infoGET /api/sandboxterminal/health{
"SandboxTerminal": {
"DefaultWorkingDirectory": "/tmp/sandbox",
"DefaultTimeout": 30,
"MaxTimeout": 300,
"EnableLogging": true,
"EnableSanitization": true,
"AllowedCommands": [],
"BlockedCommands": [
"rm -rf /",
"rm -rf /*",
"mkfs",
"dd if=/dev/zero",
":(){ :|:& };:",
"shutdown",
"reboot"
],
"EnvironmentVariables": {
"TERM": "xterm-256color",
"LANG": "en_US.UTF-8",
"PATH": "/usr/local/bin:/usr/bin:/bin"
}
}
}// In Program.cs
builder.Services.AddSandboxTerminal(builder.Configuration);
// Or with custom options - 或使用自定义选项
builder.Services.AddSandboxTerminal(options =>
{
options.DefaultTimeout = 60;
options.EnableSanitization = true;
options.BlockedCommands.Add("custom-dangerous-command");
});The service implements multiple layers of security: 服务实现了多层安全机制:
- Blocked Commands List - 阻止命令列表
- Allowed Commands List - 允许命令列表(可选)
- Dangerous Pattern Detection - 危险模式检测
- Path Traversal Prevention - 路径遍历防护
// Example of command sanitization - 命令清理示例
var sanitized = command
.Replace("$(", "\\$(") // Escape command substitution - 转义命令替换
.Replace("`", "\\`") // Escape backticks - 转义反引号
.Replace("|", "\\|"); // Escape pipes - 转义管道All commands are executed with configurable timeouts to prevent: 所有命令都使用可配置的超时执行,以防止:
- Infinite loops - 无限循环
- Long-running processes - 长时间运行的进程
- Resource exhaustion - 资源耗尽
[ApiController]
public class MyController : ControllerBase
{
private readonly ISandboxTerminalService _terminal;
public MyController(ISandboxTerminalService terminal)
{
_terminal = terminal;
}
[HttpPost("run-script")]
public async Task<IActionResult> RunScript([FromBody] string script)
{
// 检查命令安全性 - Check command safety
if (!_terminal.IsCommandSafe(script))
{
return BadRequest("Unsafe command detected");
}
// 执行命令 - Execute command
var result = await _terminal.ExecuteCommandAsync(script);
if (result.IsSuccess)
{
return Ok(result.StandardOutput);
}
else
{
return BadRequest(result.StandardError);
}
}
}[HttpGet("stream-logs")]
public async Task StreamLogs()
{
Response.ContentType = "text/plain";
await foreach (var line in _terminal.ExecuteCommandStreamAsync("tail -f /var/log/app.log"))
{
await Response.WriteAsync($"{line}\n");
await Response.Body.FlushAsync();
}
}-
Command Timeout - 命令超时
{ "exitCode": -1, "standardError": "Command timed out after 30 seconds", "isSuccess": false } -
Unsafe Command - 不安全命令
{ "exitCode": -1, "standardError": "Command blocked for security reasons", "isSuccess": false } -
Working Directory Error - 工作目录错误
{ "success": false, "message": "The specified path is invalid or inaccessible" }
- Processes are properly disposed after execution - 进程在执行后正确释放
- Process trees are killed on timeout - 超时时终止进程树
- Memory usage is monitored - 监控内存使用
- Multiple commands can run simultaneously - 多个命令可以同时运行
- Each request gets its own process - 每个请求获得自己的进程
- No shared state between executions - 执行之间无共享状态
- Configurable timeouts - 可配置超时
- Working directory isolation - 工作目录隔离
- Environment variable control - 环境变量控制
{
"Logging": {
"LogLevel": {
"Agent.Core.Services.Sandbox": "Debug"
}
}
}- Command execution start/completion - 命令执行开始/完成
- Security violations - 安全违规
- Timeout events - 超时事件
- Error conditions - 错误条件
-
Core Terminal Logic - 核心终端逻辑
- Command execution - 命令执行
- Output handling - 输出处理
- Error management - 错误管理
-
Security Features - 安全特性
- Command sanitization - 命令清理
- Dangerous command detection - 危险命令检测
- Path safety checks - 路径安全检查
-
Session Management - 会话管理
- No persistent sessions - 无持久会话
- Stateless execution - 无状态执行
-
Container Integration - 容器集成
- Local process execution instead of Docker - 本地进程执行而非Docker
- Direct OS interaction - 直接操作系统交互
-
API Design - API设计
- RESTful endpoints - RESTful端点
- Comprehensive error handling - 全面错误处理
- Streaming support - 流式支持
-
Configuration - 配置
- Flexible options system - 灵活选项系统
- Environment-specific settings - 环境特定设置
-
Docker Integration - Docker集成
- Optional container execution - 可选容器执行
- Resource limits - 资源限制
-
Session Management - 会话管理
- Persistent terminal sessions - 持久终端会话
- Interactive command execution - 交互式命令执行
-
Advanced Security - 高级安全
- Sandboxing with chroot - 使用chroot的沙盒
- User privilege management - 用户权限管理
-
Permission Denied - 权限拒绝
- Check working directory permissions - 检查工作目录权限
- Verify user context - 验证用户上下文
-
Command Not Found - 命令未找到
- Check PATH environment variable - 检查PATH环境变量
- Verify command installation - 验证命令安装
-
Timeout Issues - 超时问题
- Increase timeout values - 增加超时值
- Check for infinite loops - 检查无限循环
Enable debug logging for detailed execution information: 启用调试日志以获取详细执行信息:
{
"Logging": {
"LogLevel": {
"Agent.Core.Services.Sandbox": "Debug"
}
}
}The Sandbox Terminal integration provides a secure and efficient way to execute commands in a controlled environment. While simplified compared to the original AI-Agent Docker-based approach, it maintains the core functionality while being more suitable for local development and testing scenarios.
沙盒终端集成提供了在受控环境中执行命令的安全高效方式。虽然相比原始的AI-Agent基于Docker的方法有所简化,但它保持了核心功能,同时更适合本地开发和测试场景。