Skip to content

Instantly share code, notes, and snippets.

@Kcko
Last active April 12, 2025 07:02
Show Gist options
  • Save Kcko/990fc0765d0d5ecb325a74746b129f2f to your computer and use it in GitHub Desktop.
Save Kcko/990fc0765d0d5ecb325a74746b129f2f to your computer and use it in GitHub Desktop.
// https://javascript.plainenglish.io/the-battle-of-isolation-proxy-vs-web-workers-vs-iframe-in-frontend-development-%EF%B8%8F-3eaeef99a11d
// 1
const sandbox = new Proxy(window, {
get(target, key) {
if (key === 'document') {
throw new Error('No access to DOM!');
}
return Reflect.get(target, key);
},
set(target, key, value) {
if (key === 'location') return false;
return Reflect.set(target, key, value);
}
});
// Running plugin code in sandbox
(function(window) {
try {
window.document.title = 'Hacked!'; // Triggers exception
} catch (err) {
console.error('Illegal operation intercepted:', err);
}
})(sandbox);
// 2
const audit = new Proxy(console, {
get(target, key) {
if (key === 'log') return (...args) => {
recordLog(args); // Log recording
target[key](...args);
};
return target[key];
}
});
sandbox.console = audit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment