Sub topic 2
How CodeQL analyzes code: database, queries, results
This chapter assumes you know basic Git and GitHub, can read YAML, and have seen a GitHub Actions workflow. It explains what happens inside a CodeQL scan, so the workflow steps in later chapters make sense.
The three stages
CodeQL does not scan source text with pattern matching. It treats your code as data and asks questions about it. A scan has three stages:
- Extract. CodeQL reads your source and builds a database.
- Query. CodeQL runs a set of queries against that database.
- Report. Matches are written as results in SARIF and shown as code scanning alerts.
The database
A CodeQL database is a relational representation of one language in your repository. It holds the syntax tree, the names that refer to each other, control flow (the order in which code can run) and data flow (how values move between variables and functions). It also keeps a copy of the source files so alerts can show the code.
Each database covers one language. A repository with JavaScript and Python needs two databases, which is why the workflow in this tutorial uses a matrix job with one entry per language.
For compiled languages, the extractor watches a build to learn which files are compiled and how. For interpreted languages such as Python and JavaScript, it reads the files directly. This difference is why build modes exist, and a later chapter covers them.
Queries
A query is a program written in QL, a declarative language for asking questions about the database. Queries are grouped into query packs, and a query suite selects which queries from the packs to run.
Most security queries use data flow. They define a source, a place where untrusted data enters (for example an HTTP request parameter). They also define a sink, a place where that data becomes dangerous (for example a SQL execution call). A result appears when data can travel from a source to a sink without passing through a sanitizer. This is how CodeQL finds injection bugs across several functions and files, which simple text search cannot do.
A query file begins with metadata in a comment. The metadata controls how results are shown.
/**
* @name Call to eval
* @description Calling eval can execute attacker-controlled code.
* @kind problem
* @problem.severity warning
* @id js/example/eval-call
* @tags security
*/
import javascript
from CallExpr call
where call.getCalleeName() = "eval"
select call, "Avoid calling eval."The body has three parts:
fromdeclares the variables to search over. Here,callranges over every call expression in the database.wherefilters them. Here, only calls to a function namedevalremain.selectstates what to report: a code location and a message.
The @kind tag matters. problem reports a single location. path-problem reports a path from source to sink, and GitHub shows each step of that path in the alert.
Results and SARIF
The analysis step writes its output as SARIF (Static Analysis Results Interchange Format), a JSON format for static analysis findings. Each result holds a rule ID, a message, a severity, and one or more code locations. The CodeQL action uploads the SARIF file to GitHub, which turns each result into an alert in the Security tab and on pull requests.
Because SARIF is an open format, other tools can feed the same alert system. A later chapter shows how.
Common misunderstandings
- "CodeQL runs while the build runs." For interpreted languages, no build is needed. For compiled languages, the extractor observes the build, and a failed build gives an incomplete database.
- "Every query finds a vulnerability." Some queries report code quality issues. The
@tags securityline and the suite you choose control what you see. - "One scan covers all languages." Each language gets its own database and its own analysis, and results are uploaded under a category per language.
Exercise
A query reports an SQL injection from a request parameter to a database call, with five steps in between. Which @kind does the query most likely use, and what do the terms source and sink mean in it?
Show answer
It most likely uses path-problem, because the alert shows a path of steps. The source is where untrusted data enters (the request parameter). The sink is where the data becomes dangerous (the database call).
Exercise
Your repository contains Go and Python code. How many CodeQL databases does a scan create, and why?
Show answer
Two. A database holds one language, so each language is extracted and analyzed separately.