MSBA-369 · Lesson 6 of 12 · 20 min

Domain review: Code Security

▶ Video coming soon A video version of this lesson is on the way.

Leocadie Brannoch, the data group's CodeQL champion, ran the sixth session. Seraphine had asked for her deliberately. "I know Code Security too well," Seraphine said. "I will answer from what I did at Skerry. Leocadie can ask me the questions the way a stranger would." The plan was simple: Leocadie would describe a repository and a goal, Seraphine would say what she would configure, and Priya would check the answer against the documentation.

The domain Configure and use Code Security carries 10 to 15 percent of the exam, the lowest band, even though it holds the largest feature set. It was taught in the first seven lessons of GitHub Advanced Security: Code Security and Security Operations: Code scanning concepts, CodeQL default and advanced setup, Reading code scanning alerts, Copilot Autofix, Third-party scanning tools and SARIF, Security overview and Security campaigns. The outline asks you to enable code scanning, choose between default and advanced setup, configure CodeQL, integrate other tools through SARIF, read and act on alerts, and use Autofix and campaigns. With limited weight and broad scope, the exam tends to ask about choices and consequences rather than details.

How code scanning works

Code scanning turns the output of a static analysis tool into alerts. The tool writes results in SARIF; GitHub reads the SARIF, matches each result to a location, tracks it across commits and branches, and shows it as an alert on the Security tab and as an annotation on pull requests. CodeQL is GitHub's own engine. It extracts a database from the code, a relational representation of its structure and data flow, and then runs queries against the database. Queries are grouped into query suites: the default suite aims for high precision, and security-extended adds more queries at the cost of more false positives. Queries and libraries are distributed in CodeQL packs, which also carry model packs that teach CodeQL about frameworks and custom sources and sinks.

Two paths to code scanning results. Default setup: enabled from settings or a security configuration, languages detected automatically, no workflow file, runs on pushes to the default branch, on pull requests and weekly. Advanced setup: a codeql.yml workflow committed to the repository, with custom triggers, build steps, query suites and packs. A third path: the CodeQL CLI or a third-party tool in any CI system uploading SARIF. All three end in code scanning alerts.
Three ways to produce code scanning alerts. Default setup is the starting point; advanced setup and SARIF uploads handle what it cannot.

Default setup or advanced setup

This is the domain's most tested decision. Default setup is configured in settings, at repository level or for many repositories at once through a security configuration. GitHub detects the supported languages, chooses the queries, and runs analysis on pushes to the default branch and protected branches, on pull requests to them, and on a weekly schedule. There is no workflow file to maintain. For compiled languages it can analyze without building, using the build mode none.

Advanced setup means committing a CodeQL workflow file to the repository. You choose it when you need something default setup cannot do: custom build steps for a compiled language that will not analyze without a real build, extra query packs or custom queries, unusual triggers such as merge queues, analysis of a language default setup does not detect, or a specific runner. Advanced setup costs maintenance, because the workflow becomes code someone owns.

Choosing how to run code scanning. Default setup first; move to advanced setup for a specific need.
SituationChooseWhy
Hundreds of typical web and service repositoriesDefault setup through a security configurationNo files to maintain; consistent coverage
Firmware in C and C++ that needs the vendor toolchain to buildAdvanced setupCustom build steps and self-hosted runners
A data team that has written its own CodeQL queriesAdvanced setupCustom query packs
A repository whose code is built in another CI systemCodeQL CLI in that system, uploading SARIFAnalysis runs where the build happens
A team already using another static analysis toolUpload its SARIFResults appear beside CodeQL alerts

Skerry followed that rule. Of the 712 repositories under its Code Security configuration, 655 use default setup and 57 use advanced setup: 18 firmware repositories that need the vendor toolchain on self-hosted runners, the checkout monorepo with its merge queue, six mobile apps and 32 data pipelines with custom queries. Two legacy point-of-sale repositories built outside GitHub run the CodeQL CLI and upload SARIF.

An advanced setup workflow: merge queue trigger, a weekly schedule, two languages, the security-extended suite and a category per language. Check current action versions before copying.
name: CodeQL
on:
  push:
    branches: [main]
  pull_request:
    branches: [main]
  merge_group:
  schedule:
    - cron: "30 4 * * 1"
permissions:
  security-events: write
  contents: read
jobs:
  analyze:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        language: [java-kotlin, javascript-typescript]
    steps:
      - uses: actions/checkout@v4
      - uses: github/codeql-action/init@v4
        with:
          languages: ${{ matrix.language }}
          queries: security-extended
      - uses: github/codeql-action/autobuild@v4
      - uses: github/codeql-action/analyze@v4
        with:
          category: "/language:${{ matrix.language }}"

Read it as the exam would. The security-events: write permission lets the workflow upload results. init, an optional build step and analyze are the three stages. queries selects a suite or packs. category keeps results from different analyses of the same commit apart, so one upload does not replace another. The merge_group trigger runs analysis for merge queues. A repository cannot use default and advanced setup at the same time; switching to advanced setup means turning default setup off.

Other tools and SARIF

Any tool that writes SARIF can feed code scanning. In GitHub Actions, the upload-sarif action uploads a file; from another CI system, the CodeQL CLI or the REST API does the same. Uploads should carry a category when several tools or analyses cover one repository. SARIF uploads have limits, such as 25,000 results per run, of which only the top 5,000 are kept, and 20 runs per file; exceeding a limit causes results to be dropped or the upload to be rejected. A question about missing results from a large third-party scan often points here.

Reading and acting on alerts

A code scanning alert shows the rule, a description and help text, the location, and for data-flow queries the path from source to sink. It has two severity fields that the exam likes to contrast. Severity describes any rule: error, warning or note. Security severity applies to security rules and is a level from low to critical derived from a CVSS-like score. A setting or ruleset that blocks merges on "high or higher" refers to security severity.

Alerts are tracked across commits. If the code is fixed, the alert closes as fixed; if the problem is reintroduced later, it can reopen. Alerts can be dismissed with a reason: false positive, used in tests or won't fix, with an optional comment. Alerts found only in a pull request branch appear on the pull request and become default-branch alerts only if merged. Alert assignment lets a security team route alerts to a person.

Worked problem

Leocadie's first question

"A Go service uses default setup. The team adds a ruleset that blocks merging when code scanning finds high or higher. A pull request adds a SQL query built from user input. What does the developer see, and what can they do?"

Show solution
  1. Default setup runs on the pull request because it targets the default branch.
  2. CodeQL finds the injection and raises a result with high or critical security severity.
  3. The pull request shows the alert as an annotation, and the ruleset's code scanning rule fails, blocking merge.
  4. Copilot Autofix may suggest a fix in a comment; the developer can commit it, edit it or fix by hand.
  5. If the developer believes it is a false positive, they can dismiss the alert with a reason, subject to the team's dismissal controls; the merge then proceeds.

Answer: A blocking annotation with a possible Autofix suggestion; the developer fixes the code or, if justified, dismisses with a reason.

Copilot Autofix

Copilot Autofix generates suggested fixes for code scanning alerts. It is on by default for repositories using CodeQL where the feature is available: public repositories and repositories with Code Security. In a pull request, suggestions appear as comments that a developer can commit; on an existing alert, a developer can generate a fix and open a pull request. Autofix also supports some third-party tools. It can be turned off at enterprise, organization or repository level, and Skerry turned it off for its 18 certified firmware repositories, where every change needs a formal review trail. Autofix suggestions must be reviewed like any other change: they can be wrong, incomplete or introduce new issues.

Security campaigns

A security campaign is a time-boxed effort to fix a chosen set of code scanning alerts across many repositories. A security manager or organization owner selects alerts, often from a template such as a top CWE category, sets a due date and names a campaign contact. Developers with write access to the affected repositories are notified, see the campaign in their repositories, and get Autofix suggestions generated automatically where possible. Progress is tracked on the campaign page and can be linked to tracking issues. Campaigns became generally available for code scanning in April 2025; secret scanning campaigns are described in the documentation as a preview. Skerry's first campaign targeted SQL injection in reporting services: 186 alerts in 31 repositories, launched October 26 with a December 18 due date and Seraphine as the contact.

Worked problem

Campaign or ruleset?

Leocadie's second question: "An organization has 400 old cross-site scripting alerts across 60 repositories, and new ones keep arriving in pull requests. Which features address each problem?"

Show solution
  1. The 400 existing alerts are a backlog: a security campaign with a due date, contact and Autofix suggestions addresses them.
  2. New alerts arriving in pull requests are a prevention problem: a ruleset with a code scanning rule blocks merging when results reach a threshold.
  3. Autofix helps both: it suggests fixes in campaigns and in pull requests.
  4. Security overview tracks both: campaign progress and the trend of new alerts.

Answer: A campaign for the backlog and a code scanning ruleset for new alerts, with Autofix helping both.

Seraphine answered all of Leocadie's questions correctly, but slowly. Leocadie's verdict was that speed, not knowledge, was the risk in this domain, and that with only 10 to 15 percent of the exam at stake, Seraphine should spend her saved time on administration. Priya's cards read: "default first; advanced for builds, packs, triggers; never both; category keeps uploads apart; security severity drives gates; Autofix on by default; campaign for backlog, ruleset for new."

Languages, build modes and pull request results

CodeQL supports a defined set of languages, including C and C++, C#, Go, Java and Kotlin, JavaScript and TypeScript, Python, Ruby, Swift, and GitHub Actions workflows, with the list growing over time. Default setup detects which of them are present. For compiled languages, CodeQL has three build modes: none, which analyzes source without building and is the default for several languages in default setup; autobuild, which tries to build the project automatically; and manual, where the workflow runs the real build commands. When a question describes missing results for a compiled language that needs generated code or a vendor toolchain, manual build mode in advanced setup is the likely answer.

Pull request analysis has a cost and a benefit. The benefit is that developers see problems before merge, which is where Skerry found that most fixes happened: about half of November's new pull request alerts were fixed with Autofix and about a quarter by hand. The cost is analysis time. Incremental analysis, added for some languages in 2026, analyzes only what changed in a pull request, which shortens runs for large repositories. Questions about slow pull request checks may point to incremental analysis or, in advanced setup, to splitting languages into separate jobs.

Signals that identify which Code Security feature a question describes.
Signal in the questionPoints to
No workflow file, results appear anywayDefault setup
codeql.yml in .github/workflowsAdvanced setup
Results from a tool that is not CodeQLSARIF upload
Merge blocked on new high alertsRuleset with a code scanning rule
Many alerts fixed by a due dateSecurity campaign
Suggested code change in a pull request commentCopilot Autofix

Seraphine added one more distinction to her cards after Leocadie pointed it out. A code scanning rule in a ruleset blocks merging based on results that exist for the pull request; it does not run the analysis. If no analysis runs, because the workflow is missing or failed, the rule can block the merge for lack of results. A question about pull requests that cannot merge although the code is clean may be describing a missing or failed analysis, not a vulnerability.

Common mistakes

  • MistakeChoosing advanced setup for ordinary repositories.

    InsteadStart with default setup; use advanced setup for a specific need such as custom builds or queries.

  • MistakeRunning default and advanced setup together.

    InsteadA repository uses one or the other; switching to advanced setup turns default setup off.

  • MistakeConfusing severity with security severity.

    InsteadSecurity severity, low to critical, is what merge protection thresholds such as high or higher use.

  • MistakeUploading several SARIF analyses for one commit without categories.

    InsteadUse a category per analysis so uploads do not replace each other.

  • MistakeUsing a campaign to stop new alerts.

    InsteadCampaigns clear backlogs; rulesets and pull request checks prevent new alerts.

Summary and key terms

  • Code Security is 10 to 15 percent: code scanning, CodeQL, SARIF, Autofix and campaigns.
  • CodeQL builds a database and runs query suites; packs carry queries and models.
  • Default setup needs no workflow and suits most repositories; advanced setup is a workflow for special needs.
  • Other tools and other CI systems upload SARIF; categories separate analyses; upload limits apply.
  • Security severity drives merge protection; alerts track across commits and are dismissed with reasons.
  • Autofix is on by default with CodeQL; campaigns fix chosen alerts across repositories by a due date.
CodeQL database
A representation of code that CodeQL queries run against.
Query suite
A named set of CodeQL queries, such as the default suite or security-extended.
Default setup
Code scanning configured in settings, with automatic language detection and no workflow file.
Advanced setup
Code scanning configured with a CodeQL workflow file committed to the repository.
Security severity
The low to critical rating on security alerts that merge protection thresholds use.
Security campaign
A time-boxed effort to fix selected code scanning alerts across repositories.

Practice problems

Work each problem on paper first, then reveal the answer to check.

  1. Which permission must an advanced setup workflow have to upload results?

    Show answer

    security-events: write.

  2. What keeps two SARIF uploads for the same commit from replacing each other?

    Show answer

    Different categories.

  3. Where can Copilot Autofix be turned off?

    Show answer

    At enterprise, organization or repository level.

Lesson quiz · 5 questions

Score 60% or more to complete this lesson.

Take the quiz