• Stars
    star
    836
  • Rank 53,047 (Top 2 %)
  • Language
    JavaScript
  • License
    ISC License
  • Created over 14 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A nifty JavaScript sandbox for Node.js

🏖️ sandbox

A nifty JavaScript sandbox for Node.js.
Latest Release Build Status

What is it?

It can...

  • Be used to execute untrusted code
  • Timeout long-running code and infinite loops
  • Handle errors gracefully
  • Run restricted code
  • Pass rich data structures back
  • Capture console output
const s = new Sandbox();

s.eval("const o = { answer: 4.2 }; o", function(err, res) {
  console.log("The answer is: %d", res.answer * 10); // The answer is: 42
});

Installation

npm install --save sandbox

By default the package will attempt to download the corresponding binary release for your platform. If you wish to skip this action you can set the SANDBOX_SKIP_DOWNLOAD environment variable:

env SANDBOX_SKIP_DOWNLOAD=1 npm install --save sandbox

Usage

NOTE: As it stands, only values that can be serialized to JSON may be returned.

About

sandbox is a tool to allow safe local execution of JavaScript.

The previous version of sandbox attempted to accomplish this by spawning a child process and running the untrusted code in a new Node.js context with known exploits patched. Unfortunately this didn't work well from a security standpoint and it became increasingly difficult to keep up with the whack-a-mole of security vulnerabilities. In fact, the previous version still has unaddressed vulnerabilities that are impossible to patch with that architecture.

The current version of sandbox takes a new approach—it embeds a JavaScript interpreter in the library which executes code in a separate context in another thread. This is made possible with the help of two incredible projects:

  • Boa - a JavaScript interpreter written in Rust
  • Neon - a library for interfacing with Node-API

The major drawback to this approach is that it either requires the user to build the sandbox locally from source (which requires the user to have the rust build tools onhand) or provide pre-built binaries for every platform would need to be provided. In order to make things a little more seamless for users, we've opted to provided pre-built binaries for the following platforms:

  • Linux x86_64
  • MacOS arm64
  • MacOS x86_64
  • Windows x86_64

Given these targets we should be able to meet the needs of most users.

Building

First, if you don't already have the rust toolchain installed you can follow the instructions for installing rustup:

Install rust.

Next, running npm run build will attempt to build the project with cargo and move the compiled binary to ./index.node.

At this point you should be able to work with sandbox and run the tests: npm test.

Learn More

To learn more about Neon, see the Neon documentation.

To learn more about Rust, see the Rust documentation.

To learn more about Node, see the Node documentation.

More Repositories

1

dotfiles

Configurations for the tools I use every day
Emacs Lisp
1,040
star
2

Levenshtein

Javascript implementation of the L-diggity.
JavaScript
278
star
3

IRC-js

The best IRC library for node.js
JavaScript
179
star
4

WAT

LOLWAT?
107
star
5

Jerk

Stupidly simple IRC bots in Javascript.
JavaScript
106
star
6

protobot

Prototype IRC Bot
JavaScript
57
star
7

haml.mode

HAML syntax definition for Coda & SubEthaEdit
44
star
8

texpand

Textarea auto-expander.
JavaScript
33
star
9

CLJBIN

A Clojure pastebin with code evaluation.
Clojure
33
star
10

TRNSFR

A Transmit 4-inspired multi progress bar.
JavaScript
22
star
11

butts

#butts
21
star
12

JS-strftime

Ruby-like strftime for javascript, because date-formatting is a pain.
JavaScript
18
star
13

hinttext

Hint text the right way.
JavaScript
14
star
14

peg.vim

PEG Syntax for Vim
Vim Script
14
star
15

coloured

Pretty colours in your terminal.
JavaScript
9
star
16

twatlr.com

Twatlr web service
JavaScript
8
star
17

molotov

The exciting colour scheme that will make you code good™
Vim Script
6
star
18

Get-Off-My-Lawn

Safari version of Mike Taylor's "Get Off My Lawn" extension for Opera.
JavaScript
5
star
19

Rumen

Data extraction.
Ruby
5
star
20

butt.zone

What a nice place for a website.
HTML
5
star
21

oxpecker

Sandboxed javascript in ruby.
Ruby
4
star
22

docker-mongosh

Docker image for mongosh
Dockerfile
4
star
23

pancake

A user style to simplify waffle.io
CSS
4
star
24

clavatar-js

A ClojureScript port of Rayne's clavatar. This is a library to generate Gravatar URLs from email addresses.
JavaScript
4
star
25

snack

Please ignore
C
3
star
26

akahn-slash-asterisk

akahn/*
3
star
27

dom.js

Functional DOM-ination
JavaScript
3
star
28

Twatlr

Easy Twitter threads.
Racket
3
star
29

crapshot

crappy web screenshots (html → pdf)
C++
2
star
30

ring-cssgen

Ring middleware to automatically compile and regenerate your cssgen stylesheets.
Clojure
2
star
31

proto-misc

Miscellaneous tasty paste.
JavaScript
2
star
32

SCRLLR

Make great scrollings of the elements!!!
JavaScript
2
star
33

readthefuckingspec

Read the fucking spec.
Ruby
2
star
34

jennmoney.biz

the official site of olive garden's favorite twitter account
1
star
35

musex

Ruby
1
star
36

gf3

Private!
1
star
37

Resume

Curriculum Vitae
1
star
38

api-extractor-issue

TypeScript
1
star
39

ember-load-helpers

Autoload and register ember helpers
JavaScript
1
star