SandboxJS
SandboxJS copied to clipboard
Application crash after the result is returned
I use SandboxJS with following configuration:
class SecureEval {
private readonly sandbox: Sandbox = new Sandbox();
constructor(whitelistedItems: Record<string, unknown> = {}) {
const prototypeWhitelist = Sandbox.SAFE_PROTOTYPES;
Object.keys(whitelistedItems).forEach((key) => {
prototypeWhitelist.set(key, new Set());
});
const globals = { ...Sandbox.SAFE_GLOBALS, ...whitelistedItems };
this.sandbox = new Sandbox({
prototypeWhitelist,
globals,
executionQuota: 100000n,
});
}
build<TResponse>(
code: string
): (...parameters) => Promise<TResponse | undefined> {
return async (params) => {
try {
const script = `
return (async () => await (${code})())();
`;
const execAsync = this.sandbox.compileAsync(script);
const result = await execAsync(params).run();
return result as TResponse;
} catch (error) {
console.warn(error);
return undefined;
}
};
}
}
async function main() {
const builder = new SecureEval();
const fn = builder.build(`
async function handle(){
const res = await Promise.resolve(response.items.join(","));
return res;
}
`);
try {
const result = await fn({ response: {
items: ["a","b","c"]
} });
await fn;
} catch (err) {
console.log(err);
}
}
in main function, SandboxJS returns result but after then application crash with following error. This error cannot be handled with try/catch block.
Waiting for the debugger to disconnect...
/Users/xyz/sandboxjs-test/node_modules/@nyariv/sandboxjs/dist/node/Sandbox.js:1635
throw new ReferenceError(`${this.prop} is not defined`);
^
ReferenceError: response is not defined
at Prop.get (/Users/xyz/sandboxjs-test/node_modules/@nyariv/sandboxjs/dist/node/Sandbox.js:1635:19)
at execAsync (/Users/xyz/sandboxjs-test/node_modules/@nyariv/sandboxjs/dist/node/Sandbox.js:2645:43)
at /Users/xyz/sandboxjs-test/node_modules/@nyariv/sandboxjs/dist/node/Sandbox.js:2637:42
at /Users/xyz/sandboxjs-test/node_modules/@nyariv/sandboxjs/dist/node/Sandbox.js:2596:9
at new Promise (<anonymous>)
at asyncDone (/Users/xyz/sandboxjs-test/node_modules/@nyariv/sandboxjs/dist/node/Sandbox.js:2595:15)
at execAsync (/Users/xyz/sandboxjs-test/node_modules/@nyariv/sandboxjs/dist/node/Sandbox.js:2637:25)
at /Users/xyz/sandboxjs-test/node_modules/@nyariv/sandboxjs/dist/node/Sandbox.js:2637:42
at /Users/xyz/sandboxjs-test/node_modules/@nyariv/sandboxjs/dist/node/Sandbox.js:2596:9
at new Promise (<anonymous>)
Node.js v20.11.0
Working fine for me without changing anything? Can you check again