Skip to main content

Esta versão do GitHub Enterprise Server será descontinuada em 2026-04-09. Nenhum lançamento de patch será feito, mesmo para questões críticas de segurança. Para obter melhor desempenho, segurança aprimorada e novos recursos, atualize para a última versão do GitHub Enterprise Server. Para obter ajuda com a atualização, entre em contato com o suporte do GitHub Enterprise.

Writing custom queries for the CodeQL CLI

You can write your own CodeQL queries to find specific vulnerabilities and errors.

Quem pode usar esse recurso?

O CodeQL está disponível para os seguintes tipos de repositórios:

This article is specifically about writing queries to use with the análise de banco de dados command to produce interpreted results. For conceptual information about custom queries, see Consultas CodeQL personalizadas.

Writing a valid query

Before running a custom analysis you need to write a valid query, and save it in a file with a .ql extension. There is extensive documentation available to help you write queries. For more information, see CodeQL queries.

Including query metadata

When running queries with the database analyze command, you must include the following two properties to ensure that the results are interpreted correctly:

  • Query identifier (@id): a sequence of words composed of lowercase letters or digits, delimited by / or -, identifying and classifying the query.

  • Query type (@kind): identifies the query as a simple alert (@kind problem), an alert documented by a sequence of code locations (@kind path-problem), for extractor troubleshooting (@kind diagnostic), or a summary metric (@kind metric and @tags summary).

For more information about these metadata properties, see Metadata for CodeQL queries and the Query metadata style guide.

Including query help for custom CodeQL queries in SARIF files

For information about query help and documentation formats, see Consultas CodeQL personalizadas.

To include query help in SARIF files when running code scanning analyses:

  1. Write your query help in one of the following formats:

    • Markdown file: Save a Markdown file alongside your query with the same name (for example, my-query.md for my-query.ql)
    • .qhelp file: Write query help in .qhelp format, then convert it to Markdown before running the analysis. For more information, see Query help files and Testing query help files.
  2. Run codeql database analyze with the --sarif-add-query-help option:

    codeql database analyze <database> --format=sarif-latest --output=results.sarif --sarif-add-query-help
    

    Observação

    The --sarif-add-query-help option is available from CodeQL CLI v2.7.1 onwards.

  3. Upload the SARIF file to GitHub.

Next steps

To share and use your custom queries, see Publishing and using CodeQL packs.