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

byrnesjs

v0.1.1

Published

Package Sandboxing for NodeJS

Downloads

1

Readme

ByrnesJS

Package Sandboxing for NodeJS

When we build a NodeJS application we pull in all kinds of package dependencies to help us with all kinds of things. But how do we know that they are really doing what we think they're doing? How do you know that one of the 73 query string parsing packages you've included isn't secretly harvesting passwords and sending them to a malicious server somewhere? The fact is that unless you inspect every bit of code you import then you can't be sure.

Most packages have no need for any privileged access to the network or filesystem, so what if we could stop them having access, and only give access to the libraries that really need them?

For example, that query string parsing package doesn't need to access the network, the filesystem, or the shell, so let's just prevent it doing so.

This is where package sandboxing can help.

ByrnesJS is a library for NodeJS which will prevent any code accessing privileged operations, except for certain packages which you explicitly allow.

It allows to you only give permissions to access the filesystem, network, or shell to those libraries which should be accessing them. If a library tries to access something which hasn't been given permission to access then that access will be denied.

:warning: Note that this is experimental software. Its stability, efficacy, and performance have yet to be tested. Feel free to try it out though and raise a ticket or PR if you have any issues or suggestions.

Prerequisites

This library currently only supports Node 12.x and above

Installation

yarn

yarn add byrnesjs

npm

npm install --save byrnesjs

Usage

Calling init() on ByrnesJS SHOULD be the first thing your program does, before it loads any other libraries.

const byrnesjs = require("byrnesjs");

byrnesjs.init({
  rootDir: __dirname,
  logOnly: false,
  allow: [
    {
      module: "/", // This will trust all code in the current project (excluding node_modules). You probably always want this (you trust your own code, right?!).
      privileges: byrnesjs.PRIV_ALL, // This means all privileges
    },
    {
      module: "node_modules/somethingtrusted",
      privileges: byrnesjs.PRIV_FILESYSTEM, // This is trusted to only use the filesystem
    },
    {
      module: ["node_modules/somethingelsetrusted", "node_modules/anotherlib/"],
      privileges: [
        // These are trusted to use both the network and shell
        byrnesjs.PRIV_NETWORK,
        byrnesjs.PRIV_CHILD_PROCESS,
      ],
    },
  ],
});

Options

| Option | Type | Example | Default | Description | | --------------- | -------- | ----------------------------------- | --------------- | ---------------------------------------------------------------------------------------------------------- | | rootDir | String | __dirname | <required> | This is the root directory which will be used for path based allow entries. | | logOnly | boolean | true | false | If set to true then any violations will only be logged. If false then violations will throw an Error | | violationLogger | function | (message) => console.log(message) | console.error | A callback function to which violation messages will be sent. | | allow | Array | <see usage example> | [] | Sets the list of trusted libraries (and code) - see below |

Allow

This is the list of options within a definition within the 'allow' array

| Option | Type | Example | Default | Description | | ----------- | --------------- | -------------------------------------- | ----------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | module | String | Array | ['/', 'node_modules/trustedmodule/'] | <required> | Either a single string or array of strings to specify the module to allow. If it begins with node_modules then it is treated specially and will apply to that module. Otherwise it points to a path relative to rootDir. | | privileges | String | Array | ['fs', 'net] | <required> | Defines the privileges to be allowed. Should either be the string '*' which means allow all privileges, or an array containing one or more of the privileged module names: net, fs, or child_process | | alwaysAllow | boolean | true | false | Normally the entire call stack needs to be trusted in order for the call to be allowed. However, if one of the entries has alwaysAllow set to true then the call will be allowed even if there are untrusted entries further down the stack. This should be used sparingly as it allows a path for untrusted code to call privileged functions. |

Controlled Privileges

The privileges to which access can be controlled are:

| Module | Module ID | Indeintifier | Description | | --------------- | ---------------- | --------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | File System | fs | PRIV_FILESYSTEM | The fs module provides an API for interacting with the file system. | | Network | net | PRIV_NETWORK | The net module provides an asynchronous network API for creating stream-based TCP or IPC servers and clients. | | Child Processes | child_process | PRIV_CHILD_PROCESS | The child_process module provides the ability to spawn child processes. | | Virtual Machine | vm | PRIV_VM | The vm module enables compiling and running code within V8 Virtual Machine contexts. | | UDP/Datagram | dgram | PRIV_DGRAM | The dgram module provides an implementation of UDP Datagram sockets. | | DNS | dns | PRIV_DNS | The dns module enables name resolution. | | Worker Threads | worker_threads | PRIV_WORKER_THREADS | The worker_threads module enables the use of threads that execute JavaScript in parallel. | | Process | process | PRIV_PROCESS | The process object is a global that provides information about, and control over, the current Node.js process. Note that only some functions are overridden on this object. |

Note that when granting PRIV_NETWORK you will likely (but not necessarily) need to grant PRIV_DNS too, and vice versa.

How it works

ByrnesJS works by wrapping all the functions within the privileged modules with code which will check the caller of that function.

When one of those functions is called ByrnesJS will check the entire call stack to ensure that all of the code paths within the stack are allowed to call that privileged function.

Limitations

Due to being implemented as a pure Javascript library there are certain limitations to the current implementation:

  • Packages can include native code which can't be intercepted by this library. This would require some deeper support in the VM or OS to intercept.
  • While I think I've covered all the bases, because it's implemented at a high level there may be some ways I've missed for mailicious code to get hold of the original non-overidden functions. If you spot any ways around it then please raise an issue!
  • Some packages run scripts at the install stage (unsing the preinstall and postinstall scripts). Because ByrnesJS currently only runs at runtime it won't sandbox these scripts. You can use the --ignore-scripts flag on yarn or npm to prevent these scripts running but that will probably break stuff!

Why "ByrnesJS?"

Like Jack Byrnes, it allows you to only bring those libraries you really trust into your 'Circle of Trust'. https://www.google.com/search?q=jack+byrnes+circle+of+trust

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

:exclamation: ByrnesJS deliberately doesn't use any non-dev dependencies, so if you do want to contribute please don't add any!

License

MIT