zc2 0.0.43

P2P compute broker with credit-based billing, WAL, and broker mesh support
# Security scanning for zakuro-ai/zc.
#
# Closes #44. Hardened per #99.
#
# Tiered for the runner budget (zakuro-ai CI policy 2026-10-06):
#   T0 (PR into master/release): cargo-audit (required, fast) + gitleaks on
#      the PR diff only.
#   T2 (nightly, release/** via Build's security-full job, PRs based on
#      release/**, PRs labelled ci:full, manual): everything below, incl.
#      cargo-deny, gitleaks full history and trivy fs.
#   No run on master push: Build gates deploy; the nightly covers the rest.
#
# Three lanes:
#   - cargo-audit + cargo-deny — Rust advisory DB + licence/source policy.
#                                cargo-audit is a REQUIRED check: a new CVE
#                                in the dependency tree blocks merge. The
#                                cargo-deny sub-steps remain advisory.
#   - gitleaks                 — secrets in history / working tree (advisory).
#   - trivy fs                 — filesystem-level CVEs (catches Dockerfile
#                                base-image issues + lockfile vulns; advisory).
#
# To finish the rollout: add the `cargo-audit` job to branch-protection
# required checks (see the PR for the gh api command), then drop the
# remaining `continue-on-error` blocks lane-by-lane as each baseline is
# confirmed.

name: security

on:
  pull_request:
    types: [opened, synchronize, reopened, ready_for_review, edited]
    paths-ignore:
      - "**.md"
      - "docs/**"
      - ".github/ISSUE_TEMPLATE/**"
  schedule:
    # Nightly 18:00 UTC full scan against the latest advisory DBs.
    - cron: "0 18 * * *"
  workflow_dispatch:
  # Called by build.yml on release/** pushes so the release gate runs T2.
  workflow_call:

concurrency:
  group: security-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: ${{ github.event_name == 'pull_request' }}

env:
  # T2 scope: anything that is not a plain PR into the default branch.
  FULL_SCAN: ${{ github.event_name != 'pull_request' || contains(github.event.pull_request.labels.*.name, 'ci:full') || startsWith(github.base_ref, 'release/') }}

jobs:
  runner:
    uses: zakuro-ai/zakuro-infra/.github/workflows/pick-runner.yml@main
    permissions: { actions: read }
    secrets: inherit
    if: >-
      github.event_name != 'pull_request' || (
      github.event.pull_request.draft == false &&
      (github.event.action != 'edited' || github.event.changes.base != null) &&
      (github.base_ref == github.event.repository.default_branch || startsWith(github.base_ref, 'release/')))

  cargo-audit:
    needs: runner
    runs-on: ${{ needs.runner.outputs.runner }}
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0  # v7.0.0

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@29eef336d9b2848a0b548edc03f92a220660cdb8  # stable
        with:
          toolchain: "stable"

      - uses: Swatinem/rust-cache@e18b497796c12c097a38f9edb9d0641fb99eee32  # v2

      # Pull prebuilt cargo-audit / cargo-deny binaries instead of
      # `cargo install --locked` (which compiled both from source and was
      # ~8 min of this job's runtime). taiki-e/install-action fetches
      # pinned release binaries in seconds.
      - name: Install cargo-audit + cargo-deny
        uses: taiki-e/install-action@9e1e5806d4a4822de933115878265be9aaa786d9  # v2
        with:
          tool: cargo-audit,cargo-deny

      - name: cargo audit
        run: cargo audit --ignore RUSTSEC-0000-0000

      - name: cargo deny check advisories
        if: env.FULL_SCAN == 'true'
        run: cargo deny check advisories
        continue-on-error: true

      - name: cargo deny check bans + licenses + sources
        if: env.FULL_SCAN == 'true'
        run: cargo deny check bans licenses sources
        continue-on-error: true

  gitleaks:
    needs: runner
    runs-on: ${{ needs.runner.outputs.runner }}
    timeout-minutes: 10
    continue-on-error: true
    permissions:
      contents: read
      security-events: write
    steps:
      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0  # v7.0.0
        with:
          fetch-depth: 0

      - name: gitleaks
        uses: gitleaks/gitleaks-action@e0c47f4f8be36e29cdc102c57e68cb5cbf0e8d1e  # v3
        with:
          # PR: only the commits the PR adds. T2: full history.
          args: >-
            detect --source . --report-format sarif --report-path gitleaks.sarif
            ${{ github.event_name == 'pull_request' && format('--log-opts={0}..{1}', github.event.pull_request.base.sha, github.event.pull_request.head.sha) || '' }}
        continue-on-error: true

      - name: Upload gitleaks SARIF
        if: always()
        uses: github/codeql-action/upload-sarif@8aad20d150bbac5944a9f9d289da16a4b0d87c1e  # v4
        with:
          sarif_file: gitleaks.sarif
          category: gitleaks
        continue-on-error: true

  trivy-fs:
    needs: runner
    # T2 only (nightly / release / ci:full).
    if: github.event_name != 'pull_request' || contains(github.event.pull_request.labels.*.name, 'ci:full') || startsWith(github.base_ref, 'release/')
    runs-on: ${{ needs.runner.outputs.runner }}
    timeout-minutes: 10
    continue-on-error: true
    permissions:
      contents: read
      security-events: write
    steps:
      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0  # v7.0.0

      - name: Trivy filesystem scan
        uses: aquasecurity/trivy-action@ed142fd0673e97e23eac54620cfb913e5ce36c25  # v0.36.0
        with:
          scan-type: fs
          scan-ref: .
          format: sarif
          output: trivy.sarif
          severity: CRITICAL,HIGH
          ignore-unfixed: true

      - name: Upload Trivy SARIF
        if: always()
        uses: github/codeql-action/upload-sarif@8aad20d150bbac5944a9f9d289da16a4b0d87c1e  # v4
        with:
          sarif_file: trivy.sarif
          category: trivy-fs
        continue-on-error: true