Giter Club home page Giter Club logo

secure-require's Introduction

secure-require

A secure require implementation for ECMAScript

npm CircleCI

Feel more confident running a bunch of untrusted dependencies as a part of your application or module by allowing said dependency to only use a subset of core APIs. This allows you to make sure that none of the sub-dependencies try anything unexpected, or are able to alter the global objects of your own application code, no matter which version you upgrade to.

Notice

I'd like to humbly request you to please refrain from using this module anyplace critical since it hasn't been audited properly and is still undergoing massive changes. You should be able to better rely on it once the v1.x is released.

Security Model

  • Full global isolation: Each top-level dependency is run in a separate container.
  • Safely restrict the require tree to only a subset of allowed modules, including restricting Node.js core access.

Features

  1. Zero dependencies (wouldn't that be ironic).
  2. Uses the same stuff require uses behind-the-scenes, so performance dip should be next to none.
  3. Supports core, third party and local modules.
  4. Cache modules in the same context so that you get close to the speed of the original require function without losing any of the security guarantees.

Imposed Restrictions (Incompatibilities)

The following are the security restrictions that are imposed by the function on your dependencies to ensure your safety. In case you disagree with any of these, or have a valid use case for doing any of the following that you believe should be supported, please do not hesitate to reach out.

  1. A dependency cannot make use of any core module which isn't whitelisted throughout it's subtree (duh).
  2. Since modules are compiled in seperate VM Contexts, monkey-patching globals will not work.
  3. When a module requires a core module, they cannot set any property anywhere on that core module.
  4. The use of module.require is forbidden.
  5. No module can require the Module class anywhere down the chain.

Installation

npm i secure-require

Usage

const secureRequire = require('secure-require');
// Since secure-require doesn't have any dependencies, this should be fine.
secureRequire('acorn', []);
// This should pass since acorn is written without any dependencies or core modules.
secureRequire('base', []);
// This should fail since base requires the util core module.
secureRequire('base', ['util']);
// This should pass since we just allowed base to use the util module. Now, we
// need not worry about base doing anything funny with the filesystem or the
// network. It literally cannot.

License

MIT

secure-require's People

Contributors

dependabot[bot] avatar guybedford avatar hitsthings avatar ryzokuken avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

secure-require's Issues

Avoid repetition of whitelisted modules

First of all - great idea! I can definitely see myself integrating something like this in my codebases.

But this got me thinking about modules that are required across many files (e.g. something like request). It's going to be unmanageable repeating secureRequire("awesomeModule", ["dep1", "dep2", ...]) all over the code.

Obvious solution that comes to mind is having a configuration file that will hold whitelists:

{
  "awesomeModule": ["dep1", "dep2", ...]
}

so you can just go with secureRequire("awesomeModule").

I know this might sound like a stretch, but it the future this might even part of package.json as a vital part of your module's definition:

package.json

{
...
  "dependencies": [...],
  "security": {
    "awesomeModule": ["dep1", "dep2", ...]
  }
...
}

Or maybe you already have a solution in your mind that addresses this problem?

Potential holes

This is an awesome idea and I'm keen to try it out!

Was looking at the source and thinking about potential holes worth looking at. You've probably thought of them but I figured it couldn't hurt to help:

  • fs can be used in conjunction with eval/vm to run code, perhaps including core modules. Is there a way to avoid the fs module being a Game Over?
  • do WebAssembly modules open any holes?
  • your source already has a note about native modules.
  • can any core modules be found by requiring other core modules and navigating prototype chains?
  • can the prototype chain of the require function you pass in be navigated to access anything?
  • Can I access core modules via the debugger port if the app is run with --inspect and 'net' is permitted?
  • child_process obviously exposes everything since you can just run node again.

I'm happy to investigate any that you haven't yet, if you're looking for help.

Cheers!

Compiled dependencies

Hey there!

What is the plan for secure-require and compiled dependencies?

If someone ships a module written in C++ to npm (without requiring any node core dependencies), couldn't that someone escape the sandbox? Or at least write anything to the filesystem using POSIX APIs, open TCP sockets etc from C++?

For example, let's suppose you use Terser with secure-require on your server, but a bad actor has gained publish rights to one of Terser's dependencies (or Terser itself). Wouldn't they be able to add C++ code to this dependency that then runs natively and freely on your machine?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.