Skip to content

Commit 5afdfca

Browse files
committed
ci(codeql): run CodeQL on main, dry run and release, not per PR
CodeQL ran on every pull_request push, and PR CI's security / codeql-gate job polled up to 150 min for that run before ci-ok could pass. With ~30 PRs pushed at once, the gate jobs held runners while the CodeQL runs they waited for queued behind them, and PR CI starved for hours. - codeql.yml: triggers are push to main, a weekly schedule, workflow_call and workflow_dispatch; pull_request is gone. The open-alert check moves here, unchanged and still fail-closed, as the `alerts` job (needs analyze), so every main push and every weekly run fails while any code-scanning alert is open. Push runs keep cancel-in-progress; called and scheduled runs get a group of their own. The analysis key is pinned to the push configuration so dry-run/release uploads do not open configurations of their own on main. - _security.yml: codeql-gate removed; callers grant contents: read only. - dry-run.yml, release.yml: a `codeql` job calls codeql.yml on the dispatched commit; release verify needs it and requires success. - select-lanes.sh, require-all-green.sh: the codeql-gate lane is gone from the selector universe and from ci-ok's job map; no other lane changes. - tests: the fail-closed contract follows the check to codeql.yml and now also pins the triggers, the derived callers' grants and release verify; lane fixtures drop codeql-gate. - SECURITY.md, MAINTAINERS.md: say where CodeQL runs now. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 4cf4414 commit 5afdfca

15 files changed

Lines changed: 236 additions & 192 deletions

‎.github/workflows/_lint.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Reusable: lint only (cppcheck + clang-format).
2-
# Security-static and CodeQL gate are separate — see _security.yml.
2+
# Security-static is separate — see _security.yml; CodeQL — see codeql.yml.
33
name: Lint & Security
44

55
on:

‎.github/workflows/_security.yml‎

Lines changed: 3 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# Reusable: security-static + CodeQL gate.
1+
# Reusable: security-static + license gate.
2+
# CodeQL is not here: PRs do not run it. It runs on push to main, weekly, and
3+
# inside dry-run.yml / release.yml, which call codeql.yml directly.
24
# Runs independently from lint/test/build — does not block the main pipeline.
35
# Affects overall workflow success status.
46
name: Security Gate
@@ -52,96 +54,3 @@ jobs:
5254
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5355
run: scripts/audit-license-provenance.py
5456

55-
codeql-gate:
56-
if: ${{ inputs.lanes == 'all' || contains(fromJSON(inputs.lanes), 'codeql-gate') }}
57-
runs-on: ubuntu-latest
58-
timeout-minutes: 240
59-
# Without security-events:read this job inherits the workflow's
60-
# contents:read, the code-scanning alert API answers 403, and the gate
61-
# below reported "0 alerts" on every run it has ever made. actions:read is
62-
# what the CodeQL-run poll above needs for the same reason.
63-
permissions:
64-
contents: read
65-
security-events: read
66-
actions: read
67-
steps:
68-
- name: Wait for CodeQL on current commit (max 150 min)
69-
env:
70-
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
71-
run: |
72-
# On pull_request events github.sha is the synthetic merge commit;
73-
# CodeQL runs are recorded against the PR head SHA.
74-
CURRENT_SHA="${{ github.event.pull_request.head.sha || github.sha }}"
75-
echo "Waiting for CodeQL to complete on $CURRENT_SHA..."
76-
# 300 attempts x 30s = 150 min. The previous budget was 90 x 30s = 45 min,
77-
# which is shorter than CodeQL actually takes on this repository: a measured
78-
# run on PR #1426 (head 7b72652a) completed with conclusion=success after
79-
# 124 min, 7 minutes AFTER this gate had already given up. That marked at
80-
# least five contributor PRs red for a scan that passed. The job's own
81-
# timeout-minutes is 240, so 150 still leaves headroom.
82-
for attempt in $(seq 1 300); do
83-
LATEST=$(gh api "repos/${{ github.repository }}/actions/workflows/codeql.yml/runs?head_sha=$CURRENT_SHA&per_page=1" \
84-
--jq '.workflow_runs[] | "\(.conclusion) \(.status)"' 2>/dev/null | head -1 || echo "")
85-
if [ -z "$LATEST" ]; then
86-
echo " $attempt/300: no run yet..."; sleep 30; continue
87-
fi
88-
CONCLUSION=$(echo "$LATEST" | cut -d' ' -f1)
89-
STATUS=$(echo "$LATEST" | cut -d' ' -f2)
90-
if [ "$STATUS" = "completed" ] && [ "$CONCLUSION" = "success" ]; then
91-
echo "=== CodeQL passed ==="; exit 0
92-
elif [ "$STATUS" = "completed" ]; then
93-
echo "BLOCKED: CodeQL $CONCLUSION"; exit 1
94-
fi
95-
echo " $attempt/300: $STATUS..."; sleep 30
96-
done
97-
echo "BLOCKED: CodeQL timeout"; exit 1
98-
99-
- name: Check for open code scanning alerts
100-
env:
101-
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
102-
run: |
103-
# FAIL CLOSED. The previous form was `gh api ... 2>/dev/null || echo "0"`,
104-
# which looks defensive and is the opposite. gh writes the API error
105-
# BODY to stdout, so a 403 made ALERTS the string
106-
# {"message":"Resource not accessible by integration",...,"status":"403"}0
107-
# every `[` comparison then failed as non-integer, the `if` took its
108-
# false branch, and the step printed "CodeQL gate passed (0 alerts)"
109-
# and exited 0. That is exactly how a security gate should NEVER fail:
110-
# silently, in the direction of "everything is fine". An unreadable
111-
# alert list is now an error, not a zero.
112-
# NOTE: this runs in a command substitution, so it must `return`, not
113-
# `exit` -- an `exit` here would only end the subshell and let the
114-
# caller continue with an empty count, recreating the very bug this
115-
# replaces. The callers below check the status explicitly.
116-
count_open_alerts() {
117-
local body status
118-
body=$(gh api 'repos/${{ github.repository }}/code-scanning/alerts?state=open' \
119-
--jq 'length' 2>/dev/null)
120-
status=$?
121-
if [ $status -ne 0 ]; then
122-
echo "BLOCKED: cannot read code scanning alerts (gh exit $status)." >&2
123-
echo " Refusing to treat an unreadable alert list as zero." >&2
124-
echo " Most likely cause: the job lacks security-events:read." >&2
125-
return 1
126-
fi
127-
case "$body" in
128-
''|*[!0-9]*)
129-
echo "BLOCKED: alert count was not a number: ${body:-<empty>}" >&2
130-
return 1
131-
;;
132-
esac
133-
printf '%s' "$body"
134-
}
135-
echo "Waiting 60s for alert API to settle..."
136-
sleep 60
137-
ALERTS=$(count_open_alerts) || exit 1
138-
sleep 15
139-
ALERTS2=$(count_open_alerts) || exit 1
140-
[ "$ALERTS" -lt "$ALERTS2" ] && ALERTS=$ALERTS2
141-
if [ "$ALERTS" -gt 0 ]; then
142-
echo "BLOCKED: $ALERTS open alert(s)"
143-
gh api 'repos/${{ github.repository }}/code-scanning/alerts?state=open' \
144-
--jq '.[] | " #\(.number) [\(.rule.security_severity_level // .rule.severity)] \(.rule.id) — \(.most_recent_instance.location.path):\(.most_recent_instance.location.start_line)"' 2>/dev/null || true
145-
exit 1
146-
fi
147-
echo "=== CodeQL gate passed (0 alerts) ==="

‎.github/workflows/codeql.yml‎

Lines changed: 104 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,55 @@
11
name: CodeQL SAST
22

3+
# CodeQL runs on main, on a schedule and inside the dry run and the release --
4+
# never per pull request. A per-PR analysis (40-120 min each) plus the PR-CI
5+
# job that waited up to 150 min for it held runners that the PR legs needed:
6+
# with ~30 PRs pushed at once, PR CI starved for hours.
7+
#
8+
# push to main every merge is analysed; the `alerts` job fails the run
9+
# while any code-scanning alert is open (alert policy).
10+
# schedule weekly, so a CodeQL query update that finds something in
11+
# unchanged code is noticed without a push.
12+
# workflow_call dry-run.yml and release.yml analyse the exact commit they
13+
# test; their `codeql` job (analyze + alerts) must pass.
14+
# workflow_dispatch a manual re-run on any ref.
315
on:
416
push:
517
branches: [main]
6-
pull_request:
7-
branches: [main]
18+
schedule:
19+
- cron: "23 5 * * 1"
20+
workflow_dispatch:
21+
workflow_call:
822

923
permissions:
1024
contents: read
1125

12-
# One analysis per pull request and one per pushed branch. A newer push to main
13-
# cancels the analysis of an older main commit; only the newest commit's
14-
# results matter for main, and a merge-heavy day otherwise queues one 40-100
15-
# minute analysis per merge ahead of every pull request run.
16-
concurrency:
17-
group: codeql-${{ github.event_name }}-${{ github.event.pull_request.number || github.ref }}
18-
cancel-in-progress: true
19-
2026
jobs:
2127
analyze:
2228
runs-on: ubuntu-latest
29+
# A newer push to main cancels the analysis of an older main commit; only
30+
# the newest commit's results matter for main. Every other trigger gets a
31+
# group of its own and is never cancelled, so a dry run and a release
32+
# dispatched together cannot cancel each other's gate. In a called workflow
33+
# `github` is the CALLER's context: github.workflow names the dry run or
34+
# the release, github.run_id is the caller's run.
35+
concurrency:
36+
group: codeql-${{ github.event_name == 'push' && github.ref || format('{0}-{1}', github.workflow, github.run_id) }}
37+
cancel-in-progress: ${{ github.event_name == 'push' }}
2338
# CodeQL needs security-events: write to upload results; scoped to this job so
2439
# the workflow's top-level token stays read-only (Scorecard TokenPermissions).
2540
permissions:
2641
security-events: write
2742
actions: read
2843
contents: read
44+
env:
45+
# One code-scanning configuration for every trigger. The action derives
46+
# the key from the RUN's workflow file, which for a called workflow is
47+
# the caller: dry-run.yml / release.yml would each open a configuration
48+
# of their own on main, and an alert fixed by a later push would stay
49+
# open in the stale one until the next dry run. The value is the key the
50+
# push-to-main analysis has always used (the action documents this
51+
# variable as settable for non-standard setups).
52+
CODEQL_ACTION_ANALYSIS_KEY: .github/workflows/codeql.yml:analyze
2953
steps:
3054
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
3155

@@ -45,3 +69,73 @@ jobs:
4569
uses: github/codeql-action/analyze@1c5b675653bb5c22dbe9b12b556ec555138e09fd # v4.38.1
4670
with:
4771
category: "/language:c-cpp"
72+
73+
# Alert policy: the default branch carries ZERO open code-scanning alerts.
74+
# Any open alert -- a new finding from a merge, from the weekly run, or one
75+
# this commit adds -- fails this job: the main commit turns red, GitHub
76+
# notifies whoever triggered the run, and the dry run and the release (which
77+
# call this workflow) cannot pass until the alert is fixed or dismissed with
78+
# a reason in the Security tab. With a zero baseline, "any open alert" is
79+
# exactly "a new alert". Moved here unchanged from _security.yml's former
80+
# PR-CI codeql-gate job; it reads the default branch's alerts, as it did there.
81+
alerts:
82+
needs: [analyze]
83+
runs-on: ubuntu-latest
84+
timeout-minutes: 10
85+
# Without security-events:read this job inherits the workflow's
86+
# contents:read, the code-scanning alert API answers 403, and the former
87+
# gate reported "0 alerts" on every run it ever made.
88+
permissions:
89+
contents: read
90+
security-events: read
91+
steps:
92+
- name: Check for open code scanning alerts
93+
env:
94+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
95+
run: |
96+
# FAIL CLOSED. The previous form was `gh api ... 2>/dev/null || echo "0"`,
97+
# which looks defensive and is the opposite. gh writes the API error
98+
# BODY to stdout, so a 403 made ALERTS the string
99+
# {"message":"Resource not accessible by integration",...,"status":"403"}0
100+
# every `[` comparison then failed as non-integer, the `if` took its
101+
# false branch, and the step printed "CodeQL gate passed (0 alerts)"
102+
# and exited 0. That is exactly how a security gate should NEVER fail:
103+
# silently, in the direction of "everything is fine". An unreadable
104+
# alert list is now an error, not a zero.
105+
# NOTE: this runs in a command substitution, so it must `return`, not
106+
# `exit` -- an `exit` here would only end the subshell and let the
107+
# caller continue with an empty count, recreating the very bug this
108+
# replaces. The callers below check the status explicitly.
109+
count_open_alerts() {
110+
local body status
111+
body=$(gh api 'repos/${{ github.repository }}/code-scanning/alerts?state=open' \
112+
--jq 'length' 2>/dev/null)
113+
status=$?
114+
if [ $status -ne 0 ]; then
115+
echo "BLOCKED: cannot read code scanning alerts (gh exit $status)." >&2
116+
echo " Refusing to treat an unreadable alert list as zero." >&2
117+
echo " Most likely cause: the job lacks security-events:read." >&2
118+
return 1
119+
fi
120+
case "$body" in
121+
''|*[!0-9]*)
122+
echo "BLOCKED: alert count was not a number: ${body:-<empty>}" >&2
123+
return 1
124+
;;
125+
esac
126+
printf '%s' "$body"
127+
}
128+
echo "Waiting 60s for alert API to settle..."
129+
sleep 60
130+
ALERTS=$(count_open_alerts) || exit 1
131+
sleep 15
132+
ALERTS2=$(count_open_alerts) || exit 1
133+
[ "$ALERTS" -lt "$ALERTS2" ] && ALERTS=$ALERTS2
134+
if [ "$ALERTS" -gt 0 ]; then
135+
echo "::error title=Open code scanning alerts::$ALERTS open alert(s) on the default branch -- fix, or dismiss with a reason in the Security tab"
136+
echo "BLOCKED: $ALERTS open alert(s)"
137+
gh api 'repos/${{ github.repository }}/code-scanning/alerts?state=open' \
138+
--jq '.[] | " #\(.number) [\(.rule.security_severity_level // .rule.severity)] \(.rule.id) — \(.most_recent_instance.location.path):\(.most_recent_instance.location.start_line)"' 2>/dev/null || true
139+
exit 1
140+
fi
141+
echo "=== CodeQL gate passed (0 alerts) ==="

‎.github/workflows/dry-run.yml‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
# Each step can be skipped for faster iteration.
33
#
44
# Pipeline: lint → test → build + candidate scan/select → smoke/soak
5-
# Security: security-static + codeql-gate (independent island)
5+
# Security: security-static + license-gate (independent island)
6+
# CodeQL: codeql.yml on this commit (analyze + alerts) (independent island)
67
#
7-
# Security does NOT block lint/test/build/smoke/soak. All jobs must pass
8+
# Security and CodeQL do NOT block lint/test/build/smoke/soak. All jobs must pass
89
# for the overall workflow to be green.
910
#
1011
# Candidate VirusTotal scanning and tuple-local selection are part of the
@@ -62,15 +63,24 @@ jobs:
6263
# ── Security (independent island — does not block main pipeline) ──
6364
security:
6465
uses: ./.github/workflows/_security.yml
65-
# The called job requests security-events:read to count code-scanning
66-
# alerts; a reusable workflow cannot request more than its caller grants,
67-
# so withholding this here fails the whole run at startup.
66+
# No job in _security.yml reads code scanning or the Actions API any more
67+
# (the CodeQL gate moved to codeql.yml), so the token stays read-only.
6868
permissions:
6969
contents: read
70-
security-events: read
71-
actions: read
7270
secrets: inherit
7371

72+
# ── CodeQL: analysis of this exact commit + the open-alert check ──
73+
# PRs do not run CodeQL; the dry run and the release are where a CodeQL pass
74+
# is required. codeql.yml's analyze job uploads results and its alerts job
75+
# reads them: a called workflow cannot request more than its caller grants,
76+
# so withholding either permission fails the whole run at startup.
77+
codeql:
78+
uses: ./.github/workflows/codeql.yml
79+
permissions:
80+
contents: read
81+
security-events: write
82+
actions: read
83+
7484
# ── Lint (cppcheck + clang-format) ────────────────────────────
7585
lint:
7686
if: ${{ inputs.skip_lint != true }}

‎.github/workflows/pr.yml‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,10 @@ jobs:
2626
security:
2727
needs: [changes]
2828
uses: ./.github/workflows/_security.yml
29-
# The called job requests security-events:read to count code-scanning
30-
# alerts; a reusable workflow cannot request more than its caller grants,
31-
# so withholding this here fails the whole run at startup.
29+
# No job in _security.yml reads code scanning or the Actions API any more
30+
# (the CodeQL gate moved to codeql.yml), so the token stays read-only.
3231
permissions:
3332
contents: read
34-
security-events: read
35-
actions: read
3633
secrets: inherit
3734
with:
3835
lanes: ${{ needs.changes.outputs.lanes }}

‎.github/workflows/release.yml‎

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Release pipeline: lint → test → build → smoke/soak → draft → verify → publish
22
#
3-
# Security (security-static + codeql-gate) starts immediately and runs in
4-
# parallel with lint/test/build/smoke/soak. It only blocks the final verify
3+
# Security (security-static + license-gate) and CodeQL (codeql.yml: analysis
4+
# of this commit + the open-alert check) start immediately and run in
5+
# parallel with lint/test/build/smoke/soak. They only block the final verify
56
# step — everything else proceeds independently.
67
name: Release
78

@@ -44,15 +45,25 @@ jobs:
4445
# ── Security (starts immediately, blocks only verify) ───────────
4546
security:
4647
uses: ./.github/workflows/_security.yml
47-
# The called job requests security-events:read to count code-scanning
48-
# alerts; a reusable workflow cannot request more than its caller grants,
49-
# so withholding this here fails the whole run at startup.
48+
# No job in _security.yml reads code scanning or the Actions API any more
49+
# (the CodeQL gate moved to codeql.yml), so the token stays read-only.
5050
permissions:
5151
contents: read
52-
security-events: read
53-
actions: read
5452
secrets: inherit
5553

54+
# ── CodeQL (starts immediately, blocks only verify) ──────────────
55+
# The analysis of this exact commit + the open-alert check.
56+
# PRs do not run CodeQL; the dry run and the release are where a CodeQL pass
57+
# is required. codeql.yml's analyze job uploads results and its alerts job
58+
# reads them: a called workflow cannot request more than its caller grants,
59+
# so withholding either permission fails the whole run at startup.
60+
codeql:
61+
uses: ./.github/workflows/codeql.yml
62+
permissions:
63+
contents: read
64+
security-events: write
65+
actions: read
66+
5667
# ── 0. Preflight: refuse malformed dispatch inputs ──────────────
5768
# The tag is inputs.version VERBATIM. A bare "0.10.7" publishes a release the
5869
# installers can never resolve (they fetch releases/download/v<version>/...),
@@ -312,12 +323,12 @@ jobs:
312323
body: ${{ inputs.release_notes || '' }}
313324
generate_release_notes: ${{ inputs.release_notes == '' }}
314325

315-
# ── 7. Verify + Publish (requires security gate) ───────────────
326+
# ── 7. Verify + Publish (requires the security and CodeQL gates) ─
316327
verify:
317-
needs: [release-draft, security]
328+
needs: [release-draft, security, codeql]
318329
# Explicit condition: a deliberately skipped test ancestor must not skip
319330
# the publish chain; the draft itself proves smoke and soak both succeeded.
320-
if: ${{ !cancelled() && !failure() && needs.security.result == 'success' && needs.release-draft.result == 'success' }}
331+
if: ${{ !cancelled() && !failure() && needs.security.result == 'success' && needs.codeql.result == 'success' && needs.release-draft.result == 'success' }}
321332
runs-on: ubuntu-latest
322333
permissions:
323334
contents: write

‎MAINTAINERS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Operational authority is stricter than code review authority.
5252

5353
| Operation | Current authority | Project rule |
5454
| --- | --- | --- |
55-
| PR validation (`pr.yml`, DCO, CodeQL) | Automatic | Anyone may trigger it by opening or updating a PR. Required checks must pass. |
55+
| PR validation (`pr.yml`, DCO) | Automatic | Anyone may trigger it by opening or updating a PR. Required checks must pass. |
5656
| Dry run (`dry-run.yml`) | `@DeusData` | Delegated release operators may run dry runs after promotion. Dry-run delegation does not imply release authority. |
5757
| Smoke/soak/repro manual runs | `@DeusData` | Area reviewers may operate these runs when delegated for diagnosis. Results are advisory. |
5858
| Release workflow (`release.yml`) | `@DeusData` only | Owner-only until a release operator is explicitly promoted. Publishing, replacing releases, and tag movement remain owner-gated. |

0 commit comments

Comments
 (0)