Skip to content

Commit 5b09c71

Browse files
committed
ci: add tirith security scan and fail-fast preflight gate
Add a security gate to the CI workflow so the expensive build/test work runs only after the cheap, high-signal security checks pass. Two new jobs run unconditionally and in parallel at the front of the pipeline (not gated on `changes`, so they cover docs-only PRs and fork PRs where local git hooks never run): - `tirith-scan`: scans the whole checkout with tirith (https://tirith.sh/docs/ci/), uploads SARIF to the Security tab, and gates downstream CI. Wired on `pull_request` (not `pull_request_target`), so fork PRs only get a read-only token and cannot leak secrets while their code is scanned; the SARIF upload is therefore skipped for forks (the scan and gate still run). tirith is installed from the flake's pinned nixpkgs to match the dev shell. - `preflight`: runs the existing `gitleaks` and `treefmt` flake checks (treefmt carries actionlint + zizmor with the repo's exact ignore config) as an early fail-fast gate before any build/test work. `test` and `build-native-packages` now `needs` both gates, so they are skipped unless the gates pass. tirith details: - `--fail-on high` blocks on high/critical findings. tirith's `--ci` exit code is 0 = clean, 1 = at/above threshold, 2 = findings below threshold; the gate treats 0/2 as pass and 1 (or any other code) as a block, and the scan exit code is passed via env to avoid template injection. - `*.snap` is excluded because terminal snapshot test fixtures intentionally contain ANSI escapes that trip the `ansi_escapes` rule; the rule stays active for real source files. Heavy gate jobs run on blacksmith runners, consistent with the rest of the nix-based CI jobs.
1 parent 17911d6 commit 5b09c71

1 file changed

Lines changed: 102 additions & 2 deletions

File tree

‎.github/workflows/ci.yaml‎

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,106 @@ jobs:
2121
- id: detect
2222
uses: ./.github/actions/detect-code-changes
2323

24+
# Security gate: scan the repository with tirith (https://tirith.sh/docs/ci/)
25+
# before any of the real CI work runs. This is wired on `pull_request` (not
26+
# `pull_request_target`), so fork PRs only ever get a read-only token and
27+
# cannot leak secrets while we scan their code. The downstream jobs `needs`
28+
# this job, so they are skipped unless tirith passes. tirith is installed from
29+
# the flake's pinned nixpkgs so CI matches the dev shell.
30+
tirith-scan:
31+
name: tirith security scan
32+
runs-on: blacksmith-32vcpu-ubuntu-2404
33+
timeout-minutes: 15
34+
permissions:
35+
contents: read
36+
# Publish SARIF to the Security tab. Only effective for same-repo PRs and
37+
# pushes to main; fork PRs get a read-only token, so the upload is gated
38+
# to non-fork events below (the scan and gate still run for forks).
39+
security-events: write
40+
steps:
41+
- uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
42+
with:
43+
ref: ${{ github.event.pull_request.head.sha || github.sha }}
44+
persist-credentials: false
45+
- uses: ./.github/actions/setup-nix-cache
46+
- name: Install tirith
47+
run: nix profile install --inputs-from . nixpkgs#tirith
48+
# One pass produces the SARIF report and sets the gate exit code. tirith's
49+
# `--ci` exit code is: 0 = no findings, 1 = findings at/above --fail-on,
50+
# 2 = findings present but all below it. We capture it (rather than letting
51+
# it abort the step) so the SARIF still uploads before the gate decides.
52+
#
53+
# `*.snap` are excluded: they are terminal snapshot test fixtures that
54+
# intentionally contain ANSI escape sequences, so the `ansi_escapes` rule
55+
# (High) fires on them as a false positive. The rule stays active for real
56+
# source files; we only skip the test data.
57+
- name: Scan repository with tirith
58+
id: scan
59+
run: |
60+
set +e
61+
tirith scan --ci --fail-on high --exclude '*.snap' --format sarif ./ > results.sarif
62+
code=$?
63+
set -e
64+
echo "exit_code=$code" >> "$GITHUB_OUTPUT"
65+
echo "tirith exited ${code}"
66+
- name: Summarize findings
67+
if: always()
68+
run: |
69+
count="$(jq '[.runs[].results[]?] | length' results.sarif 2>/dev/null || echo 0)"
70+
echo "tirith reported ${count} finding(s)" | tee -a "$GITHUB_STEP_SUMMARY"
71+
jq -r '.runs[].results[]? | "- [\(.level // "warning")] \(.ruleId): \(.locations[0].physicalLocation.artifactLocation.uri)"' \
72+
results.sarif 2>/dev/null | tee -a "$GITHUB_STEP_SUMMARY" || true
73+
- name: Upload SARIF to the Security tab
74+
if: ${{ always() && !github.event.pull_request.head.repo.fork }}
75+
uses: github/codeql-action/upload-sarif@8aad20d150bbac5944a9f9d289da16a4b0d87c1e # v4.36.2
76+
with:
77+
sarif_file: results.sarif
78+
category: tirith
79+
- name: Gate CI on tirith findings
80+
if: always()
81+
env:
82+
SCAN_EXIT: ${{ steps.scan.outputs.exit_code }}
83+
run: |
84+
case "$SCAN_EXIT" in
85+
0 | 2)
86+
echo "tirith gate passed (exit ${SCAN_EXIT}: no findings, or all below 'high')"
87+
;;
88+
1)
89+
echo "::error::tirith found findings at or above the 'high' threshold; blocking downstream CI"
90+
exit 1
91+
;;
92+
*)
93+
echo "::error::tirith scan errored (exit ${SCAN_EXIT}); blocking downstream CI"
94+
exit 1
95+
;;
96+
esac
97+
98+
# Fast, unconditional fail-fast gate for the cheap-but-high-signal checks:
99+
# secret detection (gitleaks) and workflow/format lint (the treefmt check,
100+
# which runs actionlint + zizmor with the repo's exact ignore config). Runs in
101+
# parallel with tirith-scan and is NOT gated on `changes`, so secrets and
102+
# workflow regressions are caught even on docs-only PRs and on fork PRs (where
103+
# local git hooks never run). Both are existing flake checks, so this reuses
104+
# their exact behavior instead of re-specifying flags here. The heavy test job
105+
# re-runs them inside `nix flake check`; that is intentional duplication so the
106+
# gate can fail fast before any build/test work starts.
107+
preflight:
108+
name: security & lint preflight
109+
runs-on: blacksmith-32vcpu-ubuntu-2404
110+
timeout-minutes: 15
111+
permissions:
112+
contents: read
113+
steps:
114+
- uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
115+
with:
116+
ref: ${{ github.event.pull_request.head.sha || github.sha }}
117+
persist-credentials: false
118+
- uses: ./.github/actions/setup-nix-cache
119+
- name: Secret scan and workflow lint
120+
run: nix build --print-build-logs .#checks.x86_64-linux.gitleaks .#checks.x86_64-linux.treefmt
121+
24122
test:
25-
needs: changes
123+
needs: [changes, tirith-scan, preflight]
26124
if: needs.changes.outputs.code-changed == 'true'
27125
runs-on: blacksmith-32vcpu-ubuntu-2404
28126
timeout-minutes: 30
@@ -50,7 +148,7 @@ jobs:
50148
just test-node
51149
52150
build-native-packages:
53-
needs: changes
151+
needs: [changes, tirith-scan, preflight]
54152
if: needs.changes.outputs.code-changed == 'true'
55153
name: ${{ matrix.name }}
56154
permissions:
@@ -394,6 +492,8 @@ jobs:
394492
action-timeline:
395493
needs:
396494
- changes
495+
- tirith-scan
496+
- preflight
397497
- test
398498
- build-native-packages
399499
- npm-publish-dry-run-and-upload-pkg-pr-now

0 commit comments

Comments
 (0)