全栈应用服务器

  • 全栈应用服务器 > 使用指南 > 沙箱服务概述 > 安全的代码运行环境

    安全的代码运行环境

    最近更新时间: 2026-02-12 11:22:01

    概述

    在 AI Agent 开发中,执行 LLM 生成的代码是常见需求,但直接运行存在严重的安全风险,包括恶意代码注入、环境污染、资源滥用等。沙箱服务通过云端隔离的执行环境来解决这些安全问题。

    为什么需要代码隔离

    直接在主机环境中执行不受信任的代码存在以下风险:

    • 恶意代码注入 - LLM 可能生成包含恶意逻辑的代码
    • 环境污染 - 代码执行可能修改系统配置或安装冲突的依赖
    • 资源滥用 - 无限循环或大量内存分配可能导致系统崩溃
    • 多租户安全 - 一个用户的代码可能影响其他用户

    沙箱服务提供完全隔离的云端执行环境,确保安全性和可靠性。

    支持的语言

    沙箱支持多种主流编程语言:

    需要其他语言支持?可以通过自定义模板实现。

    Python

    import { Sandbox } from '@e2b/code-interpreter'
    
    async function main() {
      const sandbox = await Sandbox.create()
    
      const execution = await sandbox.runCode(
        `
    import requests
    
    response = requests.get("https://httpbin.org/json")
    print(response.json())
        `,
        { language: 'python' }
      )
    
      console.log(execution.logs.stdout)
    
      await sandbox.kill()
    }
    
    main()
    

    JavaScript & TypeScript

    import { Sandbox } from '@e2b/code-interpreter'
    
    async function main() {
      const sandbox = await Sandbox.create()
    
      const execution = await sandbox.runCode(
        `
    const https = require('https');
    
    https.get('https://httpbin.org/json', (res) => {
      let data = '';
      res.on('data', (chunk) => { data += chunk; });
      res.on('end', () => { console.log(JSON.parse(data)); });
    });
        `,
        { language: 'js' }
      )
    
      console.log(execution.logs.stdout)
    
      await sandbox.kill()
    }
    
    main()
    

    R

    import { Sandbox } from '@e2b/code-interpreter'
    
    async function main() {
      const sandbox = await Sandbox.create()
    
      const execution = await sandbox.runCode(
        `
    library(httr)
    
    response <- GET("https://httpbin.org/json")
    content <- content(response, "parsed")
    print(content)
        `,
        { language: 'r' }
      )
    
      console.log(execution.logs.stdout)
    
      await sandbox.kill()
    }
    
    main()
    

    Java

    import { Sandbox } from '@e2b/code-interpreter'
    
    async function main() {
      const sandbox = await Sandbox.create()
    
      const execution = await sandbox.runCode(
        `
    import java.net.*;
    import java.io.*;
    
    public class Main {
        public static void main(String[] args) throws Exception {
            URL url = new URL("https://httpbin.org/json");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                System.out.println(inputLine);
            }
            in.close();
        }
    }
        `,
        { language: 'java' }
      )
    
      console.log(execution.logs.stdout)
    
      await sandbox.kill()
    }
    
    main()
    

    Bash

    import { Sandbox } from '@e2b/code-interpreter'
    
    async function main() {
      const sandbox = await Sandbox.create()
    
      const execution = await sandbox.runCode(
        `
    curl -s https://httpbin.org/json
        `,
        { language: 'bash' }
      )
    
      console.log(execution.logs.stdout)
    
      await sandbox.kill()
    }
    
    main()
    

    流式输出

    Code Interpreter 支持在代码执行期间实时流式获取标准输出、错误输出和执行结果。

    流式获取 stdout/stderr

    import { Sandbox } from '@e2b/code-interpreter'
    
    async function main() {
      const sandbox = await Sandbox.create()
    
      await sandbox.runCode('print("hello"); import time; time.sleep(1); print("world")', {
        onStdout: (data) => {
          console.log(data)
        },
        onStderr: (data) => {
          console.log(data)
        },
      })
    
      await sandbox.kill()
    }
    
    main()
    

    流式获取执行结果

    import { Sandbox } from '@e2b/code-interpreter'
    
    async function main() {
      const sandbox = await Sandbox.create()
    
      await sandbox.runCode('x = 1', {
        onResult: (result) => {
          console.log(result)
        },
      })
    
      await sandbox.kill()
    }
    
    main()
    
    以上内容是否对您有帮助?