Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Application crash after the result is returned #23

Open
oguzhaneren opened this issue Feb 22, 2024 · 1 comment
Open

Application crash after the result is returned #23

oguzhaneren opened this issue Feb 22, 2024 · 1 comment

Comments

@oguzhaneren
Copy link

oguzhaneren commented Feb 22, 2024

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
@the-e3n
Copy link

the-e3n commented Mar 12, 2024

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

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants