Skip to content

Instantly share code, notes, and snippets.

@OMGZui
Created October 26, 2022 11:23
Show Gist options
  • Save OMGZui/a278e1e202c147bd319736c4018bc9d8 to your computer and use it in GitHub Desktop.
Save OMGZui/a278e1e202c147bd319736c4018bc9d8 to your computer and use it in GitHub Desktop.
var foo = "foo1";
const ctx = {
func: variable => {
console.log(variable);
}
};
function withedYourCode(code) {
code = "with(shadow) {" + code + "}";
return new Function("shadow", code);
}
const access_white_list = ["func"];
const code = `func(foo)`;
const ctxProxy = new Proxy(ctx, {
has: (target, prop) => {
if (access_white_list.includes(prop)) {
return target.hasOwnProperty(prop);
}
if (!target.hasOwnProperty(prop)) {
throw new Error(`Not found - ${prop}!`);
}
return true;
}
});
function littlePoorSandbox(code, ctx) {
withedYourCode(code).call(ctx, ctx);
}
littlePoorSandbox(code, ctxProxy);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment