https://codeql.github.com/docs/writing-codeql-queries/creating-path-queries/

路径查询可以可视化代码库中的信息流。

概述

展现从source到sink所采用的路径。要对路径建模必需要实现定义好source和sink,并且配置好他们之间的数据流关系。

路径查询示例

构建路径查询

构建路径查询需要使用特定的元数据、查询谓词和select语句结构。
语法模板

  1. /**
  2. * ...
  3. * @kind path-problem
  4. * ...
  5. */
  6. import java
  7. import semmle.code.java.dataflow.DataFlow
  8. import DataFlow::PathGraph
  9. from MyConfiguration config, DataFlow::PathNode source, DataFlow::PathNode sink
  10. where config.hasFlowPath(source, sink)
  11. select sink.getNode(), source, sink, "<message>"

定义@kind为path-problem
导入DataFlow::PathGraph
定义source和sink
定义 MyConfiguration类,用来设置数据从source到sink之间的流动

元数据

构建路径查询时必需要在元数据中包含该属性

  1. @kind path-problem

生成路径说明

  1. import DataFlow::PathGraph

自定义edges谓词

  1. query predicate edges(PathNode a, PathNode b) {
  2. /** Logical conditions which hold if `(a,b)` is an edge in the data flow graph */
  3. }

from字句

在from字句中声明source和sink

  1. from Configuration config, DataFlow::PathNode source, DataFlow::PathNode sink

where字句

  1. where config.hasFlowPath(source, sink)

select字句

路径查询的select字句由四个列组成,格式如下

  1. select element,source,sink,string

element和string分别代码告警的位置和告警的消息。
source和sink是路径图中节点。
element列可以使用source或者sink,这取决于你的需求。