keepcalculator

06 Numbers and Powers

Verify npm Package Provenance Before Installing

Card No. 06·55419 · Updated · Standard formula, shown below

This guide shows you how to check the provenance of an npm package before you install it, so you can avoid malicious or compromised dependencies. You’ll learn to verify the package’s source repository, maintainer identity, and published artifact integrity using npm’s built-in commands and the npm registry website. No prior security expertise is needed, just a terminal and a package name in mind.

What You’ll Need

  • Node.js and npm installed (npm 7 or later recommended)
  • Access to the npm registry website (or use the npm CLI)
  • The exact name of the package you want to verify

Step-by-Step

  1. Check the package’s metadata. Run npm view <package-name> in your terminal. This shows the package’s description, version, author, license, and repository URL. Look for a valid repository URL (e.g., GitHub, GitLab) that matches the package name. If the repository URL is missing or points to a suspicious domain, be cautious.
  2. Verify the author and maintainers. Run npm view <package-name> maintainers to see who publishes the package. Cross-check this with the repository’s contributor list. A well-maintained package usually has a few known maintainers. If the maintainer list is empty or contains random-looking names, it’s a red flag.
  3. Check the release history. Run npm view <package-name> time to see all published versions and their timestamps. A legitimate package has a steady release history. A sudden spike in versions or a brand-new package with a high version number (e.g., 9.9.9) could indicate a hijacked or counterfeit package.
  4. Inspect the tarball contents. Download the package tarball without installing it: npm pack <package-name>. This creates a .tgz file. Extract it with tar -xzf <package-name>.tgz and look at the files. Check for anything unusual: hidden files, obfuscated scripts, or files that don’t match the package description. Pay special attention to package.json and any install scripts.
  5. Review the install scripts. Inside the extracted package, open package.json and look at the scripts field, especially preinstall, install, and postinstall. Legitimate packages rarely have install scripts that download or execute external code. If you see something like curl ... | sh or a base64-encoded command, do not install it.
  6. Check the package on the npm website. Go to https://www.npmjs.com/package/<package-name>. Look for the “Provenance” badge or section (if available). npm shows provenance for packages published with a supported CI/CD pipeline. Also check the “GitHub Repository” link and the “Maintainers” tab. Read the README for any security-related notes.
  7. Use the npm audit command. Before installing, you can run npm audit --dry-run after adding the package to your package.json (but before installing). This will report known vulnerabilities. While this doesn’t directly verify provenance, it helps you avoid packages with known issues.
  8. Verify the package signature (if available). Some packages are signed with a PGP key. Run npm view <package-name> dist.tarball to get the tarball URL, then download it and use gpg to verify if the package provides a signature. This is rare but adds a strong layer of trust.

Common Mistakes

  • Relying only on the package name and version. Attackers often use typosquatting (e.g., lodahs instead of lodash) or similar-looking names.
  • Ignoring the repository URL. A missing or invalid repository is a major red flag.
  • Skipping the tarball inspection. Many malicious packages hide code in install scripts that only run during installation.
  • Assuming all packages on npm are safe. npm has had security incidents; always verify.
  • Not checking the maintainer list. A package with a single maintainer who has no other contributions might be a throwaway account.

Tips

  • Use the --dry-run flag with npm install to see what would be installed without actually installing it.
  • Look for packages with a high download count and many stars on GitHub, but remember that popularity isn’t a guarantee of safety.
  • Check if the package has a security policy (SECURITY.md file) in its repository.
  • Prefer packages that are part of a larger organization or have multiple maintainers.
  • If you’re unsure, search the package name on a search engine to see if there are any known issues.

Troubleshooting

  • npm view returns an error. Make sure the package name is spelled correctly. If the package is scoped (e.g., @scope/package), include the scope. If it still fails, the package might be unpublished or your npm registry is misconfigured.
  • The package tarball is huge or contains unexpected binaries. This could be a sign of a bundled executable. Examine the files carefully. If you don’t recognize the purpose, don’t install it.
  • The install script is obfuscated. If you can’t understand what the script does, treat it as malicious. Legitimate packages rarely need complex install scripts.
  • The package’s GitHub repository is empty or deleted. This is a strong indicator of a fraudulent package. Look for an alternative package with a real repository.
  • npm audit reports high-severity vulnerabilities. Even if the package is legitimate, it may have known vulnerabilities. Consider using a different version or an alternative package.

Frequently Asked Questions

What exactly is npm package provenance?

Provenance refers to the verifiable origin of a package: who published it, when, and from where. npm’s provenance feature uses signed attestations to link a package to a specific source repository and build pipeline.

Is it safe to install a package that doesn’t have provenance?

Not necessarily. Provenance is a relatively new feature, and many legitimate packages don’t have it yet. But its absence means you have less assurance, so you should do the manual checks described above.

Can I verify provenance for a package already installed?

Yes, you can run npm ls <package-name> to see the installed version, then check that version’s metadata with npm view <package-name>@<version>.

What does the ‘dist.integrity’ field in npm view mean?

That’s a SHA-512 hash of the tarball. npm uses it to verify the file hasn’t been tampered with during download. You can compare it with the hash of the tarball you downloaded with npm pack.

How do I check if a package has been compromised after I’ve installed it?

Run npm audit regularly to check for known vulnerabilities. Also watch for unusual behavior in your application, and check the package’s release notes for any security advisories.

Are there any tools that automate provenance verification?

Yes, there are third-party tools like npm-provenance-check or socket.dev that can scan packages. However, they are not official npm tools, so use them with caution and always do your own manual checks as well.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →