Skip to main content

A binding to Node.js + vm2, helps you execute JavaScript safely.

Project description

Documentation Status test

A Python 3 to Node.js + vm2 binding, helps you execute JavaScript safely.

vm2

vm2 is a node module to create real sandbox in node. The official node API vm, can only create isolate context and doesn’t prevent harmful code to damage your computer.

How it works

The module launchs a Node.js REPL server, which can be communicated with JSON. All JavaScript code are encoded in JSON and sent to the server. After the server executing the code in vm2, the result is sent back to Python.

Install

You need Node.js.

https://nodejs.org/

Install node_vm2 from pypi wheel.

pip install node_vm2

Also make sure you have node executable in PATH, or you can specify the executable with environment variable NODE_EXECUTABLE.

Additionally, you will need npm to build node_vm2 from source.

Usage

Most of the APIs are bound to vm2.

Simple eval:

from node_vm2 import eval

print(eval("['foo', 'bar'].join()"))

Use VM:

from node_vm2 import VM

with VM() as vm:
   vm.run("""
      var sum = 0, i;
      for (i = 0; i < 10; i++) sum += i;
   """)
   print(vm.run("sum"))

Use NodeVM:

from node_vm2 import NodeVM

js = """exports.greet = name => console.log(`Hello ${name}!`);"""

with NodeVM.code(js) as module:
   module.call_member("greet", "John")

It is possible to do async task with Promise:

from datetime import datetime
from node_vm2 import NodeVM

js = """
exports.test = () => {
   return new Promise(resolve => {
      setTimeout(() => {
         resolve("hello")
      }, 3000);
   });
};
"""
with NodeVM.code(js) as module:
   print(datetime.now())
   print(module.call_member("test"))
   print(datetime.now())

If you like to allow the VM to crash your server (e.g. process.exit()), you should create the VM in a separate server so it won’t affect other VMs:

from node_vm2 import VMServer, VM

with VMServer() as server:
   with VM(server=server) as vm:
      # now the vm is created in a new server
      print(vm.run("1 + 2 + 3"))

API reference

http://node-vm2.readthedocs.io/

Changelog

  • 0.4.4 (Mar 14, 2022)

    • Update vm2 to 3.9.9.

  • 0.4.3 (Feb 15, 2022)

    • Update vm2 to 3.9.7.

  • 0.4.2 (Feb 9, 2022)

    • Update vm2 to 3.9.6.

    • Fix: filename is optional.

  • 0.4.1 (Oct 20, 2021)

    • Update vm2 to 3.9.5.

  • 0.4.0 (Sep 2, 2021)

    • Update vm2 to 3.9.3.

    • Change: throw VMError when failed running node.

  • 0.3.7 (Mar 23, 2020)

    • Update vm2 to 3.9.0.

  • 0.3.6 (Apr 22, 2019)

    • Update vm2 to 3.8.0. Fix security issues.

  • 0.3.5 (Feb 10, 2019)

    • Update vm2 to 3.6.10. Fix security issues.

  • 0.3.4 (Aug 10, 2018)

    • Update vm2 to 3.6.3. Fix security issues.

  • 0.3.3 (Jul 23, 2018)

    • Fix: don’t bundle dev dependencies.

  • 0.3.2 (Jul 23, 2018)

    • Fix: getting a freezed object would crash the server.

    • Update vm2 to 3.6.2. Fix security issues.

  • 0.3.1 (Apr 25, 2017)

    • Add command arg to VMServer.

    • Fix: A dead default server is created if process spawning failed.

  • 0.3.0 (Apr 23, 2017)

    • Change: use event queue to handle console redirects.

    • Reconize object thrown by VM which doesn’t inherit built-in Error.

  • 0.2.0 (Mar 25, 2017)

    • Drop NodeBridge.

    • Add VMServer.

    • Make all VMs share a default VMServer.

    • Method rename: VM.connect -> VM.create, VM.close -> VM.destroy.

  • 0.1.0 (Mar 23, 2017)

    • First release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

node_vm2-0.4.4.tar.gz (22.9 kB view details)

Uploaded Source

Built Distribution

node_vm2-0.4.4-py3-none-any.whl (218.7 kB view details)

Uploaded Python 3

File details

Details for the file node_vm2-0.4.4.tar.gz.

File metadata

  • Download URL: node_vm2-0.4.4.tar.gz
  • Upload date:
  • Size: 22.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for node_vm2-0.4.4.tar.gz
Algorithm Hash digest
SHA256 e129c2a7a7eee9ccbcae27182d000e70a4d87081e2050723a3e2cf8ffe721211
MD5 b8b29348f6f5b7e5b5425ef387a31809
BLAKE2b-256 40a45ea9b33aced26d469da5e1abd0ff7e10fc748ffd4c8228c2c71afeda3e58

See more details on using hashes here.

File details

Details for the file node_vm2-0.4.4-py3-none-any.whl.

File metadata

  • Download URL: node_vm2-0.4.4-py3-none-any.whl
  • Upload date:
  • Size: 218.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for node_vm2-0.4.4-py3-none-any.whl
Algorithm Hash digest
SHA256 c62da6fc6792d6b7a81c8bbf5f13500b359f35d984d837cae3be5cbf676a0169
MD5 11260d6201d689b6af99d56393c623a0
BLAKE2b-256 a9ed4949f9d8b151f90d3067ea2ca8f47c6bd88961aac37442f643803f0f8145

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page