Git Product home page Git Product logo

node-dependency-analysis's Introduction

Node.js Dependency Analysis

npm version

I have 1000 dependencies in my Node project and I don't know what they're doing... how can I know more?

Google's Node Analysis tool should help!

Many Node.js projects have large package dependency trees with total transitive dependencies numbering in the hundreds, written by many authors. This tool helps developers more easily audit these dependencies through static analysis, with a focus on highlighting instances of I/O and dynamic execution, as well as patterns that obfuscate these instances. We operate under the assumption that a small number of modules exhibit these behaviors, and provide a summary of the dependency tree that not only assists developers in auditing their code, but also allows users to compare the sensitive functionality of third-party code across new installations and dependency upgrades at a glance.

Detected Patterns

  • Loading Core I/O Modules
// load http, to make network requests.
require('http');

// load fs, to read/write files.
require('fs');
  • Dynamic Arguments to Require
// loads http, but obfuscates the input to require() by computing it at runtime.
require('ht' + 'tp');

// loads fs, but obfuscates the input to require() by assigning it to another variable first.
const myStr = 'fs';
require(myStr);
  • Arbitrary Expression Evaluation
// evaluates doBadThings().
eval('doBadThings();');

// creates a function that evaluates doBadThings(), and runs it.
const fn = new Function('return doBadThings();');
fn();

// evaluates doBadThings(), similar to eval (but with a Node API).
vm.runInNewContext('doBadThings();');
  • Module-Loading Internals
// uses a function used internally in require() to load https.
Module._load('https');

// overrides the internal function so that require('foo') returns require('http').
const oldLoad = Module._load;
Module = { _load(str) { return str === 'foo' ? oldLoad('http') :oldLoad(str); } }
  • Assigning Sensitive Functions to Other Variables
// re-assigns require() to be called somewhere else.
const r = require;
r('https');

// passes require to a function that will invoke it.
Function.prototype.call(require, 'https');
  • Accessing Sensitive Functions on global
// indexes into global to get a reference to eval().
global['eval']('doBadThings()');

// Accesses require on global rather than the module-local version.
global.require('https');
  • Accessing Environment Variables
// passes the user's NPM_TOKEN env var to doBadThings().
doBadThings(process.env.NPM_TOKEN);

// enumerates thru env vars and passes them to doBadThings.
doBadThings(Object.keys(process['env']).map(k => process['env'][k]));
  • Unorthodox Function Calls
// same as `require('http')`.
require.call(null, 'http');

// also same as `require('http')`. (this gets picked up in other detections)
Function.prototype.call(require, 'http');

Distinguishing between Legitimate and Illegitimate Use Cases

Every scenario in the above-listed procedures has legitimate use cases; clearly, simply requiring a module that does I/O is not grounds to condemn it as malicious. Even “attempts” to obfuscate code can be benign; this might result from webpacking/minimization, or be a feature of a module designed to monkeypatch Node APIs for monitoring reasons.

However, it still stands that each module that invokes one of these scenarios should have an articulable reason, and the user should set a higher bar to trust these modules. For example, request is a widely-used and legitimate module that makes http requests, being a convenience wrapper around the http module. Because of this, the author of request bears a responsibility to convince their users that they are putting forth their best effort to protect their publish credentials.

There are a few different ways for users to distinguish between legitimate and illegitimate use cases:

Changes Across Versions

Compare across several versions of a module, or a dependency tree, for changes in detected patterns. This can be done upon the installation of a new module, or just simply upon every installation.

Whitelisting

Maintain a whitelist that allows certain modules to exhibit these patterns. As explained above, this means that we trust certain modules more than others; the hypothesis here (that needs to be tested) is that the number of modules that exhibit legitimate uses of each pattern is low (relative to the total number of modules).

Dynamic Analysis

Run a closely associated dynamic analysis tool during runtime that compares actual behavior of an application with the list of detected patterns, and have it report anomalies.

node-dependency-analysis's People

Contributors

jsamar17 avatar kjin avatar lee-tammy avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

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.