npm-security-best-practices
Collection of npm package manager Security Best Practices
https://github.com/lirantal/npm-security-best-practices
Last synced: about 1 month ago
JSON representation
13. Consult the Snyk Security Database for package health
Follow-up resources
14. Do not trust the official npmjs.org registry
Follow-up resources
-
npmjs.org
- based dependencies even when they are declared in a package's
package.jsonfile. This means a package may have non-registry dependencies that are invisible to users browsing the registry website.
1. Disable Post-Install Scripts
1.1. pnpm disable post-install scripts
- pnpm disables postinstall scripts by default - enable postinstall scripts or set an explicit allow-list of packages that are allowed to run postinstall scripts.
1.2. Bun disable post-install scripts
-
Bun disables postinstall scripts by default
- list of packages that are allowed to run postinstall scripts. Bun allows an "escape hatch" to allow postinstall scripts for specific trusted packages via a
trustedDependenciesfield inpackage.json.
2. Install with Cooldown
2.1. pnpm / Bun / Yarn minimumReleaseAge cooldown
2.2. Snyk automated dependency upgrades with cooldown
- Snyk automatically includes a built-in cooldown period
- Snyk automatically includes a built-in cooldown period
2.3. Bun minimumReleaseAge cooldown
2.3. Dependabot automated dependency upgrades with cooldown
2.4. Renovate bot automated dependency upgrades with cooldown
3. Harden package installs with security tools
3.2. Use Socket Firewall (sfw) for blocking malicious packages
- Socket Firewall (`sfw`) - time firewall that intercepts supported package manager commands and blocks packages flagged for malicious behavior, using Socket's deep package analysis and threat intelligence.
3. Use npq for hardening package installs
2.2. Snyk automated dependency upgrades with cooldown
- npq - risk packages.
4. Prevent npm lockfile injection
Advanced usage options
Bun lockfile linting
6. Avoid blind npm package upgrades
Lockfile management best practices
7. No plaintext secrets in .env files
Follow-up secure secrets resources
8. Work in Dev Containers
Follow-up resources
Follow-up secure secrets resources
Author
Additional mitigations
- ^7 - platform RAT via malicious post-install dependency. See also: Microsoft analysis, Snyk analysis
- ^8