mirror of
https://github.com/github/codeql.git
synced 2026-04-28 10:15:14 +02:00
Merge pull request #5219 from smowton/smowton/feature/backward-dataflow-for-fluent-methods
Java: Add backward dataflow edges through fluent function invocations.
This commit is contained in:
50
java/ql/test/library-tests/dataflow/fluent-methods/Test.java
Normal file
50
java/ql/test/library-tests/dataflow/fluent-methods/Test.java
Normal file
@@ -0,0 +1,50 @@
|
||||
public class Test {
|
||||
|
||||
private String field;
|
||||
|
||||
public Test fluentNoop() {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Test indirectlyFluentNoop() {
|
||||
return this.fluentNoop();
|
||||
}
|
||||
|
||||
public Test fluentSet(String x) {
|
||||
this.field = x;
|
||||
return this;
|
||||
}
|
||||
|
||||
public static Test identity(Test t) {
|
||||
return t;
|
||||
}
|
||||
|
||||
public String get() {
|
||||
return field;
|
||||
}
|
||||
|
||||
public static String source() {
|
||||
return "taint";
|
||||
}
|
||||
|
||||
public static void sink(String s) {}
|
||||
|
||||
public static void test1() {
|
||||
Test t = new Test();
|
||||
t.fluentNoop().fluentSet(source()).fluentNoop();
|
||||
sink(t.get()); // $hasTaintFlow=y
|
||||
}
|
||||
|
||||
public static void test2() {
|
||||
Test t = new Test();
|
||||
Test.identity(t).fluentNoop().fluentSet(source()).fluentNoop();
|
||||
sink(t.get()); // $hasTaintFlow=y
|
||||
}
|
||||
|
||||
public static void test3() {
|
||||
Test t = new Test();
|
||||
t.indirectlyFluentNoop().fluentSet(source()).fluentNoop();
|
||||
sink(t.get()); // $hasTaintFlow=y
|
||||
}
|
||||
|
||||
}
|
||||
30
java/ql/test/library-tests/dataflow/fluent-methods/flow.ql
Normal file
30
java/ql/test/library-tests/dataflow/fluent-methods/flow.ql
Normal file
@@ -0,0 +1,30 @@
|
||||
import java
|
||||
import semmle.code.java.dataflow.DataFlow
|
||||
import TestUtilities.InlineExpectationsTest
|
||||
|
||||
class Conf extends DataFlow::Configuration {
|
||||
Conf() { this = "qltest:dataflow:fluent-methods" }
|
||||
|
||||
override predicate isSource(DataFlow::Node n) {
|
||||
n.asExpr().(MethodAccess).getMethod().hasName("source")
|
||||
}
|
||||
|
||||
override predicate isSink(DataFlow::Node n) {
|
||||
exists(MethodAccess ma | ma.getMethod().hasName("sink") | n.asExpr() = ma.getAnArgument())
|
||||
}
|
||||
}
|
||||
|
||||
class HasFlowTest extends InlineExpectationsTest {
|
||||
HasFlowTest() { this = "HasFlowTest" }
|
||||
|
||||
override string getARelevantTag() { result = "hasTaintFlow" }
|
||||
|
||||
override predicate hasActualResult(Location location, string element, string tag, string value) {
|
||||
tag = "hasTaintFlow" and
|
||||
exists(DataFlow::Node src, DataFlow::Node sink, Conf conf | conf.hasFlow(src, sink) |
|
||||
sink.getLocation() = location and
|
||||
element = sink.toString() and
|
||||
value = "y"
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user