MSBA-361 · Lesson 2 of 12 · 20 min
CodeQL default and advanced setup
▶ Video coming soon A video version of this lesson is on the way.
The second session was practical. Seraphine had been asked by every group the same question: how do we switch it on? The honest answer was that there are two ways, and that most of Skerry's 712 in-scope repositories had already been switched on the easy way by the enforced security configuration. The interesting work was in the 57 repositories where the easy way had not been good enough.
How CodeQL works
CodeQL treats code as data. In a first step it extracts a repository's source into a CodeQL database, a relational representation of the code's syntax, types, control flow and data flow. In a second step it runs queries against that database. Each query is written in the CodeQL query language, describes a kind of problem, and returns the places in the code where that problem occurs. The results are written as SARIF and uploaded to code scanning.
Extraction differs by language. For interpreted languages such as JavaScript, TypeScript, Python and Ruby, CodeQL reads the source files directly. For compiled languages such as Java, Kotlin, C#, C, C++, Go and Swift, it has traditionally watched the build to see exactly how each file is compiled. Since 2024, CodeQL can also analyze Java and C# with build mode none, which reads the source without a build, and GitHub has extended the approach to other compiled languages since. Build mode none is faster and needs no build configuration, at the cost of occasionally missing details that only a real build reveals.
| Build mode | How it works | When to use it |
|---|---|---|
| none | Extracts source without building | Default for supported compiled languages; fast and simple |
| autobuild | CodeQL tries to detect and run the build itself | Projects with a standard build that CodeQL can guess |
| manual | Your workflow runs the build commands; CodeQL observes | Custom builds, unusual toolchains, generated code |
Default setup
Default setup is the switch-it-on option. A repository administrator, an organization owner, a security manager or a security configuration enables it, and GitHub decides the rest. It detects the languages in the repository, chooses build modes, and runs CodeQL using GitHub Actions without anyone writing a workflow file. Default setup scans on pushes to the default branch and protected branches, on pull requests against the default branch, and on a weekly schedule.
Default setup still uses Actions minutes. It runs on GitHub-hosted runners unless the organization has set it up to use suitable self-hosted runners, which matters for repositories whose code can only be fetched from a private network. Actions must be enabled for the repository for default setup to run.
The settings that default setup exposes are deliberately few.
| Setting | Options | What it changes |
|---|---|---|
| Languages | Detected languages, which you can deselect | Which languages are analyzed |
| Query suite | Default or Extended | How many queries run; Extended adds lower-precision and lower-severity security queries |
| Threat model | Remote sources only, or remote and local sources | Whether data from local sources such as files, environment variables and command-line arguments is treated as untrusted |
| Runner | Standard GitHub-hosted, or a labelled runner the organization provides | Where analysis runs |
Threat models need a word. By default CodeQL treats data from remote sources, such as HTTP requests, as untrusted. For a command-line tool or a script that runs on a build server, the dangerous input often comes from local sources instead: files, environment variables or arguments. Enabling the local threat model makes CodeQL treat those as sources too, which finds more problems in that kind of code and produces more noise in a typical web service.
When default setup is not enough
Seraphine's list of the 57 advanced setup repositories fell into four groups, and they illustrate when advanced setup is worth the extra work.
| Group | Repositories | Why default setup was not enough |
|---|---|---|
| Store terminal firmware | 18 in skerry-stores | C and C++ built with a vendor toolchain that only installs on the stores lab runners; needed a manual build |
| Checkout monorepo | 1 | Needed different paths analyzed for different services, custom query packs, and analysis split by category |
| Android and iOS apps | 6 | Gradle and Xcode builds with signing steps; needed a manual build on specific runners |
| Data pipelines | 32 | Wanted the local threat model for some jobs plus a custom query pack from the data group |
Advanced setup means committing a CodeQL workflow file to the repository, usually at .github/workflows/codeql.yml, and maintaining it like any other workflow. GitHub can generate a starting workflow from the repository's Security settings, and organizations can offer their own starting workflows through workflow templates, which MSBA-300 covered.
name: CodeQL
on:
push:
branches: [main]
pull_request:
branches: [main]
schedule:
- cron: "27 4 * * 2" # weekly, off the hour
permissions:
contents: read
security-events: write
jobs:
analyze:
runs-on: [self-hosted, stores-lab]
strategy:
fail-fast: false
matrix:
include:
- language: c-cpp
build-mode: manual
steps:
- uses: actions/checkout@v5
- uses: github/codeql-action/init@v4
with:
languages: ${{ matrix.language }}
build-mode: ${{ matrix.build-mode }}
queries: security-extended
- name: Build firmware
run: ./tools/build-terminal.sh --release
- uses: github/codeql-action/analyze@v4
with:
category: "/language:${{ matrix.language }}"A few parts of this workflow carry most of the meaning. The security-events: write permission lets the workflow upload results. The init step starts CodeQL with the chosen language, build mode and queries. Between init and analyze, CodeQL observes whatever build runs, which is why the build script sits there. The analyze step finishes the database, runs the queries and uploads SARIF. The category tells code scanning which analysis these results belong to, so that results from different languages or different parts of a monorepo do not replace each other.
Matrix builds and scan frequency
Repositories with several languages analyze each language as a separate job, usually through a matrix, as the workflow above does with a one-entry matrix. The checkout monorepo used a matrix with three entries: JavaScript and TypeScript for the web front end, Java with build mode none for the order services, and Go with autobuild for a small pricing service. Setting fail-fast to false lets one language finish even if another fails.
Scan frequency is a trade-off between freshness and cost. Pull request scans are the most valuable because they stop new problems. Pushes to the default branch keep the alert list accurate after merges. A weekly scheduled scan catches problems found by new queries in code that has not changed. Repositories that change rarely still need the schedule; very busy repositories may not need a scan on every push if pull requests are already scanned and merges are frequent. Since 2025, CodeQL has used incremental analysis on pull requests, analyzing only what changed where it can, and in June 2026 GitHub extended it to C, C++ and Go for projects using build mode none, with typical speed-ups of a quarter to nearly a half on longer scans.
| Trigger | Purpose | Typical setting at Skerry |
|---|---|---|
| pull_request | Prevent new alerts | Always, for the default branch |
| push to default branch | Keep the alert list accurate after merge | Always |
| schedule | Catch problems found by updated queries | Weekly, at a time spread across the hour |
| merge_group | Scan merge queue entries | Where merge queues are used, so required checks can pass |
Query suites and packs
CodeQL queries are distributed in query packs, and GitHub publishes standard query suites that select queries from those packs.
| Suite | What it includes | Available in |
|---|---|---|
| default (code-scanning) | High-precision security queries | Default and advanced setup |
| security-extended | The default suite plus lower-precision and lower-severity security queries | Default setup (as Extended) and advanced setup |
| security-and-quality | Security-extended plus maintainability and reliability queries | Advanced setup |
Teams can also write their own queries and publish them as packs. The data group had written two queries for a pattern specific to its pipeline framework, where a table name taken from job configuration was passed into a dynamic SQL statement. Leocadie Brannoch, the data group's CodeQL champion, published them as a pack that the 32 data repositories added to their workflows. Organizations with Code Security can also configure default setup to include model packs, which teach CodeQL about frameworks it does not know, such as an internal library whose functions are sources or sanitizers.
Customizing queries is part of the GH-500 security operations domain, and the exam expects candidates to know the mechanisms rather than to write CodeQL. A configuration file can add or exclude queries and packs, filter by path, and set paths to ignore, such as generated or vendored code.
name: "Skerry data pipelines CodeQL config"
queries:
- uses: security-extended
packs:
python:
- skerry/data-pipeline-queries
paths-ignore:
- "**/tests/**"
- "vendor/**"
query-filters:
- exclude:
problem.severity: recommendationRunning CodeQL outside GitHub Actions
Not every team builds with GitHub Actions. The CodeQL CLI runs the same analysis in other continuous integration systems. A pipeline downloads the CLI bundle, creates a database, analyzes it and uploads the SARIF to GitHub with the CLI's upload command or the code scanning API. Results then appear in code scanning exactly as if Actions had run them. Licensing is the same: the CLI can be used on repositories that have Code Security or on public repositories. Skerry's legacy point-of-sale build still ran on a separate build server for one more quarter, so two repositories used the CLI that way.
codeql database create pos-db --language=java --build-mode=none --source-root=.
codeql database analyze pos-db codeql/java-queries:codeql-suites/java-code-scanning.qls \
--format=sarif-latest --output=pos.sarif
codeql github upload-results --repository=skerry-stores/pos-legacy \
--ref=refs/heads/main --commit="$GIT_COMMIT" --sarif=pos.sarifSince September 2026, CodeQL also runs natively on Linux on Arm64 runners, which mattered to the platform team because Skerry's newer self-hosted runner pools were Arm-based.
Default setup at scale
At organization level, nobody enables default setup one repository at a time. Security configurations, covered in lesson 8, turn it on for every repository the configuration applies to. When default setup cannot analyze a repository, for example because a compiled language failed to build, the repository shows a failed or unsupported status. Security overview's coverage view lists those repositories, which is how Seraphine found her 57 candidates for advanced setup.
Worked problem
Choosing a setup
A data team repository contains Python jobs that read file paths from their job configuration on a build server, and a small TypeScript dashboard. Default setup is running with the default suite and remote threat model. The team says it never finds anything in the jobs. What should change?
Show solution
- Recognize that the dangerous input for build-server jobs comes from local sources, not HTTP requests.
- Enable the remote and local threat model in default setup, or set it in an advanced workflow.
- Consider the Extended suite if the team can triage more alerts.
- If a custom pack is needed for the pipeline framework, move to advanced setup or add a model pack.
Answer: Switch the threat model to include local sources, and add the data group's pack or model pack if the framework's own sources and sinks matter.
When an analysis fails
A configuration that is enabled is not the same as an analysis that runs. During Skerry's first week, 21 repositories showed a failed or missing analysis in the coverage view, and Leocadie worked through them with a short routine. She opened the repository's code scanning tool status page first, which shows for each language whether the last analysis succeeded, how many files were scanned and any error messages. Then she looked at the workflow run itself for advanced setup, or the default setup run listed under Actions for default setup.
| Symptom | Likely cause | Typical fix |
|---|---|---|
| No source code seen during the build | A compiled language built with a command CodeQL did not observe | Switch to advanced setup with manual build steps, or try build mode none where the language supports it |
| Analysis timed out or ran out of memory | A very large repository on a standard runner | Use a larger runner, split by language or narrow paths in the configuration file |
| Default setup shows a language as not analyzed | The language was added after setup was enabled, or is not supported | Re-enable default setup to pick up new languages, or use a third-party tool |
| Analysis queued for a long time | No runner available with the required label | Check runner groups and labels, as covered in MSBA-291 |
Fourteen of the 21 failures were in firmware and data pipeline repositories that needed advanced setup anyway. Five were monorepos that ran out of memory and moved to larger runners. Two were archived projects that should never have been in scope, and Ottoline approved their removal from the configuration's target list with a note in the governance repository.
Common mistakes
MistakeWriting an advanced workflow for every repository.
InsteadUse default setup wherever it works; reserve advanced setup for custom builds, queries or paths.
MistakeOmitting the category when one repository has several analyses.
InsteadGive each analysis a distinct category so results do not overwrite each other.
MistakeDropping the scheduled scan because pull requests are scanned.
InsteadKeep a weekly schedule to pick up new and improved queries.
MistakeScanning generated and vendored code.
InsteadUse paths-ignore to exclude code the team does not maintain.
Summary and key terms
- CodeQL extracts code into a database, runs queries against it and uploads SARIF results.
- Build mode none analyzes supported compiled languages without a build; autobuild and manual builds remain for other cases.
- Default setup enables CodeQL without a workflow file and scans on push, pull request and a weekly schedule.
- Advanced setup uses a committed workflow for custom builds, runners, queries, paths and categories.
- Query suites range from the high-precision default to security-extended and security-and-quality.
- Custom query packs, model packs and configuration files tailor detection to an organization's code.
- The CodeQL CLI runs the same analysis in other CI systems and uploads SARIF to GitHub.
- CodeQL database
- A queryable representation of a repository's code created by CodeQL extraction.
- Default setup
- A way to enable CodeQL code scanning without a workflow file, with GitHub choosing languages and settings.
- Advanced setup
- A way to run CodeQL through a workflow file committed to the repository.
- Build mode none
- A CodeQL mode that extracts compiled languages from source without running the build.
- Threat model
- The set of sources CodeQL treats as untrusted, such as remote or local sources.
- Query suite
- A named selection of CodeQL queries, such as default or security-extended.
- Query pack
- A published package of CodeQL queries.
- Model pack
- A pack that describes a framework's sources, sinks and sanitizers so CodeQL can understand it.
- Category
- A label that distinguishes separate analyses uploaded for the same repository.
Practice problems
Work each problem on paper first, then reveal the answer to check.
Name three events that trigger default setup scans.
Show answer
Pushes to the default or protected branches, pull requests against the default branch, and a weekly schedule.
Why does the firmware workflow put its build script between init and analyze?
Show answer
CodeQL observes the build that runs between those steps to extract compiled code.
What does the security-events write permission allow?
Show answer
Uploading code scanning results for the repository.
Lesson quiz · 5 questions
Score 60% or more to complete this lesson.
Create a free account to save your progress, take quizzes and earn a certificate.