如何基于“SARIF”格式上传结果到GitHub Security?

解读

在国内 Spring Cloud 微服务落地的真实面试中,面试官问“如何把 SARIF 上传到 GitHub Security”,并不是想听你背 GitHub 文档,而是考察三条线:

  1. 是否理解 DevSecOps 在微服务流水线中的位置——Spring Cloud 项目最终要走到云原生交付,安全左移必须闭环;
  2. 是否亲手配置过 CI/CD(GitHub Actions 或自建 Jenkins)——能讲出“谁生成、谁上传、谁消费”;
  3. 是否踩过国内网络、合规、Token 权限的坑——比如 GITHUB_TOKEN 的权限模型、Artifact 大小限制、SARIF 版本兼容性。
    一句话,你要让面试官确信:你不仅会把扫描报告推上去,还能在 Spring Cloud nightly build 里让它自动阻断高危依赖。

知识点

  1. SARIF 2.1.0 规范核心字段:runs.results.ruleId、level、locations.physicalLocation,GitHub 只认 2.1.0 版本。
  2. GitHub Security 接收端规则:
    • 单个文件 ≤ 20 MB,单次上传 ≤ 5000 results,超过必须分片。
    • 仅识别 .sarif 后缀,MIME 必须为 application/json。
  3. 上传身份:
    • PR 事件使用 GITHUB_TOKEN(需赋予 security-events: write);
    • 自建 Jenkins 通过 GitHub App 安装令牌,国内机房需配置 HTTPS_PROXY 否则上传 403。
  4. Spring Cloud 常用扫描器与 SARIF 输出:
    • OWASP Dependency Check Maven 插件 ≥ 7.0.0,配置 format=sarif;
    • SpotBugs 4.7+ 加 sarif 插件,可扫出 Spring 注入风险;
    • CodeQL 对 Kotlin/Spring 支持良好,但国内编译节点需缓存 ~/.m2 不然 30 min 超时。
  5. 上传动作:
    • GitHub Actions 官方 upload-sarif@v3,内部调用 POST /repos/owner/repo/code-scanning/sarifs;
    • 自建 Jenkins 可用 gh 命令行:gh api --method POST -H "Accept: application/vnd.github+json" -H "Authorization: Bearer $GH_TOKEN" --input report.sarif /repos/{owner}/{repo}/code-scanning/sarifs。
  6. 合规细节:
    • 若公司代码托管在 GitHub Enterprise Server 3.8+,需确认管理员开启 Code Scanning 功能开关;
    • 涉密项目要脱敏,SARIF 里 artifactLocation.uri 不能含内网 IP,建议统一替换为相对路径。

答案

以 Spring Cloud 仓库为例,生产级做法分四步:

  1. 生成:在 Maven 的 verify 阶段,同时跑 Dependency Check 与 SpotBugs,输出 SARIF。

    <plugin>
      <groupId>org.owasp</groupId>
      <artifactId>dependency-check-maven</artifactId>
      <version>8.2.1</version>
      <configuration>
        <formats>
          <format>sarif</format>
        </formats>
        <failBuildOnCVSS>7</failBuildOnCVSS>
      </configuration>
      <executions>
        <execution>
          <goals><goal>check</goal></goals>
        </execution>
      </executions>
    </plugin>
    

    运行后得到 target/dependency-check-report.sarif。

  2. 校验:用官方 sarif-tools 做预检,防止因为版本或 schema 错误导致上传 400。

    pip install sarif-tools
    sarif validate target/dependency-check-report.sarif
    
  3. 上传:

    • 若用 GitHub Actions,在 workflow 末尾加:
      - name: Upload SARIF
        uses: github/codeql-action/upload-sarif@v3
        with:
          sarif_file: target/dependency-check-report.sarif
        permissions:
          security-events: write
      
    • 若用 Jenkins(国内机房),先安装 GitHub App 并拿到 JWT,再通过 gh 上传:
      export GH_TOKEN=$(gh auth login --with-token < <(echo $JWT))
      gh api --method POST \
        -H "Accept: application/vnd.github+json" \
        /repos/mycompany/spring-cloud-order/code-scanning/sarifs \
        --input target/dependency-check-report.sarif
      
      注意设置 HTTPS_PROXY 指向公司出口代理,否则上传会超时。
  4. 闭环:在 GitHub Security 面板看到告警后,利用 code-scanning-alert-id 调用 REST API 自动创建 Jira 缺陷,并在 Spring Cloud 配置中心增加属性 security.owasp.fail-on-cvss=7,实现配置即代码的强制基线。

拓展思考

  1. 如果公司代码在 Gitee 或私有 GitLab,无法直接消费 SARIF,可以把报告转成 JUnit XML 用极狐 GitLab Security Dashboard,或者自建 SARIF 可视化服务(基于 Microsoft sarif-web-component),再通过 Webhook 把高危漏洞推送到 Spring Cloud 的告警中心。
  2. 对于多模块 Spring Cloud 单体仓库,建议在矩阵构建里按模块拆分 SARIF,然后在上传前用 sarif-multitool merge 聚合,避免结果数超过 5000 被 GitHub 拒绝。
  3. 当项目引入 Spring Native 后,类路径在编译期被 GraalVM 重写,CodeQL 会丢失字节码,此时应改用 Semgrep 输出 SARIF,并在上传前把 uri 路径映射回源码目录,确保 GitHub 能正确定位到行号。