如何使用 npm audit 与 yarn audit 并集成到 CI
解读
国内一线/二线互联网公司对前端安全的红线越来越严,CI 阶段若出现高危漏洞(CVSS≥7)会直接阻断合并。面试官问“怎么用、怎么集成”并不是想听两条命令,而是考察:
- 你对 npm/yarn 漏洞数据库来源、误报处理、白名单机制是否熟悉;
- 能否在 GitLab CI / GitHub Actions / Jenkins(国内主流)里把扫描做成「卡点」且不影响并行构建;
- 是否了解国内镜像源(淘宝、华为、腾讯云)同步延迟带来的「假阴性」及兜底方案;
- 能否把结果可视化并同步到飞书/企业微信/钉钉,方便安全运营同学闭环。
知识点
- npm audit:基于 Redis-advisory 库,本地调用 https://registry.npmjs.org/-/npm/v1/security/advisories/bulk,可附加 --audit-level=moderate|high|critical 过滤等级。
- yarn audit:v1 调用相同接口,v2+ 走 @yarnpkg/plugin-npm-audit,支持 --json 输出,便于脚本解析。
- 误报处理:npm 9+ 支持 overrides 字段,yarn 2+ 支持 resolutions + packageExtensions;两者均支持 .nsprc / .yarn-audit-known-issues.json 白名单文件,需写入 commit 并做 code review。
- CI 卡点策略:国内普遍采用「高危及以上阻断,中危提示」;CI 中先跑 lint、type,再并行跑 audit,若 audit 失败直接 exit 1,防止后续 Docker 构建浪费云资源。
- 镜像源延迟:淘宝源每天凌晨同步一次,高危漏洞从披露到同步平均滞后 6–12 h;兜底方案是在 CI 里显式设置 registry=https://registry.npmjs.org,牺牲 2~3 s 下载时间换取实时性。
- 结果通知:利用 @actions/core、@actions/github 或 Jenkins 的 qy-wechat-plugin,把 audit 结果以 Markdown 表格形式推送到企业微信群,@对应 MR 作者。
- 性能优化:monorepo 场景下使用 yarn workspaces foreach -A run audit 或 lerna exec -- npm audit,配合 --prefer-offline 与缓存,可把 400 包项目扫描耗时从 40 s 降到 8 s。
答案
-
本地预检 npm 项目:
npm audit --audit-level=high # 若需自动修复且无 Breaking Change npm audit fix --dry-run npm audit fixyarn 项目:
yarn audit --level high --json > audit.json # 解析 JSON,统计 high|critical 数量 node scripts/audit-filter.js白名单文件 .nsprc(npm)或 .yarn-audit-known-issues.json(yarn)随仓库提交,CI 会读取。
-
GitLab CI 集成(国内私有部署最常见)
stages: [install, audit, build] audit: stage: audit image: node:18-alpine before_script: - npm config set registry https://registry.npmjs.org - npm ci --prefer-offline --cache .npm --silent script: - npm audit --audit-level=high --json > npm-audit.json - node scripts/ci-audit.js npm-audit.json allow_failure: false artifacts: reports: dependency_scanning: npm-audit.json expire_in: 1 weekscripts/ci-audit.js 逻辑:解析 JSON,若 advisories 中 severity≥high 数量>0 则 process.exit(1),并把结果写入环境变量供后续通知。
-
GitHub Actions 集成(开源项目或跨境团队)
name: Security Audit on: [pull_request] jobs: audit: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: 18 registry-url: https://registry.npmjs.org - run: npm ci - run: npm audit --audit-level=high --json > audit.json - name: Parse and Comment uses: actions/github-script@v7 with: script: | const fs = require('fs'); const data = JSON.parse(fs.readFileSync('audit.json')); const highs = data.metadata.vulnerabilities.high + data.metadata.vulnerabilities.critical; if (highs > 0) { core.setFailed(`发现 ${highs} 个高危漏洞,请先修复`); github.rest.issues.createComment({ issue_number: context.issue.number, owner: context.repo.owner, repo: context.repo.repo, body: `🚨 **Security Alert** 检测到 ${highs} 个高危漏洞,请运行 \`npm audit fix\` 并验证。` }); } -
Jenkins 集成(传统企业/金融) 在 Jenkinsfile 中调用
sh 'npm audit --audit-level=high'并把结果归档,配合「质量门禁」插件:若返回码非 0 则把构建标记为 FAILED,同时通过企业微信机器人推送消息。 -
常见坑与对策
- 私有包被误报:在 package.json 配置
"private": true并在 .nsprc 里排除。 - devDependencies 误阻断:CI 里加
--production参数,仅扫描生产依赖。 - 0-day 漏洞无修复版:使用 npm overrides 或 yarn resolutions 强制升级 transitive dependency,并在 commit message 中附漏洞编号,方便安全审计追踪。
- 私有包被误报:在 package.json 配置
拓展思考
- 如何把 npm audit 与 SCA(Software Composition Analysis)平台(如奇安信、默安、悬镜)对接?——可在 CI 里把 audit.json 转成 CycloneDX SBOM 格式,通过 API 上传,统一做许可证合规与漏洞生命周期管理。
- monorepo 下子包版本不一致导致同一漏洞多次出现,如何聚合去重?——利用 yarn why 或 npm ls 解析依赖路径,按「漏洞编号 + 路径」去重后再统计。
- 国内监管要求「两小时内处置高危漏洞」,如何做到自动化热修复?——结合 Renovate 或 Dependabot 的 npm 插件,在检测到 high|critical 时自动提 MR,并触发金丝雀发布,回滚窗口控制在 5 分钟。
- 如果公司强制离线内网,如何同步漏洞库?——在内网搭建 npm registry(Verdaccio + cnpmcore),每日同步官方 advisory 数据,CI 指向内网地址,保证离线环境也能实时审计。