在 CI 中,如何生成“漏洞修复报告”并随产物上传到 Artifactory?请给出 webhook 通知格式。
解读
面试官想确认三件事:
- 你是否理解“漏洞修复报告”在 CI 中的生成时机与数据来源(安全扫描结果、依赖审计、代码 diff、commit message 等)。
- 你是否能把报告文件与 Parcel 构建产物一起作为“版本化制品”推送到企业级制品库(Artifactory),并保证可追溯。
- 你是否熟悉国内常用的 CI 平台(GitLab-CI、Jenkins、GitHub Actions 自建 Runner)与 Artifactory 的交互方式,并能给出一份可直接落地的 webhook JSON,方便安全团队或运维订阅。
知识点
- 漏洞数据源:npm audit / yarn audit / pnpm audit、Snyk CLI、Trivy、Dependency-Track、Sonatype IQ 等。
- 报告格式:SARIF 为通用标准,国内评审习惯同时提供 Markdown 或 HTML 便于阅读。
- CI 集成:GitLab-CI 的
artifacts:reports:sast、Jenkins 的archiveArtifacts+Artifactory Plugin、GitHub Actions 的actions/upload-artifact+jfrog/setup-jfrog-cli。 - Artifactory 路径规范:
<仓库>/npm/<packageName>/<version>/下放置parcel-build.tar.gz与vuln-report.sarif,通过.properties文件写入build.name、build.number、git.commit。 - Webhook 触发:Artifactory 的“Artifact was deployed”事件,国内常用企业微信、飞书、钉钉,也可推送到内部安全平台。
- Parcel 相关:虽然 Parcel 本身零配置,但需要在
package.json的postbuild钩子或 CI 阶段显式执行 audit 命令,确保报告与产物同源同次构建。
答案
步骤与示例均以 GitLab-CI + 自建 Artifactory(HA 集群,域名 artifactory.example.com)为例,其他平台只需替换命令。
- 在
.gitlab-ci.yml中新增securityjob,与buildjob 并行,保证不阻塞 Parcel 的毫秒级热更新体验:
stages: [build, security, upload]
variables:
PKG_VER: "${CI_COMMIT_TAG:-0.0.0-${CI_COMMIT_SHORT_SHA}}"
build:
stage: build
image: node:18-alpine
script:
- npm ci
- npx parcel build src/index.html --dist-dir dist --public-url ./
artifacts:
paths: [dist/]
expire_in: 1 day
security:
stage: security
image: node:18-alpine
script:
# 生成 SARIF 通用格式,便于 GitLab SAST 面板解析
- npm audit --json > audit.json || true
- npx @microsoft/sarif-multitool convert audit.json --tool npm-audit --output vuln-report.sarif
# 再生成一份中文 Markdown 供审计同事快速阅读
- npx npm-audit-html --input audit.json --output vuln-report-zh.html
artifacts:
reports:
sast: vuln-report.sarif
paths: [vuln-report.sarif, vuln-report-zh.html]
expire_in: 30 days
- 合并产物并推送 Artifactory:
upload:
stage: upload
image: releases-docker.jfrog.io/jfrog/jfrog-cli-v2:latest
dependencies: [build, security]
script:
- jf c add internal --url=https://artifactory.example.com --access-token=${ART_TOKEN}
- jf rt u "dist/*" npm-local/com/company/pkg/${PKG_VER}/ --build-name=parcel-proj --build-number=${CI_PIPELINE_ID}
- jf rt u "vuln-report.*" npm-local/com/company/pkg/${PKG_VER}/ --build-name=parcel-proj --build-number=${CI_PIPELINE_ID}
- jf rt bce parcel-proj ${CI_PIPELINE_ID} # 收集环境变量
- jf rt bp parcel-proj ${CI_PIPELINE_ID} # 发布构建信息
only: [tags, main]
- Artifactory 中开启 Webhook(Admin → Webhooks → New),事件选择“artifact was deployed”,URL 指向内部安全平台或飞书群机器人,JSON 模板如下:
{
"event": "artifact.deployed",
"timestamp": "${timestamp.iso8601}",
"repo": "${repo.key}",
"path": "${item.path}",
"name": "${item.name}",
"size": "${item.size}",
"sha256": "${item.sha256}",
"buildName": "${build.name}",
"buildNumber": "${build.number}",
"gitCommit": "${build.info.vcs.revision}",
"reportUrl": "https://artifactory.example.com/ui/repos/tree/General/npm-local/com/company/pkg/${item.repoPath.itemPath}/vuln-report-zh.html",
"downloadUrl": "https://artifactory.example.com/artifactory/npm-local/com/company/pkg/${item.repoPath.itemPath}/vuln-report.sarif"
}
国内飞书群机器人需额外加签,可在 Header 中配置 X-Hub-Signature-256,密钥由飞书提供。
- 安全团队收到 webhook 后,即可通过
downloadUrl拉取 SARIF 文件,导入 Dependency-Track 或内部缺陷平台,完成漏洞闭环。
拓展思考
- 若企业已上线“安全左移”门禁,可在
securityjob 里加allow_failure: false,并设定阈值(高危漏洞数 > 0 即失败),阻止带漏洞版本进入 Artifactory。 - 对于 Parcel 的微前端子包场景,可在
package.json中利用workspace协议一次性扫描所有子应用,再合并 SARIF,避免重复 webhook。 - 国内金融客户常要求“三码合一”:源码 commit、构建产物 SHA256、漏洞报告 SHA256,可在上传前把三码写入
build.info.properties,Artifactory 自动生成 SBOM 接口供监管审计。 - 若 Artifactory 采用“边缘节点 + 中心节点”同步,需在中心节点再触发一次“聚合 webhook”,把多节点报告汇总到总部安全大屏,避免飞书群消息轰炸。