Sub topic 11
Writing and running a custom query
This chapter assumes you can read a CodeQL workflow, know what a configuration file does, and understand that CodeQL queries run against a database built from your code. Here you will write a small query, store it in the repository, and make the scan run it.
What a query looks like
A CodeQL query is a .ql file. It starts with a metadata comment, then imports a language library, then uses a from, where, select structure similar to SQL.
/**
* @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."Each part has a job:
@nameand@descriptionappear in the alert.@kind problemmeans each result is one code location with a message. Other kinds, such aspath-problem, trace data flow from a source to a sink.@problem.severitysets how serious the alert is.@idmust be unique. Use a language prefix and a namespace you own.import javascriptloads the JavaScript library. Use the library for the language you scan.fromdeclares variables, herecall, which ranges over every call expression in the database.wherekeeps only calls whose callee name iseval.selectreturns the location and the alert message.
Where the query goes
Put the query in a directory the configuration file can point to. A query pack needs a qlpack.yml file in that directory so CodeQL knows the language library to resolve.
name: example/custom-queries
version: 0.0.1
dependencies:
codeql/javascript-all: "*"Save the query as .github/codeql/custom-queries/eval-call.ql and the pack file as .github/codeql/custom-queries/qlpack.yml.
Making the scan run it
Reference the directory from a configuration file. This one also keeps the security-extended suite and ignores test and dependency paths.
name: "Custom CodeQL configuration"
queries:
- uses: security-extended
- uses: ./.github/codeql/custom-queries
paths-ignore:
- '**/test/**'
- '**/node_modules/**'Then point the init step at the file with the config-file input.
- name: Initialize CodeQL
uses: github/codeql-action/init@v4
with:
languages: ${{ matrix.language }}
build-mode: ${{ matrix.build-mode }}
config-file: ./.github/codeql/codeql-config.ymlTesting before you push
Run the query locally with the CodeQL CLI so you do not wait for a workflow run. Build a database, then run the query against it.
- Create the database:
codeql database create js-db --language=javascript-typescript --source-root . - Run the query:
codeql query run .github/codeql/custom-queries/eval-call.ql --database js-db - Fix the query until the results match what you expect.
The VS Code extension for CodeQL gives the same workflow with a results viewer.
Common errors
| Symptom | Likely cause |
|---|---|
| Query never produces alerts | The config file is not referenced by `config-file`, or the path is wrong. |
| Analysis fails with a pack resolution error | `qlpack.yml` is missing or its dependency does not match the scanned language. |
| Alert appears but has no severity or name | The metadata comment is missing or malformed. |
| A path under `test` still shows alerts | The glob in `paths-ignore` does not match the real path. |
Exercise
You added .github/codeql/custom-queries/eval-call.ql and a qlpack.yml, but the scan reports no alerts from your query even though the code calls eval. List two things to check in the workflow and configuration.
Show answer
Check that the init step sets config-file to the configuration file path, and that the config file lists ./.github/codeql/custom-queries under queries with uses. Also confirm the repository uses advanced setup, since default setup ignores the config file, and that paths-ignore is not excluding the file that calls eval.