Skip to content

Instantly share code, notes, and snippets.

@semlinker
Created October 18, 2022 10:48
Show Gist options
  • Select an option

  • Save semlinker/15511bb4450f41fc54b21858fa87f970 to your computer and use it in GitHub Desktop.

Select an option

Save semlinker/15511bb4450f41fc54b21858fa87f970 to your computer and use it in GitHub Desktop.

Revisions

  1. semlinker created this gist Oct 18, 2022.
    17 changes: 17 additions & 0 deletions sandbox.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    function sandbox(code) {
    code = "with (sandbox) {" + code + "}";
    const fn = new Function("sandbox", code);

    return function (sandbox) {
    const sandboxProxy = new Proxy(sandbox, {
    has(target, key) {
    return true;
    },
    get(target, key) {
    if (key === Symbol.unscopables) return undefined;
    return target[key];
    },
    });
    return fn(sandboxProxy);
    };
    }