Dependency Security
Supply chain attacks, npm audit, lockfiles, and automated vulnerability scanning with Dependabot
Skills taught: websecurity.supply-chain, websecurity.npm-audit, websecurity.dependabot, websecurity.lockfiles, websecurity.sca
The supply chain threat
Modern Node.js projects average 700+ transitive dependencies. Each dependency is code you didn't write, review, or control — and each one is a potential attack vector. Supply chain attacks compromise a legitimate package to distribute malicious code to its users. They're high-value targets for attackers: compromise one popular package, infect thousands of projects automatically.
TYPOSQUATTING:
Attacker publishes 'lodahs' or 'expressjs' (looks like 'lodash'/'express')
Developer mistyped package name during install
Malicious package installs alongside legitimate code
Fix: always double-check package names; audit new dependencies
ACCOUNT TAKEOVER:
Attacker compromises the npm account of a popular package maintainer
Publishes a malicious update to a legitimate package
Example: event-stream 3.3.6 (2018) — attacker gained publish access,
added malicious code targeting bitcoin wallets in a postinstall script
Fix: lockfiles pin versions; automated alerts detect unexpected updates
DEPENDENCY CONFUSION:
If your company uses private npm registry with package 'mycompany-utils'
Attacker publishes 'mycompany-utils' to public npm with higher version number
npm may prefer the public package over your private one
Fix: scoped packages (@mycompany/utils), explicit registry config in .npmrc
MALICIOUS INSTALL SCRIPTS:
package.json can define postinstall scripts that run during npm install
Malicious package steals env vars, reads .env files, exfiltrates secrets
Fix: npm install --ignore-scripts for untrusted packages (breaks some legitimate ones)
Better: review postinstall scripts before installing unfamiliar packages
npm audit and lockfiles
# npm audit — check all dependencies against the npm advisory database
npm audit
# Shows: package name, severity, CVE, description, fix available
# Fix automatically fixable vulnerabilities (semver-compatible updates)
npm audit fix
# Fix including breaking changes (major version bumps — review manually)
npm audit fix --force
# Fail CI if any vulnerabilities above a threshold
npm audit --audit-level=high # Exit code 1 if any HIGH or CRITICAL found
# Use in GitHub Actions to gate deployments on clean audit
# View full dependency tree to understand where a vulnerability comes from
npm ls lodash # Show all packages that depend on lodash
# WHY lockfiles matter for security:
# package.json: 'express': '^4.18.0' ← accepts 4.18.0, 4.18.1, 4.19.0...
# If express@4.18.2 is compromised, npm install with no lockfile installs it
# package-lock.json pins EXACT versions of all transitive dependencies
# With lockfile: npm install always installs the same versions
# npm ci (in CI/CD): fails if package-lock.json doesn't match package.json
# NEVER add package-lock.json to .gitignore
# ALWAYS commit package-lock.json to source control
# Use npm ci instead of npm install in CI/CD pipelines
# Dependabot automatically:
# 1. Scans your dependencies daily against the GitHub Advisory Database
# 2. Opens PRs to update vulnerable packages
# 3. Optionally auto-merges patch/minor updates after CI passes
version: 2
updates:
# npm dependencies
- package-ecosystem: "npm"
directory: "/"
schedule:
interval: "weekly" # Check for updates every week
open-pull-requests-limit: 10
# Group minor and patch updates into a single PR (less noise)
groups:
minor-and-patch:
update-types:
- "minor"
- "patch"
# Auto-merge patch updates after CI passes
# (requires Branch protection + 'Require status checks' enabled)
# auto-merge: true # Uncomment after setting up branch protection
# GitHub Actions versions
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
Frequently Asked Questions
npm audit shows hundreds of vulnerabilities. Do I need to fix all of them?
Not necessarily all at once. Prioritize by: (1) severity — fix CRITICAL and HIGH first; (2) reachability — is the vulnerable code path actually used in your app? (a vulnerability in a testing library that's never in production is low risk); (3) fix availability — npm audit fix handles many automatically. For low-severity vulnerabilities in dev-only dependencies, they can often wait. Use 'npm audit --audit-level=high' to focus on what matters most. Many projects have legitimate 'accepted' low-severity vulnerabilities that are tracked and reviewed periodically.