Description
An npm dependency fetched over HTTP can be altered in transit. A modified package or an install script such as postinstall may then run in a developer environment or CI.
Potential impact
- Someone on the network path can read or change package files and metadata.
- A modified dependency can run code during the build or reach users in a release.
Remediation
- Prefer a version range from an official registry. For direct npm tarball URLs, use HTTPS. For Git dependencies, use HTTPS or SSH with host-key verification.
- Set an HTTPS registry and
strict-ssl=truein.npmrc. Internal artifact servers also need valid certificates and appropriate TLS settings. - Commit a reviewed
package-lock.jsonornpm-shrinkwrap.jsonand install withnpm ci.integritychecks the download against the recorded bytes. Verify any supplied checksum or signature against a trusted value or key. - Block outbound HTTP in CI and allow only approved domains. To restrict install scripts, consider
npm config set ignore-scripts trueand run the required scripts separately.
Examples
Before
The HTTP tarball can travel in cleartext. Current npm rejects the FTP URL, so installation of that dependency fails.
javascript
{
"name": "demo-app",
"version": "1.0.0",
"dependencies": {
"left-pad": "http://mirror.local/libs/left-pad-1.3.0.tgz",
"my-utils": "ftp://downloads.example.org/my-utils-2.1.0.tgz"
}
}
After
package.json:
json
{
"name": "demo-app",
"version": "1.0.0",
"dependencies": {
"left-pad": "^1.3.0",
"my-utils": "https://repo.example.com/npm/my-utils-2.1.0.tgz"
}
}
Project or CI .npmrc:
ini
registry=https://registry.npmjs.org/
strict-ssl=true
# Use HTTPS for the internal registry too
@company:registry=https://repo.example.com/npm/
The revised left-pad entry uses a registry version range and my-utils uses HTTPS. Review the package and lockfile together so an altered artifact cannot become your trusted baseline.