Skip to main content

About code scanning with CodeQL

You can use CodeQL to identify vulnerabilities and errors in your code. The results are shown as code scanning alerts in GitHub.

谁可以使用此功能?

Code scanning 可用于以下存储库类型:

  • GitHub.com 上的公共存储库
  • GitHub Team、GitHub Enterprise Cloud 或 GitHub Enterprise Server 上的组织拥有的存储库,已启用 GitHub Advanced Security

注意

网站管理员必须启用 code scanning,然后你才能使用此功能。 有关详细信息,请参阅“为设备配置代码扫描”。

如果企业所有者在企业级别设置了 GitHub Advanced Security 策略,则你可能无法启用或禁用 code scanning。 有关详细信息,请参阅“强制实施企业的代码安全性和分析策略”。

CodeQL 是 GitHub 开发的代码分析引擎,用于自动执行安全检查。 可使用 CodeQL 分析代码,并将结果显示为 code scanning 警报。

There are three main ways to use CodeQL analysis for code scanning:

  • Use default setup to quickly configure CodeQL analysis for code scanning on your repository. Default setup automatically chooses the languages to analyze, query suite to run, and events that trigger scans. If you prefer, you can manually select the query suite to run and languages to analyze. After you enable CodeQL, GitHub Actions will execute workflow runs to scan your code. For more information, see 配置代码扫描的默认设置.

  • Use advanced setup to add the CodeQL workflow to your repository. This generates a customizable workflow file which uses the github/codeql-action to run the CodeQL CLI. For more information, see 配置代码扫描的高级设置.

  • Run the CodeQL CLI directly in an external CI system and upload the results to GitHub. For more information, see 在现有 CI 系统上使用代码扫描.

注意

On GitHub Enterprise Server 3.16, the CodeQL action uses CodeQL CLI version 2.20.3 by default. We recommend that you use the same version of the CodeQL CLI if you run analysis in an external CI system.

For information about code scanning alerts, see About code scanning alerts.

About CodeQL

CodeQL is a programming language and associated tools that treat code like data. It was created explicitly to make it easier to analyze code and find potential vulnerabilities in your code with greater confidence than traditional static analyzers.

  1. You generate a CodeQL database to represent your codebase.
  2. Then you run CodeQL queries on that database to identify problems in the codebase.
  3. The query results are shown as code scanning alerts in GitHub when you use CodeQL with code scanning.

CodeQL supports both compiled and interpreted languages, and can find vulnerabilities and errors in code that's written in the supported languages.

CodeQL supports the following languages:

  • C/C++
  • C#
  • Go
  • Java/Kotlin
  • JavaScript/TypeScript
  • Python
  • Ruby
  • Rust(公共预览版)
  • 切换

注意

  • 使用 java-kotlin 分析用 Java 或/和 Kotlin 编写的代码。
  • 使用 javascript-typescript 分析用 JavaScript 和/或 TypeScript 编写的代码。

有关详细信息,请参阅 CodeQL 网站上的文档:“支持的语言和框架”。

重要

CodeQL does not support languages that are not listed above. This includes, but is not limited to, PHP, Scala, and others. Attempting to use CodeQL with unsupported languages may result in no alerts being generated and incomplete analysis.

Modeling custom or niche frameworks

GitHub experts, security researchers, and community contributors write libraries to model the flow of data in popular frameworks and libraries. If you use custom dependencies that aren't modeled, then you can use the CodeQL extension for Visual Studio Code to create models for these dependencies and use them to extend your analysis. For more information, see 使用 CodeQL 模型编辑器.

CodeQL queries

GitHub experts, security researchers, and community contributors write and maintain the default CodeQL queries used for code scanning. The queries are regularly updated to improve analysis and reduce any false positive results. For details of the queries available in the default and extended packs, see Queries included in the default and security-extended query suites.

Writing your own queries

The queries are open source, so you can view and contribute to the queries in the github/codeql repository. For more information, see About CodeQL queries in the CodeQL documentation.

Running additional queries

If you are scanning your code with advanced setup or an external CI system, you can run additional queries as part of your analysis.

These queries must belong to a published CodeQL query pack or a CodeQL pack in a repository.

  • When a CodeQL query pack is published to the GitHub Container registry, all the transitive dependencies required by the queries and a compilation cache are included in the package. This improves performance and ensures that running the queries in the pack gives identical results every time until you upgrade to a new version of the pack or the CLI.

  • CodeQL query packs can be downloaded from multiple GitHub container registries. For more information, see 自定义代码扫描的高级设置.

For more information, see 使用 CodeQL 包自定义分析.