CodeQL 查询语法

$HOME/vscode-codeql-starter/ql/javascript/ql/test/query-tests/Security

  1. codeql.exe database create security-test --language=javascript

基础查询语法结构

  1. /**
  2. *
  3. * Query metadata
  4. *
  5. */
  6. import /* ... CodeQL libraries or modules ... */
  7. /* ... Optional, define CodeQL classes and predicates ... */
  8. from /* ... 变量声明 ... */
  9. where /* ... 逻辑处理 ... */
  10. select /* ... 表达式 ... */

元数据

import 语句

必需,用来导入CodeQL库和模块

from 字句

可选,用来声明变量,所有变量必须指定类型
变量类型:https://codeql.github.com/docs/ql-language-reference/types/

  1. from <type> <variable name>

where 字句

可选,用来对from字句中声明的变量做逻辑处理。
可以使用聚合、谓词和逻辑公式筛选出满足条件的数据集合。
CodeQL 库对特定语言和框架的常用谓词进行分组。如上所述,您还可以在查询文件的正文或您自己的自定义模块中定义您自己的谓词。

select 字句

必需,用来输出,通常放在文件的结尾。
通常至少包括两列,Element列用来返回触发规则的代码位置,String列用来描述生成该告警的描述信息。

  1. select element, string

可以使用as关键字自定义字段名/列名
可以使用order by关键字排序

  1. import javascript
  2. from int x, int y
  3. where x = 3 and y in [0 .. 2]
  4. select x, y, x * y as product, "product: " + product
  5. order by product desc

image.png