How to Secure Third-Party Dependencies
Third-party packages are one of the largest attack surfaces in modern applications. A single compromised dependency can affect thousands of apps. This guide covers auditing, monitoring, and securing your dependency supply chain.
Find security issues automatically before attackers do.
Follow These Steps
Run npm audit regularly
Check for known vulnerabilities in your dependencies.
# Check for vulnerabilities
npm audit
# Fix automatically where possible
npm audit fix
# Fix with major version updates (review changes first)
npm audit fix --forceKeep a lockfile committed
Always commit package-lock.json to ensure reproducible builds.
# Verify lockfile exists and is committed
git ls-files package-lock.json
# Use ci for CI/CD (installs from lockfile only)
npm ciUse npm ci instead of npm install in CI/CD. It installs exactly what is in the lockfile without modifying it.
Review new dependencies before adding them
Check download counts, maintenance status, and code quality before adding a package.
# Check package details
npm info package-name
# Check for known issues
npm audit package-name
# Evaluate alternatives
# - Downloads per week (popularity)
# - Last publish date (maintenance)
# - Number of dependencies (attack surface)
# - GitHub stars and issuesSet up automated dependency monitoring
Enable GitHub Dependabot or Snyk to automatically detect and fix vulnerable dependencies.
# .github/dependabot.yml
version: 2
updates:
- package-ecosystem: "npm"
directory: "/"
schedule:
interval: "weekly"
open-pull-requests-limit: 10Use exact versions for critical dependencies
Pin exact versions for security-critical packages.
// package.json - pin critical packages
{
"dependencies": {
"next-auth": "5.0.0", // Exact version (no ^)
"bcrypt": "5.1.1", // Exact version
"jsonwebtoken": "9.0.2" // Exact version
}
}Remove unused dependencies
Reduce attack surface by removing packages you no longer use.
# Find unused dependencies
npx depcheck
# Remove unused packages
npm uninstall unused-package-nameWhat You'll Achieve
Dependencies are audited for vulnerabilities, lockfile is committed, automated monitoring is enabled, critical packages are version-pinned, and unused packages are removed.
Common Mistakes to Avoid
Mistake
Ignoring npm audit warnings
Fix
Review every audit warning. Fix critical and high severity issues immediately. Track moderate issues for the next update cycle.
Mistake
Not committing the lockfile
Fix
The lockfile ensures everyone installs the same versions. Without it, different environments may get different (potentially vulnerable) versions.
Frequently Asked Questions
Is npm audit reliable?
npm audit catches known vulnerabilities in the npm advisory database. It does not detect zero-day vulnerabilities or malicious packages without known advisories. Use it as one layer of defense alongside manual review.
Should I update all dependencies regularly?
Yes, but carefully. Use automated tools like Dependabot for minor and patch updates. Review major updates manually. Always test after updating.
Ready to Secure Your App?
VAS automatically scans your deployed app for the security issues covered in this guide. Get actionable results in minutes.
Start Security Scan