# CodeQL

**CodeQL** 是 GitHub 开发的一款语义代码分析引擎，它把代码库提取为关系数据库，让分析人员能够通过运行查询来发现缺陷和安全漏洞。GitHub 将其描述为一个把代码当作数据来处理的系统，其创建目的是比传统静态分析器更有把握地发现漏洞，它为 GitHub Advanced Security 的代码扫描功能提供支持。<sup>[1](https://docs.github.com/en/code-security/concepts/code-scanning/codeql/codeql-code-scanning)</sup> 该引擎由 Semmle 创建，现由微软维护；2019 年 GitHub 收购这家分析公司 Semmle 之后，它成为 LGTM 的继任者。<sup>[2](https://dl.acm.org/doi/full/10.1145/3728923)</sup><sup> • </sup><sup>[3](https://www.ieee-security.org/TC/SP2026/downloads/posters/sp2026posters-final46.pdf)</sup><sup> • </sup><sup>[19](https://github.blog/security/vulnerability-research/the-github-security-labs-journey-to-disclosing-500-cves-in-open-source-projects/)</sup>

| 关键事实 | 详情 |
|---|---|
| 它是什么 | GitHub 的代码分析引擎；把代码视为可查询的关系数据库，并将结果作为代码扫描警报报告<sup>[1](https://docs.github.com/en/code-security/concepts/code-scanning/codeql/codeql-code-scanning)</sup> |
| 起源 | 由 Semmle 创建、微软维护，在 GitHub 于 2019 年收购 Semmle 后接替 LGTM<sup>[2](https://dl.acm.org/doi/full/10.1145/3728923)</sup><sup> • </sup><sup>[3](https://www.ieee-security.org/TC/SP2026/downloads/posters/sp2026posters-final46.pdf)</sup><sup> • </sup><sup>[19](https://github.blog/security/vulnerability-research/the-github-security-labs-journey-to-disclosing-500-cves-in-open-source-projects/)</sup> |
| 查询语言 | QL，一种 Datalog 方言，具有类、子类型和动态分派，语法受 SQL 启发<sup>[4](https://www.plai.ifi.lmu.de/publications/scored23-diff-codeql.pdf)</sup> |
| 语言 | C/C++、C#、Go、Java/Kotlin、JavaScript/TypeScript、Python、Ruby、Rust、Swift 以及 GitHub Actions 工作流；不支持 PHP 和 Scala<sup>[1](https://docs.github.com/en/code-security/concepts/code-scanning/codeql/codeql-code-scanning)</sup> |
| 规模 | 用于超过 100,000 个代码仓库，GitHub.com 上每天运行近 150,000 次 CodeQL 检查<sup>[5](https://github.blog/changelog/2023-02-07-codeql-code-scanning-is-now-16-faster/)</sup> |
| 许可 | 查询和库采用 MIT 许可；CLI 单独许可，用于闭源代码需要商业许可<sup>[6](https://github.com/github/codeql/)</sup> |
| 实测性能 | 在一项嵌入式软件研究中，分析在 40 分钟内完成，精确率 71%，召回率 12%<sup>[2](https://dl.acm.org/doi/full/10.1145/3728923)</sup> |

## 工作原理

CodeQL 分析分三步进行：从代码库创建 CodeQL 数据库，对该数据库运行 QL 查询，然后解释结果。<sup>[7](https://codeql.github.com/docs/codeql-overview/about-codeql/)</sup> 创建数据库时，CodeQL 为每个源文件提取单一的关系表示，既捕获关于抽象语法树的语法数据，也捕获关于名称绑定和类型信息的语义数据。对于编译型语言，提取通过监控正常的构建过程来完成：编译器每处理一个源文件，就制作一份副本并收集所有相关信息。对于解释型语言，提取器直接在源代码上运行。<sup>[7](https://codeql.github.com/docs/codeql-overview/about-codeql/)</sup>

得到的数据库是代码的完整层次化表示，包括抽象语法树、控制流图和数据流图，一次针对一种语言。<sup>[7](https://codeql.github.com/docs/codeql-overview/about-codeql/)</sup><sup> • </sup><sup>[4](https://www.plai.ifi.lmu.de/publications/scored23-diff-codeql.pdf)</sup> 关于该系统的学术描述指出，每种语言都有自己的模式和提取器，数据库由代码库的多个中间表示组成。<sup>[4](https://www.plai.ifi.lmu.de/publications/scored23-diff-codeql.pdf)</sup>

**QL 语言。** 查询用 QL 编写，这是一种专门设计的面向对象查询语言。在语义上它是 Datalog 的一种方言，包含类、子类型和动态分派；其语法受 SQL 启发，操作是逻辑的和声明式的，规定结果必须满足哪些性质，而不是规定计算它们的步骤。<sup>[7](https://codeql.github.com/docs/codeql-overview/about-codeql/)</sup><sup> • </sup><sup>[4](https://www.plai.ifi.lmu.de/publications/scored23-diff-codeql.pdf)</sup><sup> • </sup><sup>[8](https://blog.doyensec.com/2022/10/06/semgrep-codeql.html)</sup> 在底层，QL 编译为 Datalog，由 CodeQL 求解器针对提取出的关系数据库对查询求值。<sup>[9](https://arxiv.org/html/2308.09660v1)</sup> 因此，一个表达“未经验证的输入流向汇聚点”的查询，是关于数据流图的声明式陈述，而不是文本模式匹配：求解器会计算出数据库中满足该陈述的路径。

有两类查询对安全工作很重要。警报查询突出显示与某个问题相关的特定代码位置，而路径查询显示指定源点与汇聚点之间的完整数据流动。<sup>[4](https://www.plai.ifi.lmu.de/publications/scored23-diff-codeql.pdf)</sup> 带有适当元数据的查询可以被解释为代码扫描警报；路径查询可以显示数据流或控制流路径，而没有元数据的查询则作为普通表格输出。<sup>[7](https://codeql.github.com/docs/codeql-overview/about-codeql/)</sup> 同一个数据库还支持变体分析，即把已知安全漏洞作为种子，在代码中寻找类似问题的过程。<sup>[7](https://codeql.github.com/docs/codeql-overview/about-codeql/)</sup>

## 语言与查询生态

CodeQL 支持 C/C++（C89 至 C23，C++98 至 C++23）、C#（最高 C# 14，.NET 最高 10）、Go（最高 1.27）、Java 7 至 26、Kotlin 1.8.0 至 2.4.20、Python 2.7 至 3.14、Ruby（最高 3.3）、Rust 2021 和 2024 版、Swift 5.4 至 6.3，以及 [ECMAScript](https://www.edgepedia.org/zh/ecmascript) 2022 或更低版本的 JavaScript/[TypeScript](https://www.edgepedia.org/zh/typescript)。对 C23 和 C++23 的支持目前处于测试阶段。<sup>[10](https://codeql.github.com/docs/codeql-overview/supported-languages-and-frameworks/)</sup> GitHub 的文档指出，CodeQL 不支持此列表之外的语言，包括 PHP 和 Scala，并且对不支持的语言尝试使用它可能不会产生任何警报，或产生不完整的分析。<sup>[1](https://docs.github.com/en/code-security/concepts/code-scanning/codeql/codeql-code-scanning)</sup>

**查询套件。** GitHub 维护三个内置套件。`default` 套件精确度很高，误报很少；`security-extended` 增加了精确度和严重性略低的查询，可能返回更多误报；`security-and-quality` 在扩展套件之外增加了针对可维护性和可靠性问题的查询。<sup>[11](https://docs.github.com/en/code-security/concepts/code-scanning/codeql/codeql-query-suites)</sup> 单个查询带有元数据，包括其预期精确度、所针对问题的严重性，以及诸如所针对的通用弱点枚举（CWE）之类的标签。<sup>[12](https://arxiv.org/html/2605.07900)</sup>

默认查询由 GitHub 专家、安全研究人员和社区贡献者编写和维护，并定期更新以改进分析、减少误报。<sup>[1](https://docs.github.com/en/code-security/concepts/code-scanning/codeql/codeql-code-scanning)</sup> 自定义查询可以作为查询包发布到 GitHub 容器注册表来共享；一个包包含其查询的所有传递依赖项和编译缓存，因此只要包或 CLI 版本不变，运行该包就会得到相同的结果。<sup>[1](https://docs.github.com/en/code-security/concepts/code-scanning/codeql/codeql-code-scanning)</sup>

## 与 GitHub 及 CI 工作流的集成

在 GitHub 代码扫描中，默认设置会自动选择要分析的语言、查询套件和触发事件；高级设置则通过 [GitHub Actions](https://www.edgepedia.org/zh/github-actions) 工作流运行 CodeQL CLI，让团队能够控制配置。<sup>[1](https://docs.github.com/en/code-security/concepts/code-scanning/codeql/codeql-code-scanning)</sup> 代码扫描开箱即用地运行数百个预定义查询，在公共仓库上免费，或作为企业版 GitHub Advanced Security 的一部分提供。<sup>[13](https://github.com/readme/guides/custom-codeql-queries)</sup>

CodeQL 也可以在 GitHub 托管工作流之外使用。CodeQL CLI 运行分析并产生结果，而 [Visual Studio Code](https://www.edgepedia.org/zh/visual-studio-code) 扩展为 QL 语言提供语法高亮、IntelliSense、代码导航和单元测试支持。<sup>[6](https://github.com/github/codeql/)</sup>

## 许可与可用性

标准的 CodeQL 库和查询在 MIT 许可下开源，并为 GitHub Advanced Security 提供支持。<sup>[6](https://github.com/github/codeql/)</sup> CodeQL CLI（包括分析引擎）单独托管、单独许可：使用它分析闭源代码需要商业许可。<sup>[6](https://github.com/github/codeql/)</sup> 这种区分使该工具对开源代码库免费，一项同行评审的评估认为这是它在安全分析人员中流行的原因之一。<sup>[2](https://dl.acm.org/doi/full/10.1145/3728923)</sup>

在商业使用方面，GitHub 的商业模式核心是销售 GitHub Advanced Security，按每个被扫描仓库中提交代码的用户数收费，而不是提供面向漏洞研究人员的独立商业访问。<sup>[14](https://goingbeyondgrep.com/posts/a-deeper-look-at-modern-sast-tools/)</sup>

## 数据一览

采用率和吞吐量数据来自 GitHub 自己的变更日志：CodeQL 为超过 100,000 个仓库的代码扫描提供支持，GitHub 每天运行近 150,000 次 CodeQL 检查。2.12 版（2023 年 2 月）与 2022 年 9 月的 2.11 版相比，在同样的 55,000 个仓库上平均分析时间缩短了 15.7%。<sup>[5](https://github.blog/changelog/2023-02-07-codeql-code-scanning-is-now-16-faster/)</sup>

**增量分析。** 一项针对真实 Ruby 项目上生产环境 CodeQL 分析的研究发现，完全增量分析最多需要约 15 秒即可更新影响最多 1,000 行代码的提交的结果，而首次从头分析耗时超过一小时；使用缓存时内存占用达到约 70 GB。混合方法将初始化时间缩短到约 15 分钟，内存降至约 20 GB，增量更新仍在一分钟以内。<sup>[9](https://arxiv.org/html/2308.09660v1)</sup>

**检测实测。** 在一项针对开源嵌入式软件的评估中，CodeQL 在 40 分钟内完成分析，发出 11,101 条警告，精确率 71%（11,101 条中 7,904 条），召回率 12%（65,263 条中 7,904 条）；同一研究报告在 258 个项目中发现了 709 个缺陷。<sup>[2](https://dl.acm.org/doi/full/10.1145/3728923)</sup> 一项针对 CVEFixes 仓库的纵向生态研究测量发现，规则准确性在各版本之间变化不大，从 v2.6.0 到 v2.24.0 最多提高 10%，但通过 CodeQL 本可以及时发现超过 50 个 CVE。<sup>[3](https://www.ieee-security.org/TC/SP2026/downloads/posters/sp2026posters-final46.pdf)</sup> 安全分析人员和开发人员已使用 CodeQL 在大型、经过充分测试的代码库（包括 [Linux 内核](https://www.edgepedia.org/zh/linux-kernel)）中发现了数千个安全漏洞。<sup>[2](https://dl.acm.org/doi/full/10.1145/3728923)</sup>

## 与其他静态分析工具的比较

有明确记录的最清晰对比是与 Semgrep 的比较。Semgrep 直接对源代码进行操作，而 CodeQL 需要可构建的环境；Semgrep 规则类似于被扫描的源语言，而 CodeQL 使用单独的领域特定查询语言。Semgrep 提供可在任何地方运行的开源和专有选项，而 CodeQL 整体上并非开源，并且对非开源代码需要付费。<sup>[15](https://docs.semgrep.dev/faq/comparisons/codeql)</sup> 安全咨询公司 Doyensec 的从业者比较补充说，CodeQL 分析包括构建代码库、创建数据库和查询数据库，因此 Semgrep 在设置和扫描时间上胜出，但 CodeQL 对代码的解释更深入，并构建了一个持久的可搜索数据库。<sup>[8](https://blog.doyensec.com/2022/10/06/semgrep-codeql.html)</sup> 这就是基于模式的工具与基于查询的语义分析之间的结构性差异：基于查询的静态应用安全测试（SAST）工具将漏洞知识编纂成类似 SQL 的查询，对从软件派生的数据库运行，而不是匹配语法模式。<sup>[16](https://doi.org/10.1109/tdsc.2024.3354789)</sup>

静态分析总体上存在有据可查的局限。一项针对真实世界程序的基准研究发现，最先进的静态分析器会漏掉 47% 到 80% 的漏洞，通过以额外成本组合多个分析器的结果，这一假阴性率可降至 30% 到 69%。<sup>[17](https://dl.acm.org/doi/10.1145/3533767.3534380)</sup>

## 近期发展

**Copilot Autofix。** 针对 CodeQL 代码扫描警报的 Copilot Autofix 于 2024 年 8 月 14 日正式面向 GitHub.com 上所有 GitHub Advanced Security 客户全面可用。测试计划的数据显示，有修复建议的漏洞在所有漏洞类型上的修复速度为原来的 3 倍，跨站脚本为 7 倍，SQL 注入为 12 倍。<sup>[18](https://github.blog/changelog/2024-08-14-copilot-autofix-for-codeql-code-scanning-alerts-is-now-generally-available/)</sup>

**语言覆盖。** 支持的语言版本持续推进：Java 最高 26，C# 最高 14 及 .NET 10，Go 最高 1.27，Python 最高 3.14，Swift 5.4 至 6.3，Rust 2021 和 2024 版，以及 C23 和 C++23 的测试版支持。<sup>[10](https://codeql.github.com/docs/codeql-overview/supported-languages-and-frameworks/)</sup> GitHub Actions 工作流现在与传统的编程语言一样，也是被分析的语言。<sup>[1](https://docs.github.com/en/code-security/concepts/code-scanning/codeql/codeql-code-scanning)</sup>

**研究方向。** 使用已知漏洞作为种子的变体分析仍然是该平台有据可查的用途。<sup>[7](https://codeql.github.com/docs/codeql-overview/about-codeql/)</sup>

## References

1. [Code scanning with CodeQL, GitHub Docs](https://docs.github.com/en/code-security/concepts/code-scanning/codeql/codeql-code-scanning)
2. [Finding 709 Defects in 258 Projects: An Experience Report on Applying CodeQL to Open-Source Embedded Software, Proceedings of the ACM on Software Engineering](https://dl.acm.org/doi/full/10.1145/3728923)
3. [Poster: An In-Depth Analysis of the CodeQL Ecosystem, IEEE S&P 2026](https://www.ieee-security.org/TC/SP2026/downloads/posters/sp2026posters-final46.pdf)
4. [Differential Static Analysis for Detecting Malicious Updates to Open Source Packages, SCORED 2023](https://www.plai.ifi.lmu.de/publications/scored23-diff-codeql.pdf)
5. [CodeQL code scanning is now 16% faster, GitHub changelog](https://github.blog/changelog/2023-02-07-codeql-code-scanning-is-now-16-faster/)
6. [github/codeql repository](https://github.com/github/codeql/)
7. [About CodeQL, CodeQL documentation](https://codeql.github.com/docs/codeql-overview/about-codeql/)
8. [Comparing Semgrep and CodeQL, Doyensec blog](https://blog.doyensec.com/2022/10/06/semgrep-codeql.html)
9. [Incrementalizing Production CodeQL Analyses, arXiv](https://arxiv.org/html/2308.09660v1)
10. [Supported languages and frameworks, CodeQL documentation](https://codeql.github.com/docs/codeql-overview/supported-languages-and-frameworks/)
11. [CodeQL query suites, GitHub Docs](https://docs.github.com/en/code-security/concepts/code-scanning/codeql/codeql-query-suites)
12. [Longitudinal Analyses of SAST Tools: A CodeQL Case Study, arXiv](https://arxiv.org/html/2605.07900)
13. [A beginner's guide to running and managing custom CodeQL queries, GitHub ReadME](https://github.com/readme/guides/custom-codeql-queries)
14. [A Deeper Look at Modern SAST Tools, Going Beyond Grep](https://goingbeyondgrep.com/posts/a-deeper-look-at-modern-sast-tools/)
15. [Compare Semgrep to CodeQL, Semgrep documentation](https://docs.semgrep.dev/faq/comparisons/codeql)
16. [Evaluating C/C++ Vulnerability Detectability of Query-Based Static Application Security Testing Tools, IEEE TDSC](https://doi.org/10.1109/tdsc.2024.3354789)
17. [An empirical study on the effectiveness of static C code analyzers for vulnerability detection, ISSTA 2022](https://dl.acm.org/doi/10.1145/3533767.3534380)
18. [Copilot Autofix for CodeQL code scanning alerts is now generally available, GitHub changelog](https://github.blog/changelog/2024-08-14-copilot-autofix-for-codeql-code-scanning-alerts-is-now-generally-available/)
19. [The GitHub Security Lab’s journey to disclosing 500 CVEs in open source projects](https://github.blog/security/vulnerability-research/the-github-security-labs-journey-to-disclosing-500-cves-in-open-source-projects/)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Named software products and platforms › Static analysis and code quality tools

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/codeql

English original: https://www.edgepedia.org/codeql
