Last active
April 12, 2025 07:02
-
-
Save Kcko/990fc0765d0d5ecb325a74746b129f2f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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