A secure require implementation for ECMAScript
A secure require implementation for ECMAScript
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.
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.
require
tree to only a subset of allowed modules, including restricting Node.js core access.require
uses behind-the-scenes, so performance dip should be next to none.require
function without losing any of the security guarantees.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.
module.require
is forbidden.Module
class anywhere down the chain.
npm i secure-require
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.