Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs: Is it possible to eval js code using runInNewContext and limit its execution time by a timeout?

Tags:

node.js

I'd like to execute an untrusted js code using runInNewContext in node.js but as far as I see there is no way to limit its execution time. Also it is a sync operation. is there a way to set timeout on it or async version of it that will allow me to control its execution from 'outside'?

UPDATE: running in an external process is no good:

  • takes too much resources
  • more importantly, I need the code to have access to my data/code though sandbox environment
like image 764
Vitaly Kushner Avatar asked Jun 30 '11 02:06

Vitaly Kushner


People also ask

Does node js have setTimeout?

The setTimeout function is found in the Timers module of Node. js.

Is node js JavaScript runtime?

To summarize, Node. js is a cross-platform JavaScript runtime environment for servers and applications. It is built on a single-threaded, non-blocking event loop, the Google Chrome V8 JavaScript engine, and a low-level I/O API. Various techniques, including the cluster module, allow Node.

What is Sandbox in node js?

VM is a simple sandbox to synchronously run untrusted code without the require feature. Only JavaScript built-in objects and Node's Buffer are available. Scheduling functions ( setInterval , setTimeout and setImmediate ) are not available by default.


1 Answers

Run script in external process using dnode or child_process.fork, set deadline timer and kill process if timeout reached or timer if script finished.

like image 88
Andrey Sidorov Avatar answered Sep 28 '22 13:09

Andrey Sidorov