fork download
  1. "use strict";
  2.  
  3. var check = (function(glob) {
  4. return function(code) {
  5. var contextProps = {};
  6. for (var key in glob) {
  7. console.log("guarding property: " + key);
  8. (function(key) {
  9. contextProps[key] = { get: function() { throw new Error("Trying to access global variable: " + key); } };
  10. })(key);
  11. }
  12. var context = {};
  13. Object.defineProperties(context, contextProps);
  14. var func = new Function('context', 'with(context) {' + code + '};');
  15. func(context);
  16. };
  17. })(global);
  18.  
  19. console.log("test");
  20. check("setTimeout(function() { console.log('error'); }, 0);");
Runtime error #stdin #stdout #stderr 0.07s 31048KB
stdin
Standard input is empty
stdout
test
guarding property: ArrayBuffer
guarding property: Int8Array
guarding property: Uint8Array
guarding property: Uint8ClampedArray
guarding property: Int16Array
guarding property: Uint16Array
guarding property: Int32Array
guarding property: Uint32Array
guarding property: Float32Array
guarding property: Float64Array
guarding property: DataView
guarding property: global
guarding property: process
guarding property: GLOBAL
guarding property: root
guarding property: Buffer
guarding property: setTimeout
guarding property: setInterval
guarding property: clearTimeout
guarding property: clearInterval
guarding property: console
stderr
Error: Trying to access global variable: setTimeout
    at Object.contextProps.(anonymous function).get (/home/0Ngcnl/prog.js:9:55)
    at eval (eval at <anonymous> (/home/0Ngcnl/prog.js:14:16))
    at /home/0Ngcnl/prog.js:15:5
    at Object.<anonymous> (/home/0Ngcnl/prog.js:20:1)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)