5.3 脚本沙箱与权限
沙箱是脚本调用编辑器的唯一入口:白名单里的 API 才放行,其余一律抛错;每次放行记一条审计。撤销沙箱后所有调用都失败,用于卸载扩展时立刻切断其能力,而不是等它自己停下来。
本章与站内既有内容的分工
| 主题 | 站内既有内容 | 本节做什么 |
|---|---|---|
| 扩展安全 | 本卷 1.3 | 1.3 管权限授予;本节管每次调用的放行与审计 |
| 脚本 API | 本卷 5.1 | 5.1 裁剪 API 形状;本节在执行层做二次校验 |
| 全卷脉络 | 本卷目录 | 本节收口脚本 API 章 |
从零实现:脚本沙箱与权限
call 依次做四件事:撤销检查、API 名合法性检查、白名单检查、记审计并执行。API 名用正则限制为「字母开头、字母数字与点」,既防止空白与命令注入,也保证审计日志可读。通配符 * 只放宽白名单,不放宽撤销与命名规则。
接口与执行边界
ScriptSandbox 示例检查调用名称与授权,然后调用宿主传入的回调。审计只记录通过检查的调用,撤销影响后续调用。执行不受信任代码需要独立的运行环境和受控接口。Node.js 也明确指出,JavaScript 的 vm 上下文不能作为安全隔离边界。 Node.js vm
可运行产物:6 条用例
下面是 src/sandbox.ts,层级 L1,完整可编译。运行命令 node --test test/05-3-script-sandbox-permissions.test.ts:
// file: src/sandbox.ts
// tier: L1
// verify: node --test test/05-3-script-sandbox-permissions.test.ts
//
// 卷四(第五章):脚本沙箱与权限。
// 沙箱是脚本调用编辑器的唯一入口:白名单里的 API 才放行,其余一律抛错;
// 每次放行记一条审计(API 名 + 递增时钟)。撤销沙箱后所有调用都失败,
// 用于卸载扩展时立刻切断其能力。
export interface SandboxCall {
api: string;
at: number;
}
const API_NAME = /^[a-z][a-z0-9.]*$/i;
export class ScriptSandbox {
#allowed: Set<string>;
#audit: SandboxCall[] = [];
#clock = 0;
#revoked = false;
constructor(allowed: string[]) {
this.#allowed = new Set(allowed);
}
call<T>(api: string, fn: () => T): T {
if (this.#revoked) throw new Error('沙箱已撤销');
if (!API_NAME.test(api)) throw new Error(`非法 API 名:${api}`);
if (!this.#allowed.has('*') && !this.#allowed.has(api)) {
throw new Error(`API 未授权:${api}`);
}
this.#clock += 1;
this.#audit.push({ api, at: this.#clock });
return fn();
}
revoke(): void {
this.#revoked = true;
}
audit(): string[] {
return this.#audit.map((c) => c.api);
}
isRevoked(): boolean {
return this.#revoked;
}
}
审计记录在放行时而不是执行后,因此即使被调用函数抛错,也能看到「它尝试过」。撤销是单向开关,不做恢复——重新启用应当创建新沙箱,避免旧脚本持有旧引用继续工作。
下面是 test/05-3-script-sandbox-permissions.test.ts,层级 L1:
// file: test/05-3-script-sandbox-permissions.test.ts
// tier: L1
// verify: node --test test/05-3-script-sandbox-permissions.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { ScriptSandbox } from '../src/sandbox.ts';
test('白名单内的调用放行并返回结果', () => {
const box = new ScriptSandbox(['editor.selection']);
assert.equal(box.call('editor.selection', () => 42), 42);
});
test('未授权 API 抛错且不执行', () => {
const box = new ScriptSandbox(['editor.selection']);
let ran = false;
assert.throws(
() => box.call('fs.write', () => {
ran = true;
}),
/API 未授权:fs.write/,
);
assert.equal(ran, false);
});
test('每次放行记录一条审计', () => {
const box = new ScriptSandbox(['ui', 'editor.selection']);
box.call('ui', () => null);
box.call('editor.selection', () => null);
assert.deepEqual(box.audit(), ['ui', 'editor.selection']);
});
test('通配符放行全部合法 API', () => {
const box = new ScriptSandbox(['*']);
assert.equal(box.call('anything.here', () => 'ok'), 'ok');
assert.deepEqual(box.audit(), ['anything.here']);
});
test('撤销沙箱后拒绝所有调用', () => {
const box = new ScriptSandbox(['ui']);
box.revoke();
assert.equal(box.isRevoked(), true);
assert.throws(() => box.call('ui', () => null), /沙箱已撤销/);
});
test('非法 API 名被拒绝且不记审计', () => {
const box = new ScriptSandbox(['*']);
assert.throws(() => box.call('rm -rf /', () => null), /非法 API 名/);
assert.deepEqual(box.audit(), []);
});
本机实测(Node v26.5.0,node --test test/05-3-script-sandbox-permissions.test.ts):tests 6 · pass 6 · fail 0,耗时约 40 ms。
前两条钉放行与拒绝;第三条钉审计;第四条钉通配符;后两条钉撤销与非法 API 名。
小结
- 沙箱在调用点做白名单、审计与撤销三重控制。
- 非法 API 名在放行前拒绝,审计日志因此保持可读。
- 通配符只放宽白名单,不放宽撤销与命名规则。
- 撤销是单向的,重新启用应创建新沙箱。
阅读导航:上一节:5.2 事件订阅与变更回调 · 下一节:6.1 编译、模块替换与状态迁移 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。