名称: 插件沙箱设置 描述: 配置插件沙箱环境,使用vm2或isolated-vm实现安全的插件执行。 允许工具: 读取, 写入, 编辑, Bash, Glob, Grep
插件沙箱设置
配置插件沙箱环境以增强安全性。
生成模式
import ivm from 'isolated-vm';
export async function runInSandbox(code: string, context: Record<string, unknown>) {
const isolate = new ivm.Isolate({ memoryLimit: 128 });
const vmContext = isolate.createContextSync();
const jail = vmContext.global;
for (const [key, value] of Object.entries(context)) {
jail.setSync(key, new ivm.ExternalCopy(value).copyInto());
}
const script = isolate.compileScriptSync(code);
const result = await script.run(vmContext, { timeout: 5000 });
isolate.dispose();
return result;
}
目标流程
- 插件架构实现
- MCP服务器安全加固