@glodon-aiot/workspace-sandbox 0.0.0-stage → 0.2.0-alpha.0
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/bin/workspace-sandbox.mjs +50 -0
- package/package.json +24 -4
- package/src/check.mjs +58 -0
- package/src/environment.mjs +27 -0
- package/src/exec.mjs +140 -0
- package/src/files.mjs +289 -0
- package/src/mxc.mjs +63 -0
- package/src/policy.mjs +168 -0
- package/src/pypiProxy.mjs +49 -0
- package/src/python.mjs +248 -0
- package/src/request.mjs +112 -0
- package/src/runPython.mjs +47 -0
- package/src/serve.mjs +65 -0
- package/src/view.mjs +144 -0
- package/README.md +0 -3
package/src/policy.mjs
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { realpathSync, statSync } from 'node:fs';
|
|
3
|
+
import { dirname, isAbsolute, join, relative, sep } from 'node:path';
|
|
4
|
+
|
|
5
|
+
const ARG_PATTERN = /^[\p{L}\p{N}_./:@=+\-,]+$/u;
|
|
6
|
+
export const MAX_TIMEOUT_MS = 120_000;
|
|
7
|
+
const DEFAULT_TIMEOUT_MS = 20_000;
|
|
8
|
+
export const MAX_OUTPUT_CHARS = 16_000;
|
|
9
|
+
|
|
10
|
+
export function sandboxError(name, message) {
|
|
11
|
+
return Object.assign(new Error(message), { code: name });
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function clampTimeout(value) {
|
|
15
|
+
const parsed = Number(value);
|
|
16
|
+
if (!Number.isFinite(parsed) || parsed <= 0) return DEFAULT_TIMEOUT_MS;
|
|
17
|
+
return Math.min(Math.trunc(parsed), MAX_TIMEOUT_MS);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function truncateOutput(text) {
|
|
21
|
+
const value = String(text ?? '');
|
|
22
|
+
if (value.length <= MAX_OUTPUT_CHARS) return value;
|
|
23
|
+
return `${value.slice(0, MAX_OUTPUT_CHARS)}\n…输出已截断`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function shellQuote(value) {
|
|
27
|
+
return `'${String(value).replaceAll("'", "'\\''")}'`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** MXC 的 command 在 macOS 和 Linux 上按 shell 规则拆分(会展开 $),每个参数都要单引号包起来。 */
|
|
31
|
+
export function quoteArg(value) {
|
|
32
|
+
return process.platform === 'win32' ? `"${String(value).replaceAll('"', '\\"')}"` : shellQuote(value);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** 工作区内的相对路径:不以 / 开头,不含 `..`、反斜杠和 NUL。 */
|
|
36
|
+
export function isSafeRelative(path) {
|
|
37
|
+
const value = String(path ?? '');
|
|
38
|
+
if (!value || value.startsWith('/') || value.includes('\\') || value.includes('\0') || value.includes(':')) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
return value.split('/').every((part) => part && part !== '.' && part !== '..');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** realpath 之后仍在 root 之内的文件,返回真实路径。 */
|
|
45
|
+
export function resolveInside(rootReal, relativePath) {
|
|
46
|
+
if (!isSafeRelative(relativePath)) throw sandboxError('SecurityError', '路径必须是工程内的相对路径。');
|
|
47
|
+
let real;
|
|
48
|
+
try {
|
|
49
|
+
real = realpathSync(join(rootReal, relativePath));
|
|
50
|
+
} catch {
|
|
51
|
+
throw sandboxError('NotFoundError', '脚本或目录不存在。');
|
|
52
|
+
}
|
|
53
|
+
const fromRoot = relative(rootReal, real);
|
|
54
|
+
if (!fromRoot || fromRoot.startsWith('..') || isAbsolute(fromRoot)) {
|
|
55
|
+
throw sandboxError('SecurityError', '路径不在工程目录内。');
|
|
56
|
+
}
|
|
57
|
+
return real;
|
|
58
|
+
}
|
|
59
|
+
|
|
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
|
+
export const SANDBOX_REQUIREMENTS_FILE = 'sandbox-requirements.txt';
|
|
89
|
+
export const MAX_REQUIREMENTS = 20;
|
|
90
|
+
export const MAX_REQUIREMENTS_CHARS = 8192;
|
|
91
|
+
|
|
92
|
+
const REQUIREMENT_NAME = '[A-Za-z0-9](?:[A-Za-z0-9._-]*[A-Za-z0-9])?';
|
|
93
|
+
const REQUIREMENT_VERSION = '[A-Za-z0-9*!][A-Za-z0-9.*+!]*';
|
|
94
|
+
const REQUIREMENT_SPEC = `(?:===|==|!=|<=|>=|~=|<|>)${REQUIREMENT_VERSION}`;
|
|
95
|
+
const REQUIREMENT_PATTERN = new RegExp(
|
|
96
|
+
`^${REQUIREMENT_NAME}(?:\\[${REQUIREMENT_NAME}(?:,${REQUIREMENT_NAME})*\\])?(?:${REQUIREMENT_SPEC}(?:,${REQUIREMENT_SPEC})*)?$`
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* 只接受包名、extras 和版本。网址、git、本地路径和 pip 选项都拒绝。
|
|
101
|
+
*/
|
|
102
|
+
export function parseSandboxRequirements(text) {
|
|
103
|
+
const source = String(text ?? '');
|
|
104
|
+
if (source.length > MAX_REQUIREMENTS_CHARS) throw sandboxError('SecurityError', 'sandbox-requirements.txt 过长。');
|
|
105
|
+
const packages = [];
|
|
106
|
+
const seen = new Set();
|
|
107
|
+
for (const rawLine of source.split(/\r?\n/)) {
|
|
108
|
+
const line = rawLine.replace(/#.*$/, '').trim();
|
|
109
|
+
if (!line) continue;
|
|
110
|
+
const compact = line.replace(/\s+/g, '');
|
|
111
|
+
if (!REQUIREMENT_PATTERN.test(compact)) {
|
|
112
|
+
throw sandboxError('SecurityError', '依赖只能写包名和版本,不能写网址、git 或本地路径。');
|
|
113
|
+
}
|
|
114
|
+
const canonical = compact.split('[')[0].split(/[=<>!~]/)[0].toLowerCase().replaceAll('_', '-').replaceAll('.', '-');
|
|
115
|
+
if (seen.has(canonical)) throw sandboxError('SecurityError', 'sandbox-requirements.txt 里有重复的包。');
|
|
116
|
+
seen.add(canonical);
|
|
117
|
+
packages.push(compact);
|
|
118
|
+
}
|
|
119
|
+
if (packages.length > MAX_REQUIREMENTS) throw sandboxError('SecurityError', '依赖最多 20 个。');
|
|
120
|
+
return packages;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function requirementsStamp(pythonReal, packages) {
|
|
124
|
+
return createHash('sha256').update(pythonReal).update('\0').update(packages.join('\n')).digest('hex');
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const LIBRARY_ROOT = /^\/(?:opt\/homebrew|usr\/local|opt\/local|Library\/Frameworks)\//;
|
|
128
|
+
const PYPI_HOSTS = new Set(['pypi.org', 'files.pythonhosted.org', 'pypi.python.org']);
|
|
129
|
+
|
|
130
|
+
/** 从 otool -L 或 ldd 的输出里取出解释器自己链接的第三方库目录。不收用户主目录。 */
|
|
131
|
+
export function libraryDirsForFiles(output, resolveReal = realpathSync) {
|
|
132
|
+
const dirs = new Set();
|
|
133
|
+
for (const match of String(output).matchAll(/(?:=>\s*)?(\/\S+)/g)) {
|
|
134
|
+
const file = match[1];
|
|
135
|
+
if (!LIBRARY_ROOT.test(file)) continue;
|
|
136
|
+
dirs.add(dirname(file));
|
|
137
|
+
try {
|
|
138
|
+
dirs.add(dirname(resolveReal(file)));
|
|
139
|
+
} catch {
|
|
140
|
+
// 记录在二进制里的路径本身就要放行,真实路径打不开时保留原目录。
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return [...dirs];
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** 模型若把文件写进这些目录,就会在工作区里复制出多份相同内容。 */
|
|
147
|
+
export const SYNTHETIC_DIRS = ['input', 'output', 'uploads', 'tmp'];
|
|
148
|
+
export const SYNTHETIC_DIR_MESSAGE =
|
|
149
|
+
'不要使用 input、output、uploads、tmp,也不要新建这些目录。请把文件直接放在工作区已有位置或根目录。';
|
|
150
|
+
|
|
151
|
+
const SYNTHETIC_DIR = /(?:^|[\s'"=;|&<>`:])\/?(?:input|output|uploads|tmp)(?=\/|[\s'"=;|&<>`:]|$)/;
|
|
152
|
+
|
|
153
|
+
export function mentionsSyntheticDir(text) {
|
|
154
|
+
return SYNTHETIC_DIR.test(String(text ?? ''));
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function isSyntheticCwd(cwd) {
|
|
158
|
+
const first = String(cwd ?? '').split('/')[0];
|
|
159
|
+
return SYNTHETIC_DIRS.includes(first);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export function isAllowedPypiConnect(authority) {
|
|
163
|
+
const value = String(authority ?? '');
|
|
164
|
+
const colon = value.lastIndexOf(':');
|
|
165
|
+
if (colon <= 0) return false;
|
|
166
|
+
const host = value.slice(0, colon).replace(/^\[|\]$/g, '').toLowerCase();
|
|
167
|
+
return value.slice(colon + 1) === '443' && PYPI_HOSTS.has(host);
|
|
168
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import http from 'node:http';
|
|
2
|
+
import net from 'node:net';
|
|
3
|
+
import { isAllowedPypiConnect } from './policy.mjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 只转发到 PyPI 的 CONNECT 代理。安装步骤里的 pip 必须走这里,
|
|
7
|
+
* 沙箱因此不需要自己能访问任意外网。
|
|
8
|
+
*/
|
|
9
|
+
export function startPypiProxy() {
|
|
10
|
+
const server = http.createServer((_request, response) => {
|
|
11
|
+
response.writeHead(403);
|
|
12
|
+
response.end();
|
|
13
|
+
});
|
|
14
|
+
server.on('connect', (request, socket, head) => {
|
|
15
|
+
if (!isAllowedPypiConnect(request.url)) {
|
|
16
|
+
socket.write('HTTP/1.1 403 Forbidden\r\nConnection: close\r\n\r\n');
|
|
17
|
+
socket.destroy();
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const host = request.url.slice(0, request.url.lastIndexOf(':'));
|
|
21
|
+
const upstream = net.connect(443, host);
|
|
22
|
+
const fail = () => {
|
|
23
|
+
socket.destroy();
|
|
24
|
+
upstream.destroy();
|
|
25
|
+
};
|
|
26
|
+
upstream.on('error', fail);
|
|
27
|
+
socket.on('error', fail);
|
|
28
|
+
upstream.on('connect', () => {
|
|
29
|
+
socket.write('HTTP/1.1 200 Connection Established\r\n\r\n');
|
|
30
|
+
if (head?.length) upstream.write(head);
|
|
31
|
+
upstream.pipe(socket);
|
|
32
|
+
socket.pipe(upstream);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
return new Promise((resolve, reject) => {
|
|
36
|
+
server.once('error', reject);
|
|
37
|
+
server.listen(0, '127.0.0.1', () => {
|
|
38
|
+
const address = server.address();
|
|
39
|
+
resolve({
|
|
40
|
+
url: `http://127.0.0.1:${address.port}`,
|
|
41
|
+
port: address.port,
|
|
42
|
+
close: () =>
|
|
43
|
+
new Promise((done) => {
|
|
44
|
+
server.close(() => done());
|
|
45
|
+
}),
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
}
|
package/src/python.mjs
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, realpathSync, statSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { basename, dirname, isAbsolute, join, relative } from 'node:path';
|
|
4
|
+
import { sandboxEnvironment } from './environment.mjs';
|
|
5
|
+
import { contained } from './mxc.mjs';
|
|
6
|
+
import {
|
|
7
|
+
libraryDirsForFiles,
|
|
8
|
+
parseSandboxRequirements,
|
|
9
|
+
requirementsStamp,
|
|
10
|
+
SANDBOX_REQUIREMENTS_FILE,
|
|
11
|
+
sandboxError,
|
|
12
|
+
quoteArg,
|
|
13
|
+
truncateOutput,
|
|
14
|
+
} from './policy.mjs';
|
|
15
|
+
import { startPypiProxy } from './pypiProxy.mjs';
|
|
16
|
+
import { cacheBase, rootHash } from './view.mjs';
|
|
17
|
+
|
|
18
|
+
const INSTALL_TIMEOUT_MS = 90_000;
|
|
19
|
+
const CERT_DIRECTORIES = ['/etc/ssl', '/private/etc/ssl', '/opt/homebrew/etc/openssl@3', '/opt/homebrew/etc/ca-certificates'];
|
|
20
|
+
|
|
21
|
+
function nativeFilesUnder(dir, files, budget) {
|
|
22
|
+
if (budget.count <= 0) return;
|
|
23
|
+
let entries;
|
|
24
|
+
try {
|
|
25
|
+
entries = readdirSync(dir);
|
|
26
|
+
} catch {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
for (const name of entries) {
|
|
30
|
+
if (budget.count <= 0) return;
|
|
31
|
+
if (name === '__pycache__') continue;
|
|
32
|
+
const full = join(dir, name);
|
|
33
|
+
let info;
|
|
34
|
+
try {
|
|
35
|
+
info = statSync(full);
|
|
36
|
+
} catch {
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
if (info.isDirectory()) nativeFilesUnder(full, files, budget);
|
|
40
|
+
else if (/\.(?:so|dylib)(?:\.\d+)*$/.test(name)) {
|
|
41
|
+
files.push(full);
|
|
42
|
+
budget.count -= 1;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function linkedLibraryDirs(files) {
|
|
48
|
+
const dumps = [];
|
|
49
|
+
for (const file of files) {
|
|
50
|
+
try {
|
|
51
|
+
dumps.push(
|
|
52
|
+
process.platform === 'darwin'
|
|
53
|
+
? execFileSync('otool', ['-L', file], { encoding: 'utf8' })
|
|
54
|
+
: process.platform === 'linux'
|
|
55
|
+
? execFileSync('ldd', [file], { encoding: 'utf8' })
|
|
56
|
+
: ''
|
|
57
|
+
);
|
|
58
|
+
} catch {
|
|
59
|
+
// 单个扩展读不到不影响其余库。
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return libraryDirsForFiles(dumps.join('\n'));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function interpreterLibraryDirs(python, prefix) {
|
|
66
|
+
const files = [python];
|
|
67
|
+
let entries = [];
|
|
68
|
+
try {
|
|
69
|
+
entries = readdirSync(join(prefix, 'lib'));
|
|
70
|
+
} catch {
|
|
71
|
+
return linkedLibraryDirs(files);
|
|
72
|
+
}
|
|
73
|
+
for (const entry of entries) nativeFilesUnder(join(prefix, 'lib', entry, 'lib-dynload'), files, { count: 100 });
|
|
74
|
+
return linkedLibraryDirs(files);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function readRequirements(rootReal) {
|
|
78
|
+
const listed = join(rootReal, SANDBOX_REQUIREMENTS_FILE);
|
|
79
|
+
if (!existsSync(listed)) return [];
|
|
80
|
+
let real;
|
|
81
|
+
try {
|
|
82
|
+
real = realpathSync(listed);
|
|
83
|
+
} catch {
|
|
84
|
+
throw sandboxError('SecurityError', 'sandbox-requirements.txt 无法读取。');
|
|
85
|
+
}
|
|
86
|
+
const fromRoot = relative(rootReal, real);
|
|
87
|
+
if (!fromRoot || fromRoot.startsWith('..') || isAbsolute(fromRoot)) {
|
|
88
|
+
throw sandboxError('SecurityError', 'sandbox-requirements.txt 不在工程目录内。');
|
|
89
|
+
}
|
|
90
|
+
return parseSandboxRequirements(readFileSync(real, 'utf8'));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function certificatePaths() {
|
|
94
|
+
return CERT_DIRECTORIES.filter((dir) => existsSync(dir));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async function installPackages({ containment, python, prefix, interpreterLibs, cacheHome, venvDir, venvPython, packages, tmp }) {
|
|
98
|
+
mkdirSync(cacheHome, { recursive: true });
|
|
99
|
+
const stampFile = join(cacheHome, 'requirements.sha256');
|
|
100
|
+
const stamp = requirementsStamp(python, packages);
|
|
101
|
+
if (existsSync(stampFile) && existsSync(venvPython) && readFileSync(stampFile, 'utf8') === stamp) {
|
|
102
|
+
return { note: '' };
|
|
103
|
+
}
|
|
104
|
+
const filesystem = {
|
|
105
|
+
readonlyPaths: [prefix, ...interpreterLibs, ...certificatePaths()],
|
|
106
|
+
readwritePaths: [cacheHome, tmp],
|
|
107
|
+
};
|
|
108
|
+
const environment = sandboxEnvironment({ tmp, pathDirs: [dirname(python)] });
|
|
109
|
+
const pipBin = process.platform === 'win32' ? join(venvDir, 'Scripts', 'pip.exe') : join(venvDir, 'bin', 'pip');
|
|
110
|
+
if (!existsSync(pipBin)) {
|
|
111
|
+
const created = await contained(
|
|
112
|
+
containment,
|
|
113
|
+
[quoteArg(python), '-I', '-m', 'venv', '--clear', quoteArg(venvDir)].join(' '),
|
|
114
|
+
{ filesystem, environment, timeoutMs: INSTALL_TIMEOUT_MS, workingDirectory: cacheHome }
|
|
115
|
+
);
|
|
116
|
+
if ((created.exitCode ?? 1) !== 0) return { failure: created };
|
|
117
|
+
}
|
|
118
|
+
const proxy = await startPypiProxy();
|
|
119
|
+
try {
|
|
120
|
+
const installed = await contained(
|
|
121
|
+
containment,
|
|
122
|
+
[
|
|
123
|
+
quoteArg(venvPython),
|
|
124
|
+
'-m',
|
|
125
|
+
'pip',
|
|
126
|
+
'install',
|
|
127
|
+
'--disable-pip-version-check',
|
|
128
|
+
'--no-input',
|
|
129
|
+
'--progress-bar',
|
|
130
|
+
'off',
|
|
131
|
+
'--no-cache-dir',
|
|
132
|
+
'--only-binary=:all:',
|
|
133
|
+
...packages.map(quoteArg),
|
|
134
|
+
].join(' '),
|
|
135
|
+
{
|
|
136
|
+
filesystem,
|
|
137
|
+
network: {
|
|
138
|
+
// networkProxy 要求 egress 默认拒绝且不能再写 allow 规则。
|
|
139
|
+
// Seatbelt 上要连本机代理,必须同时把 ingress 默认和 hostLoopback 设为允许。
|
|
140
|
+
egress: { default: 'deny' },
|
|
141
|
+
ingress: { default: 'allow', hostLoopback: 'allow' },
|
|
142
|
+
runtimeConfig: { networkProxy: proxy.url },
|
|
143
|
+
},
|
|
144
|
+
environment: {
|
|
145
|
+
...environment,
|
|
146
|
+
HTTPS_PROXY: proxy.url,
|
|
147
|
+
HTTP_PROXY: proxy.url,
|
|
148
|
+
https_proxy: proxy.url,
|
|
149
|
+
http_proxy: proxy.url,
|
|
150
|
+
PIP_INDEX_URL: 'https://pypi.org/simple',
|
|
151
|
+
...(existsSync('/etc/ssl/cert.pem') ? { SSL_CERT_FILE: '/etc/ssl/cert.pem' } : {}),
|
|
152
|
+
},
|
|
153
|
+
timeoutMs: INSTALL_TIMEOUT_MS,
|
|
154
|
+
workingDirectory: cacheHome,
|
|
155
|
+
}
|
|
156
|
+
);
|
|
157
|
+
if ((installed.exitCode ?? 1) !== 0) return { failure: installed };
|
|
158
|
+
writeFileSync(stampFile, stamp);
|
|
159
|
+
return { note: `已安装 ${packages.join(' ')}\n${truncateOutput(installed.stderr || '')}` };
|
|
160
|
+
} finally {
|
|
161
|
+
await proxy.close();
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const DEFAULT_PYTHONS = {
|
|
166
|
+
darwin: ['/opt/homebrew/bin/python3', '/usr/local/bin/python3'],
|
|
167
|
+
linux: ['/usr/bin/python3', '/usr/local/bin/python3'],
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* 宿主没指定 Python 时找一个系统解释器。macOS 的 /usr/bin/python3 是 xcrun 跳板,
|
|
172
|
+
* 它要写沙箱外的缓存,所以换成它指向的真实解释器。
|
|
173
|
+
*/
|
|
174
|
+
export function defaultPython() {
|
|
175
|
+
for (const candidate of DEFAULT_PYTHONS[process.platform] ?? []) {
|
|
176
|
+
if (existsSync(candidate)) return realpathSync(candidate);
|
|
177
|
+
}
|
|
178
|
+
return process.platform === 'darwin' ? xcrunTarget('python3') : undefined;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function xcrunTarget(name) {
|
|
182
|
+
try {
|
|
183
|
+
const found = execFileSync('/usr/bin/xcrun', ['-f', name], { encoding: 'utf8', timeout: 5000 }).trim();
|
|
184
|
+
return found && existsSync(found) ? realpathSync(found) : undefined;
|
|
185
|
+
} catch {
|
|
186
|
+
return undefined;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** 宿主传来 /usr/bin/python3 这类 xcrun 跳板时,换成它指向的解释器。 */
|
|
191
|
+
function unwrapXcrunShim(pythonReal) {
|
|
192
|
+
if (process.platform !== 'darwin' || dirname(pythonReal) !== '/usr/bin') return pythonReal;
|
|
193
|
+
return xcrunTarget(basename(pythonReal)) ?? pythonReal;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* 准备宿主指定的 Python。工程里有 sandbox-requirements.txt 时先在受限网络下装进缓存 venv。
|
|
198
|
+
* 返回脚本要用的解释器和它需要的只读目录;安装失败时返回 failure。
|
|
199
|
+
*/
|
|
200
|
+
export async function preparePython({ containment, rootReal, python, tmp }) {
|
|
201
|
+
let pythonReal;
|
|
202
|
+
try {
|
|
203
|
+
pythonReal = unwrapXcrunShim(realpathSync(python));
|
|
204
|
+
} catch {
|
|
205
|
+
throw sandboxError('NotAllowedError', 'Python 不可用。');
|
|
206
|
+
}
|
|
207
|
+
if (!statSync(pythonReal).isFile()) throw sandboxError('NotAllowedError', 'Python 不可用。');
|
|
208
|
+
const prefix = dirname(dirname(pythonReal));
|
|
209
|
+
const interpreterLibs = interpreterLibraryDirs(pythonReal, prefix);
|
|
210
|
+
const packages = readRequirements(rootReal);
|
|
211
|
+
if (packages.length === 0) {
|
|
212
|
+
return { python: pythonReal, readonlyPaths: [prefix, ...interpreterLibs], note: '' };
|
|
213
|
+
}
|
|
214
|
+
const cacheHome = join(cacheBase(), rootHash(rootReal, pythonReal));
|
|
215
|
+
const venvDir = join(cacheHome, 'venv');
|
|
216
|
+
const venvPython =
|
|
217
|
+
process.platform === 'win32' ? join(venvDir, 'Scripts', 'python.exe') : join(venvDir, 'bin', 'python');
|
|
218
|
+
const installed = await installPackages({
|
|
219
|
+
containment,
|
|
220
|
+
python: pythonReal,
|
|
221
|
+
prefix,
|
|
222
|
+
interpreterLibs,
|
|
223
|
+
cacheHome,
|
|
224
|
+
venvDir,
|
|
225
|
+
venvPython,
|
|
226
|
+
packages,
|
|
227
|
+
tmp,
|
|
228
|
+
});
|
|
229
|
+
if (installed.failure) return { failure: installed.failure };
|
|
230
|
+
const venvNativeFiles = [];
|
|
231
|
+
nativeFilesUnder(venvDir, venvNativeFiles, { count: 200 });
|
|
232
|
+
return {
|
|
233
|
+
python: venvPython,
|
|
234
|
+
venv: true,
|
|
235
|
+
readonlyPaths: [prefix, ...interpreterLibs, ...linkedLibraryDirs(venvNativeFiles), venvDir],
|
|
236
|
+
note: installed.note,
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export function installFailureResponse(failure) {
|
|
241
|
+
return {
|
|
242
|
+
exitCode: failure.exitCode ?? 1,
|
|
243
|
+
timedOut: Boolean(failure.timedOut),
|
|
244
|
+
stdout: truncateOutput(failure.stdout),
|
|
245
|
+
stderr: truncateOutput(`依赖安装失败。\n${failure.stderr || ''}\n${failure.stdout || ''}`),
|
|
246
|
+
warnings: failure.warnings ?? [],
|
|
247
|
+
};
|
|
248
|
+
}
|
package/src/request.mjs
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
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
|
+
}
|