garrison 0.9.0

Garrison - 面向 Rust 生态的一站式身份认证鉴权框架
Documentation
# Copyright (c) 2026 Kirky.X🌠
# SPDX-License-Identifier: Apache-2.0

name: Security

# 安全门禁工作流:SAST / 密钥扫描 / 依赖供应链 / SBOM / 模糊测试回归。
# 与 ci.yml(质量门)、codeql.yml(语义分析)互补,均 PR 阻断。
# 忽略清单:.semgrepignore(SAST 降噪,对齐 codeql-config.yml 排除测试先例)。
on:
  push:
    branches: [main, develop, "release/*"]
  pull_request:
    branches: [main, develop]
  workflow_dispatch:

permissions:
  contents: read

concurrency:
  group: security-${{ github.ref }}
  cancel-in-progress: true

env:
  CARGO_TERM_COLOR: always

jobs:
  # Semgrep SAST(registry 规则集:Rust 专项 + 密钥硬编码)
  # 忽略文件 .semgrepignore 排除 examples/tests/temp/benches/target(降噪不关检)。
  # 镜像 digest 与本地基线验证版本(1.168.0,0 findings)逐字节一致;升级时需
  # 本地重跑基线后同步 bump digest(显式升级哲学,与 actions SHA-pin 同源)。
  semgrep:
    name: Semgrep SAST (p/rust + p/secrets)
    runs-on: ubuntu-latest
    timeout-minutes: 10
    container:
      image: semgrep/semgrep@sha256:59fbed6127ea7c5dde3ba6a85142733bb20ea9aaa36120c953904f1539aaf66e # line-pinned
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false
      - name: Run semgrep (block on findings)
        run: semgrep scan --config p/rust --config p/secrets --error --metrics=off

  # gitleaks 全量 git history 密钥泄漏扫描
  # 复用根目录 .gitleaks.toml(官方 action 自动检测),.gitleaksignore 收录
  # 已逐条核实为非真实凭证的历史指纹(降噪不关检)。
  # fetch-depth: 0 —— 全量历史扫描(pre-commit 仅覆盖 staged,此处补全历史维度)
  gitleaks:
    name: Gitleaks (full history)
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false
          fetch-depth: 0
      - uses: gitleaks/gitleaks-action@e0c47f4f8be36e29cdc102c57e68cb5cbf0e8d1e # v3.0.0
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          GITLEAKS_CONFIG_FILE: .gitleaks.toml
        # --redact 语义由 action 内部传递,输出不含疑似密钥明文

  # cargo vet(供应商依赖人工审核记录,众包审计背书)
  # supply-chain/audits.toml 记录自研库(CodeNexus 系)审核;第三方依赖 init 豁免
  # 渐进收紧。generate-lockfile:库 crate 不提交 Cargo.lock(对齐 ci.yml deny job 惯例)
  vet:
    name: Cargo Vet (supply chain)
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false
      - name: Setup Rust toolchain (1.97.1)
        run: bash scripts/setup_rust.sh 1.97.1
        shell: bash
      - uses: taiki-e/install-action@b80e2bb2635f28eb98b9fce1e2f0887e1535c6b2 # v2.90.0
        with:
          tool: cargo-vet
      # 仓库已提交 Cargo.lock(与全仓 --locked 构建一致性约定同步),vet 按锁定的
      # 实际依赖集审计;不得 generate-lockfile 重解析——豁免清单按已提交锁计算,
      # 重解析会漂移出豁免覆盖面(2026-10-14 实录:CI 的 vet job 因此必红)。
      - name: Run cargo vet check (committed Cargo.lock)
        run: cargo vet check --locked

  # SBOM(CycloneDX)软件物料清单
  # 全特性面生成(覆盖 optional 依赖:dbnexus/jsonwebtoken/argon2 等),
  # 产物上传 artifact 供下游审计;release.yml 将其附加为 Release 资产。
  sbom:
    name: SBOM (CycloneDX)
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false
      - name: Setup Rust toolchain (1.97.1)
        run: bash scripts/setup_rust.sh 1.97.1
        shell: bash
      - uses: taiki-e/install-action@b80e2bb2635f28eb98b9fce1e2f0887e1535c6b2 # v2.90.0
        with:
          tool: cargo-cyclonedx
      - name: Generate Cargo.lock (library crate, not committed)
        run: cargo generate-lockfile
      - name: Generate SBOM (all features)
        run: cargo cyclonedx -f json --all-features
      - name: Upload SBOM artifact
        uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
        with:
          name: sbom-cyclonedx
          path: garrison.cdx.json

  # cargo-fuzz 语料回归(PR 阻断)
  # 每个解析入口 target 以现有 corpus 冒烟 60s;深扫由 fuzz-weekly.yml 周一定时承担。
  # taiki-e/install-action 不含 cargo-fuzz,改用 cargo install(--locked 固定 lockfile)
  fuzz-regression:
    name: Fuzz Regression (corpus smoke)
    runs-on: ubuntu-latest
    timeout-minutes: 25
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false
      - name: Setup Rust toolchain (nightly)
        run: bash scripts/setup_rust.sh nightly
        shell: bash
      - name: Install cargo-fuzz
        run: cargo install cargo-fuzz --locked
      - name: Fuzz corpus smoke (60s per target)
        run: |
          for target in fuzz_saml_xml fuzz_oidc_discovery fuzz_jwt_verify fuzz_http_auth_headers; do
            echo "::group::$target"
            cargo +nightly fuzz run "$target" -- -max_total_time=60
            echo "::endgroup::"
          done

  reproducible-build:
    name: Reproducible Build (byte-identical)
    runs-on: ubuntu-latest
    timeout-minutes: 30
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false
      - name: Setup Rust toolchain (1.97.1)
        run: bash scripts/setup_rust.sh 1.97.1
        shell: bash
      # --frozen 禁网络,而 runner 的 registry 缓存为空,须先经 fetch 落盘依赖
      # (一次联网解析+下载),否则离线解析直接报 no matching package
      - name: Fetch dependencies
        run: cargo fetch --locked
      - name: Verify byte-identical rebuild
        run: scripts/verify_reproducible_build.sh

  # 一键清零「测试夹具类」code scanning 告警(仅 workflow_dispatch 手动触发,需维护者执行)。
  # 判定纪律:仅处理 hard-coded-cryptographic-value 与 cleartext-logging 两类、且已逐条人工
  # 核实为测试代码/测试夹具的告警(fixed 占位密钥与本地化错误文案断言,非真实凭证泄漏);
  # 其余规则一律 SKIP 留给人工分诊。生产敏感值由 mask_metadata 与 inklog secret-scan 出站
  # 门控覆盖。全程留痕于本任务运行日志。
  dismiss-test-fixture-alerts:
    name: Dismiss test-fixture code scanning alerts
    if: github.event_name == 'workflow_dispatch'
    runs-on: ubuntu-latest
    timeout-minutes: 10
    concurrency:
      group: dismiss-alerts-${{ github.run_id }}
      cancel-in-progress: false
    permissions:
      contents: read
      security-events: write
    env:
      GH_TOKEN: ${{ github.token }}
    steps:
      - name: Dismiss open alerts classified as test fixtures (used_in_tests)
        run: |
          set -uo pipefail
          repo="${{ github.repository }}"
          # 2026-11-14 API 漂移对齐(0.9.0 发布轮实测):
          #   1) GraphQL 侧 `Repository.codeScanningAlerts` 与 `Mutation.resolveSecurityAlert`
          #      均已不在当前 schema(undefinedField),探针与 mutation 路径整体移除;
          #   2) PATCH 端点仅接受 `state` + `dismissed_reason`(注意过去分词 dismissed),
          #      `dismissal_reason` / `dismissal_comment` 均报 422 not permitted——豁免理由
          #      改为写进运行日志(本步骤输出即留痕),API 侧不再携带 comment。
          #   3) PUT 形(REST 文档旧示例)实测 404,仍只用 PATCH。
          echo "=== GET 诊断(状态行 + 令牌权限头)==="
          gh api -i "repos/$repo/code-scanning/alerts?state=open&per_page=2" 2>&1 | grep -E "^HTTP|x-github-token-permissions|message" | head -6 || true
          alerts="$(gh api "repos/$repo/code-scanning/alerts?state=open&per_page=100" \
            --jq '.[] | [.number, .rule.id, .most_recent_instance.location.path, .most_recent_instance.location.start_line] | @tsv' 2>&1)" || {
            echo "GET 失败,原样输出:"; echo "$alerts" | tail -5; exit 1; }
          count=0
          while IFS=$'\t' read -r num rule path line; do
            [ -n "$num" ] || continue
            case "$rule" in
              rust/hard-coded-cryptographic-value|rust/cleartext-logging) ;;
              *)
                echo "SKIP(非测试夹具类,需人工分诊): #$num $rule $path:$line"
                continue
                ;;
            esac
            echo "豁免理由(API 已不支持 dismissal_comment,留痕于此):[$rule] 测试夹具/测试代码($path:$line,已逐条人工核实):固定占位密钥与本地化错误文案断言,非真实凭证泄漏;生产路径敏感值由 mask_metadata 与 inklog secret-scan 出站门控覆盖。"
            if gh api -X PATCH "repos/$repo/code-scanning/alerts/$num" \
              -f state=dismissed \
              -f dismissed_reason="used in tests" \
              --silent; then
              count=$((count + 1))
              echo "dismissed #$num $rule $path:$line"
            else
              echo "FAILED #$num $rule $path:$line"
            fi
          done <<< "$alerts"
          echo "共豁免 $count 条"

  # actionlint / zizmor(2026-10-20 补齐)
  # actionlint:校验 workflow YAML 语法与表达式;zizmor:审计 Actions 的
  # 权限过宽、脚本注入、模板注入等风险。两者与本 workflow 已有 job 互补,不重复。
  workflow-hygiene:
    name: Workflow Hygiene (actionlint + zizmor)
    runs-on: ubuntu-latest
    timeout-minutes: 10
    permissions:
      contents: read
      security-events: write
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false
      - name: actionlint
        # 第三方 docker action 钉具体版本:latest 是可变 tag,上游可静默换镜像。
        # Docker Hub 镜像 tag 无 v 前缀(GitHub release 用 v1.7.7,镜像 tag 是 1.7.7;
        # 实测 v1.7.7 404、1.7.7 200),带 v 引用不存在 tag 会在拉镜像期失败、job 长红。
        uses: docker://rhysd/actionlint@sha256:b1934ee5f1c509618f2508e6eb47ee0d3520686341fec936f3b79331f9315667 # line-pinned
        with:
          # shellcheck 子检查降到 error 级:YAML 语法/表达式错误仍阻断,
          # 风格级(SC2086/SC2034 等)不阻断,避免打断既有流水线。
          args: -color -shellcheck=error
      - name: zizmor (GitHub Actions 安全审计)
        # 阻断式(2026-11-01 起):积压的 zizmor 告警已清零并根治成因,本步改为
        # 阻断——今后新增的 unpinned-uses / ref-version-mismatch / template-injection
        # 等发现在 CI 就红,不再攒成待分诊的 code scanning 告警。
        #
        # 根治的三条成因(详见 .github/zizmor.yml 与 scripts/setup_rust.sh):
        # 1) 第三方 action 钉 SHA 且行尾注释恰为上游 tag(ref-version-mismatch 归零,
        #    且 Dependabot 会同步维护 SHA + 注释,不再漂移);
        # 2) 移除 dtolnay/rust-toolchain(该仓仅 1 个 tag、每次发布强推重写分支,
        #    SHA 钉位必然脱离上游历史,impostor-commit / stale-action-refs 结构性不可满足);
        # 3) 静态矩阵值经 env 传入 run 块,不再内插 ${{ }}(template-injection 归零)。
        #
        # persona 显式钉 regular:与产生历史告警的档位一致(regular 报 mismatched
        # 版本注释,pedantic 额外报「缺版本注释」这类噪声)。
        # 版本修正(外部事实):`v0.12.0` 在 zizmorcore/zizmor-action 仓库不存在
        # (API tags 最高为 v0.7.1)。引用不存在的 action 在 step 调度期
        # ("Getting action download info" 阶段)即失败,step 级
        # continue-on-error 拦不住该阶段,job 以 setup 失败收场,
        # 本仓 Security workflow 因此长红(run 39447128551 及 rerun 实证)。
        uses: zizmorcore/zizmor-action@45d7276c9ca790aabd705e31ca99c6185c3e76dd # v0.8.1
        with:
          persona: regular