Sub topic 1
What code scanning is and where CodeQL fits
Prerequisites
This chapter assumes you already know the following:
- Basic Git and GitHub usage: repositories, branches and pull requests.
- How to read YAML.
- GitHub Actions concepts: workflows, jobs, steps and triggers.
- The common vulnerability classes, such as SQL injection and cross-site scripting.
You don't need to know any CodeQL syntax yet.
What code scanning is
Code scanning is a GitHub feature that runs static analysis on your repository and reports the problems it finds as alerts. Static analysis means examining source code without running it. The analyzer looks for patterns that are known to cause bugs or security flaws, such as user input flowing into a database query without sanitization.
Code scanning does not analyze code itself. It is the layer around an analysis tool. It handles three jobs:
- Running the tool, usually inside a GitHub Actions workflow.
- Storing the results as alerts tied to a file, a line and a commit.
- Showing those alerts in the Security tab and on pull requests.
Because the results are stored in a standard format called SARIF (Static Analysis Results Interchange Format), code scanning can display findings from many tools, not only GitHub's own.
Where CodeQL fits
CodeQL is GitHub's own analysis engine, and it is the tool most repositories use with code scanning. It treats code as data. CodeQL first builds a database that models your program, then runs queries against that database. Each query describes a kind of problem, and each match becomes an alert. The next chapter covers this model in detail.
The relationship looks like this:
The distinction matters when you troubleshoot. If alerts don't appear, the cause could be the workflow, the analysis or the upload. Knowing which stage failed narrows the search.
What CodeQL is good at
CodeQL is strongest at finding vulnerabilities that depend on how data moves through a program. A pattern matcher can spot a call to a dangerous function. CodeQL can also follow a value from an untrusted source, such as an HTTP request parameter, through assignments and function calls to a sensitive sink, such as a SQL query. This is called data flow analysis.
Typical findings include:
- Injection flaws (SQL, command, path traversal)
- Cross-site scripting
- Use of weak cryptography
- Hard-coded credentials
- Unsafe deserialization
CodeQL supports several languages, including JavaScript and TypeScript, Python, Java and Kotlin, C and C++, C#, Go, Ruby, Swift and GitHub Actions workflows themselves. The exact list changes over time, so check GitHub's documentation for the current one.
What code scanning does not replace
Static analysis has limits, and it helps to set expectations early.
| Concern | Handled by code scanning? | Better tool |
|---|---|---|
| Flaws in your own source code | Yes | CodeQL |
| Vulnerable third-party packages | No | Dependabot, dependency review |
| Secrets committed to the repository | No | Secret scanning |
| Runtime and configuration issues | No | Dynamic testing, infrastructure review |
| Business logic errors | Rarely | Code review, tests |
Code scanning also produces false positives (alerts for code that is actually safe) and false negatives (real flaws it misses). Treat alerts as leads to investigate, not as verdicts.
A first look at the setup
You can turn on code scanning in two ways. Default setup is a few clicks in the repository settings, and GitHub picks the languages and queries for you. Advanced setup is a workflow file you own and edit. Later chapters compare them. For orientation, the core of an advanced workflow is two steps around your code:
- name: Initialize CodeQL
uses: github/codeql-action/init@v4
with:
languages: python
build-mode: none
- name: Perform CodeQL analysis
uses: github/codeql-action/analyze@v4The init step prepares CodeQL, and the analyze step builds the database, runs the queries and uploads the SARIF results. This snippet is a fragment of a workflow, not a complete one. A full file also needs triggers, a job, permissions and a checkout step, which a later chapter builds.
Exercise
Exercise
A teammate says, "We have CodeQL, so we don't need Dependabot or secret scanning." Give two reasons this is wrong.
Show answer
CodeQL analyzes the source code you wrote, looking for flaws such as injection. It does not check whether your dependencies have known vulnerabilities (Dependabot and dependency review do that), and it is not designed to detect credentials committed to the repository (secret scanning does that). Each tool covers a different risk.
Exercise
In the diagram above, what is the role of SARIF, and why does it let code scanning show results from tools other than CodeQL?
Show answer
SARIF is a standard file format for static analysis results. Code scanning reads SARIF, not CodeQL's internal output, so any tool that can produce SARIF can upload findings and have them appear as alerts.