晚上睡觉脚底发热是什么原因| 为什么排卵期会出血| 什么叫熬夜| 黄色分泌物是什么原因| 克罗恩病是什么病| 变格是什么意思| 里正是什么官| 脸上脂溢性皮炎用什么药| 白面是什么| 曹洪是曹操的什么人| 北上广深是什么意思| 北京属于什么方向| 小狗肚子里有虫子吃什么药| 什么生肖怕老婆| 敢爱敢恨是什么意思| 拉肚子喝什么水| 梨状肌综合症吃什么药| 非典是什么病| 小孩病毒性感冒吃什么药效果好| 下巴脱臼是什么感觉| 孕妇咳嗽可以吃什么药| 肾结石要忌口什么东西| 苹果越狱是什么意思啊| 腰疼是什么病| 咳嗽可以吃什么食物| 米娜桑是什么意思| 曹操属什么生肖| 耳返是什么| 苯磺酸氨氯地平片什么时候吃| 维生素a是什么| 黄体不足吃什么补最快| 沉冤得雪是什么意思| ns是什么单位| 幽门螺旋杆菌感染有什么症状| 胖头鱼是什么鱼| 吃什么增强免疫力最快| 黄精为什么要九蒸九晒| 术后血压低什么原因| 省检察长是什么级别| 痛风打什么针见效最快| 张卫健属什么生肖| 伏羲是什么意思| 心慌是什么原因| 勾心斗角是什么生肖| 核桃什么时候吃最好| 很会放屁是什么原因| 深喉是什么感觉| 横财是什么意思| 脑梗怎么形成的原因是什么| 酒店尾房是什么意思| 宜祭祀是什么意思| 晚上睡觉老做梦是什么原因| 春眠不觉晓的晓是什么意思| 专业职称是什么| 什么血型是万能血型| 李世民是什么生肖| 脾胃虚寒吃什么药| 气体交换受损与什么有关| 苦荞茶和什么搭配最好| 农历3月3是什么节日| 木危读什么| 2023年五行属什么| 血压低吃什么水果| 油炸食品用什么油最好| 胃反流吃什么药效果好| 花荣的绰号是什么| 虚病是什么意思| 移居改姓始为良是什么意思| 什么叫大数据| bull是什么意思| 白带黄吃什么药| 铅中毒是什么引起的| 非营利性医院是什么意思| 小儿消化不良吃什么药最好| 嘴无味是什么病的征兆| 入肉是什么意思| 打一个喷嚏代表什么意思| 雷猴是什么意思| 阁五行属什么| 流鼻血是什么原因引起的| 冬阴功是什么意思| 肛门里面有个肉疙瘩是什么| 白细胞加号什么意思| 梦见倒房子是什么预兆| 健康管理师是干什么的| 水土不服吃什么药管用| 用牛奶敷脸有什么好处和坏处| 全麻是什么感觉| 人彘是什么意思| penguin是什么意思| 喝什么茶对肝脏好| 三伏天喝什么汤| 中元节是什么节| 左上腹是什么器官| 小孩干呕是什么原因| 身心交瘁什么意思| 牵牛花是什么颜色| 不检点是什么意思| 什么油适合高温油炸| 轮回什么意思| 吃什么瘦肚子| 蛇形分班是什么意思| 骨刺是什么原因引起的| 什么时候闰三月| 为什么经常口腔溃疡| 鱼水之欢是什么意思| 梦见红色的蛇是什么意思| 四肢肿胀是什么原因引起的| 右手麻木是什么病| 蛇屎是什么样子| 九月初九是什么节日| 皮肤软组织感染用什么消炎药| hcc是什么意思| foreverlove是什么意思| 白细胞低吃什么药可以增加白细胞| 黑鱼是什么鱼| 一点点奶茶什么最好喝| 猫来家门口有什么预兆| 夏天为什么这么热| cla是什么| 怀孕初期分泌物是什么样的| 结膜炎是什么症状| 过敏性鼻炎喷什么药| 不议价什么意思| 老舍的原名叫什么| 肌酸什么时候喝比较好| 出来混迟早要还的什么意思| 请柬写伉俪什么意思| 测血型挂什么科| 回民为什么不吃猪肉| 一头雾水什么意思| 相什么成趣| 抗战纪念日为什么是9月3日| 它们是指什么| 向日葵为什么会随着太阳转动| 非萎缩性胃炎伴糜烂是什么意思| 为什么牙龈老是出血| 中心句是什么意思| 阴茎不硬是什么原因| rag是什么意思| 茗茶是什么茶| 智商税什么意思| 女命正财代表什么| 宽字五行属什么| 民政局是干什么的| 木鱼花是什么| 平血头晕吃什么药最好| 菩提树是什么树| 放疗后不能吃什么| item什么意思| 裤裙搭配什么上衣好看| 什么方法避孕最安全有效| 梦见腿断了是什么意思| 塔罗牌愚者是什么意思| 失眠是什么原因| 一个马一个尧读什么| 大豆和黄豆有什么区别| 猜疑是什么意思| 阴唇为什么一大一小| 叶酸片什么时候吃| 一见如什么| 补肾最好的药是什么药| 李商隐是什么朝代的| 为什么医院不用咳特灵| 6月21号是什么日子| 木五行属什么| 吃得什么| 肝郁吃什么食物好| 世界杯是什么时候| 七夕送什么| 七六年属什么生肖| 7月11日什么星座| 四面八方什么生肖| 胎毛是什么| 迥异是什么意思| 菲薄是什么意思| 2013年五行属什么| 角化棘皮瘤是什么病| 半边脸疼是什么原因引起的| 四季如春是什么生肖| 红疮狼斑是什么引起的| 浑身没劲什么原因| 正月十六是什么星座| 怀孕了什么时候做检查| 脸上长粉刺是什么原因| 男戴观音女戴佛有什么讲究| 维生素b1有什么作用| 连城诀为什么不火| 主任医师是什么级别| 秃顶是什么原因造成的| 为什么一| 11月21日什么星座| 性格内向的人适合做什么工作| 什么是圆周率| 为什么会便血| 早上吃什么最有营养| 儿童口腔疱疹吃什么药| 吃枸杞有什么好处| 魔芋是什么植物| c14检查前需要注意什么| 结婚6年是什么婚| 子宫肌瘤什么不能吃| 抽筋缺什么| lmp医学上什么意思| 丙肝阳性是什么意思呢| 政治家是什么意思| 狼吞虎咽是什么生肖| 男人蛋皮痒用什么药| ova什么意思| 干呕是什么病的前兆| 主动脉硬化是什么意思| 很多屁放是什么原因| 酵母提取物是什么| 女主是什么意思| 甲钴胺片是治什么的| 丁字五行属什么| 饺子都有什么馅| 脾湿热吃什么中成药| 胆固醇高是什么引起的| Ecmo医学上是什么意思| 宫颈炎是什么原因引起的| 钾低会出现什么症状| 孩子吐了吃什么药| 平衡是什么意思| 吃生葵花籽有什么好处和坏处吗| sport什么品牌| 散光是什么症状| 时迁是什么意思| 下面老是痒是什么原因| 爱什么分明| 什么运动能长高| 足石念什么| nsa是什么意思| 家里有蜈蚣是什么原因| 例假提前是什么原因| 肾的主要功能是什么| 华人是什么意思| 什么运动使人脸部年轻| soie是什么面料| 睡觉腿麻是什么原因引起| 糖尿病人可以吃什么零食| 睡眠好的人说明什么| dr什么意思| 怀孕前壁和后壁有什么区别| 维密是什么意思| 湿肺是什么意思| 筛查是什么意思| 目前除皱最好的方法是什么| 冬虫夏草为什么叫冬虫夏草| 胃溃疡吃什么食物| 小孩儿咳嗽有什么妙招| 侄女结婚送什么礼物最好| 骨裂是什么感觉| 量是什么意思| 64年属什么| dave是什么意思| 吃猪脑有什么好处和坏处| 6朵玫瑰代表什么意思| 出片是什么意思| 星期六打喷嚏代表什么| 爱戴是什么意思| ai是什么元素| 阳光明媚下一句接什么| 政协主席什么级别| 百度
Skip to content

A beginner’s guide to running and managing custom CodeQL queries

Transform your code into a structured database that you can use to surface security vulnerabilities and discover new insights.

Artwork: Micha Huigen

Photo of Denys Lashchevskyi
Betsson logo

Denys Lashchevskyi // Staff Software Engineer, Betsson

The ReadME Project amplifies the voices of the open source community: the maintainers, developers, and teams whose contributions move the world forward every day.

Have you ever wished you could query your code the same way you query a SQL database? Well, that’s exactly what GitHub’s CodeQL enables you to do. It’s a semantic code analysis engine that transforms your code into a structured database that you can use to surface security vulnerabilities or discover new insights.

You don’t need to learn a thing about static analysis or structured queries to benefit from CodeQL. GitHub’s code scanning feature runs hundreds of predefined queries right out of the box—for free on public repositories, or as part of GitHub Advanced Security for enterprises. There are also many more niche “query packs” available that go far beyond the default scans. But while the number of ready-made queries is growing all the time, you can also create your own queries to meet your specific needs.

We’ve been writing custom CodeQL queries at Betsson for about two years, including ones to moderate package use, research and quantify code and quality metrics, and facilitate adherence to code structure and preferred architecture design. In this guide, I share some of what we’ve learned to help you get up and running with custom queries as quickly as possible.


In this guide, you will learn:

  • How to build a quick and minimal local setup.

  • How to create and run a simple custom query.

  • How to to add CodeQL scans to your CI with GitHub Actions.

  • More advanced custom query possibilities.


Set up a simple local environment

In this guide, we will use JavaScript and Visual Studio Code, but you should be able to follow along regardless of your language and code editor of choice. I invite you to fork this small repository where I collected most of the setup used for this article, including a minimal application called “health-app” that we can scan.

You need the CodeQL command-line interface (CLI) tool to create and configure databases, a language pack for your programming language of choice to convert your code into a query-able database, and one or more query packs. You can find the CLI, packs, and the Visual Studio Code plugin on the CodeQL tools page. For help setting everything up, you can refer to the CodeQL CLI quick-start documentation.

You’ll do most of your CodeQL work in the plugin for Visual Studio Code, or a similar plugin for your code editor of choice. The Visual Studio Code plugin enables you to connect to different scan targets, design queries using IntelliSense, and run/view results of your scans from produced SARIF (Static Analysis Results Interchange Format) reports.

Create and run your first custom query

Before you can run a scan, you need the following:?

  1. The project’s source code/repository

  2. A CodeQL database built from that repository

  3. A CodeQL configuration file for the project

Remember, your CodeQL setup—which includes scripts, packages, and databases—will live in a separate directory from the project you’re scanning.

Let’s start by running all commands from the project’s root directory (if you’re using my health-app repository, all of this has been done already):

Initiate CodeQL by running the following (“.” stands for the current directory):

1
codeql pack init -d . codeql

This will create the qlpack.yml file in a new subdirectory called codeql in the project’s root directory.

Configure qlpack.yml by adding the JavaScript language reference:

1
codeql pack add --dir ./codeql codeql/javascript-all

Create a database from your codebase:

1
codeql database create codeql/db -s . -l javascript

This creates a new subdirectory within the root directory called db.

Now let’s make our first custom query! Create a new file in your code editor with the following:

1
2
3
4
import javascript
from PackageDependencies deps, string name
where deps.getADependency(name, _)
select deps, "Dependency found'" + name + "'."

This is a simple query that will return all of a project’s dependencies. Save it as a .ql file inside the newly created codeql subdirectory of the project’s directory. Of course, you can create far more interesting and sophisticated queries, but let’s start here.?

From the VS Code plugin, select the db directory you just created. Then right-click anywhere within the .ql file to run the first scan. The query should produce a list of package.json dependencies.

You can perform many different types of scans with CodeQL. For example, you could block vulnerable log4j usage at scale by disallowing affected versions of the package. You could update the example query we created above to explicitly disallow any library (dotenv in our case) by assigning appropriate security severity level (read on about security severity and alert settings for available options).?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* @name dependencies
* @description finds and lists referenced dependencies
* @kind problem
* @problem.severity error
* @security-severity 10.0
* @tags setup_check
* @id setup
*/
import javascript
from PackageDependencies deps, string name
where deps.getADependency(name, _) and name.matches("dotenv")
select deps, "Dependency found'" + name + "'."

You can learn more about static analysis and using CodeQL for vulnerability detection from GitHub’s recent tutorial. A more exotic use for CodeQL would be implementing fitness functions to proactively pursue architectural designs in a measurable way.

As you can see, running custom queries locally is quite simple. Now let’s take it up a level with GitHub Actions.

Automating CodeQL scans with GitHub Actions

The easiest way to run a custom query with GitHub Actions is with GitHub’s CodeQL Analysis workflow, which uses GitHub’s CodeQL action. It has three main components: setup, runner, and reporter. The setup and runner components are pretty self-explanatory. The reporter uploads scan results and a snapshot of your database to your repository context store, and makes them available in your Security tab. The best part is that you can download the database using a GitHub API call, should you want to investigate further or explore results in a semi-manual mode.

To run the custom dependency query we created above, be sure to add both the .ql and qlpack.yml files to your repository. Then set up the Actions workflow.

If you haven’t already enabled GitHub Actions for the repository, click Settings under your repository name. If you cannot see the Actions tab, select the “...”? dropdown menu, then click Actions. Click the button that says I understand my workflows, go ahead and enable them.

On the Actions tab, click New workflow and search for CodeQL Analysis. There should be one result. Click the Configure button.

You should see an Actions workflow YAML file. Add this line to the file in the github/codeql-action/init section (remember to include the white space):

1
          queries: +./${{ env.CI_TMP_DIR }}/codeql/deps.ql

Click Commit. This should kick off a CodeQL scan. When the scan is complete you should see something like this in the repository’s Security tab:

Dependencies Screenshot

Note: If you’re using my health-app repository, please be aware that the included codeql-custom.yml workflow requires GitHub Advanced Security. If you don’t have Advanced Security, you can still test the custom workflow by following the steps above.

While this process will work for testing our workflow, in the long run, it’s better to use a custom CodeQL configuration file, not the Actions workflow, to manage which custom queries you run.

Exploring further possibilities

You can create multiple-language or multiple-configuration setups to quickly gather more information from a single run or perform multiple scans at once. For example, instead of specifying languages up front, you can automatically detect which languages are used in a repository and spawn appropriate scans based on the results. Here is an example of working with the GitHub CLI to fetch information:

1
gh api repos/${{ env.CI_REPOSITORY }}/languages -q 'keys[]'

And this documentation details how to customize your CodeQL scans.

Make something cool? Share it!

Of course, we just scratched the surface of what can and should be done with CodeQL. There is much more to be discovered in the documentation and the application itself. As you explore this powerful platform, you’ll probably find yourself making things that other people can use. If you create a query that could be useful in practically all codebases, you can submit your query to the open source CodeQL query repository. If it’s a bit more niche—for example, a query that’s only applicable to actions written in JavaScript—you can create your own query pack and share it through GitHub Packages. I look forward to seeing what you come up with.

Denys Lashchevskyi is a staff software engineer for Betsson Group, with experience in DevOps and building developer tooling for automation, integrations, and scripting. He’s been an active GitHub Actions developer for the last two years, creating and integrating actions, script development, and repository access management solutions. He loves to share his knowledge with the community, prefers simple solutions for complex problems, and tries to delete code whenever possible.

About The
ReadME Project

Coding is usually seen as a solitary activity, but it’s actually the world’s largest community effort led by open source maintainers, contributors, and teams. These unsung heroes put in long hours to build software, fix issues, field questions, and manage communities.

The ReadME Project is part of GitHub’s ongoing effort to amplify the voices of the developer community. It’s an evolving space to engage with the community and explore the stories, challenges, technology, and culture that surround the world of open source.

Follow us:

Nominate a developer

Nominate inspiring developers and projects you think we should feature in The ReadME Project.

Support the community

Recognize developers working behind the scenes and help open source projects get the resources they need.

Sign up for the newsletter

Sharpen your open source skills with tips, tools, and insights. Delivered monthly.

河东狮吼什么意思 吃什么补钙 榴莲什么时候吃最好 什么病不能吃玉米 命里缺水取什么名字好
克罗恩病吃什么药 特应性皮炎是什么意思 左眼皮跳跳好事要来到是什么歌 闰年是什么 手汗症是什么原因
95年属什么生肖 孕妇肚子疼是什么原因 hpv81阳性是什么意思 心脏早搏有什么危害 杭州的市花是什么花
白果有什么功效 住院医师是什么意思 糖吃多了有什么危害 先河是什么意思 莞字五行属什么
1958属什么生肖hcv8jop1ns7r.cn 伤官运是什么意思hlguo.com 生命科学专业学什么onlinewuye.com 手脚心出汗是什么原因hcv7jop5ns5r.cn 阳痿是什么意思chuanglingweilai.com
割伤用什么药愈合伤口zhongyiyatai.com 购物狂是什么心理疾病hcv9jop4ns7r.cn 什么时候种胡萝卜最好hcv8jop8ns9r.cn 早搏吃什么药效果好hcv8jop3ns5r.cn 朋友过生日送什么好hcv9jop6ns8r.cn
莱赛尔纤维是什么面料creativexi.com 什么是体外射精hcv9jop0ns3r.cn 痤疮长什么样hcv9jop6ns7r.cn 手机贴什么膜最好hcv8jop4ns9r.cn 迎春花是什么颜色的hcv8jop7ns4r.cn
成人改名字需要什么手续hcv8jop6ns3r.cn scj是什么意思hcv7jop7ns2r.cn 怕金森是什么症状hcv8jop7ns7r.cn 头晕想吐吃什么药hcv8jop3ns6r.cn 口腔异味挂什么科hcv7jop6ns7r.cn
百度