Commit 6b613595 by Archer Committed by GitHub

feat(sandbox): 重构代码沙盒,支持内置函数和网络请求 (#6479)

* fix(sandbox): 重构代码沙盒,支持内置函数和网络请求 (#6462)

* feat(sandbox): 重写代码沙盒 - Bun + Hono + 统一子进程模型

- 运行时: Node.js → Bun
- HTTP 框架: NestJS + Fastify → Hono
- JS 执行: isolated-vm → Bun 子进程(与 Python 统一)
- 架构: 统一子进程模型,JS 和 Python 共享同一套执行引擎

- SubprocessRunner 基类,JS/Python 各自继承
- ProcessPool 进程池预热(SANDBOX_JS_POOL_SIZE / SANDBOX_PYTHON_POOL_SIZE)
- SystemHelper 命名空间(JS 端保留向后兼容全局函数)
- 临时文件系统隔离 + 路径遍历防护 + 磁盘配额
- 请求级资源限制(timeoutMs / memoryMB / diskMB)

- JS: 原型链冻结 + Bun API 禁用 + Function 构造器注入 safe require(模块白名单)
- Python: 宿主侧正则预检 + __import__ 拦截 + resource 限制

- 移除: @nestjs/*(6个包)、fastify、isolated-vm、node-gyp、reflect-metadata、rxjs
- 新增: hono
- 保留: tiktoken
- 新增用户可用包: lodash、dayjs、axios、moment、uuid、crypto-js、qs

- 67 个测试全部通过(单元测试 + 安全测试 + 集成测试)
- vitest 独立配置,不影响全局

* fix(sandbox): 安全加固 - 扩展 Bun API 封锁、清理 process.env、闭包封装 Python import 拦截

- JS: 扩展 Bun 危险 API 封锁列表(serve/connect/listen/udpSocket/dns/plugin/build/Transpiler)
- JS: 清理 process.env,仅保留沙箱必要变量,防止泄露敏感环境变量
- Python: 用闭包封装 _safe_import,del 掉 _original_import/_make_safe_import/_BLOCKED_MODULES
  防止用户代码恢复原始 __import__
- Dockerfile: 复制 bun.lock 并使用 --frozen-lockfile 确保构建可复现

* fix(sandbox): 将 sandbox 从 pnpm workspace 中移除,独立管理依赖

* fix(sandbox): 从全局 vitest 移除 sandbox 测试,集成测试无 SANDBOX_URL 时跳过

* ci(sandbox): 添加独立测试 workflow,仅 sandbox 代码变更时触发

* refactor(sandbox): 使用 export default 启动方式,与 sandbox_server 保持一致

* fix: sandbox security hardening & comprehensive test suite

Security fixes:
- JS: freeze Function constructor to block constructor.constructor escape
- JS: handle undefined return from main() (serialize as null)
- Python: fix http_request using from-import after __import__ interception
- Python: __import__ whitelist mode blocks exec/eval import bypasses

New tests (223 passing):
- security/escape-attacks: JS/Python escape attack vectors
- security/network-security: IP blacklist, protocol restrictions, httpRequest
- compat/legacy-js: 18 backward compatibility tests
- compat/legacy-python: 21 backward compatibility tests
- boundary: timeout, memory, disk, edge cases
- examples: common user code patterns

* feat(sandbox): env vars for all limits + rewrite README

- Network limits configurable via env: SANDBOX_MAX_REQUESTS, SANDBOX_REQUEST_TIMEOUT, SANDBOX_MAX_RESPONSE_SIZE
- Resource upper bounds configurable: SANDBOX_MAX_TIMEOUT, SANDBOX_MAX_MEMORY_MB, SANDBOX_MAX_DISK_MB
- README: architecture, API docs, env var reference, how to add JS/Python packages, security overview, built-in functions

* refactor(sandbox): extract env.ts with dotenv for typed env loading

- New env.ts: dotenv.config() + typed helpers (str/int/bool)
- config.ts re-exports env for backward compatibility
- index.ts imports env first to ensure .env loaded before anything else

* refactor(sandbox): use zod for env validation and type coercion

- Replace manual parseInt/str helpers with zod schema + coerce
- Invalid env vars now fail fast with formatted error on startup
- dotenv + zod, clean and declarative

* chore(sandbox): remove unused process pool code

- Delete pool.ts and pool.test.ts (pool was never wired into runners)
- Remove PoolConfig/PooledProcess types
- Remove pool env vars from env.ts
- Clean up README

* feat(sandbox): add concurrency limiter with semaphore

- New Semaphore utility for max concurrent subprocess control
- SANDBOX_MAX_CONCURRENCY env var (default 50)
- Excess requests queue instead of spawning unbounded processes
- Health endpoint exposes concurrency stats (current/queued/max)

* test(sandbox): add semaphore tests and expand coverage to 292 cases

- New semaphore.test.ts (11 tests): acquire/release, queuing, FIFO, stats, serial execution
- JS runner: blank code, template literals, primitive returns, more modules, unicode, partial limits
- Python runner: blank code, triple quotes, primitive returns, unicode, null vars, division errors
- JS security: process.exit, globalThis, Symbol.unscopables, Proxy, dynamic import, path traversal
- Python security: pickle/multiprocessing/threading/ctypes/signal, exec bypass, __subclasses__
- Escape attacks: type() class creation, __builtins__ tampering, getattr access
- Boundary: long vars, special JSON chars, float precision, big ints, circular refs, Promise.reject

* test(sandbox): test-master review - add 31 tests, coverage report

- base-runner.test.ts (10): BaseRunner precheck, temp dir, semaphore integration
- semaphore-race.test.ts (5): race conditions, rapid acquire/release, stress test
- coverage-gaps.test.ts (16): security coverage gaps found during review
- REVIEW-REPORT.md: full test audit report

Total: 323 passed, 0 failed

* fix(sandbox): address PR #6439 review issues

Security fixes:
- Intercept Python builtins.open(), restrict file access to sandbox tmpdir
- Remove unused pool.ts, warmup.mjs, warmup.py (security risk)
- Fix DNS rebinding TOCTOU: use resolved IP for HTTP connections
- Fix symlink path traversal: use realpath instead of normpath
- Add try/finally cleanup for __import__ hook

Robustness:
- Add __SANDBOX_RESULT__ prefix to stdout parsing, prevent user output interference
- Fix disk quota tracking: deduct old file size on overwrite
- Add __import__() pattern scanning in Python precheck

Tests:
- Fix eval+__import__ test assertion (accept both catch and fail paths)

All 323 tests passing.

* fix(sandbox): remove warmup scripts COPY from Dockerfile

* docs(sandbox): add technical design document

* feat(sandbox): configurable module allowlist/blocklist via env vars

- SANDBOX_JS_ALLOWED_MODULES: JS require whitelist (comma-separated)
- SANDBOX_PYTHON_BLOCKED_MODULES: Python import blacklist (comma-separated)
- Defaults unchanged, fully backward compatible

* fix(sandbox): 修复多个安全漏洞

1. Python HTTPS DNS rebinding: HTTPS 请求现在也使用 resolved IP 发起连接
2. Python __import__ hook 恢复漏洞: 移除 finally 块中恢复原始 __import__ 的代码
3. Python 内部变量泄露: 用户代码执行前删除 _os, _socket 等内部模块引用
4. JS process 危险 API: 禁用 process.binding/dlopen/kill/chdir 等,冻结 process.env
5. Python open() fd 绕过: 阻止通过整数文件描述符绕过路径检查
6. API 输入校验: 使用 zod schema 校验请求体,限制代码大小 1MB
7. 无认证警告: SANDBOX_TOKEN 未设置时输出生产环境警告

新增 security-fixes.test.ts 包含所有修复的回归测试

* test: consolidate security tests + add integration test suite

- Merge 6 security test files into 1 consolidated security.test.ts (109 tests)
  - JS/Python module interception (precheck + runtime)
  - JS escape attacks (prototype, constructor, Reflect, globalThis)
  - Python escape attacks (__import__ hook, exec/eval, internal vars, __subclasses__)
  - SSRF protection (private IPs, cloud metadata, file protocol)
  - File system isolation (path traversal, fd, disk quota)
  - Variable injection attacks
  - API input validation

- Add black-box integration test suite functional.test.ts (56 tests)
  - Basic operations (math, string, array, JSON, regex, Date, Promise, Map/Set)
  - Variable passing (string, number, complex objects, empty, multiple)
  - Whitelisted modules (crypto-js, moment, lodash)
  - SystemHelper/system_helper (fs, delay, strToBase64, httpRequest)
  - Error handling (syntax, runtime, undefined var, timeout)
  - Network requests (GET, POST)
  - Complex scenarios (CSV pipeline, recursion, class definition)

- Remove 34 duplicate test cases across merged files
- Total: 363 passed, 8 skipped (integration API tests need server)

* fix(sandbox): z.record() zod v4 compatibility - add key type param

* feat(sandbox): add .env.template with all config options and comments

* refactor(sandbox): remove disk write support and temp filesystem

* test(sandbox): remove all fs-related tests and add test case inventory

- Remove fs read/write tests from unit, integration, boundary, examples
- Remove path traversal, absolute path, open fd, builtins.open tests from security
- Add comprehensive test/case.md with all 344 test cases categorized
- All tests pass: 344 passed, 8 skipped, 0 failed

* feat(sandbox): add GET /sandbox/modules API to list available packages and builtins

* test(sandbox): add unit tests for GET /sandbox/modules API

* refactor(test): rewrite api.test.ts to use app.request() - no external server needed

* feat(sandbox): validate SANDBOX_TOKEN charset in env schema (ASCII printable only)

* chore(sandbox): remove DESIGN.md and package-lock.json from PR

* feat(sandbox): replace spawn-per-request with process pool architecture

- Add ProcessPool (JS) and PythonProcessPool with long-lived worker processes
- Workers communicate via stdin/stdout line-based JSON protocol
- Pool size configurable via SANDBOX_POOL_SIZE env var (default 20)
- Auto-respawn workers on crash
- Semaphore-based queueing when requests exceed pool size

Performance gains (simple functions):
- JS: 22 QPS → 1,328 QPS (60x improvement)
- Python: 14.7 QPS → 3,395 QPS (231x improvement)

- Fix import.meta.dir compatibility for vitest (Node) environments
- Export poolReady promise for test initialization
- Add benchmark scripts to test/benchmark/
- All 354 tests passing (12 test files)

* chore(sandbox): clean up unused files, update README with pool architecture

- Remove test/REVIEW-REPORT.md, test/case.md, test/benchmark.ts (obsolete)
- Rewrite README: pool architecture diagram, performance benchmarks,
  SANDBOX_POOL_SIZE config, project structure, health endpoint format

* fix(sandbox): 修复进程池超时后 worker respawn 竞态条件

根因:超时 kill worker 后,exit 事件是异步的,release() 先执行时
worker 还在列表里,死 worker 被放回 idle 池,后续请求发给死进程。

修复:
- 超时回调中先 removeWorker 再 kill,防止 release 归还死 worker
- removeWorker 返回 bool,exit 事件中避免重复 respawn
- 超时回调主动触发 spawnWorker 补充池
- release 检查 worker 是否仍在池中
- spawnWorker 完成时检查 waitQueue 直接分配

* fix: security hardening & test migration to process pool

- JS worker: harden process object (kill/chdir/env freeze/binding/dlopen)
- Python worker: stack-frame based __import__ hook to block exec/eval bypass
- Python worker: BuiltinsProxy to prevent __import__ override via builtins module
- Python worker: restricted __builtins__ dict in exec_globals (no internal refs)
- Python worker: restore __import__ before each execution
- Migrate all 9 test files from JsRunner/PythonRunner to ProcessPool/PythonProcessPool
- Configure vitest for serial execution (pool size=1, fileParallelism: false)
- Fix security test assertion for builtins tampering (success=true with escaped=false)
- All 102 security tests passing

* docs(sandbox): update README with accurate benchmark data, remove non-existent features

- Update performance table with latest benchmark results (JS 1414 QPS, Python 4247 QPS)
- Remove SANDBOX_DISK_MB/SANDBOX_MAX_DISK_MB env vars (not implemented)
- Remove SystemHelper.fs.* / system_helper.fs.* docs (not implemented in workers)
- Fix security section to match actual implementation
- Update test count to 351

* refactor(sandbox): remove legacy runner/sandbox/template code

- Delete src/runner/ (base.ts, js-runner.ts, python-runner.ts)
- Delete src/sandbox/ (js-template.ts, python-template.ts, network-config.ts)
- Delete test/unit/js-runner.test.ts, test/unit/python-runner.test.ts
- Keep src/utils/semaphore.ts (generic utility, has its own tests)
- Update README project structure and test count (297 cases)

All functionality is now in src/pool/ (process-pool architecture).
297 tests passing, 0 failures.

* test(sandbox): add process pool lifecycle/respawn/concurrency tests

- ProcessPool: init/shutdown/stats, worker crash respawn, timeout respawn,
  pool-full queuing, concurrent crash isolation
- PythonProcessPool: init/shutdown/stats, timeout respawn, queuing
- 14 new test cases, total 311 passing

* fix(sandbox): ping/pong health check, replace httpbin.org with baidu.com

- Worker health check: send actual ping message and verify pong response
  instead of only checking stdin.writable (detects stuck workers)
- JS worker.ts: handle {type:'ping'} → reply {type:'pong'}
- Python worker.py: handle {type:'ping'} → reply {type:'pong'}
- ProcessPool/PythonProcessPool: rewrite pingWorker to send ping,
  wait for pong with timeout, replace worker on failure
- Replace all httpbin.org URLs with www.baidu.com in tests
  (httpbin.org unreachable from China/Sealos Devbox)
- Add 4 new health check tests (ping/pong for JS and Python pools)
- All 318 tests passing, 0 failures

* docs: add test report (test/README.md) and update README testing section

- test/README.md: detailed report with 315 passed / 3 skipped / 0 failed
- README.md: updated test section with coverage dimensions table and link to report

* docs: add functional test cases checklist (110 cases)

* fix(sandbox): fix Dockerfile Python env and import detection

1. Dockerfile: Remove broken multi-stage Python 3.11 copy.
   - The previous approach copied python3 binary from python:3.11-alpine
     but missed libpython3.11.so.1.0, causing Python pool init failure.
   - Now uses system Python from apk and installs pip packages directly.

2. worker.py: Fix false positive import blocking for third-party packages.
   - numpy/pandas were blocked because their internal 'import os' was
     detected as user-initiated (full stack scan found user code frames).
   - Changed to check only the direct caller frame: if the import comes
     from site-packages (third-party lib internals), allow it.
   - Direct user imports of blocked modules are still properly rejected.

* fix(sandbox): block dynamic import() and restrict file system access

Security fixes found during deep review:

1. JS: Block import() dynamic imports that bypass require whitelist.
   - import('fs') could read arbitrary files on the container.
   - Added static regex check to reject code containing import().

2. Python: Restrict open() to prevent user code from reading files.
   - open('/etc/passwd') was accessible from user code.
   - Added _restricted_open() that checks caller frame: only allows
     stdlib/site-packages internal calls, blocks user code (<string>).

3. Python: Remove duplicate return statement in _safe_import.

All 315 tests pass (3 skipped).

* test(sandbox): add regression tests for import() and open() security fixes

- JS: import('fs'), import('child_process'), import('os') blocked
- JS: string containing 'import' not false-positive
- Python: open('/etc/passwd'), open('/proc/self/environ'), open('/tmp/evil.txt', 'w') blocked
- Python: numpy internal open() not affected (conditional on numpy availability)

Total: 322 passed | 3 skipped (was 315 passed)

* docs(sandbox): rewrite sandbox documentation with JS + Python coverage

- Add Python language support documentation
- Add httpRequest/http_request function docs
- Add available modules list (JS whitelist + Python safe modules)
- Add security restrictions section
- Add practical examples (data processing, date calc, webhook signing)
- Add JS/Python function name mapping table

* docs(sandbox): use SystemHelper/system_helper for built-in functions

Direct calls (countToken, delay, etc.) are deprecated (kept for compat).
All examples now use SystemHelper.xxx() / system_helper.xxx().

* docs(sandbox): Python only show named-params style as recommended

* feat(sandbox): unify Python SystemHelper API with camelCase aliases

- Add camelCase aliases to Python SystemHelper: countToken, strToBase64,
  createHmac, httpRequest (matching JS API exactly)
- Update docs to use SystemHelper uniformly for both JS and Python
- snake_case methods (count_token, etc.) still work for backward compat

* feat(sandbox): add matplotlib and increase HTTP timeout to 60s

- Add matplotlib to Python dependencies
- Increase HTTP request timeout from 10s to 60s (both JS and Python)
- Update docs accordingly

* docs(sandbox): split docs for old/new sandbox versions

- sandbox.mdx → '代码运行(旧版)' for FastGPT ≤ 4.14.7 (URL unchanged)
- sandbox-v5.mdx → '代码运行' for FastGPT ≥ 4.14.8
- Both pages cross-link to each other
- meta.json updated: sandbox-v5 listed before sandbox

* docs: rename old sandbox doc to 代码运行(弃)

* refactor(sandbox): remove SANDBOX_TIMEOUT, use SANDBOX_MAX_TIMEOUT as unified timeout

* fix(sandbox): add build dependencies for matplotlib in Dockerfile

* refactor(sandbox): migrate Python from blocklist to allowlist for module control

- Change SANDBOX_PYTHON_BLOCKED_MODULES to SANDBOX_PYTHON_ALLOWED_MODULES
- Update Python worker to use allowlist instead of blocklist
- Add comprehensive safe module list: math, json, datetime, numpy, pandas, etc.
- Improve error message: 'Module X is not in the allowlist'
- Consistent with JS allowlist approach for better security

* fix(sandbox): add _strptime to allowlist and update test assertions

- Add _strptime module (required by datetime.strptime)
- Update test assertions for Python module import errors
- All 325 tests now pass (322 passed, 3 skipped)

* fix(docs): center SVG icon in size-5 container on medium screens

* docs(sandbox): simplify built-in functions and improve module documentation

- Remove delay, countToken, strToBase64, createHmac functions (keep only httpRequest)
- Convert Python module list to table format (10 tables by category)
- Reorganize usage examples with collapsible sections (JS and Python)
- Fix icon alignment in desktop/mobile sidebar navigation
- All 325 tests passing

---------

Co-authored-by: Lobster 3 <lobster3@sandbox.dev>
Co-authored-by: OpenClaw Bot <bot@openclaw.ai>
Co-authored-by: Archer <c121914yu@gmail.com>
Co-authored-by: archer <archer@archerdeMac-mini.local>

* perf: code sandbox

* update action

* Update projects/app/src/components/core/chat/ChatContainer/ChatBox/index.tsx

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* update timeout

* update memory limit function

* sandbox

* perf: process poll

* env template

* feat: code tip

* fix: code sandbox error tip

* update memory limit fn

* update memory limit fn

* fix: test

* fix: test

* fix: sandbox

---------

Co-authored-by: Archer <archer@fastgpt.io>
Co-authored-by: Lobster 3 <lobster3@sandbox.dev>
Co-authored-by: OpenClaw Bot <bot@openclaw.ai>
Co-authored-by: Archer <c121914yu@gmail.com>
Co-authored-by: archer <archer@archerdeMac-mini.local>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
parent 544faf6a
name: 'Sandbox-Test'
on:
pull_request:
paths:
- 'projects/sandbox/**'
workflow_dispatch:
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
defaults:
run:
working-directory: projects/sandbox
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.ref || github.ref }}
repository: ${{ github.event.pull_request.head.repo.full_name || github.repository }}
- uses: oven-sh/setup-bun@v2
with:
bun-version: latest
- name: Install Deps
run: bun install
- name: Run Unit Tests
run: bun run test
...@@ -476,7 +476,7 @@ button[role='tab'] { ...@@ -476,7 +476,7 @@ button[role='tab'] {
padding-bottom: 16px; padding-bottom: 16px;
} }
/* 移动端侧边栏 padding 缩小 */ /* 移动端侧边栏左侧 padding 缩小 */
@media (max-width: 767px) { @media (max-width: 767px) {
#nd-sidebar-mobile [data-radix-scroll-area-viewport] { #nd-sidebar-mobile [data-radix-scroll-area-viewport] {
padding-left: 8px; padding-left: 8px;
...@@ -498,8 +498,12 @@ button[role='tab'] { ...@@ -498,8 +498,12 @@ button[role='tab'] {
} }
/* RootToggle 图标容器:缩小到与图标匹配,修复对齐和间距 */ /* RootToggle 图标容器:缩小到与图标匹配,修复对齐和间距 */
#nd-sidebar .size-9,
#nd-sidebar .size-5,
#nd-sidebar-mobile .size-9, #nd-sidebar-mobile .size-9,
[data-radix-popper-content-wrapper] .size-9 { #nd-sidebar-mobile .size-5,
[data-radix-popper-content-wrapper] .size-9,
[data-radix-popper-content-wrapper] .size-5 {
width: 20px !important; width: 20px !important;
height: 20px !important; height: 20px !important;
flex-shrink: 0; flex-shrink: 0;
...@@ -517,6 +521,9 @@ button[role='tab'] { ...@@ -517,6 +521,9 @@ button[role='tab'] {
padding-left: 12px !important; padding-left: 12px !important;
padding-right: 12px !important; padding-right: 12px !important;
border-radius: 8px; border-radius: 8px;
display: flex !important;
align-items: center !important;
gap: 8px;
} }
/* 移动端目录下拉框(TOC Popover Trigger):文本溢出省略号 */ /* 移动端目录下拉框(TOC Popover Trigger):文本溢出省略号 */
...@@ -536,4 +543,12 @@ button[role='tab'] { ...@@ -536,4 +543,12 @@ button[role='tab'] {
overflow-wrap: break-word; overflow-wrap: break-word;
word-break: break-word; word-break: break-word;
} }
/* RootToggle 图标容器:缩小尺寸,对齐文字 */
#nd-sidebar-mobile [data-radix-popper-anchor] .size-9,
#nd-sidebar-mobile button .size-9 {
width: 20px !important;
height: 20px !important;
flex-shrink: 0;
}
} }
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
"http", "http",
"tfswitch", "tfswitch",
"variable_update", "variable_update",
"sandbox-v2",
"sandbox", "sandbox",
"loop", "loop",
"knowledge_base_search_merge", "knowledge_base_search_merge",
......
{ {
"title": "工作流节点", "title": "工作流节点",
"description": "FastGPT 工作流节点设置和使用指南", "description": "FastGPT 工作流节点设置和使用指南",
"pages": ["ai_chat","dataset_search","tool","question_classify","content_extract","user-selection","form_input","text_editor","reply","document_parsing","http","tfswitch","variable_update","sandbox","loop","knowledge_base_search_merge","coreferenceResolution","laf","custom_feedback"] "pages": [
} "ai_chat",
\ No newline at end of file "dataset_search",
"tool",
"question_classify",
"content_extract",
"user-selection",
"form_input",
"text_editor",
"reply",
"document_parsing",
"http",
"tfswitch",
"variable_update",
"sandbox-v2",
"sandbox",
"loop",
"knowledge_base_search_merge",
"coreferenceResolution",
"laf",
"custom_feedback"
]
}
---
title: Code Run
description: FastGPT Code Run node documentation (for version 4.14.8 and above)
---
> This document applies to FastGPT **version 4.14.8 and above**. For version 4.14.7 and earlier, see [Code Run (Deprecated)](/docs/introduction/guide/dashboard/workflow/sandbox).
## Features
The Code Run node executes JavaScript and Python code in a secure sandbox for data processing, format conversion, logic calculations, and similar tasks.
**Supported Languages**
- JavaScript (Bun runtime)
- Python 3
**Important Notes**
- Self-hosted users need to deploy the `fastgpt-sandbox` image and configure the `SANDBOX_URL` environment variable.
- The sandbox has a default maximum runtime of 60s (configurable).
- Code runs in isolated process pools with no access to the file system or internal network.
## Variable Input
Add variables needed for code execution in custom inputs.
**JavaScript** — Destructure in the main function parameters:
```js
async function main({data1, data2}){
return {
result: data1 + data2
}
}
```
**Python** — Receive variables by name in the main function parameters:
```python
def main(data1, data2):
return {"result": data1 + data2}
```
## Result Output
Always return an object (JS) or dict (Python).
In custom outputs, add variable names to access values by their keys. For example, if you return:
```json
{
"result": "hello",
"count": 42
}
```
Add `result` and `count` variables in custom outputs to retrieve their values.
## Built-in Functions
### httpRequest - Make HTTP Requests
Make external HTTP requests from within the sandbox. Internal network addresses are automatically blocked (SSRF protection).
**JavaScript Example:**
```js
async function main({url}){
const res = await SystemHelper.httpRequest(url, {
method: 'GET', // Request method, default GET
headers: {}, // Custom request headers
body: null, // Request body (objects are auto JSON-serialized)
timeout: 60 // Timeout in seconds, max 60s
})
return {
status: res.status,
data: res.data
}
}
```
**Python Example:**
```python
def main(url):
res = SystemHelper.httpRequest(url, method="GET", headers={}, timeout=10)
return {"status": res["status"], "data": res["data"]}
```
**Limitations:**
- Maximum 30 requests per execution
- Single request timeout: 60s
- Maximum response body: 2MB
- Only http/https protocols allowed
- Internal IPs automatically blocked (127.0.0.0/8, 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, etc.)
## Available Modules
### JavaScript Whitelist
The following npm modules are available via `require()`:
| Module | Description | Example |
|--------|-------------|---------|
| `lodash` | Utility library | `const _ = require('lodash')` |
| `moment` | Date handling | `const moment = require('moment')` |
| `dayjs` | Lightweight date library | `const dayjs = require('dayjs')` |
| `crypto-js` | Encryption library | `const CryptoJS = require('crypto-js')` |
| `uuid` | UUID generation | `const { v4 } = require('uuid')` |
| `qs` | Query string parsing | `const qs = require('qs')` |
Other modules (such as `fs`, `child_process`, `net`, etc.) are prohibited.
### Python Whitelist
The following Python standard library and third-party modules can be imported directly:
**Math and Numerical Computing**
| Module | Description |
|--------|-------------|
| `math` | Mathematical functions |
| `cmath` | Complex number math |
| `decimal` | Decimal floating-point arithmetic |
| `fractions` | Fraction arithmetic |
| `random` | Random number generation |
| `statistics` | Statistical functions |
**Data Structures and Algorithms**
| Module | Description |
|--------|-------------|
| `collections` | Container data types |
| `array` | Arrays |
| `heapq` | Heap queue |
| `bisect` | Array bisection |
| `queue` | Queues |
| `copy` | Shallow and deep copy |
**Functional Programming**
| Module | Description |
|--------|-------------|
| `itertools` | Iterator tools |
| `functools` | Higher-order functions |
| `operator` | Standard operators |
**String and Text Processing**
| Module | Description |
|--------|-------------|
| `string` | String constants |
| `re` | Regular expressions |
| `difflib` | Diff calculation |
| `textwrap` | Text wrapping |
| `unicodedata` | Unicode database |
| `codecs` | Codec registry |
**Date and Time**
| Module | Description |
|--------|-------------|
| `datetime` | Date and time |
| `time` | Time access |
| `calendar` | Calendar |
**Data Serialization**
| Module | Description |
|--------|-------------|
| `json` | JSON encoding/decoding |
| `csv` | CSV file handling |
| `base64` | Base64 encoding/decoding |
| `binascii` | Binary-to-ASCII conversion |
| `struct` | Byte string parsing |
**Encryption and Hashing**
| Module | Description |
|--------|-------------|
| `hashlib` | Hash algorithms |
| `hmac` | HMAC message authentication |
| `secrets` | Secure random numbers |
| `uuid` | UUID generation |
**Types and Abstractions**
| Module | Description |
|--------|-------------|
| `typing` | Type hints |
| `abc` | Abstract base classes |
| `enum` | Enumeration types |
| `dataclasses` | Data classes |
| `contextlib` | Context managers |
**Other Utilities**
| Module | Description |
|--------|-------------|
| `pprint` | Pretty printing |
| `weakref` | Weak references |
**Third-party Libraries**
| Module | Description |
|--------|-------------|
| `numpy` | Numerical computing |
| `pandas` | Data analysis |
| `matplotlib` | Data visualization |
**Prohibited modules:** `os`, `sys`, `subprocess`, `socket`, `urllib`, `http`, `requests`, and any modules involving system calls, network access, or file system operations.
## Security Restrictions
The sandbox provides multiple layers of security protection:
- **Module Restrictions:** Only whitelisted modules are allowed for both JS and Python
- **Network Isolation:** Internal IP requests are automatically blocked (SSRF protection)
- **File Isolation:** No read/write access to the container file system
- **Timeout Protection:** Default 60s timeout prevents infinite loops
- **Process Isolation:** Each execution runs in an independent sandbox process
## Usage Examples
### JavaScript Examples
<details>
<summary>Data Format Conversion</summary>
```js
// Convert comma-separated string to array
function main({input}){
const items = input.split(',').map(s => s.trim()).filter(Boolean)
return { items, count: items.length }
}
```
</details>
<details>
<summary>Date Calculation</summary>
```js
const dayjs = require('dayjs')
function main(){
const now = dayjs()
return {
today: now.format('YYYY-MM-DD'),
nextWeek: now.add(7, 'day').format('YYYY-MM-DD'),
timestamp: now.valueOf()
}
}
```
</details>
<details>
<summary>HTTP Request - Get Weather</summary>
```js
async function main({city}){
const res = await SystemHelper.httpRequest(
`https://api.example.com/weather?city=${city}`,
{ method: 'GET', timeout: 10 }
)
return {
temperature: res.data.temp,
weather: res.data.condition
}
}
```
</details>
<details>
<summary>Data Encryption</summary>
```js
const CryptoJS = require('crypto-js')
function main({text, key}){
const encrypted = CryptoJS.AES.encrypt(text, key).toString()
return { encrypted }
}
```
</details>
### Python Examples
<details>
<summary>Data Statistics</summary>
```python
import math
def main(numbers):
if not numbers:
return {"error": "no data"}
mean = sum(numbers) / len(numbers)
variance = sum((x - mean)**2 for x in numbers) / len(numbers)
return {
"mean": mean,
"max": max(numbers),
"min": min(numbers),
"std": math.sqrt(variance)
}
```
</details>
<details>
<summary>Date Processing</summary>
```python
from datetime import datetime, timedelta
def main(date_str):
dt = datetime.strptime(date_str, "%Y-%m-%d")
next_week = dt + timedelta(days=7)
return {
"input": date_str,
"next_week": next_week.strftime("%Y-%m-%d"),
"weekday": dt.strftime("%A")
}
```
</details>
<details>
<summary>HTTP Request - API Call</summary>
```python
def main(api_url, api_key):
res = SystemHelper.httpRequest(
api_url,
method="GET",
headers={"Authorization": f"Bearer {api_key}"},
timeout=10
)
return {
"status": res["status"],
"data": res["data"]
}
```
</details>
<details>
<summary>JSON Data Processing</summary>
```python
import json
def main(json_str):
data = json.loads(json_str)
# Extract specific fields
result = {
"names": [item["name"] for item in data if "name" in item],
"count": len(data)
}
return result
```
</details>
<details>
<summary>Regular Expression Matching</summary>
```python
import re
def main(text):
# Extract all email addresses
emails = re.findall(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b', text)
return {
"emails": emails,
"count": len(emails)
}
```
</details>
---
title: 代码运行
description: FastGPT 代码运行节点介绍(适用于 4.14.8 及以上版本)
---
> 本文档适用于 FastGPT **4.14.8 及以上版本**。4.14.7 及以下版本请参考 [代码运行(弃)](/docs/introduction/guide/dashboard/workflow/sandbox)。
## 功能
代码运行节点支持在安全沙盒中执行 JavaScript 和 Python 代码,用于数据处理、格式转换、逻辑计算等场景。
**支持的语言**
- JavaScript(基于 Bun 运行时)
- Python 3
**注意事项**
- 私有化用户需要部署 `fastgpt-sandbox` 镜像,并配置 `SANDBOX_URL` 环境变量。
- 沙盒默认最大运行 60s,可通过配置调整。
- 代码运行在隔离的进程池中,无法访问文件系统和内网。
## 变量输入
可在自定义输入中添加代码运行需要的变量。
**JavaScript** — 在 main 函数参数中解构:
```js
async function main({data1, data2}){
return {
result: data1 + data2
}
}
```
**Python** — 在 main 函数参数中按变量名接收:
```python
def main(data1, data2):
return {"result": data1 + data2}
```
## 结果输出
务必返回一个 object 对象(JS)或 dict 字典(Python)。
自定义输出中,可以添加变量名来获取对应 key 下的值。例如返回了:
```json
{
"result": "hello",
"count": 42
}
```
自定义输出中添加 `result` 和 `count` 两个变量即可获取对应的值。
## 内置函数
### httpRequest 发起 HTTP 请求
在沙盒内发起外部 HTTP 请求。自动拦截内网地址(SSRF 防护)。
**JavaScript 示例:**
```js
async function main({url}){
const res = await SystemHelper.httpRequest(url, {
method: 'GET', // 请求方法,默认 GET
headers: {}, // 自定义请求头
body: null, // 请求体(对象会自动 JSON 序列化)
timeout: 60 // 超时秒数,最大 60s
})
return {
status: res.status,
data: res.data
}
}
```
**Python 示例:**
```python
def main(url):
res = SystemHelper.httpRequest(url, method="GET", headers={}, timeout=10)
return {"status": res["status"], "data": res["data"]}
```
**限制:**
- 每次执行最多 30 个请求
- 单次请求超时 60s
- 响应体最大 2MB
- 仅允许 http/https 协议
- 自动拦截内网 IP(127.0.0.0/8, 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 等)
## 可用模块
### JavaScript 白名单模块
以下 npm 模块可通过 `require()` 使用:
| 模块 | 说明 | 示例 |
|------|------|------|
| `lodash` | 工具函数库 | `const _ = require('lodash')` |
| `moment` | 日期处理 | `const moment = require('moment')` |
| `dayjs` | 轻量日期库 | `const dayjs = require('dayjs')` |
| `crypto-js` | 加密库 | `const CryptoJS = require('crypto-js')` |
| `uuid` | UUID 生成 | `const { v4 } = require('uuid')` |
| `qs` | 查询字符串解析 | `const qs = require('qs')` |
其他模块(如 `fs`, `child_process`, `net` 等)被禁止使用。
### Python 白名单模块
以下 Python 标准库和第三方库可直接 import:
**数学和数值计算**
| 模块 | 说明 |
|------|------|
| `math` | 数学函数 |
| `cmath` | 复数数学 |
| `decimal` | 十进制浮点运算 |
| `fractions` | 分数运算 |
| `random` | 随机数生成 |
| `statistics` | 统计函数 |
**数据结构和算法**
| 模块 | 说明 |
|------|------|
| `collections` | 容器数据类型 |
| `array` | 数组 |
| `heapq` | 堆队列 |
| `bisect` | 数组二分查找 |
| `queue` | 队列 |
| `copy` | 浅拷贝和深拷贝 |
**函数式编程**
| 模块 | 说明 |
|------|------|
| `itertools` | 迭代器工具 |
| `functools` | 高阶函数 |
| `operator` | 标准运算符 |
**字符串和文本处理**
| 模块 | 说明 |
|------|------|
| `string` | 字符串常量 |
| `re` | 正则表达式 |
| `difflib` | 差异计算 |
| `textwrap` | 文本包装 |
| `unicodedata` | Unicode 数据库 |
| `codecs` | 编解码器 |
**日期和时间**
| 模块 | 说明 |
|------|------|
| `datetime` | 日期时间 |
| `time` | 时间访问 |
| `calendar` | 日历 |
**数据序列化**
| 模块 | 说明 |
|------|------|
| `json` | JSON 编解码 |
| `csv` | CSV 文件读写 |
| `base64` | Base64 编解码 |
| `binascii` | 二进制和 ASCII 转换 |
| `struct` | 字节串解析 |
**加密和哈希**
| 模块 | 说明 |
|------|------|
| `hashlib` | 哈希算法 |
| `hmac` | HMAC 消息认证 |
| `secrets` | 安全随机数 |
| `uuid` | UUID 生成 |
**类型和抽象**
| 模块 | 说明 |
|------|------|
| `typing` | 类型提示 |
| `abc` | 抽象基类 |
| `enum` | 枚举类型 |
| `dataclasses` | 数据类 |
| `contextlib` | 上下文管理器 |
**其他实用工具**
| 模块 | 说明 |
|------|------|
| `pprint` | 美化打印 |
| `weakref` | 弱引用 |
**第三方库**
| 模块 | 说明 |
|------|------|
| `numpy` | 数值计算 |
| `pandas` | 数据分析 |
| `matplotlib` | 数据可视化 |
**禁止使用的模块:** `os`, `sys`, `subprocess`, `socket`, `urllib`, `http`, `requests` 等涉及系统调用、网络访问、文件系统的模块。
## 安全限制
沙盒提供多层安全防护:
- **模块拦截**:JS 和 Python 均只允许使用白名单模块
- **网络隔离**:自动拦截内网 IP 请求(SSRF 防护)
- **文件隔离**:无法读写容器文件系统
- **超时保护**:默认 60s 超时,防止死循环
- **进程隔离**:每次执行在独立的沙盒进程中运行
## 使用示例
### JavaScript 示例
<details>
<summary>数据格式转换</summary>
```js
// 将逗号分隔的字符串转为数组
function main({input}){
const items = input.split(',').map(s => s.trim()).filter(Boolean)
return { items, count: items.length }
}
```
</details>
<details>
<summary>日期计算</summary>
```js
const dayjs = require('dayjs')
function main(){
const now = dayjs()
return {
today: now.format('YYYY-MM-DD'),
nextWeek: now.add(7, 'day').format('YYYY-MM-DD'),
timestamp: now.valueOf()
}
}
```
</details>
<details>
<summary>HTTP 请求 - 获取天气</summary>
```js
async function main({city}){
const res = await SystemHelper.httpRequest(
`https://api.example.com/weather?city=${city}`,
{ method: 'GET', timeout: 10 }
)
return {
temperature: res.data.temp,
weather: res.data.condition
}
}
```
</details>
<details>
<summary>数据加密</summary>
```js
const CryptoJS = require('crypto-js')
function main({text, key}){
const encrypted = CryptoJS.AES.encrypt(text, key).toString()
return { encrypted }
}
```
</details>
### Python 示例
<details>
<summary>数据统计</summary>
```python
import math
def main(numbers):
if not numbers:
return {"error": "no data"}
mean = sum(numbers) / len(numbers)
variance = sum((x - mean)**2 for x in numbers) / len(numbers)
return {
"mean": mean,
"max": max(numbers),
"min": min(numbers),
"std": math.sqrt(variance)
}
```
</details>
<details>
<summary>日期处理</summary>
```python
from datetime import datetime, timedelta
def main(date_str):
dt = datetime.strptime(date_str, "%Y-%m-%d")
next_week = dt + timedelta(days=7)
return {
"input": date_str,
"next_week": next_week.strftime("%Y-%m-%d"),
"weekday": dt.strftime("%A")
}
```
</details>
<details>
<summary>HTTP 请求 - API 调用</summary>
```python
def main(api_url, api_key):
res = SystemHelper.httpRequest(
api_url,
method="GET",
headers={"Authorization": f"Bearer {api_key}"},
timeout=10
)
return {
"status": res["status"],
"data": res["data"]
}
```
</details>
<details>
<summary>JSON 数据处理</summary>
```python
import json
def main(json_str):
data = json.loads(json_str)
# 提取特定字段
result = {
"names": [item["name"] for item in data if "name" in item],
"count": len(data)
}
return result
```
</details>
<details>
<summary>正则表达式匹配</summary>
```python
import re
def main(text):
# 提取所有邮箱地址
emails = re.findall(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b', text)
return {
"emails": emails,
"count": len(emails)
}
```
</details>
--- ---
title: 代码运行 title: 代码运行(弃)
description: FastGPT 代码运行节点介绍 description: FastGPT 代码运行节点介绍(适用于 4.14.7 及以下版本)
--- ---
> 本文档适用于 FastGPT **4.14.7 及以下版本**。4.14.8 及以上版本请参考 [代码运行(新版)](/docs/introduction/guide/dashboard/workflow/sandbox-v5)。
![alt text](/imgs/image.png) ![alt text](/imgs/image.png)
## 功能 ## 功能
......
...@@ -67,6 +67,7 @@ description: FastGPT Toc ...@@ -67,6 +67,7 @@ description: FastGPT Toc
- [/en/docs/introduction/guide/dashboard/workflow/question_classify](/en/docs/introduction/guide/dashboard/workflow/question_classify) - [/en/docs/introduction/guide/dashboard/workflow/question_classify](/en/docs/introduction/guide/dashboard/workflow/question_classify)
- [/en/docs/introduction/guide/dashboard/workflow/reply](/en/docs/introduction/guide/dashboard/workflow/reply) - [/en/docs/introduction/guide/dashboard/workflow/reply](/en/docs/introduction/guide/dashboard/workflow/reply)
- [/en/docs/introduction/guide/dashboard/workflow/sandbox](/en/docs/introduction/guide/dashboard/workflow/sandbox) - [/en/docs/introduction/guide/dashboard/workflow/sandbox](/en/docs/introduction/guide/dashboard/workflow/sandbox)
- [/en/docs/introduction/guide/dashboard/workflow/sandbox-v2](/en/docs/introduction/guide/dashboard/workflow/sandbox-v2)
- [/en/docs/introduction/guide/dashboard/workflow/text_editor](/en/docs/introduction/guide/dashboard/workflow/text_editor) - [/en/docs/introduction/guide/dashboard/workflow/text_editor](/en/docs/introduction/guide/dashboard/workflow/text_editor)
- [/en/docs/introduction/guide/dashboard/workflow/tfswitch](/en/docs/introduction/guide/dashboard/workflow/tfswitch) - [/en/docs/introduction/guide/dashboard/workflow/tfswitch](/en/docs/introduction/guide/dashboard/workflow/tfswitch)
- [/en/docs/introduction/guide/dashboard/workflow/tool](/en/docs/introduction/guide/dashboard/workflow/tool) - [/en/docs/introduction/guide/dashboard/workflow/tool](/en/docs/introduction/guide/dashboard/workflow/tool)
......
...@@ -67,6 +67,7 @@ description: FastGPT 文档目录 ...@@ -67,6 +67,7 @@ description: FastGPT 文档目录
- [/docs/introduction/guide/dashboard/workflow/question_classify](/docs/introduction/guide/dashboard/workflow/question_classify) - [/docs/introduction/guide/dashboard/workflow/question_classify](/docs/introduction/guide/dashboard/workflow/question_classify)
- [/docs/introduction/guide/dashboard/workflow/reply](/docs/introduction/guide/dashboard/workflow/reply) - [/docs/introduction/guide/dashboard/workflow/reply](/docs/introduction/guide/dashboard/workflow/reply)
- [/docs/introduction/guide/dashboard/workflow/sandbox](/docs/introduction/guide/dashboard/workflow/sandbox) - [/docs/introduction/guide/dashboard/workflow/sandbox](/docs/introduction/guide/dashboard/workflow/sandbox)
- [/docs/introduction/guide/dashboard/workflow/sandbox-v2](/docs/introduction/guide/dashboard/workflow/sandbox-v2)
- [/docs/introduction/guide/dashboard/workflow/text_editor](/docs/introduction/guide/dashboard/workflow/text_editor) - [/docs/introduction/guide/dashboard/workflow/text_editor](/docs/introduction/guide/dashboard/workflow/text_editor)
- [/docs/introduction/guide/dashboard/workflow/tfswitch](/docs/introduction/guide/dashboard/workflow/tfswitch) - [/docs/introduction/guide/dashboard/workflow/tfswitch](/docs/introduction/guide/dashboard/workflow/tfswitch)
- [/docs/introduction/guide/dashboard/workflow/tool](/docs/introduction/guide/dashboard/workflow/tool) - [/docs/introduction/guide/dashboard/workflow/tool](/docs/introduction/guide/dashboard/workflow/tool)
......
...@@ -12,6 +12,7 @@ export const getErrText = (err: any, def = ''): any => { ...@@ -12,6 +12,7 @@ export const getErrText = (err: any, def = ''): any => {
err?.response?.msg || err?.response?.msg ||
err?.msg || err?.msg ||
err?.error || err?.error ||
err?.code ||
def; def;
if (ERROR_RESPONSE[msg]) { if (ERROR_RESPONSE[msg]) {
......
export const JS_TEMPLATE = `function main({data1, data2}){ export const JS_TEMPLATE = `function main({data1, data2}){
return { return {
result: data1, result: data1,
data2 data2
...@@ -7,10 +7,11 @@ export const JS_TEMPLATE = `function main({data1, data2}){ ...@@ -7,10 +7,11 @@ export const JS_TEMPLATE = `function main({data1, data2}){
}`; }`;
export const PY_TEMPLATE = `def main(data1, data2): export const PY_TEMPLATE = `def main(data1, data2):
return {
"result": data1, return {
"data2": data2 "result": data1,
} "data2": data2
}
`; `;
export enum SandboxCodeTypeEnum { export enum SandboxCodeTypeEnum {
......
...@@ -25,10 +25,10 @@ export const CodeNode: FlowNodeTemplateType = { ...@@ -25,10 +25,10 @@ export const CodeNode: FlowNodeTemplateType = {
avatarLinear: 'core/workflow/template/codeRunLinear', avatarLinear: 'core/workflow/template/codeRunLinear',
colorSchema: 'lime', colorSchema: 'lime',
name: i18nT('workflow:code_execution'), name: i18nT('workflow:code_execution'),
intro: i18nT('workflow:execute_a_simple_script_code_usually_for_complex_data_processing'), intro: i18nT('workflow:code_sandbox_intro'),
showStatus: true, showStatus: true,
catchError: false, catchError: false,
courseUrl: '/docs/introduction/guide/dashboard/workflow/sandbox/', courseUrl: '/docs/introduction/guide/dashboard/workflow/sandbox-v2',
inputs: [ inputs: [
{ {
...Input_Template_DynamicInput, ...Input_Template_DynamicInput,
......
import type { ModuleDispatchProps } from '@fastgpt/global/core/workflow/runtime/type'; import type { ModuleDispatchProps } from '@fastgpt/global/core/workflow/runtime/type';
import { NodeInputKeyEnum, NodeOutputKeyEnum } from '@fastgpt/global/core/workflow/constants'; import { NodeInputKeyEnum, NodeOutputKeyEnum } from '@fastgpt/global/core/workflow/constants';
import { type DispatchNodeResultType } from '@fastgpt/global/core/workflow/runtime/type'; import { type DispatchNodeResultType } from '@fastgpt/global/core/workflow/runtime/type';
import { axios } from '../../../../common/api/axios';
import { DispatchNodeResponseKeyEnum } from '@fastgpt/global/core/workflow/runtime/constants'; import { DispatchNodeResponseKeyEnum } from '@fastgpt/global/core/workflow/runtime/constants';
import { SandboxCodeTypeEnum } from '@fastgpt/global/core/workflow/template/system/sandbox/constants';
import { getErrText } from '@fastgpt/global/common/error/utils'; import { getErrText } from '@fastgpt/global/common/error/utils';
import { codeSandbox } from '../../../../thirdProvider/codeSandbox';
type RunCodeType = ModuleDispatchProps<{ type RunCodeType = ModuleDispatchProps<{
[NodeInputKeyEnum.codeType]: string; [NodeInputKeyEnum.codeType]: string;
...@@ -22,44 +21,6 @@ type RunCodeResponse = DispatchNodeResultType< ...@@ -22,44 +21,6 @@ type RunCodeResponse = DispatchNodeResultType<
} }
>; >;
export const runCode = async ({
codeType,
code,
variables
}: {
codeType: string;
code: string;
variables: Record<string, any>;
}): Promise<{
codeReturn: Record<string, any>;
log: string;
}> => {
const url = (() => {
if (codeType == SandboxCodeTypeEnum.py) {
return `${process.env.SANDBOX_URL}/sandbox/python`;
} else {
return `${process.env.SANDBOX_URL}/sandbox/js`;
}
})();
const { data: runResult } = await axios.post<{
success: boolean;
data: {
codeReturn: Record<string, any>;
log: string;
};
}>(url, {
code,
variables
});
if (!runResult.success) {
return Promise.reject('Run code failed');
}
return runResult.data;
};
export const dispatchCodeSandbox = async (props: RunCodeType): Promise<RunCodeResponse> => { export const dispatchCodeSandbox = async (props: RunCodeType): Promise<RunCodeResponse> => {
const { const {
node: { catchError }, node: { catchError },
...@@ -79,7 +40,11 @@ export const dispatchCodeSandbox = async (props: RunCodeType): Promise<RunCodeRe ...@@ -79,7 +40,11 @@ export const dispatchCodeSandbox = async (props: RunCodeType): Promise<RunCodeRe
} }
try { try {
const { codeReturn, log } = await runCode({ codeType, code, variables: customVariables }); const { codeReturn, log } = await codeSandbox.runCode({
codeType,
code,
variables: customVariables
});
return { return {
data: { data: {
...@@ -94,7 +59,7 @@ export const dispatchCodeSandbox = async (props: RunCodeType): Promise<RunCodeRe ...@@ -94,7 +59,7 @@ export const dispatchCodeSandbox = async (props: RunCodeType): Promise<RunCodeRe
[DispatchNodeResponseKeyEnum.toolResponses]: codeReturn [DispatchNodeResponseKeyEnum.toolResponses]: codeReturn
}; };
} catch (error) { } catch (error) {
const text = getErrText(error); const text = getErrText(error, 'Request code sandbox failed');
// @adapt // @adapt
if (catchError === undefined) { if (catchError === undefined) {
......
import { SandboxCodeTypeEnum } from '@fastgpt/global/core/workflow/template/system/sandbox/constants';
import type { AxiosInstance } from 'axios';
import axios from 'axios';
export type SanndboxPackagesResponse = {
js: string[];
python: string[];
builtinGlobals: string[];
};
export class CodeSandbox {
private readonly client: AxiosInstance;
constructor() {
const baseUrl = process.env.SANDBOX_URL || '';
const token = process.env.SANDBOX_TOKEN || '';
this.client = axios.create({
baseURL: `${baseUrl.replace(/\/$/, '')}/sandbox`,
timeout: 180000,
headers: {
'Content-Type': 'application/json',
Authorization: token ? `Bearer ${token}` : undefined
}
});
this.client.interceptors.response.use(
(response) => {
const data = response.data;
if (!data.success) {
return Promise.reject(new Error(data.message || 'Request code sandbox failed'));
}
return response.data;
},
(error) => {
return Promise.reject(error);
}
);
}
async getPackages() {
const { data } = await this.client.get<SanndboxPackagesResponse>('/modules');
return data;
}
async runCode({
codeType,
code,
variables
}: {
codeType: string;
code: string;
variables: Record<string, any>;
}) {
const url = (() => {
if (codeType == SandboxCodeTypeEnum.py) {
return `/python`;
} else {
return `/js`;
}
})();
const { data } = await this.client.post<{
codeReturn: Record<string, any>;
log: string;
}>(url, { code, variables });
return data;
}
}
export const codeSandbox = new CodeSandbox();
...@@ -4,6 +4,9 @@ import { Box, type BoxProps } from '@chakra-ui/react'; ...@@ -4,6 +4,9 @@ import { Box, type BoxProps } from '@chakra-ui/react';
import MyIcon from '../../Icon'; import MyIcon from '../../Icon';
import { getWebReqUrl } from '../../../../common/system/utils'; import { getWebReqUrl } from '../../../../common/system/utils';
import usePythonCompletion from './usePythonCompletion'; import usePythonCompletion from './usePythonCompletion';
import useJSCompletion from './useJSCompletion';
import useSystemHelperCompletion from './useSystemHelperCompletion';
loader.config({ loader.config({
paths: { vs: getWebReqUrl('/js/monaco-editor.0.45.0/vs') } paths: { vs: getWebReqUrl('/js/monaco-editor.0.45.0/vs') }
}); });
...@@ -44,7 +47,12 @@ const defaultOptions = { ...@@ -44,7 +47,12 @@ const defaultOptions = {
scrollBeyondLastLine: false, scrollBeyondLastLine: false,
folding: true, folding: true,
overviewRulerBorder: false, overviewRulerBorder: false,
tabSize: 2 tabSize: 2,
wordBasedSuggestions: 'off',
quickSuggestions: { other: 'on', comments: false, strings: false },
suggest: {
showWords: false
}
}; };
const MyEditor = ({ const MyEditor = ({
...@@ -55,7 +63,7 @@ const MyEditor = ({ ...@@ -55,7 +63,7 @@ const MyEditor = ({
variables = [], variables = [],
defaultHeight = 200, defaultHeight = 200,
onOpenModal, onOpenModal,
language = 'typescript', language = 'javascript',
options, options,
...props ...props
}: Props) => { }: Props) => {
...@@ -71,6 +79,8 @@ const MyEditor = ({ ...@@ -71,6 +79,8 @@ const MyEditor = ({
); );
const registerPythonCompletion = usePythonCompletion(); const registerPythonCompletion = usePythonCompletion();
const registerJSCompletion = useJSCompletion();
const registerSystemHelperCompletion = useSystemHelperCompletion();
const handleMouseDown = useCallback((e: React.MouseEvent) => { const handleMouseDown = useCallback((e: React.MouseEvent) => {
initialY.current = e.clientY; initialY.current = e.clientY;
...@@ -139,8 +149,10 @@ const MyEditor = ({ ...@@ -139,8 +149,10 @@ const MyEditor = ({
} }
}); });
registerPythonCompletion(monaco); registerPythonCompletion(monaco);
registerJSCompletion(monaco);
registerSystemHelperCompletion(monaco);
}, },
[registerPythonCompletion] [registerPythonCompletion, registerJSCompletion, registerSystemHelperCompletion]
); );
return ( return (
......
...@@ -12,10 +12,10 @@ function getLanguage(language: string | undefined): string { ...@@ -12,10 +12,10 @@ function getLanguage(language: string | undefined): string {
fullName = 'python'; fullName = 'python';
break; break;
case 'js': case 'js':
fullName = 'typescript'; fullName = 'javascript';
break; break;
default: default:
fullName = `typescript`; fullName = `javascript`;
break; break;
} }
return fullName; return fullName;
......
import { type Monaco } from '@monaco-editor/react';
import { useCallback } from 'react';
/**
* Sandbox runtime type declarations.
* Matches the actual globals exposed by FastGPT sandbox worker (worker.ts).
*/
export const SANDBOX_GLOBALS_LIB = `
// ===== SystemHelper =====
interface SystemHelperHttpRequestOptions {
/** HTTP method, default: 'GET' */
method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
/** Custom request headers */
headers?: Record<string, string>;
/** Request body (objects auto JSON-serialized) */
body?: any;
/** Timeout in seconds, max 60 */
timeout?: number;
}
interface SystemHelperHttpResponse {
status: number;
statusText: string;
headers: Record<string, any>;
data: any;
}
interface ISystemHelper {
/** Send an HTTP request (SSRF protected, max 30 per run). */
httpRequest(url: string, options?: SystemHelperHttpRequestOptions): Promise<SystemHelperHttpResponse>;
}
declare const SystemHelper: ISystemHelper;
// ===== Node.js / Bun runtime globals =====
interface BufferConstructor {
alloc(size: number, fill?: string | number, encoding?: string): Buffer;
from(data: string | ArrayBuffer | number[] | Buffer, encoding?: string): Buffer;
concat(list: Buffer[], totalLength?: number): Buffer;
isBuffer(obj: any): obj is Buffer;
byteLength(string: string, encoding?: string): number;
}
interface Buffer extends Uint8Array {
toString(encoding?: string, start?: number, end?: number): string;
toJSON(): { type: 'Buffer'; data: number[] };
slice(start?: number, end?: number): Buffer;
length: number;
}
declare const Buffer: BufferConstructor;
declare function setTimeout(callback: (...args: any[]) => void, ms?: number, ...args: any[]): any;
declare function setInterval(callback: (...args: any[]) => void, ms?: number, ...args: any[]): any;
declare function clearTimeout(id: any): void;
declare function clearInterval(id: any): void;
// ===== Sandbox console (safe, only log) =====
interface SandboxConsole {
log(...args: any[]): void;
}
declare const console: SandboxConsole;
// ===== require (allowed modules only) =====
declare function require(module: 'lodash'): typeof import('lodash');
declare function require(module: 'moment'): typeof import('moment');
declare function require(module: 'dayjs'): typeof import('dayjs');
declare function require(module: 'crypto-js'): any;
declare function require(module: 'uuid'): { v4: () => string; v1: () => string };
declare function require(module: 'qs'): any;
declare function require(module: string): any;
`;
let monacoInstance: Monaco | null = null;
const useJSCompletion = () => {
return useCallback((monaco: Monaco) => {
if (monacoInstance === monaco) return;
monacoInstance = monaco;
const compilerOptions = {
target: monaco.languages.typescript.ScriptTarget.ESNext,
allowNonTsExtensions: true,
allowJs: true
};
monaco.languages.typescript.javascriptDefaults.setCompilerOptions(compilerOptions);
monaco.languages.typescript.typescriptDefaults.setCompilerOptions(compilerOptions);
// Inject sandbox type declarations — must follow setCompilerOptions
monaco.languages.typescript.javascriptDefaults.addExtraLib(
SANDBOX_GLOBALS_LIB,
'ts:filename/sandbox-globals.d.ts'
);
monaco.languages.typescript.typescriptDefaults.addExtraLib(
SANDBOX_GLOBALS_LIB,
'ts:filename/sandbox-globals.d.ts'
);
// Disable semantic validation — sandbox has custom module resolution (safeRequire)
// that TS cannot model. Keep syntax validation for bracket/paren errors.
const diagnosticsOptions = {
noSemanticValidation: true,
noSyntaxValidation: false,
noSuggestionDiagnostics: true
};
monaco.languages.typescript.javascriptDefaults.setDiagnosticsOptions(diagnosticsOptions);
monaco.languages.typescript.typescriptDefaults.setDiagnosticsOptions(diagnosticsOptions);
monaco.languages.registerCompletionItemProvider('javascript', {
triggerCharacters: ['.'],
provideCompletionItems: (model, position) => {
const wordInfo = model.getWordUntilPosition(position);
const currentWordPrefix = wordInfo.word;
const range = {
startLineNumber: position.lineNumber,
endLineNumber: position.lineNumber,
startColumn: wordInfo.startColumn,
endColumn: wordInfo.endColumn
};
const suggestions = [
{
label: 'console',
kind: monaco.languages.CompletionItemKind.Module,
insertText: 'console',
documentation: 'Console output (sandbox safe version, only log).',
range
},
{
label: 'require',
kind: monaco.languages.CompletionItemKind.Function,
insertText: "require('${1:module}')",
insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
documentation: 'Require allowed modules: lodash, moment, dayjs, crypto-js, uuid, qs.',
range
},
{
label: 'Buffer',
kind: monaco.languages.CompletionItemKind.Class,
insertText: 'Buffer',
documentation: 'Node.js Buffer for binary data.',
range
}
];
return {
suggestions: suggestions.filter((item) =>
item.label.toLowerCase().startsWith(currentWordPrefix.toLowerCase())
)
};
}
});
}, []);
};
export default useJSCompletion;
import { type Monaco } from '@monaco-editor/react'; import { type Monaco } from '@monaco-editor/react';
import { useCallback } from 'react'; import { useCallback } from 'react';
let monacoInstance: Monaco | null = null; let monacoInstance: Monaco | null = null;
const usePythonCompletion = () => { const usePythonCompletion = () => {
return useCallback((monaco: Monaco) => { return useCallback((monaco: Monaco) => {
if (monacoInstance === monaco) return; if (monacoInstance === monaco) return;
monacoInstance = monaco; monacoInstance = monaco;
monaco.languages.registerCompletionItemProvider('python', { monaco.languages.registerCompletionItemProvider('python', {
triggerCharacters: ['_'],
provideCompletionItems: (model, position) => { provideCompletionItems: (model, position) => {
const wordInfo = model.getWordUntilPosition(position); const wordInfo = model.getWordUntilPosition(position);
const currentWordPrefix = wordInfo.word; const currentWordPrefix = wordInfo.word;
const lineContent = model.getLineContent(position.lineNumber); const lineContent = model.getLineContent(position.lineNumber);
const textBeforeCursor = lineContent.slice(0, position.column - 1);
// Skip built-ins when in member access context (e.g. "foo.")
if (textBeforeCursor.endsWith('.')) return { suggestions: [] };
const range = { const range = {
startLineNumber: position.lineNumber, startLineNumber: position.lineNumber,
...@@ -20,37 +26,19 @@ const usePythonCompletion = () => { ...@@ -20,37 +26,19 @@ const usePythonCompletion = () => {
endColumn: wordInfo.endColumn endColumn: wordInfo.endColumn
}; };
const baseSuggestions = [ // import line — suggest common packages
{
label: 'len',
kind: monaco.languages.CompletionItemKind.Function,
insertText: 'len()',
documentation: 'get length of object',
range,
sortText: 'a'
}
];
const filtered = baseSuggestions.filter((item) =>
item.label.toLowerCase().startsWith(currentWordPrefix.toLowerCase())
);
if (lineContent.startsWith('import')) { if (lineContent.startsWith('import')) {
const importLength = 'import'.length; const importLength = 'import'.length;
const afterImport = lineContent.slice(importLength); const afterImport = lineContent.slice(importLength);
const spaceMatch = afterImport.match(/^\s*/); const spaceMatch = afterImport.match(/^\s*/);
const spaceLength = spaceMatch ? spaceMatch[0].length : 0; const spaceLength = spaceMatch ? spaceMatch[0].length : 0;
const startReplaceCol = importLength + spaceLength + 1; const startReplaceCol = importLength + spaceLength + 1;
const currentCol = position.column;
const replaceRange = new monaco.Range( const replaceRange = new monaco.Range(
position.lineNumber, position.lineNumber,
startReplaceCol, startReplaceCol,
position.lineNumber, position.lineNumber,
currentCol position.column
); );
const needsSpace = spaceLength === 0; const needsSpace = spaceLength === 0;
return { return {
suggestions: [ suggestions: [
...@@ -73,9 +61,40 @@ const usePythonCompletion = () => { ...@@ -73,9 +61,40 @@ const usePythonCompletion = () => {
}; };
} }
return { suggestions: filtered }; // General Python built-ins
}, const suggestions = [
triggerCharacters: ['.', '_'] {
label: 'len',
kind: monaco.languages.CompletionItemKind.Function,
insertText: 'len()',
documentation: 'Return the length of an object.',
range,
sortText: 'a'
},
{
label: 'print',
kind: monaco.languages.CompletionItemKind.Function,
insertText: 'print(${1:value})',
insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
documentation: 'Print values to stdout.',
range
},
{
label: 'range',
kind: monaco.languages.CompletionItemKind.Function,
insertText: 'range(${1:stop})',
insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
documentation: 'Return a range object.',
range
}
];
return {
suggestions: suggestions.filter((item) =>
item.label.toLowerCase().startsWith(currentWordPrefix.toLowerCase())
)
};
}
}); });
}, []); }, []);
}; };
......
import { type Monaco } from '@monaco-editor/react';
import { useCallback } from 'react';
let monacoInstance: Monaco | null = null;
const useSystemHelperCompletion = () => {
return useCallback((monaco: Monaco) => {
if (monacoInstance === monaco) return;
monacoInstance = monaco;
const buildSuggestions = (
monaco: Monaco,
model: Parameters<
Parameters<
typeof monaco.languages.registerCompletionItemProvider
>[1]['provideCompletionItems']
>[0],
position: Parameters<
Parameters<
typeof monaco.languages.registerCompletionItemProvider
>[1]['provideCompletionItems']
>[1],
memberSnippet: string
) => {
const lineContent = model.getLineContent(position.lineNumber);
const textBeforeCursor = lineContent.slice(0, position.column - 1);
// After "SystemHelper." — suggest members
if (textBeforeCursor.endsWith('SystemHelper.')) {
return {
suggestions: [
{
label: 'httpRequest',
kind: monaco.languages.CompletionItemKind.Method,
insertText: memberSnippet,
insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
documentation: 'Send an HTTP request. Returns { status, data }.',
range: {
startLineNumber: position.lineNumber,
endLineNumber: position.lineNumber,
startColumn: position.column,
endColumn: position.column
}
}
]
};
}
// Suggest "SystemHelper" as a global identifier
const wordInfo = model.getWordUntilPosition(position);
if (wordInfo.word.length > 0 && 'SystemHelper'.startsWith(wordInfo.word)) {
return {
suggestions: [
{
label: 'SystemHelper',
kind: monaco.languages.CompletionItemKind.Module,
insertText: 'SystemHelper',
documentation: 'Built-in helper utilities provided by FastGPT sandbox.',
range: {
startLineNumber: position.lineNumber,
endLineNumber: position.lineNumber,
startColumn: wordInfo.startColumn,
endColumn: wordInfo.endColumn
}
}
]
};
}
return { suggestions: [] };
};
// JS/TS completion provider
const jsSnippet =
"httpRequest(${1:url}, {\n\tmethod: '${2:GET}',\n\theaders: {},\n\tbody: null,\n\ttimeout: 60\n})";
for (const lang of ['javascript', 'typescript'] as const) {
monaco.languages.registerCompletionItemProvider(lang, {
triggerCharacters: ['.'],
provideCompletionItems: (model, position) =>
buildSuggestions(monaco, model, position, jsSnippet)
});
}
// Python completion provider
const pySnippet = 'httpRequest(${1:url}, method="${2:GET}", headers={}, timeout=${3:60})';
monaco.languages.registerCompletionItemProvider('python', {
triggerCharacters: ['.'],
provideCompletionItems: (model, position) =>
buildSuggestions(monaco, model, position, pySnippet)
});
}, []);
};
export default useSystemHelperCompletion;
...@@ -30,7 +30,10 @@ ...@@ -30,7 +30,10 @@
"code.Reset template": "Reset Template", "code.Reset template": "Reset Template",
"code.Reset template confirm": "Confirm reset code template? This will reset all inputs and outputs to template values. Please save your current code.", "code.Reset template confirm": "Confirm reset code template? This will reset all inputs and outputs to template values. Please save your current code.",
"code.Switch language confirm": "Switching the language will reset the code, will it continue?", "code.Switch language confirm": "Switching the language will reset the code, will it continue?",
"code_allow_packages": "Available dependencies",
"code_allow_packages_func": "Available packages: {{modules}}\nGlobal functions: {{globals}}",
"code_execution": "Code Sandbox", "code_execution": "Code Sandbox",
"code_sandbox_intro": "Execute a piece of script code, usually used for complex data processing.",
"collection_metadata_filter": "Collection Metadata Filter", "collection_metadata_filter": "Collection Metadata Filter",
"complete_extraction_result": "Complete Extraction Result", "complete_extraction_result": "Complete Extraction Result",
"complete_extraction_result_description": "A JSON string, e.g., {\"name\":\"YY\",\"Time\":\"2023/7/2 18:00\"}", "complete_extraction_result_description": "A JSON string, e.g., {\"name\":\"YY\",\"Time\":\"2023/7/2 18:00\"}",
...@@ -59,7 +62,6 @@ ...@@ -59,7 +62,6 @@
"error_catch": "Error catch", "error_catch": "Error catch",
"error_info_returns_empty_on_success": "Error information of code execution, returns empty on success", "error_info_returns_empty_on_success": "Error information of code execution, returns empty on success",
"error_text": "Error text", "error_text": "Error text",
"execute_a_simple_script_code_usually_for_complex_data_processing": "Execute a simple script code, usually for complex data processing.",
"execute_different_branches_based_on_conditions": "Execute different branches based on conditions.", "execute_different_branches_based_on_conditions": "Execute different branches based on conditions.",
"execution_error": "Execution Error", "execution_error": "Execution Error",
"external_variables": "External variables", "external_variables": "External variables",
...@@ -178,7 +180,6 @@ ...@@ -178,7 +180,6 @@
"select_default_option": "Select the default value", "select_default_option": "Select the default value",
"special_array_format": "Special array format, returns an empty array when the search result is empty.", "special_array_format": "Special array format, returns an empty array when the search result is empty.",
"start_with": "Starts With", "start_with": "Starts With",
"support_code_language": "Support import list: pandas,numpy",
"target_fields_description": "A target field consists of 'description' and 'key'. Multiple target fields can be extracted.", "target_fields_description": "A target field consists of 'description' and 'key'. Multiple target fields can be extracted.",
"template.agent": "Agent", "template.agent": "Agent",
"template.agent_intro": "Automatically select one or more functional blocks for calling through the AI model, or call plugins.", "template.agent_intro": "Automatically select one or more functional blocks for calling through the AI model, or call plugins.",
......
...@@ -30,7 +30,10 @@ ...@@ -30,7 +30,10 @@
"code.Reset template": "还原模板", "code.Reset template": "还原模板",
"code.Reset template confirm": "确认还原代码模板?将会重置所有输入和输出至模板值,请注意保存当前代码。", "code.Reset template confirm": "确认还原代码模板?将会重置所有输入和输出至模板值,请注意保存当前代码。",
"code.Switch language confirm": "切换语言将重置代码,是否继续?", "code.Switch language confirm": "切换语言将重置代码,是否继续?",
"code_allow_packages": "可用依赖",
"code_allow_packages_func": "可用依赖: {{modules}}\n全局函数: {{globals}}",
"code_execution": "代码运行", "code_execution": "代码运行",
"code_sandbox_intro": "执行一段脚本代码,通常用于进行复杂的数据处理。",
"collection_metadata_filter": "集合元数据过滤", "collection_metadata_filter": "集合元数据过滤",
"complete_extraction_result": "完整提取结果", "complete_extraction_result": "完整提取结果",
"complete_extraction_result_description": "一个 JSON 字符串,例如:{\"name:\":\"YY\",\"Time\":\"2023/7/2 18:00\"}", "complete_extraction_result_description": "一个 JSON 字符串,例如:{\"name:\":\"YY\",\"Time\":\"2023/7/2 18:00\"}",
...@@ -59,7 +62,6 @@ ...@@ -59,7 +62,6 @@
"error_catch": "报错捕获", "error_catch": "报错捕获",
"error_info_returns_empty_on_success": "代码运行错误信息,成功时返回空", "error_info_returns_empty_on_success": "代码运行错误信息,成功时返回空",
"error_text": "错误信息", "error_text": "错误信息",
"execute_a_simple_script_code_usually_for_complex_data_processing": "执行一段简单的脚本代码,通常用于进行复杂的数据处理。",
"execute_different_branches_based_on_conditions": "根据一定的条件,执行不同的分支。", "execute_different_branches_based_on_conditions": "根据一定的条件,执行不同的分支。",
"execution_error": "运行错误", "execution_error": "运行错误",
"external_variables": "外部变量", "external_variables": "外部变量",
...@@ -178,7 +180,6 @@ ...@@ -178,7 +180,6 @@
"select_default_option": "选择默认值", "select_default_option": "选择默认值",
"special_array_format": "特殊数组格式,搜索结果为空时,返回空数组。", "special_array_format": "特殊数组格式,搜索结果为空时,返回空数组。",
"start_with": "开始为", "start_with": "开始为",
"support_code_language": "支持import列表:pandas,numpy",
"target_fields_description": "由 '描述' 和 'key' 组成一个目标字段,可提取多个目标字段", "target_fields_description": "由 '描述' 和 'key' 组成一个目标字段,可提取多个目标字段",
"template.agent": "工具调用", "template.agent": "工具调用",
"template.agent_intro": "由 AI 自主决定工具调用。", "template.agent_intro": "由 AI 自主决定工具调用。",
......
...@@ -30,7 +30,10 @@ ...@@ -30,7 +30,10 @@
"code.Reset template": "重設範本", "code.Reset template": "重設範本",
"code.Reset template confirm": "確定要重設程式碼範本嗎?這將會把所有輸入和輸出重設為範本值。請儲存您目前的程式碼。", "code.Reset template confirm": "確定要重設程式碼範本嗎?這將會把所有輸入和輸出重設為範本值。請儲存您目前的程式碼。",
"code.Switch language confirm": "切換語言將重設代碼,是否繼續?", "code.Switch language confirm": "切換語言將重設代碼,是否繼續?",
"code_allow_packages": "可用依賴",
"code_allow_packages_func": "可用依賴: {{modules}}\n全域函數: {{globals}}",
"code_execution": "程式碼執行", "code_execution": "程式碼執行",
"code_sandbox_intro": "執行一段腳本程式碼,通常用於進行複雜的資料處理。",
"collection_metadata_filter": "資料集詮釋資料篩選器", "collection_metadata_filter": "資料集詮釋資料篩選器",
"complete_extraction_result": "完整擷取結果", "complete_extraction_result": "完整擷取結果",
"complete_extraction_result_description": "一個 JSON 字串,例如:{\"name\":\"YY\",\"Time\":\"2023/7/2 18:00\"}", "complete_extraction_result_description": "一個 JSON 字串,例如:{\"name\":\"YY\",\"Time\":\"2023/7/2 18:00\"}",
...@@ -59,7 +62,6 @@ ...@@ -59,7 +62,6 @@
"error_catch": "報錯捕獲", "error_catch": "報錯捕獲",
"error_info_returns_empty_on_success": "程式碼執行錯誤資訊,成功時回傳空值", "error_info_returns_empty_on_success": "程式碼執行錯誤資訊,成功時回傳空值",
"error_text": "錯誤訊息", "error_text": "錯誤訊息",
"execute_a_simple_script_code_usually_for_complex_data_processing": "執行一段簡單的腳本程式碼,通常用於複雜的資料處理。",
"execute_different_branches_based_on_conditions": "根據條件執行不同的分支。", "execute_different_branches_based_on_conditions": "根據條件執行不同的分支。",
"execution_error": "執行錯誤", "execution_error": "執行錯誤",
"external_variables": "外部變量", "external_variables": "外部變量",
...@@ -178,7 +180,6 @@ ...@@ -178,7 +180,6 @@
"select_default_option": "選擇默認值", "select_default_option": "選擇默認值",
"special_array_format": "特殊陣列格式,搜尋結果為空時,回傳空陣列。", "special_array_format": "特殊陣列格式,搜尋結果為空時,回傳空陣列。",
"start_with": "開頭為", "start_with": "開頭為",
"support_code_language": "支援 import 列表:pandas,numpy",
"target_fields_description": "由「描述」和「鍵值」組成一個目標欄位,可以擷取多個目標欄位", "target_fields_description": "由「描述」和「鍵值」組成一個目標欄位,可以擷取多個目標欄位",
"template.agent": "工具调用", "template.agent": "工具调用",
"template.agent_intro": "透過 AI 模型自動選擇一或多個功能區塊進行呼叫,也可以呼叫外掛程式。", "template.agent_intro": "透過 AI 模型自動選擇一或多個功能區塊進行呼叫,也可以呼叫外掛程式。",
......
...@@ -894,97 +894,6 @@ importers: ...@@ -894,97 +894,6 @@ importers:
specifier: ^5.0.1 specifier: ^5.0.1
version: 5.0.1 version: 5.0.1
projects/sandbox:
dependencies:
'@fastify/static':
specifier: ^7.0.4
version: 7.0.4
'@nestjs/common':
specifier: ^10.4.16
version: 10.4.16(reflect-metadata@0.2.2)(rxjs@7.8.2)
'@nestjs/core':
specifier: ^10.0.0
version: 10.4.15(@nestjs/common@10.4.16(reflect-metadata@0.2.2)(rxjs@7.8.2))(encoding@0.1.13)(reflect-metadata@0.2.2)(rxjs@7.8.2)
'@nestjs/platform-fastify':
specifier: ^10.3.8
version: 10.4.15(@fastify/static@7.0.4)(@nestjs/common@10.4.16(reflect-metadata@0.2.2)(rxjs@7.8.2))(@nestjs/core@10.4.15(@nestjs/common@10.4.16(reflect-metadata@0.2.2)(rxjs@7.8.2))(encoding@0.1.13)(reflect-metadata@0.2.2)(rxjs@7.8.2))
'@nestjs/swagger':
specifier: ^7.3.1
version: 7.4.2(@fastify/static@7.0.4)(@nestjs/common@10.4.16(reflect-metadata@0.2.2)(rxjs@7.8.2))(@nestjs/core@10.4.15(@nestjs/common@10.4.16(reflect-metadata@0.2.2)(rxjs@7.8.2))(encoding@0.1.13)(reflect-metadata@0.2.2)(rxjs@7.8.2))(reflect-metadata@0.2.2)
dayjs:
specifier: ^1.11.7
version: 1.11.13
fastify:
specifier: ^4.29.1
version: 4.29.1
isolated-vm:
specifier: ^4.7.2
version: 4.7.2
node-gyp:
specifier: ^10.1.0
version: 10.3.1
reflect-metadata:
specifier: ^0.2.0
version: 0.2.2
rxjs:
specifier: ^7.8.1
version: 7.8.2
tiktoken:
specifier: 1.0.17
version: 1.0.17
devDependencies:
'@nestjs/cli':
specifier: ^10.0.0
version: 10.4.9
'@nestjs/schematics':
specifier: ^10.0.0
version: 10.2.3(chokidar@3.6.0)(typescript@5.8.2)
'@nestjs/testing':
specifier: ^10.0.0
version: 10.4.15(@nestjs/common@10.4.16(reflect-metadata@0.2.2)(rxjs@7.8.2))(@nestjs/core@10.4.15(@nestjs/common@10.4.16(reflect-metadata@0.2.2)(rxjs@7.8.2))(encoding@0.1.13)(reflect-metadata@0.2.2)(rxjs@7.8.2))
'@types/jest':
specifier: ^29.5.2
version: 29.5.14
'@types/node':
specifier: ^20.14.2
version: 20.17.24
'@types/supertest':
specifier: ^6.0.0
version: 6.0.2
'@typescript-eslint/eslint-plugin':
specifier: ^6.21.0
version: 6.21.0(@typescript-eslint/parser@6.21.0(eslint@8.56.0)(typescript@5.8.2))(eslint@8.56.0)(typescript@5.8.2)
'@typescript-eslint/parser':
specifier: ^6.21.0
version: 6.21.0(eslint@8.56.0)(typescript@5.8.2)
eslint:
specifier: 8.56.0
version: 8.56.0
jest:
specifier: ^29.5.0
version: 29.7.0(@types/node@20.17.24)(babel-plugin-macros@3.1.0)(ts-node@10.9.2(@types/node@20.17.24)(typescript@5.8.2))
source-map-support:
specifier: ^0.5.21
version: 0.5.21
supertest:
specifier: ^6.3.3
version: 6.3.4
ts-jest:
specifier: ^29.1.0
version: 29.2.6(@babel/core@7.26.10)(@jest/transform@29.7.0)(@jest/types@29.6.3)(babel-jest@29.7.0(@babel/core@7.26.10))(jest@29.7.0(@types/node@20.17.24)(babel-plugin-macros@3.1.0)(ts-node@10.9.2(@types/node@20.17.24)(typescript@5.8.2)))(typescript@5.8.2)
ts-loader:
specifier: ^9.4.3
version: 9.5.2(typescript@5.8.2)(webpack@5.97.1)
ts-node:
specifier: ^10.9.1
version: 10.9.2(@types/node@20.17.24)(typescript@5.8.2)
tsconfig-paths:
specifier: ^4.2.0
version: 4.2.0
typescript:
specifier: ^5.1.3
version: 5.8.2
scripts/icon: scripts/icon:
dependencies: dependencies:
express: express:
......
...@@ -3,6 +3,5 @@ packages: ...@@ -3,6 +3,5 @@ packages:
- projects/app - projects/app
- projects/marketplace - projects/marketplace
- projects/mcp_server - projects/mcp_server
- projects/sandbox
- scripts/icon - scripts/icon
- sdk/* - sdk/*
...@@ -24,8 +24,9 @@ HIDE_CHAT_COPYRIGHT_SETTING= ...@@ -24,8 +24,9 @@ HIDE_CHAT_COPYRIGHT_SETTING=
# Plugin # Plugin
PLUGIN_BASE_URL=http://localhost:3003 PLUGIN_BASE_URL=http://localhost:3003
PLUGIN_TOKEN=token PLUGIN_TOKEN=token
# code sandbox url # Code sandbox server
SANDBOX_URL=http://localhost:3002 SANDBOX_URL=http://localhost:3002
SANDBOX_TOKEN=
# ai proxy api # ai proxy api
AIPROXY_API_ENDPOINT=https://localhost:3010 AIPROXY_API_ENDPOINT=https://localhost:3010
AIPROXY_API_TOKEN=aiproxy AIPROXY_API_TOKEN=aiproxy
......
...@@ -2,7 +2,7 @@ import { Box, ModalBody, useDisclosure } from '@chakra-ui/react'; ...@@ -2,7 +2,7 @@ import { Box, ModalBody, useDisclosure } from '@chakra-ui/react';
import Markdown from '@/components/Markdown'; import Markdown from '@/components/Markdown';
import MyModal from '@fastgpt/web/components/common/MyModal'; import MyModal from '@fastgpt/web/components/common/MyModal';
import { getDocPath } from '@/web/common/system/doc'; import { getDocPath } from '@/web/common/system/doc';
import React from 'react'; import React, { useCallback } from 'react';
const UseGuideModal = ({ const UseGuideModal = ({
children, children,
...@@ -18,14 +18,14 @@ const UseGuideModal = ({ ...@@ -18,14 +18,14 @@ const UseGuideModal = ({
link?: string; link?: string;
}) => { }) => {
const { isOpen, onOpen, onClose } = useDisclosure(); const { isOpen, onOpen, onClose } = useDisclosure();
const onClick = () => { const onClick = useCallback(() => {
if (link) { if (link) {
return window.open(getDocPath(link), '_blank'); return window.open(getDocPath(link), '_blank');
} }
if (text) { if (text) {
return onOpen(); return onOpen();
} }
}; }, [link, text, onOpen]);
return ( return (
<> <>
......
...@@ -268,6 +268,15 @@ const ChatItem = ({ hasPlanCheck, ...props }: Props) => { ...@@ -268,6 +268,15 @@ const ChatItem = ({ hasPlanCheck, ...props }: Props) => {
} }
]); ]);
} }
} else if (groupedValues.length === 0) {
// 对于非最后一条的空 AI 消息,也补充一个空节点,避免消息"消失"
groupedValues.push([
{
text: {
content: ''
}
}
]);
} }
return groupedValues; return groupedValues;
......
...@@ -1171,7 +1171,6 @@ const ChatBox = ({ ...@@ -1171,7 +1171,6 @@ const ChatBox = ({
return result; return result;
}, [chatType, chatRecords, expandedDeletedGroups]); }, [chatType, chatRecords, expandedDeletedGroups]);
//chat history //chat history
const hasPlanCheck = const hasPlanCheck =
lastInteractive?.type === 'agentPlanCheck' && !lastInteractive.params.confirmed; lastInteractive?.type === 'agentPlanCheck' && !lastInteractive.params.confirmed;
......
import { GET } from '@/web/common/api/request';
import type { SanndboxPackagesResponse } from '@fastgpt/service/thirdProvider/codeSandbox';
export const getSandboxPackages = async () =>
GET<SanndboxPackagesResponse>('/core/workflow/getSandboxPackages');
...@@ -14,7 +14,6 @@ import RenderOutput from '../render/RenderOutput'; ...@@ -14,7 +14,6 @@ import RenderOutput from '../render/RenderOutput';
import CodeEditor from '@fastgpt/web/components/common/Textarea/CodeEditor'; import CodeEditor from '@fastgpt/web/components/common/Textarea/CodeEditor';
import { Box, Button, Flex } from '@chakra-ui/react'; import { Box, Button, Flex } from '@chakra-ui/react';
import { useConfirm } from '@fastgpt/web/hooks/useConfirm'; import { useConfirm } from '@fastgpt/web/hooks/useConfirm';
import QuestionTip from '@fastgpt/web/components/common/MyTooltip/QuestionTip';
import { import {
JS_TEMPLATE, JS_TEMPLATE,
PY_TEMPLATE, PY_TEMPLATE,
...@@ -30,6 +29,9 @@ import { useMemoEnhance } from '@fastgpt/web/hooks/useMemoEnhance'; ...@@ -30,6 +29,9 @@ import { useMemoEnhance } from '@fastgpt/web/hooks/useMemoEnhance';
import { WorkflowUtilsContext } from '../../../context/workflowUtilsContext'; import { WorkflowUtilsContext } from '../../../context/workflowUtilsContext';
import { WorkflowActionsContext } from '../../../context/workflowActionsContext'; import { WorkflowActionsContext } from '../../../context/workflowActionsContext';
import { WorkflowUIContext } from '../../../context/workflowUIContext'; import { WorkflowUIContext } from '../../../context/workflowUIContext';
import { useRequest } from '@fastgpt/web/hooks/useRequest';
import { getSandboxPackages } from './api';
import MyTooltip from '@fastgpt/web/components/common/MyTooltip';
const NodeCode = ({ data, selected }: NodeProps<FlowNodeItemType>) => { const NodeCode = ({ data, selected }: NodeProps<FlowNodeItemType>) => {
const { t } = useTranslation(); const { t } = useTranslation();
...@@ -51,6 +53,22 @@ const NodeCode = ({ data, selected }: NodeProps<FlowNodeItemType>) => { ...@@ -51,6 +53,22 @@ const NodeCode = ({ data, selected }: NodeProps<FlowNodeItemType>) => {
content: t('workflow:code.Switch language confirm') content: t('workflow:code.Switch language confirm')
}); });
const { data: packages } = useRequest(getSandboxPackages, {
manual: false,
errorToast: ''
});
const packageText = useMemo(() => {
const packagesList =
codeType.value === SandboxCodeTypeEnum.js
? packages?.js.join(', ')
: packages?.python.join(', ');
return t('workflow:code_allow_packages_func', {
modules: packagesList,
globals: packages?.builtinGlobals.join(', ')
});
}, [packages, codeType.value]);
const CustomComponent = useMemo(() => { const CustomComponent = useMemo(() => {
return { return {
[NodeInputKeyEnum.code]: (item: FlowNodeInputItemType) => { [NodeInputKeyEnum.code]: (item: FlowNodeInputItemType) => {
...@@ -88,9 +106,15 @@ const NodeCode = ({ data, selected }: NodeProps<FlowNodeItemType>) => { ...@@ -88,9 +106,15 @@ const NodeCode = ({ data, selected }: NodeProps<FlowNodeItemType>) => {
})(); })();
}} }}
/> />
{codeType.value === 'py' && (
<QuestionTip ml={2} label={t('workflow:support_code_language')} /> {!!packages && (
<MyTooltip label={packageText}>
<Box ml={2} fontSize={'sm'} color={'primary.500'}>
{t('workflow:code_allow_packages')}
</Box>
</MyTooltip>
)} )}
<PopoverConfirm <PopoverConfirm
Trigger={ Trigger={
<Box cursor={'pointer'} color={'primary.500'} fontSize={'xs'} ml="auto" mr={2}> <Box cursor={'pointer'} color={'primary.500'} fontSize={'xs'} ml="auto" mr={2}>
...@@ -135,7 +159,7 @@ const NodeCode = ({ data, selected }: NodeProps<FlowNodeItemType>) => { ...@@ -135,7 +159,7 @@ const NodeCode = ({ data, selected }: NodeProps<FlowNodeItemType>) => {
); );
} }
}; };
}, [codeType, nodeId, t, presentationMode, onChangeNode]); }, [packageText, codeType, nodeId, t, presentationMode, onChangeNode]);
const { isTool, commonInputs } = useMemoEnhance( const { isTool, commonInputs } = useMemoEnhance(
() => splitToolInputs(inputs, nodeId), () => splitToolInputs(inputs, nodeId),
......
import { NextAPI } from '@/service/middleware/entry';
import { authCert } from '@fastgpt/service/support/permission/auth/common';
import { codeSandbox } from '@fastgpt/service/thirdProvider/codeSandbox';
import type { ApiRequestProps, ApiResponseType } from '@fastgpt/service/type/next';
export type ResponseType = {};
async function handler(
req: ApiRequestProps,
res: ApiResponseType<ResponseType>
): Promise<ResponseType> {
await authCert({ req, authToken: true });
return codeSandbox.getPackages();
}
export default NextAPI(handler);
...@@ -4,8 +4,8 @@ import { POST } from '@fastgpt/service/common/api/plusRequest'; ...@@ -4,8 +4,8 @@ import { POST } from '@fastgpt/service/common/api/plusRequest';
import { getLogger, LogCategories } from '@fastgpt/service/common/logger'; import { getLogger, LogCategories } from '@fastgpt/service/common/logger';
import { S3Buckets } from '@fastgpt/service/common/s3/constants'; import { S3Buckets } from '@fastgpt/service/common/s3/constants';
import { InitialErrorEnum } from '@fastgpt/service/common/system/constants'; import { InitialErrorEnum } from '@fastgpt/service/common/system/constants';
import { runCode } from '@fastgpt/service/core/workflow/dispatch/tools/codeSandbox';
import { loadModelProviders } from '@fastgpt/service/thirdProvider/fastgptPlugin/model'; import { loadModelProviders } from '@fastgpt/service/thirdProvider/fastgptPlugin/model';
import { codeSandbox } from '@fastgpt/service/thirdProvider/codeSandbox';
export const instrumentationCheck = async () => { export const instrumentationCheck = async () => {
const logger = getLogger(LogCategories.SYSTEM); const logger = getLogger(LogCategories.SYSTEM);
...@@ -50,13 +50,11 @@ export const instrumentationCheck = async () => { ...@@ -50,13 +50,11 @@ export const instrumentationCheck = async () => {
} }
// sandbox // sandbox
try { try {
await runCode({ await codeSandbox.runCode({
codeType: SandboxCodeTypeEnum.py, codeType: SandboxCodeTypeEnum.py,
code: `def main(): code: `def main():
print("Hello, World!") print("Hello, World!")
return { return {}`,
}
`,
variables: {} variables: {}
}); });
} catch (error) { } catch (error) {
......
# ===== Server =====
# Port the sandbox server listens on
SANDBOX_PORT=3000
# Auth token for API requests (empty = no auth)
SANDBOX_TOKEN=
# ===== Resource Limits =====
# Execution timeout per request (ms)
SANDBOX_MAX_TIMEOUT=60000
# Maximum allowed memory per user code execution (MB)
# Note: System automatically adds 50MB for runtime overhead
# Actual process limit = SANDBOX_MAX_MEMORY_MB + 50MB
SANDBOX_MAX_MEMORY_MB=256
# ===== Process Pool =====
# Number of pre-warmed worker processes (JS + Python)
SANDBOX_POOL_SIZE=20
# ===== Network Request Limits =====
# Maximum number of HTTP requests per execution
SANDBOX_REQUEST_MAX_COUNT=30
# Timeout for each outbound HTTP request (ms)
SANDBOX_REQUEST_TIMEOUT=60000
# Maximum response body size for outbound requests
SANDBOX_REQUEST_MAX_RESPONSE_MB=10
# Maximum request body size for outbound requests (MB)
SANDBOX_REQUEST_MAX_BODY_MB=5
# ===== Module Control =====
# JS allowed modules whitelist (comma-separated)
SANDBOX_JS_ALLOWED_MODULES=lodash,dayjs,moment,uuid,crypto-js,qs,url,querystring
# Python allowed modules whitelist (comma-separated)
SANDBOX_PYTHON_ALLOWED_MODULES=math,cmath,decimal,fractions,random,statistics,collections,array,heapq,bisect,queue,copy,itertools,functools,operator,string,re,difflib,textwrap,unicodedata,codecs,datetime,time,calendar,_strptime,json,csv,base64,binascii,struct,hashlib,hmac,secrets,uuid,typing,abc,enum,dataclasses,contextlib,pprint,weakref,numpy,pandas,matplotlib
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
tsconfigRootDir: __dirname,
sourceType: 'module'
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: ['plugin:@typescript-eslint/recommended'],
root: true,
env: {
node: true,
jest: true
},
ignorePatterns: ['.eslintrc.js'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-unused-vars': 'warn',
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/no-var-requires': 'off'
}
};
# --------- install dependence ----------- # ===== Bun 构建层 =====
FROM python:3.11-alpine AS python_base FROM oven/bun:1-alpine AS builder
# 安装make和g++以及libseccomp开发包
RUN apk add --no-cache make g++ tar wget gperf automake libtool linux-headers libseccomp-dev
WORKDIR /app WORKDIR /app
COPY projects/sandbox/requirements.txt /app/requirements.txt COPY projects/sandbox/package.json projects/sandbox/bun.lock ./
RUN bun install --frozen-lockfile
# 先安装Cython和其他Python依赖 COPY projects/sandbox/src ./src
RUN pip install --no-cache-dir -i https://mirrors.aliyun.com/pypi/simple Cython && \ COPY projects/sandbox/tsconfig.json ./
pip install --no-cache-dir -i https://mirrors.aliyun.com/pypi/simple -r /app/requirements.txt
# 下载、编译并安装libseccomp及其Python绑定
ENV VERSION_RELEASE=2.5.5
RUN wget https://github.com/seccomp/libseccomp/releases/download/v2.5.5/libseccomp-2.5.5.tar.gz && \
tar -zxvf libseccomp-2.5.5.tar.gz && \
cd libseccomp-2.5.5 && \
./configure --prefix=/usr && \
make && \
make install && \
cd src/python && \
python setup.py install && \
cd /app && \
rm -rf libseccomp-2.5.5 libseccomp-2.5.5.tar.gz
FROM node:20.14.0-alpine AS install
# ===== 运行层 =====
FROM oven/bun:1-alpine AS runner
WORKDIR /app WORKDIR /app
ARG proxy # 安装 Python、依赖包及工具
RUN [ -z "$proxy" ] || sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories # - util-linux: 提供 prlimit 命令(内存限制)
RUN apk add --no-cache make g++ python3 RUN apk add --no-cache python3 py3-pip libffi util-linux && \
apk add --no-cache --virtual .build-deps gcc g++ musl-dev python3-dev libffi-dev
# copy py3.11 COPY projects/sandbox/requirements.txt /tmp/requirements.txt
COPY --from=python_base /usr/local /usr/local RUN pip3 install --no-cache-dir --break-system-packages -r /tmp/requirements.txt && \
rm /tmp/requirements.txt && \
RUN npm install -g pnpm@9.4.0 apk del .build-deps
RUN [ -z "$proxy" ] || pnpm config set registry https://registry.npmmirror.com
# 复制 node_modules 和源码
COPY pnpm-lock.yaml pnpm-workspace.yaml ./ COPY --from=builder /app/node_modules ./node_modules
COPY ./projects/sandbox/package.json ./projects/sandbox/package.json COPY --from=builder /app/src ./src
COPY --from=builder /app/package.json ./
RUN [ -f pnpm-lock.yaml ] || (echo "Lockfile not found." && exit 1)
# 创建非 root 用户运行沙箱
RUN pnpm i RUN addgroup -S sandbox && adduser -S sandbox -G sandbox && \
chown -R sandbox:sandbox /app
# --------- builder ----------- USER sandbox
FROM node:20.14.0-alpine AS builder
WORKDIR /app
COPY package.json pnpm-workspace.yaml /app/
COPY --from=install /app/node_modules /app/node_modules
COPY ./projects/sandbox /app/projects/sandbox
COPY --from=install /app/projects/sandbox /app/projects/sandbox
RUN npm install -g pnpm@9.4.0
RUN pnpm --filter=sandbox build
# --------- runner -----------
FROM node:20.14.0-alpine AS runner
WORKDIR /app
RUN apk add --no-cache libffi libffi-dev strace bash
COPY --from=python_base /usr/local /usr/local
COPY --from=builder /app/node_modules /app/node_modules
COPY --from=builder /app/projects/sandbox /app/projects/sandbox
ENV NODE_ENV=production ENV NODE_ENV=production
ENV PATH="/usr/local/bin:${PATH}" ENV SANDBOX_PORT=3000
EXPOSE 3000
CMD ["node", "--no-node-snapshot", "projects/sandbox/dist/main.js"] CMD ["bun", "run", "src/index.ts"]
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"deleteOutDir": true,
"plugins": ["@nestjs/swagger"]
}
}
{ {
"name": "sandbox", "name": "sandbox",
"version": "4.8.3", "version": "5.0.0",
"description": "", "description": "FastGPT Code Sandbox - Bun + Hono + 统一子进程模型",
"author": "", "author": "",
"private": true, "private": true,
"license": "UNLICENSED", "license": "UNLICENSED",
"scripts": { "scripts": {
"build": "nest build", "dev": "bun run --watch src/index.ts",
"start": "nest start", "start": "bun run src/index.ts",
"dev": "NODE_OPTIONS='--no-node-snapshot' nest start --watch", "test": "vitest run",
"start:debug": "nest start --debug --watch", "test:watch": "vitest"
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
}, },
"dependencies": { "dependencies": {
"@fastify/static": "^7.0.4", "axios": "^1.7.9",
"@nestjs/common": "^10.4.16", "crypto-js": "^4.2.0",
"@nestjs/core": "^10.0.0", "dayjs": "^1.11.13",
"@nestjs/platform-fastify": "^10.3.8", "dotenv": "^17.3.1",
"@nestjs/swagger": "^7.3.1", "hono": "^4.7.6",
"dayjs": "^1.11.7", "lodash": "^4.17.21",
"fastify": "^4.29.1", "moment": "^2.30.1",
"isolated-vm": "^4.7.2", "qs": "^6.13.1",
"node-gyp": "^10.1.0", "tiktoken": "1.0.17",
"reflect-metadata": "^0.2.0", "uuid": "^9.0.1",
"rxjs": "^7.8.1", "zod": "^4.3.6"
"tiktoken": "1.0.17"
}, },
"devDependencies": { "devDependencies": {
"@nestjs/cli": "^10.0.0", "@types/bun": "^1.2.4",
"@nestjs/schematics": "^10.0.0",
"@nestjs/testing": "^10.0.0",
"@types/jest": "^29.5.2",
"@types/node": "^20.14.2", "@types/node": "^20.14.2",
"@types/supertest": "^6.0.0", "vitest": "^3.0.9",
"@typescript-eslint/eslint-plugin": "^6.21.0", "@vitest/coverage-v8": "^3.0.9",
"@typescript-eslint/parser": "^6.21.0", "typescript": "^5.7.3"
"eslint": "8.56.0",
"jest": "^29.5.0",
"source-map-support": "^0.5.21",
"supertest": "^6.3.3",
"ts-jest": "^29.1.0",
"ts-loader": "^9.4.3",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.1.3"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
} }
} }
numpy numpy
pandas pandas
\ No newline at end of file matplotlib
\ No newline at end of file
import { Module } from '@nestjs/common';
import { SandboxController } from './sandbox/sandbox.controller';
import { SandboxService } from './sandbox/sandbox.service';
@Module({
imports: [],
controllers: [SandboxController],
providers: [SandboxService]
})
export class AppModule {}
/**
* 配置入口 - 从 env.ts 重新导出
*
* 保持向后兼容:其他模块继续 import { config } from './config'
*/
import { env } from './env';
export const config = env;
/**
* 环境变量加载与校验
*
* 使用 dotenv 加载 .env 文件,zod 做类型转换和校验。
*/
import dotenv from 'dotenv';
import { z } from 'zod';
dotenv.config();
/** coerce 数字,带默认值 */
const int = (defaultValue: number) => z.coerce.number().int().default(defaultValue);
/** 字符串,带默认值 */
const str = (defaultValue: string) => z.string().default(defaultValue);
const envSchema = z.object({
// ===== 服务 =====
SANDBOX_PORT: int(3000),
/** Bearer token,仅允许 ASCII 可打印字符(RFC 6750) */
SANDBOX_TOKEN: z
.string()
.default('')
.refine((v) => v === '' || /^[\x21-\x7E]+$/.test(v), {
message:
'SANDBOX_TOKEN contains invalid characters. Only ASCII printable characters (no spaces) are allowed.'
}),
// ===== 进程池 =====
/** 进程池大小(预热 worker 数量) */
SANDBOX_POOL_SIZE: int(20).pipe(z.number().min(1).max(100)),
// ===== 资源限制 =====
SANDBOX_MAX_TIMEOUT: int(60000).pipe(z.number().min(1000).max(600000)),
SANDBOX_MAX_MEMORY_MB: int(256).pipe(z.number().min(32).max(4096)),
// ===== 网络请求限制 =====
SANDBOX_REQUEST_MAX_COUNT: int(30).pipe(z.number().min(1).max(1000)),
SANDBOX_REQUEST_TIMEOUT: int(60000).pipe(z.number().min(1000).max(300000)),
SANDBOX_REQUEST_MAX_RESPONSE_MB: int(10).pipe(z.number().min(1).max(100)),
SANDBOX_REQUEST_MAX_BODY_MB: int(5).pipe(z.number().min(1).max(100)),
// ===== 模块控制 =====
/** JS 可用模块白名单,逗号分隔 */
SANDBOX_JS_ALLOWED_MODULES: str('lodash,dayjs,moment,uuid,crypto-js,qs,url,querystring'),
/** Python 可用模块白名单,逗号分隔 */
SANDBOX_PYTHON_ALLOWED_MODULES: str(
'math,cmath,decimal,fractions,random,statistics,' +
'collections,array,heapq,bisect,queue,copy,' +
'itertools,functools,operator,' +
'string,re,difflib,textwrap,unicodedata,codecs,' +
'datetime,time,calendar,_strptime,' +
'json,csv,base64,binascii,struct,' +
'hashlib,hmac,secrets,uuid,' +
'typing,abc,enum,dataclasses,contextlib,' +
'pprint,' +
'numpy,pandas,matplotlib'
)
});
const parsed = envSchema.safeParse(process.env);
if (!parsed.success) {
console.error('❌ Invalid environment variables:');
console.error(parsed.error.format());
process.exit(1);
}
const e = parsed.data;
/** 类型安全的配置对象,字段名与代码风格一致 */
export const env = {
// 服务
port: e.SANDBOX_PORT,
token: e.SANDBOX_TOKEN,
// 资源限制
maxTimeoutMs: e.SANDBOX_MAX_TIMEOUT,
maxMemoryMB: e.SANDBOX_MAX_MEMORY_MB,
/** 运行时内存开销(运行时 + 沙箱代码) */
RUNTIME_MEMORY_OVERHEAD_MB: 50,
// 进程池
poolSize: e.SANDBOX_POOL_SIZE,
// 网络请求限制
maxRequests: e.SANDBOX_REQUEST_MAX_COUNT,
requestTimeoutMs: e.SANDBOX_REQUEST_TIMEOUT,
maxResponseSize: e.SANDBOX_REQUEST_MAX_RESPONSE_MB,
maxRequestBodySize: e.SANDBOX_REQUEST_MAX_BODY_MB,
// 模块控制
jsAllowedModules: e.SANDBOX_JS_ALLOWED_MODULES.split(',')
.map((s) => s.trim())
.filter(Boolean),
pythonAllowedModules: e.SANDBOX_PYTHON_ALLOWED_MODULES.split(',')
.map((s) => s.trim())
.filter(Boolean)
} as const;
export type Env = typeof env;
import { ExceptionFilter, Catch, ArgumentsHost } from '@nestjs/common';
import { FastifyRequest, FastifyReply } from 'fastify';
import { getErrText } from './utils';
@Catch()
export class HttpExceptionFilter implements ExceptionFilter {
catch(error: any, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse<FastifyReply>();
response.status(500).send({
success: false,
time: new Date(),
message: getErrText(error)
});
}
}
import './env'; // dotenv 最先加载
import { Hono } from 'hono';
import { bearerAuth } from 'hono/bearer-auth';
import { z } from 'zod';
import { config } from './config';
import { ProcessPool } from './pool/process-pool';
import { PythonProcessPool } from './pool/python-process-pool';
import type { ExecuteOptions } from './types';
import { getErrText } from './utils';
/** 请求体校验 schema */
const executeSchema = z.object({
code: z
.string()
.min(1)
.max(5 * 1024 * 1024), // 最大 5MB 代码
variables: z.record(z.string(), z.any()).default({})
});
const app = new Hono();
/** 进程池 */
const jsPool = new ProcessPool(config.poolSize);
const pythonPool = new PythonProcessPool(config.poolSize);
const poolReady = Promise.all([jsPool.init(), pythonPool.init()])
.then(() => {
console.log(`Process pools ready: JS=${config.poolSize}, Python=${config.poolSize} workers`);
})
.catch((err) => {
console.log('Failed to init process pool:', err.message);
process.exit(1);
});
/** 健康检查(不需要认证) */
app.get('/health', (c) => {
const jsStats = jsPool.stats;
const pyStats = pythonPool.stats;
const isReady = jsStats.total > 0 && pyStats.total > 0;
return c.json({ status: isReady ? 'ok' : 'degraded' }, isReady ? 200 : 503);
});
/** 认证中间件:仅当配置了 token 时启用 */
if (config.token) {
app.use('/sandbox/*', bearerAuth({ token: config.token }));
} else {
console.warn(
'⚠️ WARNING: SANDBOX_TOKEN is not set. API endpoints are unauthenticated. Set SANDBOX_TOKEN in production!'
);
}
/** JS 执行 */
app.post('/sandbox/js', async (c) => {
try {
const raw = await c.req.json();
const parsed = executeSchema.safeParse(raw);
if (!parsed.success) {
return c.json(
{
success: false,
message: `Invalid request: ${parsed.error.issues[0]?.message || 'validation failed'}`
},
400
);
}
const result = await jsPool.execute(parsed.data as ExecuteOptions);
return c.json(result);
} catch (err: any) {
console.log('JS sandbox error:', err);
return c.json({
success: false,
message: getErrText(err)
});
}
});
/** Python 执行 */
app.post('/sandbox/python', async (c) => {
try {
const raw = await c.req.json();
const parsed = executeSchema.safeParse(raw);
if (!parsed.success) {
return c.json(
{
success: false,
message: `Invalid request: ${parsed.error.issues[0]?.message || 'validation failed'}`
},
400
);
}
const result = await pythonPool.execute(parsed.data as ExecuteOptions);
return c.json(result);
} catch (err: any) {
console.log('Python sandbox error:', err);
return c.json({
success: false,
message: getErrText(err)
});
}
});
/** 查询可用模块 */
app.get('/sandbox/modules', (c) => {
return c.json({
success: true,
data: {
js: config.jsAllowedModules,
python: config.pythonAllowedModules,
builtinGlobals: ['SystemHelper.httpRequest']
}
});
});
/** 启动服务 */
console.log(`Sandbox server starting on port ${config.port}...`);
export default {
port: config.port,
fetch: app.fetch
};
/** 导出 app 和 poolReady 供测试使用 */
export { app, poolReady };
import { NestFactory } from '@nestjs/core';
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
import { AppModule } from './app.module';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { HttpExceptionFilter } from './http-exception.filter';
import { ResponseInterceptor } from './response';
async function bootstrap(port: number) {
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter({
bodyLimit: 50 * 1048576 // 50MB
})
);
// 使用全局异常过滤器
app.useGlobalFilters(new HttpExceptionFilter());
app.useGlobalInterceptors(new ResponseInterceptor());
const config = new DocumentBuilder()
.setTitle('Cats example')
.setDescription('The cats API description')
.setVersion('1.0')
.addTag('cats')
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api', app, document);
try {
await app.listen(port, '0.0.0.0');
console.log(`Application is running on: ${await app.getUrl()}`);
} catch (error) {
if ((error as any).code === 'EADDRINUSE') {
console.warn(`Port ${port} is already in use, trying next port...`);
await bootstrap(port + 1);
} else {
console.error(`Failed to start application: ${(error as Error).message}`);
process.exit(1);
}
}
}
bootstrap(3000);
/**
* ProcessPool - JS (Bun) 子进程池
*
* 继承 BaseProcessPool,提供 Bun worker 的 spawn 配置。
*/
import { join } from 'path';
import { config } from '../config';
import { BaseProcessPool } from './base-process-pool';
const WORKER_SCRIPT = join(
typeof import.meta.dir === 'string' ? import.meta.dir : new URL('.', import.meta.url).pathname,
'worker.ts'
);
export class ProcessPool extends BaseProcessPool {
constructor(poolSize?: number) {
super(poolSize, {
name: 'JS',
workerScript: WORKER_SCRIPT,
spawnCommand: (script) => `exec bun run ${script}`,
allowedModules: config.jsAllowedModules
});
}
}
/**
* PythonProcessPool - Python 子进程池
*
* 继承 BaseProcessPool,提供 Python worker 的 spawn 配置。
*/
import { join } from 'path';
import { config } from '../config';
import { BaseProcessPool } from './base-process-pool';
const WORKER_SCRIPT = join(
typeof import.meta.dir === 'string' ? import.meta.dir : new URL('.', import.meta.url).pathname,
'worker.py'
);
export class PythonProcessPool extends BaseProcessPool {
constructor(poolSize?: number) {
super(poolSize, {
name: 'Python',
workerScript: WORKER_SCRIPT,
spawnCommand: (script) => `exec python3 -u ${script}`,
allowedModules: config.pythonAllowedModules
});
}
}
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
@Injectable()
export class ResponseInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
return next.handle().pipe(
map((data) => ({
success: true,
data
}))
);
}
}
export const pythonScript = `
import os
import subprocess
import json
import ast
import base64
def extract_imports(code):
tree = ast.parse(code)
imports = []
for node in ast.walk(tree):
if isinstance(node, (ast.Import, ast.ImportFrom)):
if isinstance(node, ast.Import):
for alias in node.names:
imports.append(f"import {alias.name}")
elif isinstance(node, ast.ImportFrom):
module = node.module
for alias in node.names:
imports.append(f"from {module} import {alias.name}")
return imports
seccomp_prefix = """
import platform
import sys
# Skip seccomp on macOS since it's Linux-specific
if platform.system() == 'Linux':
try:
from seccomp import *
import errno
allowed_syscalls = [
# File operations - READ ONLY (removed SYS_WRITE)
"syscall.SYS_READ",
# Removed "syscall.SYS_WRITE" - no general write access
"syscall.SYS_OPEN", # Still needed for reading files
"syscall.SYS_OPENAT", # Still needed for reading files
"syscall.SYS_CLOSE",
"syscall.SYS_FSTAT",
"syscall.SYS_LSTAT",
"syscall.SYS_STAT",
"syscall.SYS_NEWFSTATAT",
"syscall.SYS_LSEEK",
"syscall.SYS_GETDENTS64",
"syscall.SYS_FCNTL",
"syscall.SYS_ACCESS",
"syscall.SYS_FACCESSAT",
# Memory management - essential for Python
"syscall.SYS_MMAP",
"syscall.SYS_BRK",
"syscall.SYS_MPROTECT",
"syscall.SYS_MUNMAP",
"syscall.SYS_MREMAP",
# Process/thread operations
"syscall.SYS_GETUID",
"syscall.SYS_GETGID",
"syscall.SYS_GETEUID",
"syscall.SYS_GETEGID",
"syscall.SYS_GETPID",
"syscall.SYS_GETPPID",
"syscall.SYS_GETTID",
"syscall.SYS_EXIT",
"syscall.SYS_EXIT_GROUP",
# Signal handling
"syscall.SYS_RT_SIGACTION",
"syscall.SYS_RT_SIGPROCMASK",
"syscall.SYS_RT_SIGRETURN",
"syscall.SYS_SIGALTSTACK",
# Time operations
"syscall.SYS_CLOCK_GETTIME",
"syscall.SYS_GETTIMEOFDAY",
"syscall.SYS_TIME",
# Threading/synchronization
"syscall.SYS_FUTEX",
"syscall.SYS_SET_ROBUST_LIST",
"syscall.SYS_GET_ROBUST_LIST",
"syscall.SYS_CLONE",
# System info
"syscall.SYS_UNAME",
"syscall.SYS_ARCH_PRCTL",
"syscall.SYS_RSEQ",
# I/O operations
"syscall.SYS_IOCTL",
"syscall.SYS_POLL",
"syscall.SYS_SELECT",
"syscall.SYS_PSELECT6",
# Process scheduling
"syscall.SYS_SCHED_YIELD",
"syscall.SYS_SCHED_GETAFFINITY",
# Additional Python runtime essentials
"syscall.SYS_GETRANDOM",
"syscall.SYS_GETCWD",
"syscall.SYS_READLINK",
"syscall.SYS_READLINKAT",
]
allowed_syscalls_tmp = allowed_syscalls
L = []
for item in allowed_syscalls_tmp:
item = item.strip()
parts = item.split(".")[1][4:].lower()
L.append(parts)
f = SyscallFilter(defaction=KILL)
for item in L:
f.add_rule(ALLOW, item)
# Only allow writing to stdout and stderr for output
f.add_rule(ALLOW, "write", Arg(0, EQ, sys.stdout.fileno()))
f.add_rule(ALLOW, "write", Arg(0, EQ, sys.stderr.fileno()))
# Remove other write-related syscalls
# f.add_rule(ALLOW, 307) # Removed - might be file creation
# f.add_rule(ALLOW, 318) # Removed - might be file creation
# f.add_rule(ALLOW, 334) # Removed - might be file creation
f.load()
except ImportError:
# seccomp module not available, skip security restrictions
pass
"""
def remove_print_statements(code):
class PrintRemover(ast.NodeTransformer):
def visit_Expr(self, node):
if (
isinstance(node.value, ast.Call)
and isinstance(node.value.func, ast.Name)
and node.value.func.id == "print"
):
return None
return node
tree = ast.parse(code)
modified_tree = PrintRemover().visit(tree)
ast.fix_missing_locations(modified_tree)
return ast.unparse(modified_tree)
def detect_dangerous_imports(code):
# Add file writing modules to the blacklist
dangerous_modules = [
"os", "sys", "subprocess", "shutil", "socket", "ctypes",
"multiprocessing", "threading", "pickle",
# Additional modules that can write files
"tempfile", "pathlib", "io", "fileinput"
]
tree = ast.parse(code)
for node in ast.walk(tree):
if isinstance(node, ast.Import):
for alias in node.names:
if alias.name in dangerous_modules:
return alias.name
elif isinstance(node, ast.ImportFrom):
if node.module in dangerous_modules:
return node.module
return None
def detect_file_write_operations(code):
"""Detect potential file writing operations in code"""
dangerous_patterns = [
'open(', 'file(', 'write(', 'writelines(',
'with open', 'f.write', '.write(',
'create', 'mkdir', 'makedirs'
]
for pattern in dangerous_patterns:
if pattern in code:
return f"File write operation detected: {pattern}"
return None
def run_pythonCode(data:dict):
if not data or "code" not in data:
return {"error": "Invalid request format: missing code"}
code = data.get("code")
if not code or not code.strip():
return {"error": "Code cannot be empty"}
code = remove_print_statements(code)
dangerous_import = detect_dangerous_imports(code)
if dangerous_import:
return {"error": f"Importing {dangerous_import} is not allowed."}
# Check for file write operations
write_operation = detect_file_write_operations(code)
if write_operation:
return {"error": f"File write operations are not allowed: {write_operation}"}
# Handle variables - default to empty dict if not provided or None
variables = data.get("variables", {})
if variables is None:
variables = {}
imports = "\\n".join(extract_imports(code))
var_def = ""
# Process variables with proper validation
for k, v in variables.items():
if not isinstance(k, str) or not k.strip():
return {"error": f"Invalid variable name: {repr(k)}"}
# Use repr() to properly handle Python True/False/None values
try:
one_var = f"{k} = {repr(v)}\\n"
var_def = var_def + one_var
except Exception as e:
return {"error": f"Error processing variable {k}: {str(e)}"}
# Create a safe main function call with error handling
output_code = '''if __name__ == '__main__':
import inspect
try:
# Get main function signature
sig = inspect.signature(main)
params = list(sig.parameters.keys())
# Create arguments dict from available variables
available_vars = {''' + ', '.join([f'"{k}": {k}' for k in variables.keys()]) + '''}
# Match parameters with available variables
args = []
kwargs = {}
for param_name in params:
if param_name in available_vars:
args.append(available_vars[param_name])
else:
# Check if parameter has default value
param = sig.parameters[param_name]
if param.default is not inspect.Parameter.empty:
break # Stop adding positional args, rest will use defaults
else:
raise TypeError(f"main() missing required argument: '{param_name}'. Available variables: {list(available_vars.keys())}")
# Call main function
if args:
res = main(*args)
else:
res = main()
print(res)
except Exception as e:
print({"error": f"Error calling main function: {str(e)}"})
'''
code = imports + "\\n" + seccomp_prefix + "\\n" + var_def + "\\n" + code + "\\n" + output_code
# Note: We still need to create the subprocess file for execution,
# but user code cannot write additional files
tmp_file = os.path.join(data["tempDir"], "subProcess.py")
with open(tmp_file, "w", encoding="utf-8") as f:
f.write(code)
try:
result = subprocess.run(["python3", tmp_file], capture_output=True, text=True, timeout=10)
if result.returncode == -31:
return {"error": "Dangerous behavior detected (likely file write attempt)."}
if result.stderr != "":
return {"error": result.stderr}
out = ast.literal_eval(result.stdout.strip())
return out
except subprocess.TimeoutExpired:
return {"error": "Timeout error or blocked by system security policy"}
except Exception as e:
return {"error": str(e)}
`;
export class RunCodeDto {
code: string;
variables: object;
}
export class RunCodeResponse {
codeReturn: Record<string, any>;
log: string;
}
import * as crypto from 'crypto';
import * as querystring from 'querystring';
export const createHmac = (algorithm: string, secret: string) => {
const timestamp = Date.now().toString();
const stringToSign = `${timestamp}\n${secret}`;
// 创建 HMAC
const hmac = crypto.createHmac(algorithm, secret);
hmac.update(stringToSign, 'utf8');
const signData = hmac.digest();
const sign = querystring.escape(Buffer.from(signData).toString('base64'));
return {
timestamp,
sign
};
};
export const timeDelay = (time: number) => {
return new Promise((resolve, reject) => {
if (time > 10000) {
reject('Delay time must be less than 10');
}
setTimeout(() => {
resolve('');
}, time);
});
};
export const strToBase64 = (str: string, prefix: string = '') => {
const base64_string = Buffer.from(str, 'utf-8').toString('base64');
return `${prefix}${base64_string}`;
};
import { Tiktoken } from 'tiktoken/lite';
const cl100k_base = require('tiktoken/encoders/cl100k_base');
export const countToken = (text: string = '') => {
const enc = new Tiktoken(cl100k_base.bpe_ranks, cl100k_base.special_tokens, cl100k_base.pat_str);
const encodeText = enc.encode(text);
return encodeText.length;
};
import { Test, TestingModule } from '@nestjs/testing';
import { SandboxController } from './sandbox.controller';
import { SandboxService } from './sandbox.service';
describe('SandboxController', () => {
let controller: SandboxController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [SandboxController],
providers: [SandboxService]
}).compile();
controller = module.get<SandboxController>(SandboxController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});
import { Controller, Post, Body, HttpCode } from '@nestjs/common';
import { RunCodeDto } from './dto/create-sandbox.dto';
import { runJsSandbox, runPythonSandbox } from './utils';
@Controller('sandbox')
export class SandboxController {
constructor() {}
@Post('/js')
@HttpCode(200)
runJs(@Body() codeProps: RunCodeDto) {
return runJsSandbox(codeProps);
}
@Post('/python')
@HttpCode(200)
runPython(@Body() codeProps: RunCodeDto) {
return runPythonSandbox(codeProps);
}
}
import { Module } from '@nestjs/common';
import { SandboxService } from './sandbox.service';
import { SandboxController } from './sandbox.controller';
@Module({
controllers: [SandboxController],
providers: [SandboxService]
})
export class SandboxModule {}
import { Test, TestingModule } from '@nestjs/testing';
import { SandboxService } from './sandbox.service';
describe('SandboxService', () => {
let service: SandboxService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [SandboxService]
}).compile();
service = module.get<SandboxService>(SandboxService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});
import { Injectable } from '@nestjs/common';
import { RunCodeDto } from './dto/create-sandbox.dto';
@Injectable()
export class SandboxService {
runJs(params: RunCodeDto) {
return {};
}
}
import { RunCodeDto, RunCodeResponse } from 'src/sandbox/dto/create-sandbox.dto';
import IsolatedVM, { ExternalCopy, Isolate, Reference } from 'isolated-vm';
import { mkdtemp, writeFile } from 'fs/promises';
import { tmpdir } from 'os';
import { join } from 'path';
import { rmSync } from 'fs';
import { countToken } from './jsFn/tiktoken';
import { timeDelay } from './jsFn/delay';
import { strToBase64 } from './jsFn/str2Base64';
import { createHmac } from './jsFn/crypto';
import { spawn } from 'child_process';
import { pythonScript } from './constants';
const CustomLogStr = 'CUSTOM_LOG';
export const runJsSandbox = async ({
code,
variables = {}
}: RunCodeDto): Promise<RunCodeResponse> => {
/*
Rewrite code to add custom functions: Promise function; Log.
*/
function getFnCode(code: string) {
// rewrite log
code = code.replace(/console\.log/g, `${CustomLogStr}`);
// Promise function rewrite
const rewriteSystemFn = `
const thisDelay = (...args) => global_delay.applySyncPromise(undefined,args)
`;
// rewrite delay
code = code.replace(/delay\((.*)\)/g, `thisDelay($1)`);
const runCode = `
(async() => {
try {
${rewriteSystemFn}
${code}
const res = await main(variables, {})
return JSON.stringify(res);
} catch(err) {
return JSON.stringify({ERROR: err?.message ?? err})
}
})
`;
return runCode;
}
// Register global function
function registerSystemFn(jail: IsolatedVM.Reference<Record<string | number | symbol, any>>) {
return Promise.all([
jail.set('global_delay', new Reference(timeDelay)),
jail.set('countToken', countToken),
jail.set('strToBase64', strToBase64),
jail.set('createHmac', createHmac)
]);
}
const logData = [];
const isolate = new Isolate({ memoryLimit: 32 });
const context = await isolate.createContext();
const jail = context.global;
try {
// Add global variables
await Promise.all([
jail.set('variables', new ExternalCopy(variables).copyInto()),
jail.set(CustomLogStr, function (...args) {
logData.push(
args
.map((item) => (typeof item === 'object' ? JSON.stringify(item, null, 2) : item))
.join(', ')
);
}),
registerSystemFn(jail)
]);
// Run code
const fn = await context.eval(getFnCode(code), { reference: true, timeout: 10000 });
try {
// Get result and parse
const value = await fn.apply(undefined, [], { result: { promise: true } });
const result = JSON.parse(value.toLocaleString());
// release memory
context.release();
isolate.dispose();
if (result.ERROR) {
return Promise.reject(result.ERROR);
}
return {
codeReturn: result,
log: logData.join('\n')
};
} catch (error) {
context.release();
isolate.dispose();
return Promise.reject('Not an invalid response.You must return an object');
}
} catch (err) {
console.log(err);
context.release();
isolate.dispose();
return Promise.reject(err);
}
};
const PythonScriptFileName = 'main.py';
export const runPythonSandbox = async ({
code,
variables = {}
}: RunCodeDto): Promise<RunCodeResponse> => {
// Validate input parameters
if (!code || typeof code !== 'string' || !code.trim()) {
return Promise.reject('Code cannot be empty');
}
// Ensure variables is an object
if (variables === null || variables === undefined) {
variables = {};
}
if (typeof variables !== 'object' || Array.isArray(variables)) {
return Promise.reject('Variables must be an object');
}
const tempDir = await mkdtemp(join(tmpdir(), 'python_script_tmp_'));
const dataJson = JSON.stringify({ code, variables, tempDir });
const dataBase64 = Buffer.from(dataJson).toString('base64');
const mainCallCode = `
import json
import base64
data = json.loads(base64.b64decode('${dataBase64}').decode('utf-8'))
res = run_pythonCode(data)
print(json.dumps(res))
`;
const fullCode = [pythonScript, mainCallCode].filter(Boolean).join('\n');
const { path: tempFilePath, cleanup } = await createTempFile(tempDir, fullCode);
const pythonProcess = spawn('python3', ['-u', tempFilePath]);
const stdoutChunks: string[] = [];
const stderrChunks: string[] = [];
pythonProcess.stdout.on('data', (data) => stdoutChunks.push(data.toString()));
pythonProcess.stderr.on('data', (data) => stderrChunks.push(data.toString()));
const stdoutPromise = new Promise<string>((resolve) => {
pythonProcess.on('close', (code) => {
if (code !== 0) {
resolve(JSON.stringify({ error: stderrChunks.join('') }));
} else {
resolve(stdoutChunks.join(''));
}
});
});
const stdout = await stdoutPromise.finally(() => {
cleanup();
});
try {
const parsedOutput = JSON.parse(stdout);
if (parsedOutput.error) {
return Promise.reject(parsedOutput.error || 'Unknown error');
}
return { codeReturn: parsedOutput, log: '' };
} catch (err) {
if (
stdout.includes('malformed node or string on line 1') ||
stdout.includes('invalid syntax (<unknown>, line 1)')
) {
return Promise.reject(`The result should be a parsable variable, such as a list. ${stdout}`);
} else if (stdout.includes('Unexpected end of JSON input')) {
return Promise.reject(`Not allowed print or ${stdout}`);
}
return Promise.reject(`Run failed: ${err}`);
}
};
// write full code into a tmp file
async function createTempFile(tempFileDirPath: string, context: string) {
const tempFilePath = join(tempFileDirPath, PythonScriptFileName);
try {
await writeFile(tempFilePath, context);
return {
path: tempFilePath,
cleanup: () => {
rmSync(tempFileDirPath, {
recursive: true,
force: true
});
}
};
} catch (err) {
return Promise.reject(`write file err: ${err}`);
}
}
/** 执行请求参数 */
export type ExecuteOptions = {
code: string;
variables: Record<string, any>;
};
/** 执行结果 */
export type ExecuteResult = {
success: boolean;
data?: {
codeReturn: any;
log: string;
};
message?: string;
};
export const replaceSensitiveText = (text: string) => {
// 1. http link
text = text.replace(/(?<=https?:\/\/)[^\s]+/g, 'xxx');
// 2. nx-xxx 全部替换成xxx
text = text.replace(/ns-[\w-]+/g, 'xxx');
return text;
};
export const getErrText = (err: any, def = '') => {
const msg: string = typeof err === 'string' ? err : err?.message ?? def;
msg && console.log('error =>', msg);
return replaceSensitiveText(msg);
};
export const getErrText = (err: any, def = ''): any => {
const msg: string =
typeof err === 'string'
? err || def
: err?.response?.data?.message ||
err?.response?.message ||
err?.message ||
err?.response?.data?.msg ||
err?.response?.msg ||
err?.msg ||
err?.error ||
def;
// Axios special
if (err?.errors && Array.isArray(err.errors) && err.errors.length > 0) {
return err.errors[0].message;
}
// msg && console.log('error =>', msg);
return msg;
};
/**
* Semaphore - 简单信号量,控制最大并发数
*
* 超出并发上限的请求排队等待,避免子进程数爆炸。
*/
export class Semaphore {
private current = 0;
private queue: (() => void)[] = [];
constructor(private readonly max: number) {}
/** 获取许可,超出上限则排队 */
acquire(): Promise<void> {
if (this.current < this.max) {
this.current++;
return Promise.resolve();
}
return new Promise<void>((resolve) => {
this.queue.push(resolve);
});
}
/** 释放许可,唤醒队列中下一个 */
release(): void {
const next = this.queue.shift();
if (next) {
next();
} else {
this.current--;
}
}
get stats() {
return { current: this.current, queued: this.queue.length, max: this.max };
}
}
#!/bin/bash
# FastGPT Sandbox Python 压测脚本
# 用法: SANDBOX_TOKEN=xxx ./bench-sandbox-python.sh
# SANDBOX_URL=http://host:3000 SANDBOX_TOKEN=xxx ./bench-sandbox-python.sh
set -eo pipefail
BASE="${SANDBOX_URL:-http://localhost:3000}"
TOKEN="${SANDBOX_TOKEN:-}"
DURATION="${BENCH_DURATION:-10}"
# 构建 npx autocannon 认证参数
AUTH_ARGS=""
if [ -n "$TOKEN" ]; then
AUTH_ARGS="-H Authorization=Bearer%20${TOKEN}"
fi
echo "========================================"
echo " FastGPT Sandbox Python 压测"
echo " 服务: $BASE"
echo " 认证: $([ -n "$TOKEN" ] && echo '已配置' || echo '未配置')"
echo "========================================"
# 健康检查
HEALTH=$(curl -sf "$BASE/health" 2>/dev/null) || {
echo "错误: Sandbox 服务未启动 ($BASE/health)"
exit 1
}
echo "健康状态: $(echo "$HEALTH" | grep -o '"status":"[^"]*"' || echo "$HEALTH")"
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "▶ 测试 1: 普通函数 (简单计算)"
echo " 并发: 50 持续: ${DURATION}s"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
npx autocannon -c 50 -d "$DURATION" -m POST \
-H "Content-Type=application/json" \
$AUTH_ARGS \
-b '{"code":"def main(variables):\n return 1 + 1","variables":{}}' \
"${BASE}/sandbox/python"
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "▶ 测试 2: 长时间IO函数 (sleep 500ms)"
echo " 并发: 50 持续: ${DURATION}s"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
npx autocannon -c 50 -d "$DURATION" -m POST \
-H "Content-Type=application/json" \
$AUTH_ARGS \
-b '{"code":"import time\ndef main(variables):\n time.sleep(0.5)\n return \"done\"","variables":{}}' \
"${BASE}/sandbox/python"
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "▶ 测试 3: 高CPU函数 (大量计算)"
echo " 并发: 10 持续: ${DURATION}s"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
npx autocannon -c 10 -d "$DURATION" -m POST \
-H "Content-Type=application/json" \
$AUTH_ARGS \
-b '{"code":"import math\ndef main(variables):\n s=0\n for i in range(5000000):\n s+=math.sqrt(i)\n return s","variables":{}}' \
"${BASE}/sandbox/python"
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "▶ 测试 4: 高内存函数 (大列表)"
echo " 并发: 10 持续: ${DURATION}s"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
npx autocannon -c 10 -d "$DURATION" -m POST \
-H "Content-Type=application/json" \
$AUTH_ARGS \
-b '{"code":"def main(variables):\n arr = [i*i for i in range(2000000)]\n return len(arr)","variables":{}}' \
"${BASE}/sandbox/python"
echo ""
echo "========================================"
echo " 压测完成!"
echo "========================================"
#!/bin/bash
# FastGPT Sandbox JS 压测脚本
# 用法: SANDBOX_TOKEN=xxx ./bench-sandbox.sh
# SANDBOX_URL=http://host:3000 SANDBOX_TOKEN=xxx ./bench-sandbox.sh
set -eo pipefail
BASE="${SANDBOX_URL:-http://localhost:3000}"
TOKEN="${SANDBOX_TOKEN:-}"
DURATION="${BENCH_DURATION:-10}"
# 构建 npx autocannon 认证参数
AUTH_ARGS=""
if [ -n "$TOKEN" ]; then
AUTH_ARGS="-H Authorization=Bearer%20${TOKEN}"
fi
echo "========================================"
echo " FastGPT Sandbox JS 压测"
echo " 服务: $BASE"
echo " 认证: $([ -n "$TOKEN" ] && echo '已配置' || echo '未配置')"
echo "========================================"
# 健康检查
HEALTH=$(curl -sf "$BASE/health" 2>/dev/null) || {
echo "错误: Sandbox 服务未启动 ($BASE/health)"
exit 1
}
echo "健康状态: $(echo "$HEALTH" | grep -o '"status":"[^"]*"' || echo "$HEALTH")"
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "▶ 测试 1: 普通函数 (简单计算)"
echo " 并发: 50 持续: ${DURATION}s"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
npx autocannon -c 50 -d "$DURATION" -m POST \
-H "Content-Type=application/json" \
$AUTH_ARGS \
-b '{"code":"function main() { return 1 + 1; }","variables":{}}' \
"${BASE}/sandbox/js"
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "▶ 测试 2: 长时间IO函数 (delay 500ms)"
echo " 并发: 50 持续: ${DURATION}s"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
npx autocannon -c 50 -d "$DURATION" -m POST \
-H "Content-Type=application/json" \
$AUTH_ARGS \
-b '{"code":"async function main() { await delay(500); return \"done\"; }","variables":{}}' \
"${BASE}/sandbox/js"
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "▶ 测试 3: 高CPU函数 (大量计算)"
echo " 并发: 10 持续: ${DURATION}s"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
npx autocannon -c 10 -d "$DURATION" -m POST \
-H "Content-Type=application/json" \
$AUTH_ARGS \
-b '{"code":"function main() { let s=0; for(let i=0;i<50000000;i++) s+=Math.sqrt(i); return s; }","variables":{}}' \
"${BASE}/sandbox/js"
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "▶ 测试 4: 高内存函数 (分配大数组)"
echo " 并发: 10 持续: ${DURATION}s"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
npx autocannon -c 10 -d "$DURATION" -m POST \
-H "Content-Type=application/json" \
$AUTH_ARGS \
-b '{"code":"function main() { const arr = new Array(5000000).fill(0).map((_,i)=>i*i); return arr.length; }","variables":{}}' \
"${BASE}/sandbox/js"
echo ""
echo "========================================"
echo " 压测完成!"
echo "========================================"
/**
* 旧版 JS 代码兼容性测试
*
* 验证新沙盒能正确执行旧版 FastGPT 生成的 JS 代码写法,包括:
* - main(variables, {}) 两参数调用
* - 全局 delay() / countToken() / strToBase64() / createHmac()
* - console.log 捕获
* - require 白名单模块(lodash, dayjs)
* - 各种 main 函数签名
*/
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import { ProcessPool } from '../../src/pool/process-pool';
let pool: ProcessPool;
beforeAll(async () => {
pool = new ProcessPool(1);
await pool.init();
});
afterAll(async () => {
await pool.shutdown();
});
describe('旧版 JS 兼容性', () => {
// ===== main 函数签名兼容 =====
it('main(variables, {}) 两参数写法', async () => {
const result = await pool.execute({
code: `async function main(variables, extra) {
return { name: variables.name, hasExtra: extra !== undefined };
}`,
variables: { name: 'FastGPT' }
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn.name).toBe('FastGPT');
});
it('main({变量解构}) 写法', async () => {
const result = await pool.execute({
code: `async function main({ name, age }) {
return { name, age };
}`,
variables: { name: 'test', age: 18 }
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn).toEqual({ name: 'test', age: 18 });
});
it('main() 无参数写法 — 通过闭包访问 variables', async () => {
const result = await pool.execute({
code: `async function main() {
return { name: variables.name };
}`,
variables: { name: 'closure-test' }
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn.name).toBe('closure-test');
});
it('非 async main 函数', async () => {
const result = await pool.execute({
code: `function main(v) {
return { sum: v.a + v.b };
}`,
variables: { a: 10, b: 20 }
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn.sum).toBe(30);
});
// ===== 全局内置函数兼容 =====
it('全局 delay() 函数', async () => {
const result = await pool.execute({
code: `async function main() {
const start = Date.now();
await delay(100);
return { elapsed: Date.now() - start };
}`,
variables: {}
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn.elapsed).toBeGreaterThanOrEqual(80);
});
it('全局 countToken() 函数', async () => {
const result = await pool.execute({
code: `async function main() {
return { count: countToken("hello world test string") };
}`,
variables: {}
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn.count).toBeGreaterThan(0);
});
it('全局 strToBase64() 函数', async () => {
const result = await pool.execute({
code: `async function main() {
return { b64: strToBase64("hello") };
}`,
variables: {}
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn.b64).toBe(Buffer.from('hello').toString('base64'));
});
it('全局 strToBase64() 带 prefix', async () => {
const result = await pool.execute({
code: `async function main() {
return { b64: strToBase64("hello", "data:text/plain;base64,") };
}`,
variables: {}
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn.b64).toBe(
'data:text/plain;base64,' + Buffer.from('hello').toString('base64')
);
});
it('全局 createHmac() 函数', async () => {
const result = await pool.execute({
code: `async function main() {
const r = createHmac("sha256", "my-secret");
return { ts: r.timestamp, sign: r.sign };
}`,
variables: {}
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn.ts).toBeTruthy();
expect(result.data?.codeReturn.sign).toBeTruthy();
});
// ===== console.log 兼容 =====
it('console.log 被捕获到 log 字段', async () => {
const result = await pool.execute({
code: `async function main() {
console.log("step 1");
console.log("step 2", { key: "value" });
return { done: true };
}`,
variables: {}
});
expect(result.success).toBe(true);
expect(result.data?.log).toContain('step 1');
expect(result.data?.log).toContain('step 2');
expect(result.data?.log).toContain('key');
});
// ===== require 白名单模块 =====
it('require lodash 正常使用', async () => {
const result = await pool.execute({
code: `async function main(v) {
const _ = require('lodash');
return { result: _.sum(v.arr) };
}`,
variables: { arr: [1, 2, 3, 4, 5] }
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn.result).toBe(15);
});
it('require dayjs 正常使用', async () => {
const result = await pool.execute({
code: `async function main() {
const dayjs = require('dayjs');
return { valid: dayjs('2024-01-01').isValid() };
}`,
variables: {}
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn.valid).toBe(true);
});
it('require qs 正常使用', async () => {
const result = await pool.execute({
code: `async function main() {
const qs = require('qs');
return { str: qs.stringify({ a: 1, b: 2 }) };
}`,
variables: {}
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn.str).toBe('a=1&b=2');
});
// ===== 典型旧版代码模式 =====
it('旧版典型写法:HTTP 请求签名', async () => {
const result = await pool.execute({
code: `async function main(variables, extra) {
const hmacResult = createHmac("sha256", variables.secret);
const token = strToBase64(variables.secret);
return {
timestamp: hmacResult.timestamp,
sign: hmacResult.sign,
token: token
};
}`,
variables: { secret: 'my-webhook-secret' }
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn.timestamp).toBeTruthy();
expect(result.data?.codeReturn.sign).toBeTruthy();
expect(result.data?.codeReturn.token).toBeTruthy();
});
it('旧版典型写法:数据处理 + lodash', async () => {
const result = await pool.execute({
code: `async function main(variables) {
const _ = require('lodash');
const items = variables.data;
const grouped = _.groupBy(items, 'type');
const counts = _.mapValues(grouped, arr => arr.length);
return counts;
}`,
variables: {
data: [
{ type: 'a', val: 1 },
{ type: 'b', val: 2 },
{ type: 'a', val: 3 }
]
}
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn).toEqual({ a: 2, b: 1 });
});
it('旧版典型写法:字符串处理 + delay', async () => {
const result = await pool.execute({
code: `async function main(variables) {
await delay(50);
const count = countToken(variables.text);
const encoded = strToBase64(variables.text);
return { count, encoded, length: variables.text.length };
}`,
variables: { text: 'Hello FastGPT sandbox!' }
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn.count).toBeGreaterThan(0);
expect(result.data?.codeReturn.encoded).toBeTruthy();
expect(result.data?.codeReturn.length).toBe(22);
});
it('返回非对象值(旧版可能返回数组)', async () => {
const result = await pool.execute({
code: `async function main(v) {
return [v.a, v.b, v.a + v.b];
}`,
variables: { a: 1, b: 2 }
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn).toEqual([1, 2, 3]);
});
it('返回嵌套复杂对象', async () => {
const result = await pool.execute({
code: `async function main(v) {
return {
user: { name: v.name, tags: ['admin', 'user'] },
meta: { count: 42, active: true }
};
}`,
variables: { name: 'test' }
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn.user.name).toBe('test');
expect(result.data?.codeReturn.user.tags).toEqual(['admin', 'user']);
expect(result.data?.codeReturn.meta.count).toBe(42);
});
});
/**
* 旧版 Python 代码兼容性测试
*
* 验证新沙盒能正确执行旧版 FastGPT 生成的 Python 代码写法,包括:
* - main() 无参数(通过全局变量访问)
* - main(variables) 单参数字典
* - main(a, b, c) 多参数展开
* - print 被收集到 log(旧版直接移除)
* - 危险模块拦截
* - 各种返回值类型
*/
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import { PythonProcessPool } from '../../src/pool/python-process-pool';
let pool: PythonProcessPool;
beforeAll(async () => {
pool = new PythonProcessPool(1);
await pool.init();
});
afterAll(async () => {
await pool.shutdown();
});
describe('旧版 Python 兼容性', () => {
// ===== main 函数签名兼容 =====
it('main(variables) 单参数字典写法', async () => {
const result = await pool.execute({
code: `def main(variables):
return {"name": variables["name"], "age": variables["age"]}`,
variables: { name: 'FastGPT', age: 3 }
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn).toEqual({ name: 'FastGPT', age: 3 });
});
it('main(a, b) 多参数展开写法', async () => {
const result = await pool.execute({
code: `def main(a, b):
return {"sum": a + b}`,
variables: { a: 10, b: 20 }
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn.sum).toBe(30);
});
it('main(a, b, c) 三参数展开', async () => {
const result = await pool.execute({
code: `def main(a, b, c):
return {"result": a * b + c}`,
variables: { a: 3, b: 4, c: 5 }
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn.result).toBe(17);
});
it('main() 无参数写法', async () => {
const result = await pool.execute({
code: `def main():
return {"ok": True}`,
variables: {}
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn.ok).toBe(true);
});
it('旧版写法:main 外部直接访问全局变量', async () => {
const result = await pool.execute({
code: `prefix = name + "_suffix"
def main(name, age):
return {"result": prefix, "name": name, "age": age}`,
variables: { name: 'hello', age: 18 }
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn.result).toBe('hello_suffix');
expect(result.data?.codeReturn.name).toBe('hello');
expect(result.data?.codeReturn.age).toBe(18);
});
it('旧版写法:无参 main 通过全局变量访问', async () => {
const result = await pool.execute({
code: `def main():
return {"name": name, "age": age}`,
variables: { name: 'test', age: 25 }
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn).toEqual({ name: 'test', age: 25 });
});
it('main 带默认参数', async () => {
const result = await pool.execute({
code: `def main(name, greeting="Hello"):
return {"msg": f"{greeting}, {name}!"}`,
variables: { name: 'World' }
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn.msg).toBe('Hello, World!');
});
// ===== 返回值类型兼容 =====
it('返回列表(旧版常见)', async () => {
const result = await pool.execute({
code: `def main(variables):
return [variables["a"], variables["b"], variables["a"] + variables["b"]]`,
variables: { a: 1, b: 2 }
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn).toEqual([1, 2, 3]);
});
it('返回嵌套字典', async () => {
const result = await pool.execute({
code: `def main(variables):
return {
"user": {"name": variables["name"], "tags": ["admin"]},
"count": 42
}`,
variables: { name: 'test' }
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn.user.name).toBe('test');
expect(result.data?.codeReturn.count).toBe(42);
});
it('返回布尔值和 None 转换', async () => {
const result = await pool.execute({
code: `def main(variables):
return {"active": True, "deleted": False, "extra": None}`,
variables: {}
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn.active).toBe(true);
expect(result.data?.codeReturn.deleted).toBe(false);
expect(result.data?.codeReturn.extra).toBeNull();
});
// ===== print 行为 =====
it('print 输出收集到 log(不影响返回值)', async () => {
const result = await pool.execute({
code: `def main(variables):
print("debug step 1")
print("processing", variables["name"])
return {"done": True}`,
variables: { name: 'test' }
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn.done).toBe(true);
expect(result.data?.log).toContain('debug step 1');
expect(result.data?.log).toContain('processing');
});
// ===== 危险模块拦截 =====
it('import os 被拦截', async () => {
const result = await pool.execute({
code: `import os
def main():
return {"cwd": os.getcwd()}`,
variables: {}
});
expect(result.success).toBe(false);
expect(result.message).toContain('os');
});
it('import subprocess 被拦截', async () => {
const result = await pool.execute({
code: `import subprocess
def main():
return {"out": subprocess.check_output(["ls"])}`,
variables: {}
});
expect(result.success).toBe(false);
expect(result.message).toContain('subprocess');
});
it('from sys import path 被拦截', async () => {
const result = await pool.execute({
code: `from sys import path
def main():
return {"path": path}`,
variables: {}
});
expect(result.success).toBe(false);
expect(result.message).toContain('sys');
});
// ===== 安全模块允许 =====
it('import json 允许', async () => {
const result = await pool.execute({
code: `import json
def main(variables):
data = json.dumps(variables)
parsed = json.loads(data)
return {"parsed": parsed}`,
variables: { key: 'value' }
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn.parsed).toEqual({ key: 'value' });
});
it('import math 允许', async () => {
const result = await pool.execute({
code: `import math
def main(variables):
return {"sqrt": math.sqrt(variables["n"]), "pi": round(math.pi, 4)}`,
variables: { n: 16 }
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn.sqrt).toBe(4);
expect(result.data?.codeReturn.pi).toBe(3.1416);
});
it('import re 允许', async () => {
const result = await pool.execute({
code: `import re
def main(variables):
matches = re.findall(r'\\d+', variables["text"])
return {"numbers": matches}`,
variables: { text: 'abc123def456' }
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn.numbers).toEqual(['123', '456']);
});
// ===== 典型旧版代码模式 =====
it('旧版典型写法:数据过滤', async () => {
const result = await pool.execute({
code: `def main(variables):
items = variables["items"]
filtered = [x for x in items if x["score"] >= 60]
return {"passed": len(filtered), "total": len(items)}`,
variables: {
items: [
{ name: 'A', score: 80 },
{ name: 'B', score: 45 },
{ name: 'C', score: 90 },
{ name: 'D', score: 55 }
]
}
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn).toEqual({ passed: 2, total: 4 });
});
it('旧版典型写法:字符串处理', async () => {
const result = await pool.execute({
code: `def main(variables):
text = variables["text"]
words = text.split()
return {
"word_count": len(words),
"upper": text.upper(),
"reversed": text[::-1]
}`,
variables: { text: 'hello world' }
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn.word_count).toBe(2);
expect(result.data?.codeReturn.upper).toBe('HELLO WORLD');
expect(result.data?.codeReturn.reversed).toBe('dlrow olleh');
});
it('旧版典型写法:日期处理', async () => {
const result = await pool.execute({
code: `from datetime import datetime, timedelta
def main(variables):
dt = datetime.strptime(variables["date"], "%Y-%m-%d")
next_day = dt + timedelta(days=1)
return {"next": next_day.strftime("%Y-%m-%d"), "weekday": dt.strftime("%A")}`,
variables: { date: '2024-01-01' }
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn.next).toBe('2024-01-02');
expect(result.data?.codeReturn.weekday).toBe('Monday');
});
it('旧版典型写法:辅助函数 + main', async () => {
const result = await pool.execute({
code: `def calculate_tax(amount, rate):
return round(amount * rate, 2)
def main(variables):
amount = variables["amount"]
tax = calculate_tax(amount, 0.13)
return {"amount": amount, "tax": tax, "total": amount + tax}`,
variables: { amount: 100 }
});
expect(result.success).toBe(true);
expect(result.data?.codeReturn).toEqual({ amount: 100, tax: 13, total: 113 });
});
});
/**
* API 测试 - 使用 app.request() 直接测试 Hono 路由
* 无需启动服务或配置 SANDBOX_URL
*/
import { describe, it, expect, beforeAll } from 'vitest';
import { app, poolReady } from '../../src/index';
import { config } from '../../src/config';
/** 构造请求 headers,自动带上 auth(如果配置了 token) */
function headers(extra: Record<string, string> = {}): Record<string, string> {
const h: Record<string, string> = { ...extra };
if (config.token) {
h['Authorization'] = `Bearer ${config.token}`;
}
return h;
}
describe('API Routes', () => {
beforeAll(async () => {
await poolReady;
}, 30000);
// ===== Health =====
it('GET /health 返回 200', async () => {
const res = await app.request('/health');
expect(res.status).toBe(200);
const data = await res.json();
expect(data.status).toBe('ok');
});
// ===== JS =====
it('POST /sandbox/js 正常执行', async () => {
const res = await app.request('/sandbox/js', {
method: 'POST',
headers: headers({ 'Content-Type': 'application/json' }),
body: JSON.stringify({
code: 'async function main(v) { return { hello: v.name } }',
variables: { name: 'world' }
})
});
const data = await res.json();
expect(data.success).toBe(true);
expect(data.data.codeReturn.hello).toBe('world');
});
it('POST /sandbox/js 忽略额外参数', async () => {
const res = await app.request('/sandbox/js', {
method: 'POST',
headers: headers({ 'Content-Type': 'application/json' }),
body: JSON.stringify({
code: 'async function main(v) { return { ok: true } }',
variables: {}
})
});
const data = await res.json();
expect(data.success).toBe(true);
});
it('POST /sandbox/js 安全拦截', async () => {
const res = await app.request('/sandbox/js', {
method: 'POST',
headers: headers({ 'Content-Type': 'application/json' }),
body: JSON.stringify({
code: 'async function main() { require("child_process"); return {} }',
variables: {}
})
});
const data = await res.json();
expect(data.success).toBe(false);
expect(data.message).toContain('not allowed');
});
// ===== Python =====
it('POST /sandbox/python 正常执行', async () => {
const res = await app.request('/sandbox/python', {
method: 'POST',
headers: headers({ 'Content-Type': 'application/json' }),
body: JSON.stringify({
code: 'def main(variables):\n return {"hello": variables["name"]}',
variables: { name: 'world' }
})
});
const data = await res.json();
expect(data.success).toBe(true);
expect(data.data.codeReturn.hello).toBe('world');
});
it('POST /sandbox/python 安全拦截', async () => {
const res = await app.request('/sandbox/python', {
method: 'POST',
headers: headers({ 'Content-Type': 'application/json' }),
body: JSON.stringify({
code: 'import os\ndef main(v):\n return {}',
variables: {}
})
});
const data = await res.json();
expect(data.success).toBe(false);
expect(data.message).toContain('not in the allowlist');
});
// ===== Modules =====
it('GET /sandbox/modules 返回可用模块列表', async () => {
const res = await app.request('/sandbox/modules', {
headers: headers()
});
expect(res.status).toBe(200);
const data = await res.json();
expect(data.success).toBe(true);
expect(data.data.js).toEqual(config.jsAllowedModules);
expect(data.data.builtinGlobals).toContain('SystemHelper.httpRequest');
expect(data.data.python).toEqual(config.pythonAllowedModules);
});
});
// ===== 错误处理安全 =====
describe('API 错误处理安全', () => {
beforeAll(async () => {
await poolReady;
}, 30000);
it('JS 执行异常不泄露堆栈', async () => {
const res = await app.request('/sandbox/js', {
method: 'POST',
headers: headers({ 'Content-Type': 'application/json' }),
body: JSON.stringify({
code: 'async function main() { null.x; }',
variables: {}
})
});
const data = await res.json();
expect(data.success).toBe(false);
// 错误信息不应包含宿主进程的真实文件路径(如 node_modules、/src/pool/)
const msg = data.message || '';
expect(msg).not.toContain('node_modules');
expect(msg).not.toContain('/src/pool/');
expect(msg).not.toContain('process-pool');
});
it('无效 JSON body 返回 400', async () => {
const res = await app.request('/sandbox/js', {
method: 'POST',
headers: headers({ 'Content-Type': 'application/json' }),
body: 'this is not json'
});
// Hono 解析 JSON 失败会抛异常,被 catch 捕获返回报错
// 或者 zod 校验失败返回 400
expect([400, 200]).toContain(res.status);
const data = await res.json();
if (res.status === 400) {
expect(data.success).toBe(false);
expect(data.message).toMatch(/invalid|validation/i);
} else {
// catch 分支
expect(data.success).toBe(false);
console.log(data, 123213213);
expect(data.message).toContain('is not valid JSON');
}
});
});
// ===== Zod 校验失败(有效 JSON 但 schema 不匹配) =====
describe('API Zod 校验失败', () => {
beforeAll(async () => {
await poolReady;
}, 30000);
it('JS: code 为数字返回 400', async () => {
const res = await app.request('/sandbox/js', {
method: 'POST',
headers: headers({ 'Content-Type': 'application/json' }),
body: JSON.stringify({ code: 123, variables: {} })
});
expect(res.status).toBe(400);
const data = await res.json();
expect(data.success).toBe(false);
expect(data.message).toMatch(/Invalid request/i);
});
it('JS: 缺少 code 字段返回 400', async () => {
const res = await app.request('/sandbox/js', {
method: 'POST',
headers: headers({ 'Content-Type': 'application/json' }),
body: JSON.stringify({ variables: {} })
});
expect(res.status).toBe(400);
const data = await res.json();
expect(data.success).toBe(false);
expect(data.message).toMatch(/Invalid request/i);
});
it('JS: code 为空字符串返回 400', async () => {
const res = await app.request('/sandbox/js', {
method: 'POST',
headers: headers({ 'Content-Type': 'application/json' }),
body: JSON.stringify({ code: '', variables: {} })
});
expect(res.status).toBe(400);
const data = await res.json();
expect(data.success).toBe(false);
});
it('Python: code 为数字返回 400', async () => {
const res = await app.request('/sandbox/python', {
method: 'POST',
headers: headers({ 'Content-Type': 'application/json' }),
body: JSON.stringify({ code: 123, variables: {} })
});
expect(res.status).toBe(400);
const data = await res.json();
expect(data.success).toBe(false);
expect(data.message).toMatch(/Invalid request/i);
});
it('Python: 缺少 code 字段返回 400', async () => {
const res = await app.request('/sandbox/python', {
method: 'POST',
headers: headers({ 'Content-Type': 'application/json' }),
body: JSON.stringify({ variables: {} })
});
expect(res.status).toBe(400);
const data = await res.json();
expect(data.success).toBe(false);
expect(data.message).toMatch(/Invalid request/i);
});
it('Python: 无效 JSON body 返回错误', async () => {
const res = await app.request('/sandbox/python', {
method: 'POST',
headers: headers({ 'Content-Type': 'application/json' }),
body: 'this is not json'
});
const data = await res.json();
expect(data.success).toBe(false);
});
});
/**
* Auth 测试
* 默认 SANDBOX_TOKEN 为空,auth 中间件不启用。
* 设置 SANDBOX_TOKEN=xxx 运行可测试鉴权逻辑。
*/
describe.skipIf(!config.token)('API Auth (requires SANDBOX_TOKEN)', () => {
it('无 Token 返回 401', async () => {
const res = await app.request('/sandbox/js', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
code: 'async function main() { return {} }',
variables: {}
})
});
expect(res.status).toBe(401);
});
it('错误 Token 返回 401', async () => {
const res = await app.request('/sandbox/js', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer wrong-token'
},
body: JSON.stringify({
code: 'async function main() { return {} }',
variables: {}
})
});
expect(res.status).toBe(401);
});
it('正确 Token 返回 200', async () => {
const res = await app.request('/sandbox/js', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${config.token}`
},
body: JSON.stringify({
code: 'async function main() { return { ok: true } }',
variables: {}
})
});
const data = await res.json();
expect(data.success).toBe(true);
});
});
{
"compilerOptions": {
"target": "es2022",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"baseUrl": "."
},
"include": ["**/*.test.ts"],
"exclude": ["**/node_modules"]
}
/**
* Semaphore 信号量单元测试
*
* 测试并发控制核心逻辑:
* - 基本 acquire/release 流程
* - 超出 max 后排队等待
* - release 唤醒队列中下一个
* - stats 返回正确的 current/queued/max
* - 并发数为 1 时串行执行
* - 大量并发请求排队后依次完成
*/
import { describe, it, expect } from 'vitest';
import { Semaphore } from '../../src/utils/semaphore';
describe('Semaphore', () => {
// ===== 基本流程 =====
it('acquire 在未满时立即返回', async () => {
const sem = new Semaphore(3);
// 三次 acquire 都应该立即 resolve
await sem.acquire();
await sem.acquire();
await sem.acquire();
expect(sem.stats).toEqual({ current: 3, queued: 0, max: 3 });
});
it('release 减少 current 计数', async () => {
const sem = new Semaphore(2);
await sem.acquire();
await sem.acquire();
expect(sem.stats.current).toBe(2);
sem.release();
expect(sem.stats.current).toBe(1);
sem.release();
expect(sem.stats.current).toBe(0);
});
it('stats 返回正确的 current/queued/max', async () => {
const sem = new Semaphore(2);
expect(sem.stats).toEqual({ current: 0, queued: 0, max: 2 });
await sem.acquire();
expect(sem.stats).toEqual({ current: 1, queued: 0, max: 2 });
await sem.acquire();
expect(sem.stats).toEqual({ current: 2, queued: 0, max: 2 });
// 第三个会排队(不 await,因为它不会 resolve)
const p3 = sem.acquire();
expect(sem.stats).toEqual({ current: 2, queued: 1, max: 2 });
// 第四个也排队
const p4 = sem.acquire();
expect(sem.stats).toEqual({ current: 2, queued: 2, max: 2 });
// release 唤醒队列中第一个,queued 减 1,current 不变(因为立即被新的占用)
sem.release();
await p3;
expect(sem.stats).toEqual({ current: 2, queued: 1, max: 2 });
sem.release();
await p4;
expect(sem.stats).toEqual({ current: 2, queued: 0, max: 2 });
});
// ===== 排队与唤醒 =====
it('超出 max 后排队等待,release 唤醒下一个', async () => {
const sem = new Semaphore(1);
const order: number[] = [];
await sem.acquire();
order.push(1);
// 第二个 acquire 会排队
const p2 = sem.acquire().then(() => {
order.push(2);
});
expect(sem.stats.queued).toBe(1);
// release 唤醒排队的
sem.release();
await p2;
expect(order).toEqual([1, 2]);
expect(sem.stats.queued).toBe(0);
sem.release();
expect(sem.stats.current).toBe(0);
});
it('release 按 FIFO 顺序唤醒', async () => {
const sem = new Semaphore(1);
const order: number[] = [];
await sem.acquire();
const p1 = sem.acquire().then(() => {
order.push(1);
});
const p2 = sem.acquire().then(() => {
order.push(2);
});
const p3 = sem.acquire().then(() => {
order.push(3);
});
expect(sem.stats.queued).toBe(3);
// 依次 release,应按 FIFO 顺序唤醒
sem.release();
await p1;
sem.release();
await p2;
sem.release();
await p3;
expect(order).toEqual([1, 2, 3]);
});
// ===== 并发数为 1 时串行执行 =====
it('max=1 时保证串行执行', async () => {
const sem = new Semaphore(1);
const log: string[] = [];
const task = async (name: string, delayMs: number) => {
await sem.acquire();
log.push(`${name}-start`);
await new Promise((r) => setTimeout(r, delayMs));
log.push(`${name}-end`);
sem.release();
};
// 同时启动三个任务
await Promise.all([task('A', 50), task('B', 50), task('C', 50)]);
// 串行执行:每个任务的 start 必须在前一个 end 之后
// A-start, A-end, B-start, B-end, C-start, C-end
for (let i = 0; i < log.length - 1; i += 2) {
const startIdx = i;
const endIdx = i + 1;
expect(log[endIdx]).toContain('-end');
expect(log[startIdx]).toContain('-start');
// end 在 start 之后
expect(endIdx).toBeGreaterThan(startIdx);
}
// 更严格:不能有两个 start 连续出现(说明并行了)
for (let i = 0; i < log.length - 1; i++) {
if (log[i].endsWith('-start')) {
expect(log[i + 1]).toContain('-end');
}
}
});
// ===== 大量并发 =====
it('大量并发请求排队后依次完成', async () => {
const sem = new Semaphore(3);
const total = 20;
let completed = 0;
let maxConcurrent = 0;
let currentRunning = 0;
const tasks = Array.from({ length: total }, (_, i) =>
(async () => {
await sem.acquire();
currentRunning++;
if (currentRunning > maxConcurrent) {
maxConcurrent = currentRunning;
}
// 模拟异步工作
await new Promise((r) => setTimeout(r, 10));
currentRunning--;
completed++;
sem.release();
})()
);
await Promise.all(tasks);
// 全部完成
expect(completed).toBe(total);
// 最大并发不超过 max
expect(maxConcurrent).toBeLessThanOrEqual(3);
// 最终状态归零
expect(sem.stats.current).toBe(0);
expect(sem.stats.queued).toBe(0);
});
it('max=1 大量并发严格串行', async () => {
const sem = new Semaphore(1);
const total = 10;
let maxConcurrent = 0;
let currentRunning = 0;
const tasks = Array.from({ length: total }, () =>
(async () => {
await sem.acquire();
currentRunning++;
if (currentRunning > maxConcurrent) maxConcurrent = currentRunning;
await new Promise((r) => setTimeout(r, 5));
currentRunning--;
sem.release();
})()
);
await Promise.all(tasks);
expect(maxConcurrent).toBe(1);
expect(sem.stats.current).toBe(0);
});
// ===== 边界情况 =====
it('release 无排队时 current 不会变为负数', () => {
const sem = new Semaphore(3);
// 没有 acquire 就 release
sem.release();
// current 变为 -1,这是实现的已知行为(调用者应保证配对使用)
expect(sem.stats.current).toBe(-1);
});
it('max 为很大的数时不排队', async () => {
const sem = new Semaphore(1000);
const promises = Array.from({ length: 100 }, () => sem.acquire());
await Promise.all(promises);
expect(sem.stats.current).toBe(100);
expect(sem.stats.queued).toBe(0);
});
it('acquire 返回的 Promise 是 void', async () => {
const sem = new Semaphore(1);
const result = await sem.acquire();
expect(result).toBeUndefined();
sem.release();
});
});
// ============================================================
// 竞态条件补充(原 semaphore-race.test.ts)
// ============================================================
describe('Semaphore 竞态条件补充', () => {
it('release 过多后 acquire 仍能正常工作', async () => {
const sem = new Semaphore(2);
sem.release();
expect(sem.stats.current).toBe(-1);
await sem.acquire();
expect(sem.stats.current).toBe(0);
await sem.acquire();
expect(sem.stats.current).toBe(1);
await sem.acquire();
expect(sem.stats.current).toBe(2);
const p = sem.acquire();
expect(sem.stats.queued).toBe(1);
sem.release();
await p;
});
it('快速交替 acquire/release 不丢失状态', async () => {
const sem = new Semaphore(1);
for (let i = 0; i < 100; i++) {
await sem.acquire();
sem.release();
}
expect(sem.stats.current).toBe(0);
expect(sem.stats.queued).toBe(0);
});
it('异步任务异常后 release 仍被调用(模拟 try/finally)', async () => {
const sem = new Semaphore(2);
const errors: string[] = [];
const task = async (shouldFail: boolean) => {
await sem.acquire();
try {
if (shouldFail) throw new Error('task failed');
return 'ok';
} catch (e: any) {
errors.push(e.message);
return 'error';
} finally {
sem.release();
}
};
const results = await Promise.all([
task(false),
task(true),
task(false),
task(true),
task(false)
]);
expect(results.filter((r) => r === 'ok')).toHaveLength(3);
expect(results.filter((r) => r === 'error')).toHaveLength(2);
expect(errors).toHaveLength(2);
expect(sem.stats.current).toBe(0);
expect(sem.stats.queued).toBe(0);
});
it('max=0 时所有 acquire 都排队', async () => {
const sem = new Semaphore(0);
const p1 = sem.acquire();
const p2 = sem.acquire();
expect(sem.stats.queued).toBe(2);
expect(sem.stats.current).toBe(0);
sem.release();
await p1;
sem.release();
await p2;
});
it('并发 acquire 后批量 release', async () => {
const sem = new Semaphore(2);
await sem.acquire();
await sem.acquire();
const waiters = Array.from({ length: 5 }, () => sem.acquire());
expect(sem.stats.queued).toBe(5);
for (let i = 0; i < 7; i++) {
sem.release();
}
await Promise.all(waiters);
expect(sem.stats.queued).toBe(0);
expect(sem.stats.current).toBe(0);
});
});
#!/bin/bash
temp_dir=$(mktemp -d)
trap 'rm -rf "$temp_dir"' EXIT
syscall_table_file="$temp_dir/syscall_table.txt"
code_file="$temp_dir/test_code.py"
strace_log="$temp_dir/strace.log"
syscalls_file="$temp_dir/syscalls.txt"
code='
import pandas as pd
def main():
data = {"Name": ["Alice", "Bob"], "Age": [25, 30]}
df = pd.DataFrame(data)
return {
"head": df.head().to_dict()
}
'
if ! ausyscall --dump > "$syscall_table_file" 2>/dev/null; then
grep -E '^#define __NR_' /usr/include/asm/unistd_64.h | \
sed 's/#define __NR_//;s/[ \t]\+/ /g' | \
awk '{print $1, $2}' > "$syscall_table_file"
fi
echo "$code" > "$code_file"
strace -ff -e trace=all -o "$strace_log" python3 "$code_file" >/dev/null 2>&1
cat "$strace_log"* 2>/dev/null | grep -oE '^[[:alnum:]_]+' | sort -u > "$syscalls_file"
allowed_syscalls=()
while read raw_name; do
go_name=$(echo "$raw_name" | tr 'a-z' 'A-Z' | sed 's/-/_/g')
allowed_syscalls+=("\"syscall.SYS_${go_name}\"")
done < "$syscalls_file"
echo "allowed_syscalls = ["
printf ' %s,\n' "${allowed_syscalls[@]}" | paste -sd ' \n'
echo "]"
\ No newline at end of file
{
"extends": "./tsconfig.json",
"exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
}
{ {
"compilerOptions": { "compilerOptions": {
"module": "commonjs", "target": "ES2022",
"declaration": true, "module": "ESNext",
"removeComments": true, "moduleResolution": "bundler",
"emitDecoratorMetadata": true, "esModuleInterop": true,
"experimentalDecorators": true, "strict": true,
"allowSyntheticDefaultImports": true,
"target": "ES2021",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"skipLibCheck": true, "skipLibCheck": true,
"strictNullChecks": false, "outDir": "./dist",
"noImplicitAny": false, "rootDir": "./src",
"strictBindCallApply": false, "declaration": true,
"forceConsistentCasingInFileNames": false, "resolveJsonModule": true,
"noFallthroughCasesInSwitch": false "types": ["bun-types"]
} },
"include": ["src/**/*"],
"exclude": ["node_modules", "dist", "test"]
} }
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
coverage: {
enabled: true,
reporter: ['text', 'text-summary', 'html', 'json-summary', 'json'],
reportOnFailure: true,
all: false, // 只包含被测试实际覆盖的文件,不包含空目录
include: ['src/**/*.ts'],
cleanOnRerun: false
},
root: '.',
include: ['test/**/*.test.ts'],
testTimeout: 30000,
hookTimeout: 30000,
fileParallelism: false,
maxConcurrency: 1,
isolate: false,
env: {
SANDBOX_MAX_TIMEOUT: '5000',
SANDBOX_TOKEN: 'test'
}
}
});
...@@ -15,7 +15,7 @@ export default defineConfig({ ...@@ -15,7 +15,7 @@ export default defineConfig({
reporter: ['text', 'text-summary', 'html', 'json-summary', 'json'], reporter: ['text', 'text-summary', 'html', 'json-summary', 'json'],
reportOnFailure: true, reportOnFailure: true,
all: false, // 只包含被测试实际覆盖的文件,不包含空目录 all: false, // 只包含被测试实际覆盖的文件,不包含空目录
include: ['projects/**/*.ts', 'packages/**/*.ts'], include: ['projects/app/**/*.ts', 'packages/**/*.ts'],
exclude: [ exclude: [
'**/node_modules/**', '**/node_modules/**',
'**/*.spec.ts', '**/*.spec.ts',
...@@ -48,7 +48,6 @@ export default defineConfig({ ...@@ -48,7 +48,6 @@ export default defineConfig({
include: [ include: [
'test/**/*.test.ts', 'test/**/*.test.ts',
'projects/app/test/**/*.test.ts', 'projects/app/test/**/*.test.ts',
'projects/sandbox/test/**/*.test.ts',
'projects/marketplace/test/**/*.test.ts' 'projects/marketplace/test/**/*.test.ts'
] ]
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment