我想在 Azure DevOps 中运行 custom codeql 查询以实现高级安全性(这是现在在 Azure DevOps 中可用的功能,而不仅仅是 GitHub)。 内置默认查询(如安全性和质量)工作正常。我晚上批量运行它们。
但我现在正在构建一个新的单独管道,该管道也将在晚上批量运行。有了它,我想运行一些自定义查询,在这个相当大的 JavaScript git 存储库中寻找特定的不需要的模式。
所有这些都在 Azure DevOps Services 中存储和运行。我从一个简单的 find all ToDo codeql 查询开始,让事情顺利进行。但我收到此错误,我在 documentation 或 Troubleshooting code Scanning faq 中找不到该错误,或者看到其他人以前遇到过:
/opt/hostedtoolcache/CodeQL/2.17.2/x64/codeql/codeql database init --codescanning-config=/home/vsts/work/1/s/.azuredevops/customcodeql/customconfig.yaml --db-cluster /home/vsts/work/_temp/advancedsecurity.codeql/d --source-root=/home/vsts/work/1/s --language=javascript --calculate-baseline
A fatal error occurred: No queries defined for javascript
##[warning] Error running the 'database init' CodeQL command for javascript (2)
##[error]Error running the 'database init' CodeQL command for javascript (2)
====================================================================================================
Analyzing CodeQL execution results.
CodeQL analysis finished with exit code 2.
我有一个包含任务的 yaml 管道
AdvancedSecurity-Codeql-Init@1
、 AdvancedSecurity-Codeql-Autobuild@1
和AdvancedSecurity-Codeql-Analyze@1
。
任务 AdvancedSecurity-Codeql-Init@1
指向一个 codeqlconfig yaml 文件,在该文件中我指向一个简单的 todo codeql 查询。据我所知,这是如何根据我读过的文档来完成的,例如:使用自定义查询进行分析.
我曾经把codeql查询的路径写错了。然后我收到错误消息:
A fatal error occurred: ./azuredevops/customcodeql/todos.ql is not a .ql file, .qls file, a directory, or a query pack specification.
我更正了路径(在前面添加了 .,因此它变成了 ./.azuredevops....)并且不再出现无法找到文件类型的错误,所以我假设它现在找到了该文件。
但是现在我收到了另一个错误:“没有为 javascript 定义查询”,所以假设它找到了我的 todos.ql 但无论如何它都不起作用。
这是我使用的三个文件:
这是取自 examples 的 todo.ql :
/**
* @id js/javascript/todocomment
* @name TODO_comments
* @description Finds comments containing the word TODO
* @kind problem
* @problem.severity recommendation
* @tags comment
* TODO
*/
import javascript
from Comment c
where c.getText().regexpMatch("(?si).*\\bTODO\\b.*")
select c
这是代码ql customconfig.yaml:
name: "Run custom queries"
disable-default-queries: true
queries:
- name: TODO_comments
uses: ./.azuredevops/customcodeql/todos.ql
paths:
- src
paths-ignore:
- '**/node_modules'
- '**/*.test.js'
query-filters:
- include:
kind: problem
这是在 Azure DevOps 中作为批处理作业运行的 yaml 管道的一部分。 大约 7 秒后,任务
AdvancedSecurity-Codeql-Init@1
版本 1.1.262 出现错误
- stage: Analyze_Custom_CQL
jobs:
- job: Analyze
steps:
- task: AdvancedSecurity-Codeql-Init@1
inputs:
languages: 'javascript'
loglevel: '3'
configfilepath: '$(build.sourcesDirectory)/.azuredevops/customcodeql/customconfig.yaml'
- task: AdvancedSecurity-Codeql-Autobuild@1
displayName: 'Advanced Security Autobuild'
- task: AdvancedSecurity-Codeql-Analyze@1
我是否在 todos.ql、元数据或其他内容中遗漏了某些内容?我已更改过滤器并包含在 customconfig.yaml 中。有或没有它们都尝试过。
我可以使用下面简化的 CodeQL 查询、配置和管道定义来重现相同的问题。
/.azuredevops/customcodeql/todos.ql(内容复制自github/codeql)
/**
* @id js/examples/todocomment
* @name TODO comments
* @description Finds comments containing the word TODO
* @kind problem
* @problem.severity recommendation
* @tags comment
* TODO
*/
import javascript
from Comment c
where c.getText().regexpMatch("(?si).*\\bTODO\\b.*")
select c, "TODO comments indicate that the code may not be complete."
/.azuredevops/customcodeql/customconfig.yml
name: "Run custom queries"
# When using a configuration file, if you do not disable default queries,
# then the default CodeQL queries in the `code-scanning` query suite will also execute upon analysis.
disable-default-queries: true
# To reference local queries saved to your repository,
# the path must start with `./` followed by the path to the custom query or queries.
# Names for each query referenced is optional.
queries:
- uses: ./.azuredevops/customcodeql/todos.ql
JSCodeQL.yml
steps:
- task: AdvancedSecurity-Codeql-Init@1
inputs:
languages: 'javascript'
loglevel: '2'
configfilepath: '$(build.sourcesDirectory)/.azuredevops/customcodeql/customconfig.yml'
此外,我还检查了查询文件元数据和警报消息指南,查询文件应该是正确的。
对于此问题以及您需要运行自定义 javascript CodeQL 查询以进行代码扫描,您可以在开发者社区 - Azure DevOps 中报告问题,支持团队可以在其中与工程团队合作以获得进一步的帮助和见解。