SandboxJS icon indicating copy to clipboard operation
SandboxJS copied to clipboard

Application crash after the result is returned

Open oguzhaneren opened this issue 1 year ago • 1 comments

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

oguzhaneren avatar Feb 22 '24 06:02 oguzhaneren

Working fine for me without changing anything? Can you check again

image

the-e3n avatar Mar 12 '24 17:03 the-e3n