https://codeql.github.com/docs/writing-codeql-queries/creating-path-queries/
路径查询可以可视化代码库中的信息流。
概述
展现从source到sink所采用的路径。要对路径建模必需要实现定义好source和sink,并且配置好他们之间的数据流关系。
路径查询示例
构建路径查询
构建路径查询需要使用特定的元数据、查询谓词和select语句结构。
语法模板
/**
* ...
* @kind path-problem
* ...
*/
import java
import semmle.code.java.dataflow.DataFlow
import DataFlow::PathGraph
from MyConfiguration config, DataFlow::PathNode source, DataFlow::PathNode sink
where config.hasFlowPath(source, sink)
select sink.getNode(), source, sink, "<message>"
定义@kind为path-problem
导入DataFlow::PathGraph
定义source和sink
定义 MyConfiguration类,用来设置数据从source到sink之间的流动
元数据
构建路径查询时必需要在元数据中包含该属性
@kind path-problem
生成路径说明
import DataFlow::PathGraph
自定义edges谓词
query predicate edges(PathNode a, PathNode b) {
/** Logical conditions which hold if `(a,b)` is an edge in the data flow graph */
}
from字句
在from字句中声明source和sink
from Configuration config, DataFlow::PathNode source, DataFlow::PathNode sink
where字句
where config.hasFlowPath(source, sink)
select字句
路径查询的select字句由四个列组成,格式如下
select element,source,sink,string
element和string分别代码告警的位置和告警的消息。
source和sink是路径图中节点。
element列可以使用source或者sink,这取决于你的需求。