Repository navigation
173 lines (167 loc) · 7.19 KB
/
Copy pathcodeql.yml
File metadata and controls
173 lines (167 loc) · 7.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# CodeQL code scanning: injection, path traversal, unsafe deserialisation and the like in
# our own code. Findings go to the repository's Security tab (Code scanning) rather than
# failing the build, so a new alert is triaged instead of blocking an unrelated PR. The
# dependency and secret scan that does gate PRs is the `security scan` job in engine-ci.yml.
#
# The payload's C is not analysed here: it builds only with the PS5 Payload SDK.
#
# Off the critical path: neither `PR gate` (engine-ci.yml) nor release-tag.yml waits for this
# workflow, and it must not be made a required check (a PR that touches none of the paths below
# never gets one, and a merge-queue entry never runs it). It runs on every push to main, weekly,
# and on a pull request only when it changes code in one of the four languages analysed here; a
# PR of docs, templates or the website has nothing for it to read. Results still upload to the
# Security tab, and open alerts still stop scripts/release.sh.
#
# On a pull request the Rust analysis (the slowest of the four) runs only when the PR changes Rust
# (`*.rs`, a Cargo.toml or Cargo.lock), the CodeQL config or this workflow; the `languages` job
# picks the list. Pushes to main, the weekly run and manual runs analyse all four.
name: codeql
on:
push:
branches: [main]
pull_request:
# actions, javascript-typescript, python, rust: their sources and the manifests that pull in
# dependencies, plus this workflow and its config.
paths:
- ".github/workflows/**"
- ".github/actions/**"
- ".github/codeql/**"
- "**/*.rs"
- "**/Cargo.toml"
- "**/Cargo.lock"
- "**/*.js"
- "**/*.mjs"
- "**/*.cjs"
- "**/*.jsx"
- "**/*.ts"
- "**/*.tsx"
- "**/*.html"
- "**/package.json"
- "**/package-lock.json"
- "**/*.py"
schedule:
# Weekly, so new CodeQL queries also run over code nobody touched.
- cron: "17 4 * * 1"
workflow_dispatch:
concurrency:
group: codeql-${{ github.ref }}
cancel-in-progress: true
permissions:
contents: read
jobs:
# The languages this run analyses: all four, except a pull request that changes no Rust.
languages:
name: codeql languages
runs-on: ubuntu-24.04
timeout-minutes: 5
outputs:
list: ${{ steps.pick.outputs.list }}
count: ${{ steps.pick.outputs.count }}
steps:
# A PR run checks out the PR's merge commit: HEAD^1..HEAD is exactly what the PR changes.
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
if: github.event_name == 'pull_request'
with:
persist-credentials: false
fetch-depth: 2
sparse-checkout: .github/codeql
- name: Pick the languages
id: pick
env:
EVENT: ${{ github.event_name }}
run: |
set -euo pipefail
rust=true
if [ "$EVENT" = pull_request ]; then
# An unreadable diff keeps Rust in.
if files="$(git diff --no-renames --name-only HEAD^1 HEAD)"; then
printf '%s\n' "$files" | sed 's/^/changed: /'
# A here-string, not a pipe: grep -q stops reading at the first match, and under
# pipefail the writer's SIGPIPE would read as "no Rust".
if ! grep -Eq '(^|/)(Cargo\.toml|Cargo\.lock)$|\.rs$|^\.github/codeql/|^\.github/workflows/codeql\.yml$' <<< "$files"; then
rust=false
fi
fi
fi
if [ "$rust" = true ]; then
list='["actions","javascript-typescript","python","rust"]'
else
list='["actions","javascript-typescript","python"]'
fi
echo "languages: $list"
echo "list=$list" >> "$GITHUB_OUTPUT"
echo "count=$(printf '%s' "$list" | jq length)" >> "$GITHUB_OUTPUT"
analyze:
name: codeql (${{ matrix.language }})
needs: languages
runs-on: ubuntu-24.04
timeout-minutes: 45
permissions:
contents: read
security-events: write
strategy:
fail-fast: false
matrix:
language: ${{ fromJSON(needs.languages.outputs.list) }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
with:
persist-credentials: false
submodules: true
- uses: github/codeql-action/init@1fc58d04a4d4374f921bdac573c3811c240f9c26 # v4
with:
languages: ${{ matrix.language }}
build-mode: none
queries: security-extended
config-file: ./.github/codeql/codeql-config.yml
- uses: github/codeql-action/analyze@1fc58d04a4d4374f921bdac573c3811c240f9c26 # v4
with:
category: /language:${{ matrix.language }}
# Fails when this change leaves an open code-scanning alert: a new finding is fixed (or, when it
# is by design, dismissed with its reason) before it merges or ships. Already-dismissed alerts
# don't count. Push and same-repo pull requests only (a fork's token cannot read the alerts).
gate:
name: codeql gate
needs: [languages, analyze]
if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository)
runs-on: ubuntu-24.04
timeout-minutes: 15
permissions:
contents: read
security-events: read
steps:
- name: No open code-scanning alert
env:
GH_TOKEN: ${{ github.token }}
REPO: ${{ github.repository }}
REF: ${{ github.event_name == 'pull_request' && format('refs/pull/{0}/merge', github.event.pull_request.number) || github.ref }}
# The commit this run analysed. For a pull request that is this run's own merge
# commit: the event's merge_commit_sha can still be the previous push's when two
# pushes land close together, and then no analysis ever matches it.
SHA: ${{ github.sha }}
# How many languages the analyze job ran (the languages job).
WANT: ${{ needs.languages.outputs.count }}
run: |
set -euo pipefail
# The uploads just made turn into analyses, then alerts, after a short delay: wait
# until every analysed language's analysis of this commit is in.
for _ in $(seq 1 30); do
n="$(gh api "repos/$REPO/code-scanning/analyses?ref=$REF&per_page=100" \
--jq "[.[] | select(.commit_sha == \"$SHA\")] | map(.category) | unique | length")"
[ "$n" -ge "$WANT" ] && break
sleep 20
done
# Fewer and the alert query below would pass on part of the picture.
if [ "$n" -lt "$WANT" ]; then
echo "::error::only $n of $WANT CodeQL analyses of $SHA arrived in time: re-run this job"
exit 1
fi
sleep 20
open="$(gh api --paginate "repos/$REPO/code-scanning/alerts?ref=$REF&state=open&per_page=100" \
--jq '.[] | "\(.rule.id) \(.most_recent_instance.location.path):\(.most_recent_instance.location.start_line) \(.html_url)"')"
if [ -n "$open" ]; then
echo "$open"
echo "::error::open code-scanning alerts (above): fix them, or dismiss a by-design one with its reason"
exit 1
fi
echo "no open code-scanning alerts"