依赖管理(npm audit)
npm audit 扫描项目依赖树,检测已知安全漏洞并提供修复建议。
基本使用
运行审计
Bash
# 检查依赖漏洞
npm audit
# 以 JSON 格式输出
npm audit --json
# 只显示严重漏洞
npm audit --audit-level=high
审计输出解读
Bash
# npm audit security report
#
┌───────────────────────────────────────────────────────────────────┐
│ high │ Prototype Pollution in minimist │
├───────────┼───────────────────────────────────────────────────────┤
│ Package │ minimist │
│ Patched │ >=0.2.1 <1.0.0-0 || >=1.2.3 │
│ Path │ myapp > build > minimist │
│ More info │ https://npmjs.com/advisories/1179 │
└───────────┴───────────────────────────────────────────────────────┘
关键字段说明:
- Severity:漏洞等级
- Package:存在漏洞的包名
- Patched:已修复的版本范围
- Path:依赖路径
自动修复
npm audit fix
Bash
# 自动修复兼容的漏洞
npm audit fix
# 强制安装破坏性更新(可能升级主版本)
npm audit fix --force
# 仅检查不修复
npm audit fix --dry-run
修复策略
| 参数 | 说明 |
|---|---|
npm audit fix | 安全修复,不破坏语义版本 |
npm audit fix --force | 强制更新,可能升级主版本 |
npm audit fix --only=prod | 仅修复生产依赖 |
处理无法修复的漏洞
1. 手动更新依赖
JSON
# 查看依赖版本
npm list vulnerable-package
# 更新到安全版本
npm install vulnerable-package@latest
# 更新特定版本
npm install vulnerable-package@1.2.3
2. 使用 overrides(npm 8.3+)
JSON
{
"overrides": {
"vulnerable-package": "^2.0.0"
}
}
3. 使用 resolutions(Yarn)
JavaScript
{
"resolutions": {
"vulnerable-package": "^2.0.0"
}
}
4. 替换依赖包
Bash
// 如果某包长期有漏洞,考虑替换为维护更好的替代品
// 如:request → axios / node-fetch
// 如:express-session → @quixo3/prisma-session-store
漏洞等级与优先级
| 等级 | 说明 | 处理优先级 |
|---|---|---|
| critical | 可被远程利用,影响严重 | 立即修复 |
| high | 严重漏洞,可能被利用 | 尽快修复 |
| moderate | 中等风险 | 计划修复 |
| low | 低风险漏洞 | 评估后决定 |
忽略特定漏洞
临时忽略
ini
# 忽略特定 advisory
npm audit --omit=advisory-id
.npmrc 配置
JSON
# .npmrc
audit=false # 禁用自动审计
永久忽略(记录原因)
YAML
{
"scripts": {
"audit": "npm audit --ignore-scripts"
}
}
注意:忽略漏洞前务必评估风险,记录忽略原因和期限。
CI/CD 集成
GitHub Actions
YAML
# .github/workflows/audit.yml
name: Security Audit
on: [push, pull_request]
jobs:
audit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18'
- run: npm audit --audit-level=high
GitLab CI
Bash
# .gitlab-ci.yml
audit:
stage: test
script:
- npm audit --audit-level=moderate
allow_failure: false
npm ci 安全安装
Bash
# CI 环境推荐使用 npm ci
npm ci --ignore-scripts # 跳过脚本执行,提高安全性
npm audit --audit-level=high
依赖安全最佳实践
1. 定期更新依赖
Bash
# 检查过期依赖
npm outdated
# 交互式更新
npx npm-check -u
2. 锁定版本
Bash
# 提交 package-lock.json
git add package-lock.json
# 使用精确版本
npm install package@1.2.3 --save-exact
3. 审查新依赖
Bash
# 安装前检查
npm view package versions
npm view package time
npm view package repository
# 检查依赖树
npm install package --dry-run
4. 最小化依赖
Bash
# 分析包大小
npx cost-of-modules
# 查找重复依赖
npx npm-dedupe
其他安全工具
Snyk
YAML
# 安装
npm install -g snyk
# 扫描
snyk test
# 监控
snyk monitor
# 自动修复
snyk wizard
npm audit + GitHub Dependabot
Bash
# .github/dependabot.yml
version: 2
updates:
- package-ecosystem: "npm"
directory: "/"
schedule:
interval: "weekly"
open-pull-requests-limit: 10
常见问题排查
依赖树过深
Bash
# 查看依赖树
npm ls vulnerable-package
# 查看路径
npm ls vulnerable-package --all
传递依赖漏洞
text
# 强制覆盖传递依赖版本
npm install parent-package --force
# 使用 npm dedupe 减少重复
npm dedupe
注意:
npm audit fix --force可能破坏兼容性,生产环境谨慎使用。
要点总结
- 定期运行
npm audit检查漏洞,优先处理 critical/high 级别 - 使用
npm audit fix安全修复,--force谨慎使用 - 无法修复的漏洞使用 overrides 或 resolutions 强制指定版本
- CI/CD 中集成审计检查,阻止有漏洞的代码合并
- 配合 Dependabot 或 Snyk 实现持续监控
📝 发现内容有误?点击此处直接编辑