npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

expression-sandbox

v1.1.1

Published

A small tool for evaluating JavaScript code in a secure sandbox, for Node.js or the modern browser.

Downloads

22

Readme

expression-sandbox Build Status

This lets you compile and run someone else's arbitrary JavaScript safely within your own JavaScript program.

UPDATE:

This was more of a fun/experimental project. If anyone would like to take over the repo, just ask. Otherwise, it will remain archived.

Installation

npm install --save expression-sandbox

Usage

var compiler = require('expression-sandbox');
var code = compiler('Math.round(a + b / 3)');

var result = code({a: 7, b: 5, Math});
console.log(result); // => 9

What kinds of things are protected against?

If you don't pass anything into the sandbox context, then you're protected EVERYWHERE!

From inside the sandbox...

  • You can't modify anything that wasn't created in the sandbox.
    • you can't modify native prototypes
    • you can't modify any object that's reachable from the native prototypes
    • you can't modify anything that was passed into the sandbox
    • you can't modify the sandbox context object itself
  • You can't access the global object.
    • This is specifically forbidden. You're not allowed to access it even if it's passed into a sandbox explicitly
  • You can't compile code with Function or eval.
  • You can't access properties that start with _ on objects that are passed into the sandbox.
  • You can't access any scope anywhere except for what is specifically passed into the sandbox. Anything passed into the sandbox can be accessed, but not modified.
  • You can only return primitive values.
    • You can't return objects
    • You can't return functions

What kinds of things are NOT protected against?

Any sensitive information that is passed into the sandbox can be accessed by the unsecure code (unless that information is behind a property that starts with _)

Am I wrong?

If you think there are vulnerabilities in the sandbox that I didn't think of, please create a Github issue to bring them to my attention.

Caveats

This package replaces some built-in objects with Proxies. In rare cases, this can cause odd behvaior with with === operator, if you cache one of these objects before this package is loaded. For this reason, it is recommended that this package is loaded before any other JavaScript code runs.

There's one more caveat.

If you pass an object into a sandbox, and then you pass that object into a function that was created outside the sandbox, the function will receive a proxy of the original object. To test if that proxy represents a specific non-proxy object, you can use compiler.equals(), as shown below.

var compiler = require('expression-sandbox');
var obj = {};

function badTest(a) {
	return a === obj;
}

function goodTest(a) {
	return compiler.equals(a, obj);
}

var context = {obj, badTest, goodTest};
compiler('badTest(obj)')(context); // Returns false
compiler('goodTest(obj)')(context); // Returns true

But don't worry, 99% of the time this issue will never come up.

Why the fork?

The opinions behind nx-compile differ from the opinions behind expression-sandbox. For example, expression-sandbox is not interested in providing a non-secure version of itself. The "small modules" rule suggests that such functionality should be in its own module. Additionally, expression-sandbox aims to secure anything you put into the sandbox—not just globals and native prototypes. Finally, expression-sandbox keeps its independence from nx-framework, to be as generic as possible.