|
1 | 1 | name: CodeQL SAST |
2 | 2 |
|
| 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. |
3 | 15 | on: |
4 | 16 | push: |
5 | 17 | branches: [main] |
6 | | - pull_request: |
7 | | - branches: [main] |
| 18 | + schedule: |
| 19 | + - cron: "23 5 * * 1" |
| 20 | + workflow_dispatch: |
| 21 | + workflow_call: |
8 | 22 |
|
9 | 23 | permissions: |
10 | 24 | contents: read |
11 | 25 |
|
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 | | - |
20 | 26 | jobs: |
21 | 27 | analyze: |
22 | 28 | 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' }} |
23 | 38 | # CodeQL needs security-events: write to upload results; scoped to this job so |
24 | 39 | # the workflow's top-level token stays read-only (Scorecard TokenPermissions). |
25 | 40 | permissions: |
26 | 41 | security-events: write |
27 | 42 | actions: read |
28 | 43 | 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 |
29 | 53 | steps: |
30 | 54 | - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
31 | 55 |
|
|
45 | 69 | uses: github/codeql-action/analyze@1c5b675653bb5c22dbe9b12b556ec555138e09fd # v4.38.1 |
46 | 70 | with: |
47 | 71 | 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) ===" |
0 commit comments