Skip to content

Domain 4: Configure and Use Code Scanning with CodeQL (25%) โ€‹

โ† Domain 3 ยท Next Domain โ†’

Exam Tip

Know the difference between default setup and advanced setup, when to use each, and how code scanning integrates with branch protection to block PRs. Also understand SARIF โ€” it's frequently tested as the way to bring third-party SAST results into GitHub.


What is Code Scanning? โ€‹

Code scanning is a Static Application Security Testing (SAST) feature that analyzes source code to find security vulnerabilities and coding errors.

GitHub's code scanning is powered by CodeQL โ€” a semantic code analysis engine that treats code as data, allowing you to query it for vulnerability patterns.

Supported Languages โ€‹

  • C / C++
  • C# / .NET
  • Go
  • Java / Kotlin
  • JavaScript / TypeScript
  • Python
  • Ruby
  • Swift (for iOS/macOS apps)

CodeQL Setup Options โ€‹

Default Setup โ€‹

The fastest way to enable CodeQL โ€” GitHub automatically:

  • Detects the languages in your repository
  • Selects the appropriate query suite
  • Configures scan triggers (push to default branch, PRs to default branch)
  • No workflow YAML file needed

Enable via: Settings โ†’ Code security โ†’ Code scanning โ†’ Set up โ†’ Default

Best for

Repositories where you want immediate, zero-configuration scanning. Ideal for most projects.

Advanced Setup โ€‹

A GitHub Actions workflow file (.github/workflows/codeql.yml) gives you full control over:

  • Which query suites to run (default, extended, custom)
  • Which branches to scan
  • Scan schedule (cron)
  • Build commands for compiled languages
  • Custom CodeQL packs

Enable via: Settings โ†’ Code security โ†’ Code scanning โ†’ Set up โ†’ Advanced

Example Advanced CodeQL Workflow โ€‹

yaml
name: CodeQL Analysis

on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main]
  schedule:
    - cron: '0 2 * * 1'   # Every Monday at 2am UTC

jobs:
  analyze:
    name: Analyze
    runs-on: ubuntu-latest
    permissions:
      security-events: write
      contents: read

    strategy:
      matrix:
        language: [javascript, python]

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Initialize CodeQL
        uses: github/codeql-action/init@v3
        with:
          languages: ${{ matrix.language }}
          queries: security-extended   # or: security-and-quality

      - name: Autobuild
        uses: github/codeql-action/autobuild@v3

      - name: Perform CodeQL Analysis
        uses: github/codeql-action/analyze@v3

CodeQL Query Suites โ€‹

SuiteDescriptionWhen to use
security-extended (default)Security queries + additional CWE coverageMost repositories
security-and-qualitySecurity + code quality queries (can be noisy)When you want code quality coverage too
Custom packsYour own or third-party CodeQL queriesSpecialized security requirements

Exam Trap

Default setup uses the security-extended query suite by default โ€” not security-and-quality. The quality suite generates more alerts and is not enabled by default because it may produce more noise.


Code Scanning Alerts โ€‹

Alert Properties โ€‹

Each code scanning alert includes:

  • Rule ID (e.g., js/sql-injection)
  • Severity: Critical, High, Medium, Low, Note, Warning
  • CWE category (e.g., CWE-89 for SQL Injection)
  • Location: File, line number, and code snippet
  • Description and recommended remediation
  • Path: The data flow from source to sink (for dataflow vulnerabilities)

Alert States โ€‹

StateMeaning
OpenActive vulnerability, needs fix
FixedCode was changed and re-scan shows no violation
Dismissed โ€” Won't fixAccepted risk; not going to fix
Dismissed โ€” False positiveNot actually a vulnerability
Dismissed โ€” Used in testsOnly in test code, not production

Alert Severity and CVSS โ€‹

Code scanning alerts use two severity scales:

  • Security Severity (CVSS-based): Critical, High, Medium, Low โ€” for security vulnerabilities
  • Alert Severity: Error, Warning, Note โ€” for code quality issues

SARIF (Static Analysis Results Interchange Format) โ€‹

SARIF is an open standard (JSON-based) format for representing static analysis results. It allows you to bring results from any SAST tool into GitHub's code scanning interface.

Why SARIF Matters โ€‹

  • You can use CodeQL AND third-party tools (Snyk, Checkmarx, SonarCloud, Semgrep, Veracode)
  • All results appear in the unified Security โ†’ Code scanning alerts view
  • Enables comparison and deduplication across tools

Uploading SARIF Results โ€‹

yaml
- name: Upload SARIF
  uses: github/codeql-action/upload-sarif@v3
  with:
    sarif_file: results.sarif
    category: my-tool-name   # Distinguishes this tool's results

SARIF File Requirements โ€‹

  • Must be valid JSON conforming to the SARIF 2.1.0 schema
  • Maximum file size: 64MB (uncompressed)
  • Results are retained for 90 days

Code Scanning in Pull Requests โ€‹

How PR Integration Works โ€‹

  1. When a PR is opened targeting a protected branch, the code scanning workflow runs
  2. New alerts introduced by the PR appear as checks on the PR
  3. If the check fails: the PR is blocked from merging (when branch protection requires it)
  4. Alerts are displayed inline at the affected line in the PR diff

Configuring Branch Protection for Code Scanning โ€‹

In branch protection rules for main:

  • Require status checks to pass before merging
  • Add the CodeQL check: CodeQL (or your tool's check name)
  • With this in place, PRs that introduce new code scanning alerts cannot merge

Exam Trap

Code scanning only blocks merges when you configure the check in branch protection rules. Enabling code scanning alone does not block PRs โ€” you must also configure the branch protection rule to require the check.


Comparing Default vs Advanced Setup โ€‹

Default SetupAdvanced Setup
ConfigurationZero YAML โ€” GitHub auto-configures.github/workflows/codeql.yml required
Language detectionAutomaticManual (matrix configuration)
Query suitesecurity-extended (auto)Configurable (any suite or custom queries)
ScheduleOn push and PR to default branchFully configurable (cron, any branch)
Build stepAutomatic (autobuild)Manual (specify build commands)
Best forQuick start, standard projectsMonorepos, compiled languages needing custom build, custom queries

Troubleshooting Code Scanning โ€‹

ProblemLikely CauseFix
No alerts generatedLanguage not supported or wrong language configVerify language matrix in workflow
Autobuild failsCompiled language requires specific build stepsUse advanced setup with manual build commands
Too many alerts (noise)security-and-quality suite enabledSwitch to security-extended
PR check not blocking mergeBranch protection not requiring the checkAdd CodeQL to required status checks in branch protection
SARIF upload failsFile too large or invalid formatValidate SARIF schema, check 64MB limit

Domain 4 Quick Quiz

1 / 5
โ“

What is the difference between CodeQL default setup and advanced setup?

(Click to reveal)
๐Ÿ’ก
Default setup: Zero YAML, GitHub auto-detects languages and configures scanning โ€” fastest to enable. Advanced setup: You write a codeql.yml workflow for custom query suites, build steps, and schedules โ€” maximum control.

โ† Domain 3 ยท Next Domain โ†’

Happy Studying! ๐Ÿš€ โ€ข Privacy-friendly analytics โ€” no cookies, no personal data
Privacy Policy โ€ข AI Disclaimer โ€ข Report an issue