@glodon-aiot/workspace-sandbox 0.2.0-alpha.2 → 0.2.0-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,10 +1,10 @@
1
1
  # @glodon-aiot/workspace-sandbox
2
2
 
3
- 供端侧宿主调用:在工程目录内读写文件,并在 Microsoft Execution Containers 里执行命令和脚本。
3
+ 供端侧宿主调用:在工程目录内读写文件,并在 Microsoft Execution Containers(MXC)里执行命令和脚本。
4
4
 
5
- 需要 Node.js 24。页面用 WebSocket 直连本进程,宿主只负责启动、关闭和转发就绪信息。页面协议里没有本地绝对路径。只使用 `ws://127.0.0.1`,不提供 `wss://`。
5
+ 需要 Node.js 24。生产接入使用协议 2:页面通过 WebSocket 直连本进程,宿主只管理进程并转发状态。文件字节和命令输出不经过宿主,页面也拿不到工程绝对路径。
6
6
 
7
- ## 直连
7
+ ## 生产接入
8
8
 
9
9
  宿主启动时绑定系统分配的回环端口,并把页面来源放进允许列表:
10
10
 
@@ -12,88 +12,91 @@
12
12
  workspace-sandbox --listen --root /path/to/project --origin https://app.example
13
13
  ```
14
14
 
15
- stdout 第一行是 `ready`(`url`、`token`、`pid`)或 `fatal`(`reason`、不含路径的 `message`)。之后 stdout 只写 `status` 和 `bye`。令牌不写进 URL、日志和 stderr。
15
+ `--origin` 可以重复。沙箱只绑定 `127.0.0.1:0`,只提供 `ws://`,不提供 `wss://`。
16
16
 
17
- 页面连上后,第一帧必须是 `{ "protocol": 2, "op": "auth", "token": "..." }`。文件读写用文本帧加二进制分片(最大 1 MB)。命令的 stdout / stderr 按 32 KB 或 50 ms 合并成文本帧,结束时发 `exit`。超过 16000 字符的输出另写 `tmp/sandbox-exec/<id>.log`,最多 8 MB。
17
+ stdout 第一行是以下两种管理事件之一:
18
18
 
19
- stdin 只接受 `{"op":"status"}` 和 `{"op":"shutdown"}`。WebSocket 上不能关机、改根目录或改来源。
19
+ ```json
20
+ { "protocol": 2, "op": "ready", "url": "ws://127.0.0.1:53124", "token": "<64 位十六进制>", "pid": 1234 }
21
+ ```
20
22
 
21
- ## 运行环境
23
+ ```json
24
+ { "protocol": 2, "op": "fatal", "reason": "invalid-config", "message": "沙箱启动参数不正确。" }
25
+ ```
22
26
 
23
- 帧格式是普通 WebSocket,不调用某一种 WebView 的接口。Qt WebEngine、WebView2 或其他内核,只要下面的条件成立,就能连上。
27
+ `fatal.message` 不包含工程路径或令牌。`ready` 之后 stdout 只写 `status` 和 `bye`;宿主必须持续读取 stdout 和 stderr,避免管道堵塞。令牌不能写入宿主日志。
24
28
 
25
- 本进程:
29
+ 宿主把 `sandbox: { state, url, token, protocol }` 交给页面。页面建立连接后的第一帧必须是:
26
30
 
27
- - Node.js 24 或更新版本。
28
- - 能在 `127.0.0.1` 上绑定系统分配的临时端口。只监听这个地址,不提供 `wss://`。
29
- - 命令隔离依赖当前系统的容器能力:macOS 用 Seatbelt,Windows 用 ProcessContainer,Linux 用 Bubblewrap。Windows 上 `exec` 暂不可用。
31
+ ```json
32
+ { "protocol": 2, "op": "auth", "token": "..." }
33
+ ```
30
34
 
31
- 发起连接的页面:
35
+ 认证成功后,文件和命令请求直接在页面与沙箱之间传输。WebSocket 不接受 `shutdown`、更换 `root` 或更换来源。
32
36
 
33
- - 提供 `WebSocket`,并且 HTTPS 页面可以连接 `ws://127.0.0.1`。Chromium(Qt WebEngine、WebView2)不把回环地址当成混合内容。
34
- - 浏览器会带上 `Origin`。这个值必须与启动时的某个 `--origin` 完全一致,例如 `https://app.example`。`file://` 的来源是 `null`,只有调试时加上 `--allow-null-origin` 才接受。
35
- - 公网来源访问回环地址时,嵌入环境要放行这次本地网络访问。沙箱只对匹配的来源返回 `Access-Control-Allow-Private-Network: true`。
36
- - 页面 CSP 的 `connect-src` 放行 `ws://127.0.0.1:*`。
37
- - 令牌放在页面内存里,同一页面的脚本可以读到。页面本身应是可信来源。
37
+ ### 宿主只做什么
38
38
 
39
- ## 启动
39
+ 1. 启动 `--listen --root --origin`,并把进程放进独立进程组或 Windows Job。
40
+ 2. 读取 `ready`,把地址、令牌和协议号交给页面。
41
+ 3. 可选地向 stdin 写 `{"op":"status"}`。
42
+ 4. 关闭工程时向 stdin 写 `{"op":"shutdown"}`;3 秒未退出则结束整个进程组。
43
+ 5. 监听异常退出并通知页面。文件操作和命令输出不在宿主中转。
40
44
 
41
- 旧的行协议仍可用于单次请求和测试。stdin 每行一个 JSON 请求,stdout 每行一个 JSON 响应,用 `id` 对应。不监听端口。
45
+ 换工程或重启沙箱时必须使用新的端口和令牌。运行中的调用不会自动重放。
42
46
 
43
- ```bash
44
- workspace-sandbox --serve
45
- ```
47
+ ### 页面要求
46
48
 
47
- 文件操作可以同时进行。`exec` 和 `run_python` 一次只能跑一个,第二个返回 `AbortError`。
49
+ 帧格式是普通 WebSocket,不依赖 WebView、Qt 或某一种浏览器内核。页面只要能创建 WebSocket,就可以连接沙箱。
48
50
 
49
- 也可以不带 `--serve`,把单个 JSON 写到 stdin,stdout 返回一个 JSON。
51
+ - 页面能够连接 `ws://127.0.0.1:*`。页面策略限制连接地址时,把该地址加入允许列表。
52
+ - 握手带上的 Origin 与启动参数中的某个 `--origin` 完全一致。宿主把自己的页面来源传给沙箱。
53
+ - 某些嵌入环境会拦截公网页面对回环地址的访问。宿主只放行自己的页面访问 `127.0.0.1`,匹配来源的预检会返回 `Access-Control-Allow-Private-Network: true`。
54
+ - `file://` 页面的 Origin 通常是 `null`,只有调试时才使用 `--allow-null-origin`。
55
+ - SDK 只加载在可信页面中;同一页面里的脚本能够读取内存中的令牌。
50
56
 
51
- ```bash
52
- workspace-sandbox --manifest
53
- workspace-sandbox --check
54
- ```
57
+ ## 协议 2 数据面
55
58
 
56
- `--manifest` 打印包名、版本、协议号、平台和 Node 版本。`--check` 在临时目录里跑 shell、Python、Node 和断网检查,失败时退出码为 1。
59
+ 文本帧是 UTF-8 JSON,最大 256 KB;文件内容使用二进制帧,单片载荷最大 1 MB。所有业务请求带客户端生成的字符串 `id`。
57
60
 
58
- ## root
61
+ | 操作 | 行为 |
62
+ | --- | --- |
63
+ | `list` / `stat` | 读取目录和文件元数据;目录最多返回 2000 项 |
64
+ | `read` | 按 `offset` / `maxBytes` 读取窗口,最大 50 MB |
65
+ | `write` | 先发大小和路径,再按偏移发送二进制片;同目录临时文件写完后原子替换 |
66
+ | `mkdir` / `remove` / `move` | 修改工程内文件和目录 |
67
+ | `exec` / `run_python` | 流式返回 `stdout` / `stderr`,最后发送 `exit` |
68
+ | `cancel` | 取消同一 `id` 的请求;命令会结束整个子进程树 |
59
69
 
60
- `root` 由宿主写成自己选定的工程目录绝对路径。请求里的其他路径都相对这个目录。
70
+ 路径使用 `/`,只允许工程根内的相对路径。空字符串和 `.` 表示工程根;`..`、盘符、反斜杠和绝对路径会返回 `SecurityError`。请求中不能传入或替换 `root`。
61
71
 
62
- 本进程信任 `root`。能往 stdin 写 JSON 的一方,可以在该目录里列出、读取、写入和删除。文件操作在本进程内完成,不进入隔离容器。
72
+ 文件操作最多并发 8 个,其中写入最多 1 个。一个工程同时只运行一个 `exec` 或 `run_python`,第二个返回 `AbortError`。写入和待发送数据都使用 4 MB 背压窗口,不会无限堆积到内存。
63
73
 
64
- ## 请求
74
+ 命令输出按累计 32 KB 或 50 ms 合并发送。SDK 观察结果仍限制为每个流最后 16000 字符;超出部分写到 `tmp/sandbox-exec/<id>.log`,日志最多 8 MB。每次新命令开始前会清理超过 24 小时的日志。
65
75
 
66
- `protocol` 可省略;写出时必须是 `1`。`op` 省略时按 `run_python` 处理。
76
+ 文件覆盖使用临时文件和原子替换。宿主或其他程序正在修改/占用目标时,操作返回 `NoModificationAllowedError`,原文件保持不变。
67
77
 
68
- 路径用 `/`,相对工程根。空字符串和 `.` 表示工程根。不能含 `..`、盘符、反斜杠或绝对路径。`input`、`output`、`uploads`、`tmp` 这些目录名不可用。
78
+ ## 运行环境
69
79
 
70
- 成功时为 `{ "protocol": 1, "ok": true, ... }`。失败时为 `{ "protocol": 1, "ok": false, "name", "message" }`。`--serve` 下若请求带了字符串 `id`,响应里会原样带回。
80
+ 本进程:
71
81
 
72
- | op | 请求字段 | 成功时多出的字段 |
73
- | --- | --- | --- |
74
- | `list` | `path` | `entries`:`{ name, kind: "file" \| "directory", size?, lastModified? }[]` |
75
- | `stat` | `path` | `entry`:同样的字段 |
76
- | `read` | `path` | `contentBase64`。单文件不超过 50 MB |
77
- | `write` | `path`、`contentBase64`、`createDirectories` | 无 |
78
- | `mkdir` | `path` | 无 |
79
- | `remove` | `path`、`recursive` | 无。非空目录要 `recursive: true` |
80
- | `move` | `from`、`to` | 无 |
81
- | `exec` | `cwd`、`command` 或 `argv`、`timeoutMs`、`python` | `exitCode`、`timedOut`、`stdout`、`stderr`、`cwd?` |
82
- | `run_python` | `script`、`args`、`timeoutMs`、`python` | `exitCode`、`timedOut`、`stdout`、`stderr`、`warnings` |
82
+ - Node.js 24 或更新版本。
83
+ - 能在 `127.0.0.1` 上绑定系统分配的临时端口。只监听这个地址,不提供 `wss://`。
84
+ - 命令隔离依赖当前系统的容器能力:macOS 用 Seatbelt,Windows 用 ProcessContainer,Linux 用 Bubblewrap。Windows 上 `exec` 暂不可用。
83
85
 
84
- `lastModified` 是 Unix 毫秒,`size` 是字节。`timeoutMs` 默认 20 秒,最大 120 秒。命令输出超过 16000 个字符会截断。
86
+ ## 诊断
85
87
 
86
- `exec` 的 `command` 和 `argv` 二选一。`argv` 第一项只能是 `python3`、`node` 或 `sh`,第二项是工程内的脚本路径。Windows 上 `exec` 暂不可用。
88
+ ```bash
89
+ workspace-sandbox --manifest
90
+ workspace-sandbox --check
91
+ ```
87
92
 
88
- `run_python` 的 `script` 必须是工程内的 `.py` 相对路径。`python` 由宿主写成解释器的绝对路径。macOS 和 Linux 上可以省略,此时使用系统里的 `python3`。
93
+ `--manifest` 打印包名、版本、协议号、平台和 Node 版本。`--check` 在临时目录里跑 shell、Python、Node 和断网检查,失败时退出码为 1。
89
94
 
90
- ```json
91
- { "id": "1", "protocol": 1, "op": "read", "root": "/path/to/project", "path": "notes/readme.txt" }
92
- ```
95
+ ## root
93
96
 
94
- ```json
95
- { "id": "2", "protocol": 1, "op": "exec", "root": "/path/to/project", "cwd": ".", "command": "python3 scripts/hello.py" }
96
- ```
97
+ `root` 由宿主通过 `--root` 固定,WebSocket 请求不能携带或更换它。其他路径都相对这个目录。
98
+
99
+ 本进程信任宿主给出的 `root`。通过认证的数据面可以在该目录里列出、读取、写入和删除。文件操作在本进程内完成,不进入隔离容器。
97
100
 
98
101
  ## 命令隔离
99
102
 
@@ -1,20 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  import { manifest, selfCheck } from '../src/check.mjs';
3
3
  import { fatalLine, runListen } from '../src/listen.mjs';
4
- import { handleRequest, PROTOCOL } from '../src/request.mjs';
5
- import { runServe } from '../src/serve.mjs';
6
-
7
- function readStdin() {
8
- return new Promise((resolve, reject) => {
9
- let body = '';
10
- process.stdin.setEncoding('utf8');
11
- process.stdin.on('data', (chunk) => {
12
- body += chunk;
13
- });
14
- process.stdin.on('end', () => resolve(body));
15
- process.stdin.on('error', reject);
16
- });
17
- }
18
4
 
19
5
  function optionValue(name) {
20
6
  const index = process.argv.indexOf(name);
@@ -51,9 +37,6 @@ if (process.argv.includes('--listen')) {
51
37
  } catch {
52
38
  process.exit(1);
53
39
  }
54
- } else if (process.argv.includes('--serve')) {
55
- await runServe();
56
- process.exit(0);
57
40
  } else if (process.argv.includes('--manifest')) {
58
41
  response = manifest();
59
42
  } else if (process.argv.includes('--check')) {
@@ -67,14 +50,7 @@ if (process.argv.includes('--listen')) {
67
50
  }
68
51
  response = report;
69
52
  } else {
70
- let request;
71
- try {
72
- request = JSON.parse((await readStdin()) || '{}');
73
- } catch {
74
- request = undefined;
75
- }
76
- response = request
77
- ? await handleRequest(request)
78
- : { protocol: PROTOCOL, ok: false, name: 'TypeError', message: '请求不是合法的 JSON。' };
53
+ process.stdout.write(`${JSON.stringify(fatalLine('invalid-config'))}\n`);
54
+ process.exit(1);
79
55
  }
80
- process.stdout.write(JSON.stringify(response));
56
+ process.stdout.write(`${JSON.stringify(response)}\n`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@glodon-aiot/workspace-sandbox",
3
- "version": "0.2.0-alpha.2",
3
+ "version": "0.2.0-alpha.3",
4
4
  "description": "供端侧宿主调用:在工程目录内读写文件,并在 Microsoft Execution Containers 里执行命令和脚本。",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/check.mjs CHANGED
@@ -1,7 +1,8 @@
1
1
  import { mkdtempSync, readFileSync, realpathSync, rmSync } from 'node:fs';
2
2
  import { tmpdir } from 'node:os';
3
3
  import { join } from 'node:path';
4
- import { PROTOCOL, handleRequest } from './request.mjs';
4
+ import { runExec } from './exec.mjs';
5
+ import { LISTEN_PROTOCOL } from './listen.mjs';
5
6
  import { removeView } from './view.mjs';
6
7
 
7
8
  const pkg = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8'));
@@ -10,7 +11,7 @@ export function manifest() {
10
11
  return {
11
12
  name: pkg.name,
12
13
  version: pkg.version,
13
- protocol: PROTOCOL,
14
+ protocol: LISTEN_PROTOCOL,
14
15
  platform: `${process.platform}-${process.arch}`,
15
16
  node: process.versions.node,
16
17
  };
@@ -34,21 +35,25 @@ export async function selfCheck({ python } = {}) {
34
35
  try {
35
36
  const checks = [];
36
37
  for (const probe of PROBES) {
37
- const response = await handleRequest({
38
- protocol: PROTOCOL,
39
- op: 'exec',
40
- root,
41
- cwd: '.',
42
- command: probe.command,
43
- timeoutMs: 20_000,
44
- ...(python ? { python } : {}),
45
- });
46
- const passed = response.ok && response.exitCode === 0 && probe.expect.test(response.stdout);
47
- checks.push({
48
- name: probe.name,
49
- ok: passed,
50
- ...(passed ? {} : { detail: response.ok ? `${response.stdout}${response.stderr}`.trim() : response.message }),
51
- });
38
+ try {
39
+ const response = await runExec({
40
+ op: 'exec',
41
+ root,
42
+ rootReal: root,
43
+ cwd: '.',
44
+ command: probe.command,
45
+ timeoutMs: 20_000,
46
+ ...(python ? { python } : {}),
47
+ });
48
+ const passed = response.exitCode === 0 && probe.expect.test(response.stdout);
49
+ checks.push({
50
+ name: probe.name,
51
+ ok: passed,
52
+ ...(passed ? {} : { detail: `${response.stdout}${response.stderr}`.trim() }),
53
+ });
54
+ } catch (error) {
55
+ checks.push({ name: probe.name, ok: false, detail: error?.message || '检查失败。' });
56
+ }
52
57
  }
53
58
  return { ...manifest(), ok: checks.every((check) => check.ok), checks };
54
59
  } finally {
package/src/files.mjs CHANGED
@@ -1,5 +1,4 @@
1
- import { randomBytes } from 'node:crypto';
2
- import { lstat, mkdir, open, opendir, readFile, readdir, realpath, rename, rm, stat, unlink, writeFile } from 'node:fs/promises';
1
+ import { lstat, mkdir, open, opendir, readdir, realpath, rename, rm, stat, unlink } from 'node:fs/promises';
3
2
  import { basename, dirname, isAbsolute, join, relative, sep } from 'node:path';
4
3
  import { isSyntheticCwd, sandboxError, SYNTHETIC_DIR_MESSAGE } from './policy.mjs';
5
4
 
@@ -8,10 +7,6 @@ export const MAX_FILE_BYTES = 50 * 1024 * 1024;
8
7
 
9
8
  const DOM_ERROR = /^[A-Z][A-Za-z]+Error$/;
10
9
 
11
- export function assertWithinQuota(byteLength, limit = MAX_FILE_BYTES) {
12
- if (byteLength > limit) throw sandboxError('QuotaExceededError', '文件超过 50 MB。');
13
- }
14
-
15
10
  /** 页面传来的相对路径。空路径是工程根。拒绝绝对路径、盘符、反斜杠和 `..`。 */
16
11
  export function splitWorkspacePath(path, options = {}) {
17
12
  let rel = String(path ?? '').trim();
@@ -141,61 +136,6 @@ async function listOp(request) {
141
136
  return { entries };
142
137
  }
143
138
 
144
- async function readOp(request, maxBytes) {
145
- const real = await resolveWorkspacePath(request.rootReal, request.path);
146
- let info;
147
- try {
148
- info = await stat(real);
149
- } catch (error) {
150
- if (error?.code === 'ENOENT') throw sandboxError('NotFoundError', '目标不存在。');
151
- throw error;
152
- }
153
- if (info.isDirectory()) throw sandboxError('TypeMismatchError', '不是文件。');
154
- assertWithinQuota(info.size, maxBytes);
155
- const bytes = await readFile(real);
156
- return { contentBase64: Buffer.from(bytes).toString('base64') };
157
- }
158
-
159
- async function ensureParent(real, createDirectories) {
160
- const parent = dirname(real);
161
- let info;
162
- try {
163
- info = await stat(parent);
164
- } catch (error) {
165
- if (error?.code !== 'ENOENT') throw error;
166
- }
167
- if (info?.isDirectory()) return parent;
168
- if (info || !createDirectories) throw sandboxError('NotFoundError', '父目录不存在。');
169
- await mkdir(parent, { recursive: true });
170
- return parent;
171
- }
172
-
173
- async function writeOp(request, maxBytes) {
174
- if (request.contentBase64 !== undefined && typeof request.contentBase64 !== 'string') {
175
- throw sandboxError('TypeError', 'contentBase64 必须是字符串。');
176
- }
177
- const real = await resolveWorkspacePath(request.rootReal, request.path);
178
- let existing;
179
- try {
180
- existing = await stat(real);
181
- } catch (error) {
182
- if (error?.code !== 'ENOENT') throw error;
183
- }
184
- if (existing?.isDirectory()) throw sandboxError('TypeMismatchError', '不能把目录当文件写。');
185
- const parent = await ensureParent(real, request.createDirectories === true);
186
- const bytes = Buffer.from(request.contentBase64 ?? '', 'base64');
187
- assertWithinQuota(bytes.length, maxBytes);
188
- const temporary = join(parent, `.${basename(real)}.${randomBytes(6).toString('hex')}.tmp`);
189
- try {
190
- await writeFile(temporary, bytes);
191
- await rename(temporary, real);
192
- } catch (error) {
193
- await unlink(temporary).catch(() => undefined);
194
- throw error;
195
- }
196
- return {};
197
- }
198
-
199
139
  async function mkdirOp(request) {
200
140
  const real = await resolveWorkspacePath(request.rootReal, request.path);
201
141
  try {
@@ -362,8 +302,6 @@ export async function listLimited(rootReal, path, limit = 2000) {
362
302
  const OPERATIONS = {
363
303
  stat: statOp,
364
304
  list: listOp,
365
- read: readOp,
366
- write: writeOp,
367
305
  mkdir: mkdirOp,
368
306
  remove: removeOp,
369
307
  move: moveOp,
@@ -371,13 +309,12 @@ const OPERATIONS = {
371
309
 
372
310
  /**
373
311
  * 工程目录内的文件操作。不进入 MXC,由调用方保证 rootReal 是真实工程根。
374
- * limits 只给测试收紧配额,协议请求里没有这个字段。
375
312
  */
376
- export async function runFileOp(request, limits = {}) {
313
+ export async function runFileOp(request) {
377
314
  const operation = OPERATIONS[request.op];
378
315
  if (!operation) throw sandboxError('NotSupportedError', `不支持的操作 ${String(request.op)}。`);
379
316
  try {
380
- return await operation(request, limits.maxBytes ?? MAX_FILE_BYTES);
317
+ return await operation(request);
381
318
  } catch (error) {
382
319
  throw asFileError(error);
383
320
  }
package/src/runPython.mjs CHANGED
@@ -7,7 +7,7 @@ import { mentionsSyntheticDir, quoteArg, resolvePythonRun, SYNTHETIC_DIR_MESSAGE
7
7
  import { defaultPython, installFailureResponse, preparePython } from './python.mjs';
8
8
  import { outputMappings, toVirtualText } from './view.mjs';
9
9
 
10
- /** 旧的 run_python 请求:在工程根目录用宿主指定的 Python 跑一个 .py 脚本。 */
10
+ /** 在工程根目录用宿主指定的 Python 跑一个 .py 脚本。 */
11
11
  export async function runPython(request) {
12
12
  if (mentionsSyntheticDir(request.script)) {
13
13
  return { exitCode: 1, timedOut: false, stdout: '', stderr: `${SYNTHETIC_DIR_MESSAGE}\n` };
package/src/request.mjs DELETED
@@ -1,112 +0,0 @@
1
- import { realpathSync, statSync } from 'node:fs';
2
- import { isAbsolute } from 'node:path';
3
- import { runExec } from './exec.mjs';
4
- import { runFileOp } from './files.mjs';
5
- import { clampTimeout, isSafeRelative, sandboxError } from './policy.mjs';
6
- import { runPython } from './runPython.mjs';
7
- import { outputMappings, toVirtualText } from './view.mjs';
8
-
9
- export const PROTOCOL = 1;
10
- const MAX_COMMAND_CHARS = 64 * 1024;
11
- const MAX_ARGV = 64;
12
-
13
- function requireRoot(root) {
14
- if (typeof root !== 'string' || !isAbsolute(root)) throw sandboxError('NotAllowedError', '缺少工程目录。');
15
- let real;
16
- try {
17
- real = realpathSync(root);
18
- } catch {
19
- throw sandboxError('NotFoundError', '工程目录不存在。');
20
- }
21
- if (!statSync(real).isDirectory()) throw sandboxError('TypeMismatchError', '工程目录不是文件夹。');
22
- return real;
23
- }
24
-
25
- function requireText(value, label) {
26
- if (typeof value !== 'string' || value.includes('\0')) throw sandboxError('TypeError', `${label}必须是字符串。`);
27
- return value;
28
- }
29
-
30
- function parseExec(raw) {
31
- const hasCommand = raw.command !== undefined;
32
- const hasArgv = raw.argv !== undefined;
33
- if (hasCommand === hasArgv) throw sandboxError('TypeError', 'command 和 argv 必须二选一。');
34
- const cwd = raw.cwd === undefined || raw.cwd === '' || raw.cwd === '.' ? '.' : requireText(raw.cwd, 'cwd ');
35
- if (cwd !== '.' && !isSafeRelative(cwd)) {
36
- throw sandboxError('SecurityError', 'cwd 必须是工程内的相对路径。');
37
- }
38
- if (raw.python !== undefined && (typeof raw.python !== 'string' || !isAbsolute(raw.python))) {
39
- throw sandboxError('NotAllowedError', 'python 必须是绝对路径。');
40
- }
41
- const request = { op: 'exec', cwd, python: raw.python };
42
- if (hasCommand) {
43
- const command = requireText(raw.command, 'command ');
44
- if (!command.trim()) throw sandboxError('TypeError', 'command 不能为空。');
45
- if (command.length > MAX_COMMAND_CHARS) throw sandboxError('QuotaExceededError', 'command 过长。');
46
- return { ...request, command };
47
- }
48
- if (!Array.isArray(raw.argv) || raw.argv.length < 2 || raw.argv.length > MAX_ARGV) {
49
- throw sandboxError('TypeError', `argv 需要程序和脚本,最多 ${MAX_ARGV} 项。`);
50
- }
51
- const argv = raw.argv.map((item) => requireText(item, 'argv 每一项'));
52
- if (argv.join('').length > MAX_COMMAND_CHARS) throw sandboxError('QuotaExceededError', 'argv 过长。');
53
- if (!isSafeRelative(argv[1])) throw sandboxError('SecurityError', '脚本必须是工程内的相对路径。');
54
- return { ...request, argv };
55
- }
56
-
57
- const FILE_OPS = new Set(['stat', 'list', 'read', 'write', 'mkdir', 'remove', 'move']);
58
-
59
- function parseFile(raw, op) {
60
- const request = { op, root: raw.root, rootReal: requireRoot(raw.root) };
61
- if (op === 'move') {
62
- request.from = raw.from ?? '';
63
- request.to = raw.to ?? '';
64
- return request;
65
- }
66
- request.path = raw.path ?? '';
67
- if (op === 'write') {
68
- if (raw.contentBase64 !== undefined && typeof raw.contentBase64 !== 'string') {
69
- throw sandboxError('TypeError', 'contentBase64 必须是字符串。');
70
- }
71
- request.contentBase64 = typeof raw.contentBase64 === 'string' ? raw.contentBase64 : '';
72
- request.createDirectories = raw.createDirectories === true;
73
- }
74
- if (op === 'remove') request.recursive = raw.recursive === true;
75
- return request;
76
- }
77
-
78
- export function parseRequest(raw) {
79
- if (!raw || typeof raw !== 'object' || Array.isArray(raw)) throw sandboxError('TypeError', '请求必须是 JSON 对象。');
80
- if (raw.protocol !== undefined && raw.protocol !== PROTOCOL) {
81
- throw sandboxError('NotSupportedError', `沙箱协议 ${raw.protocol} 不支持,当前为 ${PROTOCOL}。`);
82
- }
83
- const op = raw.op ?? 'run_python';
84
- if (FILE_OPS.has(op)) return parseFile(raw, op);
85
- const base = { root: raw.root, rootReal: requireRoot(raw.root), timeoutMs: clampTimeout(raw.timeoutMs) };
86
- if (op === 'exec') return { ...base, ...parseExec(raw) };
87
- if (op === 'run_python') return { ...base, op, script: raw.script, args: raw.args, python: raw.python };
88
- throw sandboxError('NotSupportedError', `不支持的操作 ${String(op)}。`);
89
- }
90
-
91
- export async function handleRequest(raw) {
92
- try {
93
- const request = parseRequest(raw);
94
- const result =
95
- request.op === 'exec' ? await runExec(request) : request.op === 'run_python' ? await runPython(request) : await runFileOp(request);
96
- return { protocol: PROTOCOL, ok: true, ...result };
97
- } catch (error) {
98
- let rootReal;
99
- try {
100
- rootReal = realpathSync(raw.root);
101
- } catch {
102
- rootReal = undefined;
103
- }
104
- const mappings = outputMappings({ rootReal, rootGiven: typeof raw?.root === 'string' ? raw.root : undefined });
105
- return {
106
- protocol: PROTOCOL,
107
- ok: false,
108
- name: error?.code && /^[A-Z][A-Za-z]+Error$/.test(error.code) ? error.code : 'NotAllowedError',
109
- message: toVirtualText(error?.message || '沙箱执行失败。', mappings),
110
- };
111
- }
112
- }
package/src/serve.mjs DELETED
@@ -1,65 +0,0 @@
1
- import { createInterface } from 'node:readline';
2
- import { handleRequest, PROTOCOL } from './request.mjs';
3
-
4
- function isCommand(raw) {
5
- if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return false;
6
- const op = raw.op ?? 'run_python';
7
- return op === 'exec' || op === 'run_python';
8
- }
9
-
10
- /**
11
- * 常驻进程:stdin 每行一个 JSON 请求,stdout 每行一个 JSON 响应,用 id 对应。
12
- * 文件操作可以同时进行。命令仍是一次一个,第二个直接返回 AbortError。
13
- */
14
- export function runServe(input = process.stdin, output = process.stdout) {
15
- return new Promise((resolve, reject) => {
16
- const lines = createInterface({ input, crlfDelay: Infinity });
17
- let pending = 0;
18
- let closed = false;
19
- let commandBusy = false;
20
- const finish = () => {
21
- if (closed && pending === 0) resolve();
22
- };
23
- const respond = (raw, response) => {
24
- const id = raw && typeof raw === 'object' && typeof raw.id === 'string' ? raw.id : undefined;
25
- output.write(`${JSON.stringify(id === undefined ? response : { id, ...response })}\n`);
26
- };
27
- lines.on('line', (line) => {
28
- const text = line.trim();
29
- if (!text) return;
30
- pending += 1;
31
- let raw;
32
- try {
33
- raw = JSON.parse(text);
34
- } catch {
35
- raw = undefined;
36
- }
37
- const command = isCommand(raw);
38
- if (command && commandBusy) {
39
- respond(raw, { protocol: PROTOCOL, ok: false, name: 'AbortError', message: '已有一个命令在运行。' });
40
- pending -= 1;
41
- finish();
42
- return;
43
- }
44
- if (command) commandBusy = true;
45
- const task = raw
46
- ? handleRequest(raw)
47
- : Promise.resolve({ protocol: PROTOCOL, ok: false, name: 'TypeError', message: '请求不是合法的 JSON。' });
48
- task
49
- .then(
50
- (response) => respond(raw, response),
51
- () => respond(raw, { protocol: PROTOCOL, ok: false, name: 'NotAllowedError', message: '沙箱执行失败。' })
52
- )
53
- .finally(() => {
54
- if (command) commandBusy = false;
55
- pending -= 1;
56
- finish();
57
- });
58
- });
59
- lines.on('close', () => {
60
- closed = true;
61
- finish();
62
- });
63
- input.on?.('error', reject);
64
- });
65
- }