Skip to content

Node.js supply-chain security with LavaMoat and SES

Learn how to protect a Node.js project from malicious npm packages that vulnerability scanners miss. You will switch off install scripts by default, lock down each dependency with LavaMoat, and see where SES runtime isolation fits.

This guide shows Node.js teams how to defend against malicious npm packages, meaning code published to the npm registry on purpose to steal or damage. npm audit only flags packages with a known CVE (a publicly catalogued vulnerability), so a brand-new package that reads your environment variables during install, or a hijacked release of a trusted library, slips straight through. Real attacks like event-stream and ua-parser-js worked this way and were caught only after public disclosure. The practical defence starts with turning off install-time lifecycle scripts by default. Next, you limit what each dependency can reach with LavaMoat, a tool that gives every package its own permission policy. Where you can, you also isolate code at runtime with SES (Secure ECMAScript, a locked-down version of JavaScript). By the end you will have script blocking and a LavaMoat policy working in your project.

How to verify

See what your install actually runs and where the surface area is:

npm install --ignore-scripts --dry-run | head -30  # what would happen
npm pkg get scripts                                 # your own scripts
find node_modules -name package.json -exec jq -r '.scripts | with_entries(select(.key | test("install|prepublish")))' {} \; | grep -v null
ls .lavamoat/ 2>/dev/null                           # if you've configured it
node -e "require('ses'); new Compartment().evaluate('1+1')" # SES loaded?

If your fresh install runs 50+ lifecycle scripts you’ve never reviewed, you’re trusting 50+ separate maintainers with shell access to your build machine.

What’s happening

npm has three execution surfaces a malicious package can use without changing your code: lifecycle scripts (preinstall, install, postinstall, prepublish) run on every npm install with full filesystem and network access; module load runs top-level code in every required file (most apps execute thousands of require() calls and each is JavaScript that can do anything); runtime call is the only surface inside your own code’s execution, but by then anything malicious has had two free shots.

--ignore-scripts neutralizes the first surface. Adopt it as a project-wide setting in .npmrc. For genuinely-needed lifecycle scripts (sharp, better-sqlite3, node-gyp builds), maintain an allowlist with the can-i-ignore-scripts tool or a manual review.

LavaMoat (from MetaMask) wraps the module system: instead of every required module sharing the same global scope, LavaMoat applies a per-package policy at runtime. A package whose policy says “only filesystem read access” cannot suddenly open a network socket — even if it tries. The policy is generated from observed behavior during a controlled run and committed; deviations on subsequent runs are detected.

SES (Secure ECMAScript, the basis of TC39’s “frozen realm” proposal) provides hardened JavaScript primitives — frozen globals, no eval by default, defensive Math, Array, etc. LavaMoat uses SES under the hood. You can use SES directly to create isolated compartments for evaluating untrusted code (plugin systems, sandboxed evaluators).

The procedure

  1. Lock down lifecycle scripts globally. Add to .npmrc at the project root:

    ignore-scripts=true

    This affects npm install only; lifecycle scripts in your own package.json (via npm run) still execute. To re-enable scripts for specific packages, install with --include-scripts and review.

  2. Identify which third-party packages legitimately need install scripts:

    npx can-i-ignore-scripts

    The tool flags packages that have a real native-build dependency (sharp, sqlite bindings) versus those whose postinstall just sends a survey URL.

  3. Install LavaMoat for runtime policy enforcement. Generate the policy from a clean run:

    npm install --save-dev @lavamoat/allow-scripts lavamoat
    npx lavamoat generate           # records observed access
    git add lavamoat/             # commit the policy

    The policy file lists each package and what globals/builtins/runtime APIs it touched during the recording run.

  4. Run production with LavaMoat enforcing the policy. Replace the entry-point invocation:

    # before
    node dist/main.js
    # after
    npx lavamoat dist/main.js

    Any package attempting access outside its policy entry throws a runtime exception, which surfaces in your logs.

  5. For plugin systems that evaluate untrusted code, use SES compartments directly:

    import 'ses';
    lockdown();
    const c = new Compartment({ console });
    const result = c.evaluate(untrustedCode);  // runs with no network/fs by default
  6. Combine with package signing (npm 9+ supports npm install --sig-verify against the registry signing key) and per-organization scoped registries so a typosquat in the public registry cannot be confused with your internal package.

Common pitfalls

  • ignore-scripts=true breaks packages that need a postinstall (sharp, better-sqlite3, husky). Maintain an allowlist via @lavamoat/allow-scripts and explicitly opt-in the packages that need it.
  • LavaMoat’s policy is recorded from an observed run. If a code path wasn’t exercised during recording, its access isn’t in the policy, and production hits a runtime exception. Run the policy generator against a full test suite, not a manual smoke test.
  • SES lockdown() freezes built-in globals. Some libraries (older lodash, libraries that monkey-patch Promise) break under SES. Test in staging with lockdown() before deciding it works for your app.
  • Lifecycle scripts run as your local user with your filesystem. A leaked ~/.aws/credentials or ~/.npmrc token via a malicious install is the standard exfil path. Audit machines that run npm install as if they were developer laptops — they are.
  • npm audit signatures (npm 9+) verifies tarball signatures against the registry but does not stop a malicious package whose author is verified. Signing proves identity, not intent.

For dependency CVE scanning that catches the post-disclosure version of this risk, see npm audit and Snyk. In the engagements Stack Harbor runs, ignore-scripts=true is the default in every project, the allowlist is reviewed quarterly, LavaMoat is enabled for services that handle credentials or PII, and the build machine isolation is treated like any other production host as part of managed operations — supply-chain attacks are an MSP problem, not a developer problem.