Protect your applications from security vulnerabilities.
Verify that CDN-loaded modules haven't been tampered with:
<script type="module"
src="https://cdn.example.com/module.js"
integrity="sha384-abc123..."
crossorigin="anonymous">
</script>
✅ Benefits: Protects against CDN compromises and man-in-the-middle attacks
Regularly scan dependencies for vulnerabilities:
# npm audit
npm audit
npm audit fix
# Check specific packages
npm audit --json | grep severity
Automated Tools:
Be cautious with dynamic imports using user input:
❌ Dangerous:
// User can inject malicious paths
const module = await import(userInput);
✅ Safe:
// Whitelist allowed modules
const allowed = ['en', 'es', 'fr'];
if (allowed.includes(lang)) {
await import(`./i18n/${lang}.js`);
}
Restrict module sources with CSP headers:
Content-Security-Policy:
script-src 'self' https://trusted-cdn.com;
script-src-elem 'self' https://trusted-cdn.com;
⚠️ Note:
ES Modules respect CSP's script-src directive. Test thoroughly!
Always commit and use lockfiles:
Node.js 20+ includes an experimental permission model for restricting module capabilities:
# Restrict file system access
node --experimental-permission --allow-fs-read=./src --allow-fs-write=./dist app.js
# Restrict network access
node --experimental-permission --allow-net=api.example.com app.js
Track every module in your dependency tree for supply chain transparency:
# Generate SBOM with npm
npm sbom --sbom-format cyclonedx
# Verify package provenance
npm audit signatures
# Check provenance attestations
npm pack --provenance
npm now supports provenance attestations that cryptographically link published packages to their source repository and build process.