from Method method where method.hasName("fromXML") and method.getDeclaringType().hasQualifiedName("com.thoughtworks.xstream", "XStream") select method
根据Method name 和 interface name 查询
比如我想查询ContentTypeHandler 的所有子类toObject方法
1 2 3 4 5
import java
from Method method where method.hasName("toObject") and method.getDeclaringType().getASupertype().hasQualifiedName("org.apache.struts2.rest.handler", "ContentTypeHandler") select method
from Method method where method.hasName("toObject") and method.getDeclaringType().getAnAncestor().hasQualifiedName("org.apache.struts2.rest.handler", "ContentTypeHandler") select method
也可以用getDeclaringType()* 类似的还有getDeclaringType()+
有个问题是,万一一个类实现了多个接口是不是也可以这么用? 答案是是的
getAxxxx,如果有多个结果会以多行的形式按照一定的顺序显示出来。
比如getAParamType
获取Method的parameter
getAParamType() Gets the type of a formal parameter of this callable
getParameter(int n) Gets the formal parameter at the specified (zero-based) position.
getParameterType(int n) Gets the type of the formal parameter at the specified (zero-based) position
1 2 3 4 5
import java
from MethodAccess call, Method method where method.hasName("toObject") and method.getDeclaringType().getAnAncestor().hasQualifiedName("org.apache.struts2.rest.handler", "ContentTypeHandler") and call.getMethod() = method select method.getParameter(0)
MethodAccess
一般是先查method,与MethodAccess.getMethod() 进行比较。
比如查ContentTypeHandler 的 toObject() 方法的调用。
1 2 3 4 5
import java
from MethodAccess call, Method method where method.hasName("toObject") and method.getDeclaringType().getASupertype().hasQualifiedName("org.apache.struts2.rest.handler", "ContentTypeHandler") and call.getMethod() = method selectcall
上面这种查询方式不行,只能查到JsonLibHandler 这样显式定义的。
对于这种, 真正用的并没有查到
怎么改进呢? 也可以使用getAnAncestor() 或者getASupertype()*
1 2 3 4 5
import java
from MethodAccess call, Method method where method.hasName("toObject") and method.getDeclaringType().getAnAncestor().hasQualifiedName("org.apache.struts2.rest.handler", "ContentTypeHandler") and call.getMethod() = method selectcall
这种查询能够涵盖上面的两种情况
从上面可以看到MethodAccess 的查询依赖于Method 的查询。
获取MethodAccess 的 argument
getATypeArgument Gets a type argument supplied as part of this method access, if any. getAnArgument Gets an argument supplied to the method that is invoked using this method access. getArgument(int n) Gets the argument at the specified (zero-based) position in this method access.
getTypeArgument(int n) Gets the type argument at the specified (zero-based) position in this method access, if any.
1 2 3 4 5
import java
from MethodAccess call, Method method where method.hasName("toObject") and method.getDeclaringType().getAnAncestor().hasQualifiedName("org.apache.struts2.rest.handler", "ContentTypeHandler") and call.getMethod() = method select call.getArgument(0)
You can also write exists( | | ). This is equivalent to exists( | and ).
This quantified formula introduces some new variables. It holds if there is at least one set of values that the variables could take to make the formula in the body true.
For example, exists(int i | i instanceof OneTwoThree) introduces a temporary variable of type int and holds if any value of that variable has type OneTwoThree.
说人话就是,variable满足formula 则返回true 否则返回false
Node 的 方法
asExpr Gets the expression corresponding to this node, if any. asParameter Gets the parameter corresponding to this node, if any.
from MyTaintTrackingConfiguration config, DataFlow::Node source, DataFlow::Node sink where config.hasFlow(source, sink) selectsource, sink
能查出来,但是没有显示具体的path,后来查看文档,应该是可以显示path的。
Running path queries in VS Code
Open a path query in the editor.
Right-click in the query window and select CodeQL: Run Query. (Alternatively, run the command from the Command Palette.)
Once the query has finished running, you can see the results in the Results view as usual (under alerts in the dropdown menu). Each query result describes the flow of information between a source and a sink.
Expand the result to see the individual steps that the data follows.
Click each step to jump to it in the source code and investigate the problem further.
To navigate the path from your keyboard, you can bind shortcuts to the CodeQL: Show Previous Step on Path and CodeQL: Show Next Step on Path commands.