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

gnash

v0.1.0

Published

A tool to help you know your dependencies through static analysis

Downloads

4

Readme

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.