codeql by github/awesome-copilot
npx skills add https://github.com/github/awesome-copilot --skill codeql此技能提供配置和运行 CodeQL 代码扫描的操作指南——包括通过 GitHub Actions 工作流和独立的 CodeQL CLI。
当请求涉及以下内容时,请使用此技能:
codeql.yml GitHub Actions 工作流codeql database create, database analyze, github upload-results)CodeQL 支持以下语言标识符:
| 语言 | 标识符 | 替代标识符 |
|---|---|---|
| C/C++ |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
c-cppc, cpp |
| C# | csharp | — |
| Go | go | — |
| Java/Kotlin | java-kotlin | java, kotlin |
| JavaScript/TypeScript | javascript-typescript | javascript, typescript |
| Python | python | — |
| Ruby | ruby | — |
| Rust | rust | — |
| Swift | swift | — |
| GitHub Actions | actions | — |
替代标识符等同于标准标识符(例如,
javascript不会排除 TypeScript 分析)。
none 构建模式。.github/workflows/codeql.yml 文件,以便完全控制触发器、构建模式、查询套件和矩阵策略。要从默认设置切换到高级设置:首先禁用默认设置,然后提交工作流文件。
定义扫描运行时机:
on:
push:
branches: [main, protected]
pull_request:
branches: [main]
schedule:
- cron: '30 6 * * 1' # 每周一 UTC 时间 6:30
push — 在推送到指定分支时进行扫描;结果出现在安全选项卡中pull_request — 扫描 PR 合并提交;结果以 PR 检查注释形式出现schedule — 对默认分支进行定期扫描(cron 必须存在于默认分支上)merge_group — 如果仓库使用合并队列,请添加此触发器要跳过仅涉及文档的 PR 扫描:
on:
pull_request:
paths-ignore:
- '**/*.md'
- '**/*.txt'
paths-ignore控制工作流是否运行,而不是控制分析哪些文件。
设置最小权限原则:
permissions:
security-events: write # 上传 SARIF 结果所需
contents: read # 检出代码所需
actions: read # 使用 codeql-action 的私有仓库所需
使用矩阵策略并行分析每种语言:
jobs:
analyze:
name: Analyze (${{ matrix.language }})
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- language: javascript-typescript
build-mode: none
- language: python
build-mode: none
对于编译语言,设置适当的 build-mode:
none — 无需构建(C/C++、C#、Java、Rust 支持)autobuild — 自动构建检测manual — 自定义构建命令(仅限高级设置)有关每种语言的详细自动构建行为和运行器要求,请搜索
references/compiled-languages.md。
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Initialize CodeQL
uses: github/codeql-action/init@v4
with:
languages: ${{ matrix.language }}
build-mode: ${{ matrix.build-mode }}
queries: security-extended
dependency-caching: true
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v4
with:
category: "/language:${{ matrix.language }}"
查询套件选项:
security-extended — 默认安全查询加上额外的覆盖范围security-and-quality — 安全加上代码质量查询packs: 输入使用自定义查询包(例如,codeql/javascript-queries:AlertSuppression.ql)依赖项缓存: 在 init 操作上设置 dependency-caching: true,以便跨运行缓存已恢复的依赖项。
分析类别: 使用 category 在单体仓库中区分 SARIF 结果(例如,按语言、按组件)。
对于具有多个组件的单体仓库,使用 category 参数来分隔 SARIF 结果:
category: "/language:${{ matrix.language }}/component:frontend"
要将分析限制在特定目录,请使用 CodeQL 配置文件 (.github/codeql/codeql-config.yml):
paths:
- apps/
- services/
paths-ignore:
- node_modules/
- '**/test/**'
在工作流中引用它:
- uses: github/codeql-action/init@v4
with:
config-file: .github/codeql/codeql-config.yml
如果 autobuild 失败或需要自定义构建命令:
- language: c-cpp
build-mode: manual
然后在 init 和 analyze 之间添加显式构建步骤:
- if: matrix.build-mode == 'manual'
name: Build
run: |
make bootstrap
make release
下载 CodeQL 捆绑包(包含 CLI + 预编译查询):
# 从 https://github.com/github/codeql-action/releases 下载
# 解压并添加到 PATH
export PATH="$HOME/codeql:$PATH"
# 验证安装
codeql resolve packs
codeql resolve languages
始终使用 CodeQL 捆绑包,而不是独立的 CLI 下载。捆绑包确保查询兼容性并提供预编译查询以获得更好的性能。
# 单一语言
codeql database create codeql-db \
--language=javascript-typescript \
--source-root=src
# 多种语言(集群模式)
codeql database create codeql-dbs \
--db-cluster \
--language=java,python \
--command=./build.sh \
--source-root=src
对于编译语言,通过 --command 提供构建命令。
codeql database analyze codeql-db \
javascript-code-scanning.qls \
--format=sarif-latest \
--sarif-category=javascript \
--output=results.sarif
常用查询套件:<language>-code-scanning.qls、<language>-security-extended.qls、<language>-security-and-quality.qls。
codeql github upload-results \
--repository=owner/repo \
--ref=refs/heads/main \
--commit=<commit-sha> \
--sarif=results.sarif
需要具有 security-events: write 权限的 GITHUB_TOKEN 环境变量。
为避免运行多个命令时重复 JVM 初始化:
codeql execute cli-server
有关详细的 CLI 命令参考,请搜索
references/cli-commands.md。
警报有两个严重性维度:
Error、Warning、NoteCritical、High、Medium、Low(源自 CVSS 分数;显示优先级更高)GitHub Copilot 自动修复功能会自动为拉取请求中的 CodeQL 警报生成修复建议——无需 Copilot 订阅。在提交前请仔细审查建议。
error/critical/high 严重性级别的警报,检查会失败有关详细的警报管理指南,请搜索
references/alert-management.md。
- uses: github/codeql-action/init@v4
with:
packs: |
my-org/my-security-queries@1.0.0
codeql/javascript-queries:AlertSuppression.ql
使用 CodeQL CLI 创建和发布包:
# 初始化新包
codeql pack init my-org/my-queries
# 安装依赖项
codeql pack install
# 发布到 GitHub Container Registry
codeql pack publish
对于高级查询和路径配置,创建 .github/codeql/codeql-config.yml:
paths:
- apps/
- services/
paths-ignore:
- '**/test/**'
- node_modules/
queries:
- uses: security-extended
packs:
javascript-typescript:
- my-org/my-custom-queries
工作流日志包含关键指标:
要启用详细诊断:
--verbosity=progress++ 和 --logdir=codeql-logs| 问题 | 解决方案 |
|---|---|
| 工作流未触发 | 验证 on: 触发器是否匹配事件;检查 paths/branches 过滤器;确保工作流存在于目标分支上 |
Resource not accessible 错误 | 添加 security-events: write 和 contents: read 权限 |
| 自动构建失败 | 切换到 build-mode: manual 并添加显式构建命令 |
| 未看到源代码 | 验证 --source-root、构建命令和语言标识符 |
| C# 编译器失败 | 检查 /p:EmitCompilerGeneratedFiles=true 与 .sqlproj 或旧项目的冲突 |
| 扫描行数少于预期 | 从 none 切换到 autobuild/manual;验证构建是否编译了所有源代码 |
| Kotlin 处于无构建模式 | 禁用并重新启用默认设置以切换到 autobuild |
| 每次运行都缓存未命中 | 验证 init 操作上的 dependency-caching: true |
| 磁盘/内存不足 | 使用更大的运行器;通过 paths 配置减少分析范围;使用 build-mode: none |
| SARIF 上传失败 | 确保令牌具有 security-events: write 权限;检查 10 MB 文件大小限制 |
| SARIF 结果超出限制 | 使用不同的 --sarif-category 分割到多个上传中;减少查询范围 |
| 两个 CodeQL 工作流 | 如果使用高级设置,请禁用默认设置,或删除旧的工作流文件 |
| 分析速度慢 | 启用依赖项缓存;使用 --threads=0;减少查询套件范围 |
有关包含详细解决方案的全面故障排除,请搜索
references/troubleshooting.md。
| 代码库大小 | 内存 | CPU |
|---|---|---|
| 小型 (<100K LOC) | 8 GB+ | 2 核 |
| 中型 (100K–1M LOC) | 16 GB+ | 4–8 核 |
| 大型 (>1M LOC) | 64 GB+ | 8 核 |
所有大小:SSD 且可用磁盘空间 ≥14 GB。
将 CodeQL actions 固定到特定主版本:
uses: github/codeql-action/init@v4 # 推荐
uses: github/codeql-action/autobuild@v4
uses: github/codeql-action/analyze@v4
为了最大程度的安全性,请固定到完整的提交 SHA 而不是版本标签。
有关详细文档,请根据需要加载以下参考文件:
references/workflow-configuration.md — 完整的工作流触发器、运行器和配置选项
trigger, schedule, paths-ignore, db-location, model packs, alert severity, merge protection, concurrency, config filereferences/cli-commands.md — 完整的 CodeQL CLI 命令参考
database create, database analyze, upload-results, resolve packs, cli-server, installation, CI integrationreferences/sarif-output.md — SARIF v2.1.0 对象模型、上传限制和第三方支持
sarifLog, result, location, region, codeFlow, fingerprint, suppression, upload limits, third-party, precision, references/compiled-languages.md — 每种语言的构建模式和自动构建行为
C/C++, C#, Java, Go, Rust, Swift, autobuild, build-mode, hardware, dependency cachingreferences/troubleshooting.md — 全面的错误诊断和解决方案
no source code, out of disk, out of memory, 403, C# compiler, analysis too long, fewer lines, Kotlin, extraction errors, debug logging, , references/alert-management.md — 警报严重性、分类、Copilot 自动修复和驳回
severity, security severity, CVSS, Copilot Autofix, dismiss, triage, PR alerts, data flow, merge protection, REST API每周安装次数
266
仓库
GitHub 星标数
26.7K
首次出现
6 天前
安全审计
安装在
gemini-cli244
codex244
opencode240
cursor238
github-copilot238
kimi-cli236
This skill provides procedural guidance for configuring and running CodeQL code scanning — both through GitHub Actions workflows and the standalone CodeQL CLI.
Use this skill when the request involves:
codeql.yml GitHub Actions workflowcodeql database create, database analyze, github upload-results)CodeQL supports the following language identifiers:
| Language | Identifier | Alternatives |
|---|---|---|
| C/C++ | c-cpp | c, cpp |
| C# | csharp | — |
| Go | go | — |
| Java/Kotlin | java-kotlin | java, kotlin |
| JavaScript/TypeScript |
Alternative identifiers are equivalent to the standard identifier (e.g.,
javascriptdoes not exclude TypeScript analysis).
none build mode for most languages..github/workflows/codeql.yml file for full control over triggers, build modes, query suites, and matrix strategies.To switch from default to advanced: disable default setup first, then commit the workflow file.
Define when scanning runs:
on:
push:
branches: [main, protected]
pull_request:
branches: [main]
schedule:
- cron: '30 6 * * 1' # Weekly Monday 6:30 UTC
push — scans on every push to specified branches; results appear in Security tabpull_request — scans PR merge commits; results appear as PR check annotationsschedule — periodic scans of the default branch (cron must exist on default branch)merge_group — add if repository uses merge queuesTo skip scans for documentation-only PRs:
on:
pull_request:
paths-ignore:
- '**/*.md'
- '**/*.txt'
paths-ignorecontrols whether the workflow runs, not which files are analyzed.
Set least-privilege permissions:
permissions:
security-events: write # Required to upload SARIF results
contents: read # Required to checkout code
actions: read # Required for private repos using codeql-action
Use a matrix strategy to analyze each language in parallel:
jobs:
analyze:
name: Analyze (${{ matrix.language }})
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- language: javascript-typescript
build-mode: none
- language: python
build-mode: none
For compiled languages, set the appropriate build-mode:
none — no build required (supported for C/C++, C#, Java, Rust)autobuild — automatic build detectionmanual — custom build commands (advanced setup only)For detailed per-language autobuild behavior and runner requirements, search
references/compiled-languages.md.
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Initialize CodeQL
uses: github/codeql-action/init@v4
with:
languages: ${{ matrix.language }}
build-mode: ${{ matrix.build-mode }}
queries: security-extended
dependency-caching: true
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v4
with:
category: "/language:${{ matrix.language }}"
Query suite options:
security-extended — default security queries plus additional coveragesecurity-and-quality — security plus code quality queriespacks: input (e.g., codeql/javascript-queries:AlertSuppression.ql)Dependency caching: Set dependency-caching: true on the init action to cache restored dependencies across runs.
Analysis category: Use category to distinguish SARIF results in monorepos (e.g., per-language, per-component).
For monorepos with multiple components, use the category parameter to separate SARIF results:
category: "/language:${{ matrix.language }}/component:frontend"
To restrict analysis to specific directories, use a CodeQL configuration file (.github/codeql/codeql-config.yml):
paths:
- apps/
- services/
paths-ignore:
- node_modules/
- '**/test/**'
Reference it in the workflow:
- uses: github/codeql-action/init@v4
with:
config-file: .github/codeql/codeql-config.yml
If autobuild fails or custom build commands are needed:
- language: c-cpp
build-mode: manual
Then add explicit build steps between init and analyze:
- if: matrix.build-mode == 'manual'
name: Build
run: |
make bootstrap
make release
Download the CodeQL bundle (includes CLI + precompiled queries):
# Download from https://github.com/github/codeql-action/releases
# Extract and add to PATH
export PATH="$HOME/codeql:$PATH"
# Verify installation
codeql resolve packs
codeql resolve languages
Always use the CodeQL bundle, not a standalone CLI download. The bundle ensures query compatibility and provides precompiled queries for better performance.
# Single language
codeql database create codeql-db \
--language=javascript-typescript \
--source-root=src
# Multiple languages (cluster mode)
codeql database create codeql-dbs \
--db-cluster \
--language=java,python \
--command=./build.sh \
--source-root=src
For compiled languages, provide the build command via --command.
codeql database analyze codeql-db \
javascript-code-scanning.qls \
--format=sarif-latest \
--sarif-category=javascript \
--output=results.sarif
Common query suites: <language>-code-scanning.qls, <language>-security-extended.qls, <language>-security-and-quality.qls.
codeql github upload-results \
--repository=owner/repo \
--ref=refs/heads/main \
--commit=<commit-sha> \
--sarif=results.sarif
Requires GITHUB_TOKEN environment variable with security-events: write permission.
To avoid repeated JVM initialization when running multiple commands:
codeql execute cli-server
For detailed CLI command reference, search
references/cli-commands.md.
Alerts have two severity dimensions:
Error, Warning, NoteCritical, High, Medium, Low (derived from CVSS scores; takes display precedence)GitHub Copilot Autofix generates fix suggestions for CodeQL alerts in pull requests automatically — no Copilot subscription required. Review suggestions carefully before committing.
error/critical/high severity alertsFor detailed alert management guidance, search
references/alert-management.md.
- uses: github/codeql-action/init@v4
with:
packs: |
my-org/my-security-queries@1.0.0
codeql/javascript-queries:AlertSuppression.ql
Use the CodeQL CLI to create and publish packs:
# Initialize a new pack
codeql pack init my-org/my-queries
# Install dependencies
codeql pack install
# Publish to GitHub Container Registry
codeql pack publish
For advanced query and path configuration, create .github/codeql/codeql-config.yml:
paths:
- apps/
- services/
paths-ignore:
- '**/test/**'
- node_modules/
queries:
- uses: security-extended
packs:
javascript-typescript:
- my-org/my-custom-queries
Workflow logs include key metrics:
To enable detailed diagnostics:
--verbosity=progress++ and --logdir=codeql-logs| Problem | Solution |
|---|---|
| Workflow not triggering | Verify on: triggers match event; check paths/branches filters; ensure workflow exists on target branch |
Resource not accessible error | Add security-events: write and contents: read permissions |
| Autobuild failure | Switch to build-mode: manual and add explicit build commands |
| No source code seen | Verify , build command, and language identifier |
For comprehensive troubleshooting with detailed solutions, search
references/troubleshooting.md.
| Codebase Size | RAM | CPU |
|---|---|---|
| Small (<100K LOC) | 8 GB+ | 2 cores |
| Medium (100K–1M LOC) | 16 GB+ | 4–8 cores |
| Large (>1M LOC) | 64 GB+ | 8 cores |
All sizes: SSD with ≥14 GB free disk space.
Pin CodeQL actions to a specific major version:
uses: github/codeql-action/init@v4 # Recommended
uses: github/codeql-action/autobuild@v4
uses: github/codeql-action/analyze@v4
For maximum security, pin to a full commit SHA instead of a version tag.
For detailed documentation, load the following reference files as needed:
references/workflow-configuration.md — Full workflow trigger, runner, and configuration options
trigger, schedule, paths-ignore, db-location, model packs, alert severity, merge protection, concurrency, config filereferences/cli-commands.md — Complete CodeQL CLI command reference
Weekly Installs
266
Repository
GitHub Stars
26.7K
First Seen
6 days ago
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
gemini-cli244
codex244
opencode240
cursor238
github-copilot238
kimi-cli236
security-severitySARIF uploadSARIF limitsjavascript-typescript |
javascript, typescript |
| Python | python | — |
| Ruby | ruby | — |
| Rust | rust | — |
| Swift | swift | — |
| GitHub Actions | actions | — |
--source-root| C# compiler failure | Check for /p:EmitCompilerGeneratedFiles=true conflicts with .sqlproj or legacy projects |
| Fewer lines scanned than expected | Switch from none to autobuild/manual; verify build compiles all source |
| Kotlin in no-build mode | Disable and re-enable default setup to switch to autobuild |
| Cache miss every run | Verify dependency-caching: true on init action |
| Out of disk/memory | Use larger runners; reduce analysis scope via paths config; use build-mode: none |
| SARIF upload fails | Ensure token has security-events: write; check 10 MB file size limit |
| SARIF results exceed limits | Split across multiple uploads with different --sarif-category; reduce query scope |
| Two CodeQL workflows | Disable default setup if using advanced setup, or remove old workflow file |
| Slow analysis | Enable dependency caching; use --threads=0; reduce query suite scope |
database create, database analyze, upload-results, resolve packs, cli-server, installation, CI integrationreferences/sarif-output.md — SARIF v2.1.0 object model, upload limits, and third-party support
sarifLog, result, location, region, codeFlow, fingerprint, suppression, upload limits, third-party, precision, security-severityreferences/compiled-languages.md — Build modes and autobuild behavior per language
C/C++, C#, Java, Go, Rust, Swift, autobuild, build-mode, hardware, dependency cachingreferences/troubleshooting.md — Comprehensive error diagnosis and resolution
no source code, out of disk, out of memory, 403, C# compiler, analysis too long, fewer lines, Kotlin, extraction errors, debug logging, SARIF upload, SARIF limitsreferences/alert-management.md — Alert severity, triage, Copilot Autofix, and dismissal
severity, security severity, CVSS, Copilot Autofix, dismiss, triage, PR alerts, data flow, merge protection, REST API