11name : 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.
3+ # One analysis per language: c-cpp, javascript-typescript (graph-ui, pkg/npm),
4+ # python, actions. Scope: .github/codeql/codeql-config.yml (first-party source).
75#
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.
6+ # FULL scan -- push to main, weekly, dry run, release: every language; c-cpp
7+ # traces the whole production build, vendored C included.
8+ # PR scan -- pr.yml's `codeql` job (light: true), needed by ci-ok. One leg per
9+ # language whose scope the PR changes (codeql-* lanes of
10+ # scripts/ci/select-lanes.sh; an undeterminable diff or a change to the
11+ # workflows, the build or the CodeQL machinery selects every language).
12+ # Each leg analyses its language's WHOLE first-party scope, so cross-file
13+ # data flow is kept; c-cpp compiles the vendored objects before tracing
14+ # starts, except vendored files the PR changes. Diff-informed analysis then
15+ # restricts the REPORTED alerts to the changed lines; what is extracted and
16+ # analysed is never narrowed by the diff.
17+ # There is no pull_request trigger and no job anywhere that polls for a CodeQL
18+ # run: the former PR gate waited up to 150 min per PR for a full analysis and,
19+ # with ~30 PRs pushed at once, starved PR CI for hours.
1520on :
1621 push :
1722 branches : [main]
1823 schedule :
1924 - cron : " 23 5 * * 1"
2025 workflow_dispatch :
26+ inputs :
27+ light :
28+ description : " PR-style c-cpp build (vendored objects compiled untraced) -- to time it"
29+ type : boolean
30+ default : false
2131 workflow_call :
32+ inputs :
33+ light :
34+ description : " PR scan: vendored C compiled untraced except changed files; diff-informed results"
35+ type : boolean
36+ default : false
37+ matrix :
38+ description : ' The legs to run, {"include":[...]} (pr.yml: the selected codeql-* lanes); empty = every language'
39+ type : string
40+ default : " "
2241
2342permissions :
2443 contents : read
2544
2645jobs :
2746 analyze :
2847 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.
48+ strategy :
49+ fail-fast : false
50+ # The same legs (and keys) everywhere, so a PR's analysis and main's
51+ # share one code-scanning configuration per language.
52+ matrix : ${{ fromJSON(inputs.matrix || '{"include":[{"language":"c-cpp","build-mode":"manual","lane":"codeql-cpp"},{"language":"javascript-typescript","build-mode":"none","lane":"codeql-js"},{"language":"python","build-mode":"none","lane":"codeql-py"},{"language":"actions","build-mode":"none","lane":"codeql-actions"}]}') }}
53+ # Superseded runs are always cancelled: a newer push to main cancels the
54+ # analysis of an older main commit (batched merges -> only the newest main
55+ # commit is scanned), a newer push to a PR cancels that PR's scan. Every
56+ # other trigger gets a group of its own and is never cancelled: a dry run
57+ # or a release tests ONE commit, and two dispatched together must not
58+ # cancel each other's gate. Per language, so legs never cancel each other.
59+ # In a called workflow `github` is the CALLER's context: event_name is
60+ # pull_request for pr.yml, github.workflow names the dry run or release.
3561 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' }}
38- # CodeQL needs security-events: write to upload results; scoped to this job so
39- # the workflow's top-level token stays read-only (Scorecard TokenPermissions).
62+ group : codeql-${{ matrix.language }}-${{ github.event_name == 'push' && github.ref || github.event_name == 'pull_request' && format('pr-{0}', github.event.pull_request.number) || format('{0}-{1}', github.workflow, github.run_id) }}
63+ cancel-in-progress : ${{ github.event_name == 'push' || github.event_name == 'pull_request' }}
64+ # CodeQL needs security-events: write to upload results (and read for the
65+ # alert check); scoped to this job so the workflow's top-level token stays
66+ # read-only (Scorecard TokenPermissions).
4067 permissions :
4168 security-events : write
4269 actions : read
4370 contents : read
4471 env :
4572 # One code-scanning configuration for every trigger. The action derives
4673 # 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).
74+ # the caller: pr.yml / dry-run.yml / release.yml would each open a
75+ # configuration of their own, and on main an alert fixed by a later push
76+ # would stay open in a stale one. The value is the key the push-to-main
77+ # analysis has always used (the action documents this variable as
78+ # settable for non-standard setups).
5279 CODEQL_ACTION_ANALYSIS_KEY : .github/workflows/codeql.yml:analyze
80+ # Diff-informed analysis: codeql-action v4.38.1 (CLI 2.27.0) reports a
81+ # pull request's alerts only on its changed lines when the
82+ # diff_informed_queries feature is on (default on; this variable forces
83+ # it, src/feature-flags.ts). It applies only when the event payload is a
84+ # pull request -- pr.yml's call -- and is inert on main, dry run, release.
85+ CODEQL_ACTION_DIFF_INFORMED_QUERIES : ${{ inputs.light && 'true' || '' }}
86+ # No compiler cache: a cache hit skips the compiler, and CodeQL extracts
87+ # only what a traced compiler invocation actually compiles.
88+ CBM_NO_CCACHE : " 1"
5389 steps :
5490 - uses : actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
91+ with :
92+ # The PR scan reads the merge commit's first parent (the base) to
93+ # find the vendored files the PR changes.
94+ fetch-depth : ${{ inputs.light && 2 || 1 }}
5595
5696 - name : Install build dependencies
97+ if : ${{ matrix.language == 'c-cpp' }}
5798 run : sudo apt-get update && sudo apt-get install -y zlib1g-dev
5899
100+ # PR c-cpp: compile the third-party objects (tree-sitter grammars,
101+ # sqlite, mimalloc, lz4, zstd, the tree-sitter runtime: ~1.3 GB of
102+ # vendored C against ~45 MB first-party) BEFORE tracing starts, so the
103+ # traced build below compiles first-party code only ...
104+ - name : Build third-party objects (PR scan, untraced)
105+ if : ${{ matrix.language == 'c-cpp' && inputs.light }}
106+ run : scripts/build.sh --vendored-only
107+
108+ # ... except the vendored files this PR changes: those are rebuilt
109+ # under tracing (an undeterminable diff leaves the list empty; the scan
110+ # still covers all first-party C).
111+ - name : Vendored files this PR changes (traced too)
112+ if : ${{ matrix.language == 'c-cpp' && inputs.light }}
113+ run : scripts/ci/codeql-retrace-vendored.sh "$RUNNER_TEMP/codeql-retrace.txt"
114+
59115 - name : Initialize CodeQL
60116 uses : github/codeql-action/init@1c5b675653bb5c22dbe9b12b556ec555138e09fd # v4.38.1
61117 with :
62- languages : c-cpp
63- build-mode : manual
118+ languages : ${{ matrix.language }}
119+ build-mode : ${{ matrix.build-mode }}
120+ config-file : ./.github/codeql/codeql-config.yml
64121
65- - name : Build for CodeQL analysis
122+ - name : Build for CodeQL analysis (full)
123+ if : ${{ matrix.language == 'c-cpp' && !inputs.light }}
66124 run : scripts/build.sh
67125
126+ - name : Build for CodeQL analysis (PR scan, first-party + changed vendored)
127+ if : ${{ matrix.language == 'c-cpp' && inputs.light }}
128+ run : scripts/build.sh --reuse-vendored --retrace-from "$RUNNER_TEMP/codeql-retrace.txt"
129+
68130 - name : Perform CodeQL Analysis
69131 uses : github/codeql-action/analyze@1c5b675653bb5c22dbe9b12b556ec555138e09fd # v4.38.1
70132 with :
71- category : " /language:c-cpp "
133+ category : " /language:${{ matrix.language }} "
72134
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 :
135+ # Alert policy: the default branch carries ZERO open code-scanning
136+ # alerts. Any open alert on the analysed ref fails this leg: on main the
137+ # commit turns red and GitHub notifies whoever triggered the run; the
138+ # dry run and the release cannot pass; a PR (refs/pull/<n>/merge: what
139+ # its changed lines introduce) fails ci-ok. With a zero baseline "any
140+ # open alert" is exactly "a new alert"; a dismissal (with a reason, in
141+ # the Security tab) holds on every ref. Every leg checks after its own
142+ # upload, so each language's alerts are seen by at least its own leg.
143+ # Moved from _security.yml's former PR-CI codeql-gate job.
92144 - name : Check for open code scanning alerts
93145 env :
94146 GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
147+ ALERT_REF : ${{ github.ref }}
95148 run : |
96149 # FAIL CLOSED. The previous form was `gh api ... 2>/dev/null || echo "0"`,
97150 # which looks defensive and is the opposite. gh writes the API error
@@ -108,7 +161,7 @@ jobs:
108161 # replaces. The callers below check the status explicitly.
109162 count_open_alerts() {
110163 local body status
111- body=$(gh api ' repos/${{ github.repository }}/code-scanning/alerts?state=open' \
164+ body=$(gh api " repos/${{ github.repository }}/code-scanning/alerts?state=open&ref=$ALERT_REF" \
112165 --jq 'length' 2>/dev/null)
113166 status=$?
114167 if [ $status -ne 0 ]; then
@@ -132,9 +185,9 @@ jobs:
132185 ALERTS2=$(count_open_alerts) || exit 1
133186 [ "$ALERTS" -lt "$ALERTS2" ] && ALERTS=$ALERTS2
134187 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"
188+ echo "::error title=Open code scanning alerts::$ALERTS open alert(s) on $ALERT_REF -- fix, or dismiss with a reason in the Security tab"
136189 echo "BLOCKED: $ALERTS open alert(s)"
137- gh api ' repos/${{ github.repository }}/code-scanning/alerts?state=open' \
190+ gh api " repos/${{ github.repository }}/code-scanning/alerts?state=open&ref=$ALERT_REF" \
138191 --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
139192 exit 1
140193 fi
0 commit comments