@glodon-aiot/workspace-sandbox 0.2.0-alpha.3 → 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 CHANGED
@@ -64,12 +64,12 @@ stdout 第一行是以下两种管理事件之一:
64
64
  | `read` | 按 `offset` / `maxBytes` 读取窗口,最大 50 MB |
65
65
  | `write` | 先发大小和路径,再按偏移发送二进制片;同目录临时文件写完后原子替换 |
66
66
  | `mkdir` / `remove` / `move` | 修改工程内文件和目录 |
67
- | `exec` / `run_python` | 流式返回 `stdout` / `stderr`,最后发送 `exit` |
67
+ | `exec` | 流式返回 `stdout` / `stderr`,最后发送 `exit` |
68
68
  | `cancel` | 取消同一 `id` 的请求;命令会结束整个子进程树 |
69
69
 
70
70
  路径使用 `/`,只允许工程根内的相对路径。空字符串和 `.` 表示工程根;`..`、盘符、反斜杠和绝对路径会返回 `SecurityError`。请求中不能传入或替换 `root`。
71
71
 
72
- 文件操作最多并发 8 个,其中写入最多 1 个。一个工程同时只运行一个 `exec` 或 `run_python`,第二个返回 `AbortError`。写入和待发送数据都使用 4 MB 背压窗口,不会无限堆积到内存。
72
+ 文件操作最多并发 8 个,其中写入最多 1 个。一个工程同时只运行一个 `exec`,第二个返回 `AbortError`。写入和待发送数据都使用 4 MB 背压窗口,不会无限堆积到内存。
73
73
 
74
74
  命令输出按累计 32 KB 或 50 ms 合并发送。SDK 观察结果仍限制为每个流最后 16000 字符;超出部分写到 `tmp/sandbox-exec/<id>.log`,日志最多 8 MB。每次新命令开始前会清理超过 24 小时的日志。
75
75
 
@@ -100,6 +100,6 @@ workspace-sandbox --check
100
100
 
101
101
  ## 命令隔离
102
102
 
103
- `exec` 和 `run_python` 在 Microsoft Execution Containers 里执行:macOS 用 Seatbelt,Windows 用 ProcessContainer,Linux 用 Bubblewrap。工程目录可读写。工程外的符号链接和出站网络会被拒绝。子进程不继承宿主的环境变量。返回的 `stdout`、`stderr` 和 `cwd` 里,工程真实路径换成相对路径,用户主目录换成 `~`。
103
+ `exec` 在 Microsoft Execution Containers 里执行:macOS 用 Seatbelt,Windows 用 ProcessContainer,Linux 用 Bubblewrap。工程目录可读写。工程外的符号链接和出站网络会被拒绝。子进程不继承宿主的环境变量。返回的 `stdout`、`stderr` 和 `cwd` 里,工程真实路径换成相对路径,用户主目录换成 `~`。
104
104
 
105
105
  工程根目录可以放 `sandbox-requirements.txt`。每行一个包名,可以写 extras 和版本,最多 20 行。网址、git 和本地路径会被拒绝。执行 Python 前把这些包装进该工程专用的虚拟环境,安装只访问 `pypi.org`、`files.pythonhosted.org` 和 `pypi.python.org` 的 443 端口。脚本运行时不再联网。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@glodon-aiot/workspace-sandbox",
3
- "version": "0.2.0-alpha.3",
3
+ "version": "0.2.0-alpha.4",
4
4
  "description": "供端侧宿主调用:在工程目录内读写文件,并在 Microsoft Execution Containers 里执行命令和脚本。",
5
5
  "license": "MIT",
6
6
  "type": "module",
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 / runPythonCommand 可在测试里替换,默认走 MXC。
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' || message.op === 'run_python') {
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, statSync } from 'node:fs';
3
- import { dirname, isAbsolute, join, relative, sep } from 'node:path';
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/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
- /** 在工程根目录用宿主指定的 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
- }