Skip to content

Commit 0660e9c

Browse files
[EDI] Using custom queries with the CodeQL CLI (#59665)
Co-authored-by: Sam Browning <106113886+sabrowning1@users.noreply.github.com>
1 parent 0123cba commit 0660e9c

6 files changed

Lines changed: 168 additions & 92 deletions

File tree

content/code-security/concepts/code-scanning/codeql/codeql-query-packs.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,22 @@ contentType: concepts
1616

1717
## About {% data variables.product.prodname_codeql %} packs
1818

19-
{% data variables.product.prodname_codeql %} packs are used to create, share, depend on, and run {% data variables.product.prodname_codeql %} queries and libraries. {% data variables.product.prodname_codeql %} packs contain queries, library files, query suites, and metadata. You can customize your {% data variables.product.prodname_codeql %} analysis by downloading packs created by others and running them on your codebase.
19+
{% data variables.product.prodname_codeql %} packs are used to create, share, depend on, and run {% data variables.product.prodname_codeql %} queries and libraries. You can customize your {% data variables.product.prodname_codeql %} analysis by downloading packs created by others and running them on your codebase.
20+
21+
Each {% data variables.product.prodname_codeql %} pack requires a `qlpack.yml` file in its root directory that specifies:
22+
23+
* How to compile the queries
24+
* Dependencies on other {% data variables.product.prodname_codeql %} packs and libraries
25+
* Query suite definitions
26+
27+
For more information about `qlpack.yml` properties, see [AUTOTITLE](/code-security/codeql-cli/getting-started-with-the-codeql-cli/customizing-analysis-with-codeql-packs#codeqlpack-yml-properties).
28+
29+
Additionally, a {% data variables.product.prodname_codeql %} pack can contain:
30+
31+
* Custom queries (`.ql` files)
32+
* Library files
33+
* Query suites
34+
* Metadata
2035

2136
The {% data variables.product.prodname_codeql_cli %} bundle includes queries that are maintained by {% data variables.product.company_short %} experts, security researchers, and community contributors. If you want to run queries developed by other organizations, {% data variables.product.prodname_codeql %} query packs provide an efficient and reliable way to download and run queries, while model packs ({% data variables.release-phases.public_preview %}) can be used to expand {% data variables.product.prodname_code_scanning %} analysis to recognize libraries and frameworks that are not supported by default.
2237

@@ -47,4 +62,13 @@ For more information about compatibility between published query packs and diffe
4762

4863
You can also use the {% data variables.product.prodname_codeql_cli %} to create your own {% data variables.product.prodname_codeql %} packs, add dependencies to packs, and install or update dependencies.
4964

50-
You can publish {% data variables.product.prodname_codeql %} packs that you have created, using the {% data variables.product.prodname_codeql_cli %}. For more information on publishing and downloading {% data variables.product.prodname_codeql %} packs, see [AUTOTITLE](/code-security/codeql-cli/using-the-advanced-functionality-of-the-codeql-cli/publishing-and-using-codeql-packs).
65+
## Publishing and sharing {% data variables.product.prodname_codeql %} packs
66+
67+
You can share custom queries with the broader {% data variables.product.prodname_codeql %} community by:
68+
69+
* Publishing to {% data variables.product.prodname_registry %}: Make your pack publicly available for other users to discover and use.
70+
* Contributing to the {% data variables.product.prodname_codeql %} repository: Submit queries that would benefit the wider community by opening a pull request to the official repository.
71+
72+
For more information about publishing and downloading {% data variables.product.prodname_codeql %} packs, see [AUTOTITLE](/code-security/codeql-cli/using-the-advanced-functionality-of-the-codeql-cli/publishing-and-using-codeql-packs).
73+
74+
For information about contributing to {% data variables.product.prodname_codeql %}, see [Contributing to {% data variables.product.prodname_codeql %}](https://github.com/github/codeql/blob/main/CONTRIBUTING.md).
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
title: Custom CodeQL queries
3+
shortTitle: Custom queries
4+
intro: Custom queries extend {% data variables.product.prodname_codeql %}'s built-in security analysis to detect vulnerabilities and enforce coding standards specific to your codebase.
5+
product: '{% data reusables.gated-features.codeql %}'
6+
versions:
7+
fpt: '*'
8+
ghes: '*'
9+
ghec: '*'
10+
topics:
11+
- Code Security
12+
- Code scanning
13+
- CodeQL
14+
contentType: concepts
15+
---
16+
17+
## What are custom {% data variables.product.prodname_codeql %} queries?
18+
19+
Custom queries extend {% data variables.product.prodname_codeql %}'s built-in security analysis to detect vulnerabilities, coding standards, and patterns specific to your codebase.
20+
21+
{% data reusables.codeql-cli.advanced-query-execution %}
22+
23+
## When to use custom queries
24+
25+
Use custom queries to:
26+
27+
* Detect vulnerabilities specific to your application's architecture or frameworks
28+
* Enforce organization-specific coding standards or best practices
29+
* Find patterns not covered by standard {% data variables.product.prodname_codeql %} query packs
30+
* Analyze {% data variables.product.prodname_codeql %} databases with the `database analyze` command using the {% data variables.product.prodname_codeql_cli %} to produce interpreted results
31+
32+
## Query structure
33+
34+
Custom queries are written in query files, which are saved with the `.ql` extension. These files also contain important metadata that provides information about the query's purpose and tells the {% data variables.product.prodname_codeql_cli %} how to process results. Required properties include:
35+
36+
* **Query identifier (`@id`)**: Lowercase letters or digits, delimited by `/` or `-`
37+
* **Query type (`@kind`)**: One of:
38+
* `problem` - Simple alert
39+
* `path-problem` - Alert with code location sequence
40+
* `diagnostic` - Extractor troubleshooting
41+
* `metric` - Summary metric (requires `@tags summary`)
42+
43+
> [!NOTE]
44+
> Metadata requirements may differ if you want to use your query with other applications. For more information, see [Metadata for {% data variables.product.prodname_codeql %} queries](https://codeql.github.com/docs/writing-codeql-queries/metadata-for-codeql-queries/#metadata-for-codeql-queries).
45+
46+
For more information about query metadata, see [Metadata for {% data variables.product.prodname_codeql %} queries](https://codeql.github.com/docs/writing-codeql-queries/metadata-for-codeql-queries/#metadata-for-codeql-queries) and the [Query metadata style guide](https://github.com/github/codeql/blob/main/docs/query-metadata-style-guide.md).
47+
48+
## Query documentation
49+
50+
Query documentation helps users understand what a query detects and how to address identified issues. You can include documentation for your custom queries in two formats:
51+
52+
* **Markdown files**: Saved alongside the query, can be included in SARIF files and displayed in the {% data variables.product.prodname_code_scanning %} UI
53+
* **`.qhelp` files**: Consistent with standard {% data variables.product.prodname_codeql %} queries, but must be converted to Markdown for use with {% data variables.product.prodname_code_scanning %}
54+
55+
When SARIF files containing query help are uploaded to {% data variables.product.prodname_dotcom %}, the documentation appears in the {% data variables.product.prodname_code_scanning %} UI for any alerts generated by the query.
56+
57+
For more information, see [Query help files](https://codeql.github.com/docs/writing-codeql-queries/query-help-files/#query-help-files).
58+
59+
## Sharing custom queries
60+
61+
You can share custom queries with the community by publishing your own query packs. See [AUTOTITLE](/code-security/tutorials/customize-code-scanning/publishing-and-using-codeql-packs).
62+
63+
## Further reading
64+
65+
* [AUTOTITLE](/code-security/how-tos/scan-code-for-vulnerabilities/scan-from-the-command-line/using-custom-queries-with-the-codeql-cli)
66+
* [{% data variables.product.prodname_codeql %} queries](https://codeql.github.com/docs/writing-codeql-queries/codeql-queries/#codeql-queries)

content/code-security/concepts/code-scanning/codeql/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ contentType: concepts
1212
children:
1313
- /about-code-scanning-with-codeql
1414
- /codeql-query-suites
15+
- /custom-codeql-queries
1516
- /about-the-codeql-cli
1617
- /about-codeql-for-vs-code
1718
- /about-codeql-workspaces

content/code-security/how-tos/scan-code-for-vulnerabilities/scan-from-the-command-line/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ topics:
1313
children:
1414
- /setting-up-the-codeql-cli
1515
- /advanced-setup-of-the-codeql-cli
16-
- /using-custom-queries-with-the-codeql-cli
16+
- /writing-and-sharing-custom-queries-for-the-codeql-cli
1717
- /testing-custom-queries
1818
- /testing-query-help-files
1919
- /specifying-command-options-in-a-codeql-configuration-file

content/code-security/how-tos/scan-code-for-vulnerabilities/scan-from-the-command-line/using-custom-queries-with-the-codeql-cli.md

Lines changed: 0 additions & 89 deletions
This file was deleted.

0 commit comments

Comments
 (0)