|
| 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) |
0 commit comments