CodeQL Code Scanning
Skill files7 files

references/

SKILL.md406 lines
1
---
2
name: codeql
3
description: Comprehensive guide for setting up and configuring CodeQL code scanning via GitHub Actions workflows and the CodeQL CLI. This skill should be used when users need help with code scanning configuration, CodeQL workflow files, CodeQL CLI commands, SARIF output, security analysis setup, or troubleshooting CodeQL analysis.
4
---
5
6
# CodeQL Code Scanning
7
8
This skill provides procedural guidance for configuring and running CodeQL code scanning — both through GitHub Actions workflows and the standalone CodeQL CLI.
9
10
## When to Use This Skill
11
12
Use this skill when the request involves:
13
14
- Creating or customizing a `codeql.yml` GitHub Actions workflow
15
- Choosing between default setup and advanced setup for code scanning
16
- Configuring CodeQL language matrix, build modes, or query suites
17
- Running CodeQL CLI locally (`codeql database create`, `database analyze`, `github upload-results`)
18
- Understanding or interpreting SARIF output from CodeQL
19
- Troubleshooting CodeQL analysis failures (build modes, compiled languages, runner requirements)
20
- Setting up CodeQL for monorepos with per-component scanning
21
- Configuring dependency caching, custom query packs, or model packs
22
23
## Supported Languages
24
25
CodeQL supports the following language identifiers:
26
27
| Language | Identifier | Alternatives |
28
|---|---|---|
29
| C/C++ | `c-cpp` | `c`, `cpp` |
30
| C# | `csharp` | — |
31
| Go | `go` | — |
32
| Java/Kotlin | `java-kotlin` | `java`, `kotlin` |
33
| JavaScript/TypeScript | `javascript-typescript` | `javascript`, `typescript` |
34
| Python | `python` | — |
35
| Ruby | `ruby` | — |
36
| Rust | `rust` | — |
37
| Swift | `swift` | — |
38
| GitHub Actions | `actions` | — |
39
40
> Alternative identifiers are equivalent to the standard identifier (e.g., `javascript` does not exclude TypeScript analysis).
41
42
## Core Workflow — GitHub Actions
43
44
### Step 1: Choose Setup Type
45
46
- **Default setup** — Enable from repository Settings → Advanced Security → CodeQL analysis. Best for getting started quickly. Uses `none` build mode for most languages.
47
- **Advanced setup** — Create a `.github/workflows/codeql.yml` file for full control over triggers, build modes, query suites, and matrix strategies.
48
49
To switch from default to advanced: disable default setup first, then commit the workflow file.
50
51
### Step 2: Configure Workflow Triggers
52
53
Define when scanning runs:
54
55
```yaml
56
on:
57
push:
58
branches: [main, protected]
59
pull_request:
60
branches: [main]
61
schedule:
62
- cron: '30 6 * * 1' # Weekly Monday 6:30 UTC
63
```
64
65
- `push` — scans on every push to specified branches; results appear in Security tab
66
- `pull_request` — scans PR merge commits; results appear as PR check annotations
67
- `schedule` — periodic scans of the default branch (cron must exist on default branch)
68
- `merge_group` — add if repository uses merge queues
69
70
To skip scans for documentation-only PRs:
71
72
```yaml
73
on:
74
pull_request:
75
paths-ignore:
76
- '**/*.md'
77
- '**/*.txt'
78
```
79
80
> `paths-ignore` controls whether the workflow runs, not which files are analyzed.
81
82
### Step 3: Configure Permissions
83
84
Set least-privilege permissions:
85
86
```yaml
87
permissions:
88
security-events: write # Required to upload SARIF results
89
contents: read # Required to checkout code
90
actions: read # Required for private repos using codeql-action
91
```
92
93
### Step 4: Configure Language Matrix
94
95
Use a matrix strategy to analyze each language in parallel:
96
97
```yaml
98
jobs:
99
analyze:
100
name: Analyze (${{ matrix.language }})
101
runs-on: ubuntu-latest
102
strategy:
103
fail-fast: false
104
matrix:
105
include:
106
- language: javascript-typescript
107
build-mode: none
108
- language: python
109
build-mode: none
110
```
111
112
For compiled languages, set the appropriate `build-mode`:
113
- `none` — no build required (supported for C/C++, C#, Java, Rust)
114
- `autobuild` — automatic build detection
115
- `manual` — custom build commands (advanced setup only)
116
117
> For detailed per-language autobuild behavior and runner requirements, search `references/compiled-languages.md`.
118
119
### Step 5: Configure CodeQL Init and Analysis
120
121
```yaml
122
steps:
123
- name: Checkout repository
124
uses: actions/checkout@v4
125
126
- name: Initialize CodeQL
127
uses: github/codeql-action/init@v4
128
with:
129
languages: ${{ matrix.language }}
130
build-mode: ${{ matrix.build-mode }}
131
queries: security-extended
132
dependency-caching: true
133
134
- name: Perform CodeQL Analysis
135
uses: github/codeql-action/analyze@v4
136
with:
137
category: "/language:${{ matrix.language }}"
138
```
139
140
**Query suite options:**
141
- `security-extended` — default security queries plus additional coverage
142
- `security-and-quality` — security plus code quality queries
143
- Custom query packs via `packs:` input (e.g., `codeql/javascript-queries:AlertSuppression.ql`)
144
145
**Dependency caching:** Set `dependency-caching: true` on the `init` action to cache restored dependencies across runs.
146
147
**Analysis category:** Use `category` to distinguish SARIF results in monorepos (e.g., per-language, per-component).
148
149
### Step 6: Monorepo Configuration
150
151
For monorepos with multiple components, use the `category` parameter to separate SARIF results:
152
153
```yaml
154
category: "/language:${{ matrix.language }}/component:frontend"
155
```
156
157
To restrict analysis to specific directories, use a CodeQL configuration file (`.github/codeql/codeql-config.yml`):
158
159
```yaml
160
paths:
161
- apps/
162
- services/
163
paths-ignore:
164
- node_modules/
165
- '**/test/**'
166
```
167
168
Reference it in the workflow:
169
170
```yaml
171
- uses: github/codeql-action/init@v4
172
with:
173
config-file: .github/codeql/codeql-config.yml
174
```
175
176
### Step 7: Manual Build Steps (Compiled Languages)
177
178
If `autobuild` fails or custom build commands are needed:
179
180
```yaml
181
- language: c-cpp
182
build-mode: manual
183
```
184
185
Then add explicit build steps between `init` and `analyze`:
186
187
```yaml
188
- if: matrix.build-mode == 'manual'
189
name: Build
190
run: |
191
make bootstrap
192
make release
193
```
194
195
## Core Workflow — CodeQL CLI
196
197
### Step 1: Install the CodeQL CLI
198
199
Download the CodeQL bundle (includes CLI + precompiled queries):
200
201
```bash
202
# Download from https://github.com/github/codeql-action/releases
203
# Extract and add to PATH
204
export PATH="$HOME/codeql:$PATH"
205
206
# Verify installation
207
codeql resolve packs
208
codeql resolve languages
209
```
210
211
> Always use the CodeQL bundle, not a standalone CLI download. The bundle ensures query compatibility and provides precompiled queries for better performance.
212
213
### Step 2: Create a CodeQL Database
214
215
```bash
216
# Single language
217
codeql database create codeql-db \
218
--language=javascript-typescript \
219
--source-root=src
220
221
# Multiple languages (cluster mode)
222
codeql database create codeql-dbs \
223
--db-cluster \
224
--language=java,python \
225
--command=./build.sh \
226
--source-root=src
227
```
228
229
For compiled languages, provide the build command via `--command`.
230
231
### Step 3: Analyze the Database
232
233
```bash
234
codeql database analyze codeql-db \
235
javascript-code-scanning.qls \
236
--format=sarif-latest \
237
--sarif-category=javascript \
238
--output=results.sarif
239
```
240
241
Common query suites: `<language>-code-scanning.qls`, `<language>-security-extended.qls`, `<language>-security-and-quality.qls`.
242
243
### Step 4: Upload Results to GitHub
244
245
```bash
246
codeql github upload-results \
247
--repository=owner/repo \
248
--ref=refs/heads/main \
249
--commit=<commit-sha> \
250
--sarif=results.sarif
251
```
252
253
Requires `GITHUB_TOKEN` environment variable with `security-events: write` permission.
254
255
### CLI Server Mode
256
257
To avoid repeated JVM initialization when running multiple commands:
258
259
```bash
260
codeql execute cli-server
261
```
262
263
> For detailed CLI command reference, search `references/cli-commands.md`.
264
265
## Alert Management
266
267
### Severity Levels
268
269
Alerts have two severity dimensions:
270
- **Standard severity:** `Error`, `Warning`, `Note`
271
- **Security severity:** `Critical`, `High`, `Medium`, `Low` (derived from CVSS scores; takes display precedence)
272
273
### Copilot Autofix
274
275
GitHub Copilot Autofix generates fix suggestions for CodeQL alerts in pull requests automatically — no Copilot subscription required. Review suggestions carefully before committing.
276
277
### Alert Triage in PRs
278
279
- Alerts appear as check annotations on changed lines
280
- Check fails by default for `error`/`critical`/`high` severity alerts
281
- Configure merge protection rulesets to customize the threshold
282
- Dismiss false positives with a documented reason for audit trail
283
284
> For detailed alert management guidance, search `references/alert-management.md`.
285
286
## Custom Queries and Packs
287
288
### Using Custom Query Packs
289
290
```yaml
291
- uses: github/codeql-action/init@v4
292
with:
293
packs: |
294
my-org/my-security-queries@1.0.0
295
codeql/javascript-queries:AlertSuppression.ql
296
```
297
298
### Creating Custom Query Packs
299
300
Use the CodeQL CLI to create and publish packs:
301
302
```bash
303
# Initialize a new pack
304
codeql pack init my-org/my-queries
305
306
# Install dependencies
307
codeql pack install
308
309
# Publish to GitHub Container Registry
310
codeql pack publish
311
```
312
313
### CodeQL Configuration File
314
315
For advanced query and path configuration, create `.github/codeql/codeql-config.yml`:
316
317
```yaml
318
paths:
319
- apps/
320
- services/
321
paths-ignore:
322
- '**/test/**'
323
- node_modules/
324
queries:
325
- uses: security-extended
326
packs:
327
javascript-typescript:
328
- my-org/my-custom-queries
329
```
330
331
## Code Scanning Logs
332
333
### Summary Metrics
334
335
Workflow logs include key metrics:
336
- **Lines of code in codebase** — baseline before extraction
337
- **Lines extracted** — including external libraries and auto-generated files
338
- **Extraction errors/warnings** — files that failed or produced warnings during extraction
339
340
### Debug Logging
341
342
To enable detailed diagnostics:
343
- **GitHub Actions:** re-run the workflow with "Enable debug logging" checked
344
- **CodeQL CLI:** use `--verbosity=progress++` and `--logdir=codeql-logs`
345
346
## Troubleshooting
347
348
### Common Issues
349
350
| Problem | Solution |
351
|---|---|
352
| Workflow not triggering | Verify `on:` triggers match event; check `paths`/`branches` filters; ensure workflow exists on target branch |
353
| `Resource not accessible` error | Add `security-events: write` and `contents: read` permissions |
354
| Autobuild failure | Switch to `build-mode: manual` and add explicit build commands |
355
| No source code seen | Verify `--source-root`, build command, and language identifier |
356
| C# compiler failure | Check for `/p:EmitCompilerGeneratedFiles=true` conflicts with `.sqlproj` or legacy projects |
357
| Fewer lines scanned than expected | Switch from `none` to `autobuild`/`manual`; verify build compiles all source |
358
| Kotlin in no-build mode | Disable and re-enable default setup to switch to `autobuild` |
359
| Cache miss every run | Verify `dependency-caching: true` on `init` action |
360
| Out of disk/memory | Use larger runners; reduce analysis scope via `paths` config; use `build-mode: none` |
361
| SARIF upload fails | Ensure token has `security-events: write`; check 10 MB file size limit |
362
| SARIF results exceed limits | Split across multiple uploads with different `--sarif-category`; reduce query scope |
363
| Two CodeQL workflows | Disable default setup if using advanced setup, or remove old workflow file |
364
| Slow analysis | Enable dependency caching; use `--threads=0`; reduce query suite scope |
365
366
> For comprehensive troubleshooting with detailed solutions, search `references/troubleshooting.md`.
367
368
### Hardware Requirements (Self-Hosted Runners)
369
370
| Codebase Size | RAM | CPU |
371
|---|---|---|
372
| Small (<100K LOC) | 8 GB+ | 2 cores |
373
| Medium (100K–1M LOC) | 16 GB+ | 4–8 cores |
374
| Large (>1M LOC) | 64 GB+ | 8 cores |
375
376
All sizes: SSD with ≥14 GB free disk space.
377
378
### Action Versioning
379
380
Pin CodeQL actions to a specific major version:
381
382
```yaml
383
uses: github/codeql-action/init@v4 # Recommended
384
uses: github/codeql-action/autobuild@v4
385
uses: github/codeql-action/analyze@v4
386
```
387
388
For maximum security, pin to a full commit SHA instead of a version tag.
389
390
## Reference Files
391
392
For detailed documentation, load the following reference files as needed:
393
394
- `references/workflow-configuration.md` — Full workflow trigger, runner, and configuration options
395
- Search patterns: `trigger`, `schedule`, `paths-ignore`, `db-location`, `model packs`, `alert severity`, `merge protection`, `concurrency`, `config file`
396
- `references/cli-commands.md` — Complete CodeQL CLI command reference
397
- Search patterns: `database create`, `database analyze`, `upload-results`, `resolve packs`, `cli-server`, `installation`, `CI integration`
398
- `references/sarif-output.md` — SARIF v2.1.0 object model, upload limits, and third-party support
399
- Search patterns: `sarifLog`, `result`, `location`, `region`, `codeFlow`, `fingerprint`, `suppression`, `upload limits`, `third-party`, `precision`, `security-severity`
400
- `references/compiled-languages.md` — Build modes and autobuild behavior per language
401
- Search patterns: `C/C++`, `C#`, `Java`, `Go`, `Rust`, `Swift`, `autobuild`, `build-mode`, `hardware`, `dependency caching`
402
- `references/troubleshooting.md` — Comprehensive error diagnosis and resolution
403
- Search patterns: `no source code`, `out of disk`, `out of memory`, `403`, `C# compiler`, `analysis too long`, `fewer lines`, `Kotlin`, `extraction errors`, `debug logging`, `SARIF upload`, `SARIF limits`
404
- `references/alert-management.md` — Alert severity, triage, Copilot Autofix, and dismissal
405
- Search patterns: `severity`, `security severity`, `CVSS`, `Copilot Autofix`, `dismiss`, `triage`, `PR alerts`, `data flow`, `merge protection`, `REST API`
406

© GitHub, Inc. · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 14,621 prompts

More by @OpenSourceBuffGuy

See all
Sea turtle carrying floating film studio island prompt image

Sea turtle carrying floating film studio island

Draws a giant sea turtle carrying a floating island film studio through a starry sky, where fairies polish rough film strips into golden film.

image-prompt, nano-banana, 3d-render, cartoon, fantasy-world, creatures, animals, whimsical, dreamy, cinematic, dramatic-lighting, vibrant-colors, bokeh, miniature, widescreen, night-scene, illustration, concept-art, surreal, storytelling, clay-3d

Desert buggy escapes drones past industrial outpost prompt image

Desert buggy escapes drones past industrial outpost

Builds a 15-second desert chase video where a racer in an armored buggy escapes drones past an industrial outpost, using five reference images with set jobs.

video-prompt, hailuo, action, multi-shot, timed-beats, camera-movement, reference-image, character-consistency, negative-prompt, cinematic, sci-fi, cars, landscape, aerial-view, tracking-shot, photorealistic, sound-effects, structured-output, long-form, widescreen

1 view
Cozy Pixel Art Farm Island Village prompt image

Cozy Pixel Art Farm Island Village

Draws a cozy 16-bit pixel-art farming village across six small islands joined by bridges, with exact counts for buildings, animals and crops.

image-prompt, gpt-image, pixel-art, isometric, game-assets, concept-art, maps, worldbuilding, cozy, vibrant-colors, ocean, landscape, wide-shot, widescreen, game-development, video-games, structured-output, whimsical, summer, high-angle, aerial-view, animals, negative-prompt, cityscape

1 view

Reviews

Loading reviews…

Discussion

Loading comments…