@glodon-aiot/workspace-sandbox 0.2.0-alpha.2 → 0.2.0-alpha.4
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 +61 -58
- package/bin/workspace-sandbox.mjs +3 -27
- package/package.json +1 -1
- package/src/check.mjs +22 -17
- package/src/files.mjs +3 -66
- package/src/listen.mjs +3 -9
- package/src/policy.mjs +2 -31
- package/src/request.mjs +0 -112
- package/src/runPython.mjs +0 -47
- package/src/serve.mjs +0 -65
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
|
|
5
|
+
需要 Node.js 24。生产接入使用协议 2:页面通过 WebSocket 直连本进程,宿主只管理进程并转发状态。文件字节和命令输出不经过宿主,页面也拿不到工程绝对路径。
|
|
6
6
|
|
|
7
|
-
##
|
|
7
|
+
## 生产接入
|
|
8
8
|
|
|
9
9
|
宿主启动时绑定系统分配的回环端口,并把页面来源放进允许列表:
|
|
10
10
|
|
|
@@ -12,91 +12,94 @@
|
|
|
12
12
|
workspace-sandbox --listen --root /path/to/project --origin https://app.example
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
`--origin` 可以重复。沙箱只绑定 `127.0.0.1:0`,只提供 `ws://`,不提供 `wss://`。
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
stdout 第一行是以下两种管理事件之一:
|
|
18
18
|
|
|
19
|
-
|
|
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
|
-
|
|
27
|
+
`fatal.message` 不包含工程路径或令牌。`ready` 之后 stdout 只写 `status` 和 `bye`;宿主必须持续读取 stdout 和 stderr,避免管道堵塞。令牌不能写入宿主日志。
|
|
24
28
|
|
|
25
|
-
|
|
29
|
+
宿主把 `sandbox: { state, url, token, protocol }` 交给页面。页面建立连接后的第一帧必须是:
|
|
26
30
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
31
|
+
```json
|
|
32
|
+
{ "protocol": 2, "op": "auth", "token": "..." }
|
|
33
|
+
```
|
|
30
34
|
|
|
31
|
-
|
|
35
|
+
认证成功后,文件和命令请求直接在页面与沙箱之间传输。WebSocket 不接受 `shutdown`、更换 `root` 或更换来源。
|
|
32
36
|
|
|
33
|
-
|
|
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
|
-
|
|
45
|
+
换工程或重启沙箱时必须使用新的端口和令牌。运行中的调用不会自动重放。
|
|
42
46
|
|
|
43
|
-
|
|
44
|
-
workspace-sandbox --serve
|
|
45
|
-
```
|
|
47
|
+
### 页面要求
|
|
46
48
|
|
|
47
|
-
|
|
49
|
+
帧格式是普通 WebSocket,不依赖 WebView、Qt 或某一种浏览器内核。页面只要能创建 WebSocket,就可以连接沙箱。
|
|
48
50
|
|
|
49
|
-
|
|
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
|
-
|
|
52
|
-
workspace-sandbox --manifest
|
|
53
|
-
workspace-sandbox --check
|
|
54
|
-
```
|
|
57
|
+
## 协议 2 数据面
|
|
55
58
|
|
|
56
|
-
|
|
59
|
+
文本帧是 UTF-8 JSON,最大 256 KB;文件内容使用二进制帧,单片载荷最大 1 MB。所有业务请求带客户端生成的字符串 `id`。
|
|
57
60
|
|
|
58
|
-
|
|
61
|
+
| 操作 | 行为 |
|
|
62
|
+
| --- | --- |
|
|
63
|
+
| `list` / `stat` | 读取目录和文件元数据;目录最多返回 2000 项 |
|
|
64
|
+
| `read` | 按 `offset` / `maxBytes` 读取窗口,最大 50 MB |
|
|
65
|
+
| `write` | 先发大小和路径,再按偏移发送二进制片;同目录临时文件写完后原子替换 |
|
|
66
|
+
| `mkdir` / `remove` / `move` | 修改工程内文件和目录 |
|
|
67
|
+
| `exec` | 流式返回 `stdout` / `stderr`,最后发送 `exit` |
|
|
68
|
+
| `cancel` | 取消同一 `id` 的请求;命令会结束整个子进程树 |
|
|
59
69
|
|
|
60
|
-
`root
|
|
70
|
+
路径使用 `/`,只允许工程根内的相对路径。空字符串和 `.` 表示工程根;`..`、盘符、反斜杠和绝对路径会返回 `SecurityError`。请求中不能传入或替换 `root`。
|
|
61
71
|
|
|
62
|
-
|
|
72
|
+
文件操作最多并发 8 个,其中写入最多 1 个。一个工程同时只运行一个 `exec`,第二个返回 `AbortError`。写入和待发送数据都使用 4 MB 背压窗口,不会无限堆积到内存。
|
|
63
73
|
|
|
64
|
-
|
|
74
|
+
命令输出按累计 32 KB 或 50 ms 合并发送。SDK 观察结果仍限制为每个流最后 16000 字符;超出部分写到 `tmp/sandbox-exec/<id>.log`,日志最多 8 MB。每次新命令开始前会清理超过 24 小时的日志。
|
|
65
75
|
|
|
66
|
-
|
|
76
|
+
文件覆盖使用临时文件和原子替换。宿主或其他程序正在修改/占用目标时,操作返回 `NoModificationAllowedError`,原文件保持不变。
|
|
67
77
|
|
|
68
|
-
|
|
78
|
+
## 运行环境
|
|
69
79
|
|
|
70
|
-
|
|
80
|
+
本进程:
|
|
71
81
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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
|
-
|
|
86
|
+
## 诊断
|
|
85
87
|
|
|
86
|
-
|
|
88
|
+
```bash
|
|
89
|
+
workspace-sandbox --manifest
|
|
90
|
+
workspace-sandbox --check
|
|
91
|
+
```
|
|
87
92
|
|
|
88
|
-
`
|
|
93
|
+
`--manifest` 打印包名、版本、协议号、平台和 Node 版本。`--check` 在临时目录里跑 shell、Python、Node 和断网检查,失败时退出码为 1。
|
|
89
94
|
|
|
90
|
-
|
|
91
|
-
{ "id": "1", "protocol": 1, "op": "read", "root": "/path/to/project", "path": "notes/readme.txt" }
|
|
92
|
-
```
|
|
95
|
+
## root
|
|
93
96
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
+
`root` 由宿主通过 `--root` 固定,WebSocket 请求不能携带或更换它。其他路径都相对这个目录。
|
|
98
|
+
|
|
99
|
+
本进程信任宿主给出的 `root`。通过认证的数据面可以在该目录里列出、读取、写入和删除。文件操作在本进程内完成,不进入隔离容器。
|
|
97
100
|
|
|
98
101
|
## 命令隔离
|
|
99
102
|
|
|
100
|
-
`exec`
|
|
103
|
+
`exec` 在 Microsoft Execution Containers 里执行:macOS 用 Seatbelt,Windows 用 ProcessContainer,Linux 用 Bubblewrap。工程目录可读写。工程外的符号链接和出站网络会被拒绝。子进程不继承宿主的环境变量。返回的 `stdout`、`stderr` 和 `cwd` 里,工程真实路径换成相对路径,用户主目录换成 `~`。
|
|
101
104
|
|
|
102
105
|
工程根目录可以放 `sandbox-requirements.txt`。每行一个包名,可以写 extras 和版本,最多 20 行。网址、git 和本地路径会被拒绝。执行 Python 前把这些包装进该工程专用的虚拟环境,安装只访问 `pypi.org`、`files.pythonhosted.org` 和 `pypi.python.org` 的 443 端口。脚本运行时不再联网。
|
|
@@ -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
|
-
|
|
71
|
-
|
|
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
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 {
|
|
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:
|
|
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
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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 {
|
|
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
|
|
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
|
|
317
|
+
return await operation(request);
|
|
381
318
|
} catch (error) {
|
|
382
319
|
throw asFileError(error);
|
|
383
320
|
}
|
package/src/listen.mjs
CHANGED
|
@@ -17,7 +17,6 @@ import {
|
|
|
17
17
|
} from './files.mjs';
|
|
18
18
|
import { sandboxError } from './policy.mjs';
|
|
19
19
|
import { runExec } from './exec.mjs';
|
|
20
|
-
import { runPython } from './runPython.mjs';
|
|
21
20
|
import { cacheBase, rootHash } from './view.mjs';
|
|
22
21
|
|
|
23
22
|
export const LISTEN_PROTOCOL = 2;
|
|
@@ -149,7 +148,7 @@ async function writeExecLog(rootReal, id, text) {
|
|
|
149
148
|
|
|
150
149
|
/**
|
|
151
150
|
* 在 127.0.0.1 上提供协议 2 的 WebSocket。stdout 只写 ready、fatal、status、bye。
|
|
152
|
-
* execute
|
|
151
|
+
* execute 可在测试里替换,默认走 MXC。
|
|
153
152
|
*/
|
|
154
153
|
export function runListen({
|
|
155
154
|
root,
|
|
@@ -159,7 +158,6 @@ export function runListen({
|
|
|
159
158
|
output = process.stdout,
|
|
160
159
|
signals = true,
|
|
161
160
|
execute = runExec,
|
|
162
|
-
runPythonCommand = runPython,
|
|
163
161
|
runFile = runFileOp,
|
|
164
162
|
renameFile = rename,
|
|
165
163
|
}) {
|
|
@@ -340,7 +338,7 @@ export function runListen({
|
|
|
340
338
|
await beginWrite(socket, message, pendingWrites);
|
|
341
339
|
return;
|
|
342
340
|
}
|
|
343
|
-
if (message.op === 'exec'
|
|
341
|
+
if (message.op === 'exec') {
|
|
344
342
|
void beginCommand(socket, message);
|
|
345
343
|
return;
|
|
346
344
|
}
|
|
@@ -542,11 +540,7 @@ export function runListen({
|
|
|
542
540
|
await sendJson(socket, { protocol: LISTEN_PROTOCOL, id: message.id, op: kind, text });
|
|
543
541
|
},
|
|
544
542
|
};
|
|
545
|
-
const result = controller.signal.aborted
|
|
546
|
-
? null
|
|
547
|
-
: message.op === 'run_python'
|
|
548
|
-
? await runPythonCommand(request)
|
|
549
|
-
: await execute(request);
|
|
543
|
+
const result = controller.signal.aborted ? null : await execute(request);
|
|
550
544
|
if (controller.signal.aborted || !result) {
|
|
551
545
|
await sendJson(socket, { protocol: LISTEN_PROTOCOL, id: message.id, ...failure(sandboxError('AbortError', '已取消。')) });
|
|
552
546
|
return;
|
package/src/policy.mjs
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { createHash } from 'node:crypto';
|
|
2
|
-
import { realpathSync
|
|
3
|
-
import { dirname, isAbsolute, join, relative
|
|
2
|
+
import { realpathSync } from 'node:fs';
|
|
3
|
+
import { dirname, isAbsolute, join, relative } from 'node:path';
|
|
4
4
|
|
|
5
|
-
const ARG_PATTERN = /^[\p{L}\p{N}_./:@=+\-,]+$/u;
|
|
6
5
|
export const MAX_TIMEOUT_MS = 120_000;
|
|
7
6
|
const DEFAULT_TIMEOUT_MS = 20_000;
|
|
8
7
|
export const MAX_OUTPUT_CHARS = 16_000;
|
|
@@ -57,34 +56,6 @@ export function resolveInside(rootReal, relativePath) {
|
|
|
57
56
|
return real;
|
|
58
57
|
}
|
|
59
58
|
|
|
60
|
-
/**
|
|
61
|
-
* 脚本必须是工程内的 .py 相对路径。解释器由宿主指定,参数不能带命令行控制符。
|
|
62
|
-
*/
|
|
63
|
-
export function resolvePythonRun(root, script, args, pythonPath) {
|
|
64
|
-
if (!root || !pythonPath) throw sandboxError('NotAllowedError', '缺少工程目录或 Python。');
|
|
65
|
-
const rootReal = realpathSync(root);
|
|
66
|
-
const pythonReal = realpathSync(pythonPath);
|
|
67
|
-
if (!statSync(pythonReal).isFile()) throw sandboxError('NotAllowedError', 'Python 不可用。');
|
|
68
|
-
const relativeScript = String(script ?? '').replace(/\\/g, '/').replace(/^\.\//, '');
|
|
69
|
-
if (!relativeScript.endsWith('.py') || !isSafeRelative(relativeScript)) {
|
|
70
|
-
throw sandboxError('SecurityError', '脚本必须是工程内的 .py 相对路径。');
|
|
71
|
-
}
|
|
72
|
-
const scriptReal = resolveInside(rootReal, relativeScript);
|
|
73
|
-
if (!statSync(scriptReal).isFile()) throw sandboxError('TypeMismatchError', '脚本不是文件。');
|
|
74
|
-
const safeArgs = Array.isArray(args) ? args.map(String) : [];
|
|
75
|
-
if (safeArgs.some((arg) => !ARG_PATTERN.test(arg))) {
|
|
76
|
-
throw sandboxError('SecurityError', '脚本参数含有不允许的字符。');
|
|
77
|
-
}
|
|
78
|
-
return {
|
|
79
|
-
root: rootReal,
|
|
80
|
-
python: pythonReal,
|
|
81
|
-
script: scriptReal,
|
|
82
|
-
relativeScript: relative(rootReal, scriptReal).split(sep).join('/'),
|
|
83
|
-
args: safeArgs,
|
|
84
|
-
pythonPrefix: dirname(dirname(pythonReal)),
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
|
|
88
59
|
export const SANDBOX_REQUIREMENTS_FILE = 'sandbox-requirements.txt';
|
|
89
60
|
export const MAX_REQUIREMENTS = 20;
|
|
90
61
|
export const MAX_REQUIREMENTS_CHARS = 8192;
|
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/runPython.mjs
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { mkdtempSync, realpathSync, rmSync } from 'node:fs';
|
|
2
|
-
import { tmpdir } from 'node:os';
|
|
3
|
-
import { dirname, join } from 'node:path';
|
|
4
|
-
import { sandboxEnvironment } from './environment.mjs';
|
|
5
|
-
import { contained, selectContainment } from './mxc.mjs';
|
|
6
|
-
import { mentionsSyntheticDir, quoteArg, resolvePythonRun, SYNTHETIC_DIR_MESSAGE, truncateOutput } from './policy.mjs';
|
|
7
|
-
import { defaultPython, installFailureResponse, preparePython } from './python.mjs';
|
|
8
|
-
import { outputMappings, toVirtualText } from './view.mjs';
|
|
9
|
-
|
|
10
|
-
/** 旧的 run_python 请求:在工程根目录用宿主指定的 Python 跑一个 .py 脚本。 */
|
|
11
|
-
export async function runPython(request) {
|
|
12
|
-
if (mentionsSyntheticDir(request.script)) {
|
|
13
|
-
return { exitCode: 1, timedOut: false, stdout: '', stderr: `${SYNTHETIC_DIR_MESSAGE}\n` };
|
|
14
|
-
}
|
|
15
|
-
const plan = resolvePythonRun(request.root, request.script, request.args, request.python ?? defaultPython());
|
|
16
|
-
const containment = await selectContainment();
|
|
17
|
-
const tmp = realpathSync(mkdtempSync(join(tmpdir(), 'glodon-sandbox-')));
|
|
18
|
-
try {
|
|
19
|
-
const python = await preparePython({ containment, rootReal: plan.root, python: plan.python, tmp });
|
|
20
|
-
if (python.failure) return installFailureResponse(python.failure);
|
|
21
|
-
const command = [quoteArg(python.python), '-I', quoteArg(plan.script), ...plan.args.map(quoteArg)].join(' ');
|
|
22
|
-
const result = await contained(containment, command, {
|
|
23
|
-
filesystem: {
|
|
24
|
-
readonlyPaths: python.readonlyPaths,
|
|
25
|
-
readwritePaths: [plan.root, tmp],
|
|
26
|
-
},
|
|
27
|
-
network: { egress: { default: 'deny' } },
|
|
28
|
-
environment: sandboxEnvironment({
|
|
29
|
-
tmp,
|
|
30
|
-
pathDirs: [dirname(python.python)],
|
|
31
|
-
extra: { SANDBOX_OUTPUT: tmp },
|
|
32
|
-
}),
|
|
33
|
-
timeoutMs: request.timeoutMs,
|
|
34
|
-
workingDirectory: plan.root,
|
|
35
|
-
});
|
|
36
|
-
const mappings = outputMappings({ rootReal: plan.root, rootGiven: request.root });
|
|
37
|
-
return {
|
|
38
|
-
exitCode: result.exitCode ?? 1,
|
|
39
|
-
timedOut: Boolean(result.timedOut),
|
|
40
|
-
stdout: truncateOutput(toVirtualText(result.stdout, mappings)),
|
|
41
|
-
stderr: truncateOutput(toVirtualText(`${python.note}${result.stderr || ''}`, mappings)),
|
|
42
|
-
warnings: result.warnings ?? [],
|
|
43
|
-
};
|
|
44
|
-
} finally {
|
|
45
|
-
rmSync(tmp, { recursive: true, force: true });
|
|
46
|
-
}
|
|
47
|
-
}
|
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
|
-
}
|