Skip to content

Commit 46d6f5a

Browse files
committed
Java: Update some tests.
1 parent acf6a77 commit 46d6f5a

File tree

26 files changed

+152
-95
lines changed

26 files changed

+152
-95
lines changed

java/ql/test/TestUtilities/InlineFlowTest.qll

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private predicate defaultSource(DataFlow::Node src) {
4747
src.asExpr().(MethodAccess).getMethod().getName() = ["source", "taint"]
4848
}
4949

50-
private module DefaultFlowConf implements DataFlow::ConfigSig {
50+
module DefaultFlowConf implements DataFlow::ConfigSig {
5151
predicate isSource(DataFlow::Node n) { defaultSource(n) }
5252

5353
predicate isSink(DataFlow::Node n) {
@@ -98,42 +98,35 @@ class InlineFlowTest extends InlineExpectationsTest {
9898
override string getARelevantTag() { result = ["hasValueFlow", "hasTaintFlow"] }
9999

100100
override predicate hasActualResult(Location location, string element, string tag, string value) {
101-
if exists(EnableLegacyConfiguration e)
102-
then
103-
tag = "hasValueFlow" and
104-
exists(DataFlow::Node src, DataFlow::Node sink | getValueFlowConfig().hasFlow(src, sink) |
105-
sink.getLocation() = location and
106-
element = sink.toString() and
107-
if exists(getSourceArgString(src)) then value = getSourceArgString(src) else value = ""
108-
)
109-
or
110-
tag = "hasTaintFlow" and
111-
exists(DataFlow::Node src, DataFlow::Node sink |
112-
getTaintFlowConfig().hasFlow(src, sink) and not getValueFlowConfig().hasFlow(src, sink)
113-
|
114-
sink.getLocation() = location and
115-
element = sink.toString() and
116-
if exists(getSourceArgString(src)) then value = getSourceArgString(src) else value = ""
117-
)
118-
else (
119-
tag = "hasValueFlow" and
120-
exists(DataFlow::Node src, DataFlow::Node sink | DefaultValueFlow::hasFlow(src, sink) |
121-
sink.getLocation() = location and
122-
element = sink.toString() and
123-
if exists(getSourceArgString(src)) then value = getSourceArgString(src) else value = ""
124-
)
125-
or
126-
tag = "hasTaintFlow" and
127-
exists(DataFlow::Node src, DataFlow::Node sink |
128-
DefaultTaintFlow::hasFlow(src, sink) and not DefaultValueFlow::hasFlow(src, sink)
129-
|
130-
sink.getLocation() = location and
131-
element = sink.toString() and
132-
if exists(getSourceArgString(src)) then value = getSourceArgString(src) else value = ""
133-
)
101+
tag = "hasValueFlow" and
102+
exists(DataFlow::Node src, DataFlow::Node sink | hasValueFlow(src, sink) |
103+
sink.getLocation() = location and
104+
element = sink.toString() and
105+
if exists(getSourceArgString(src)) then value = getSourceArgString(src) else value = ""
106+
)
107+
or
108+
tag = "hasTaintFlow" and
109+
exists(DataFlow::Node src, DataFlow::Node sink |
110+
hasTaintFlow(src, sink) and not hasValueFlow(src, sink)
111+
|
112+
sink.getLocation() = location and
113+
element = sink.toString() and
114+
if exists(getSourceArgString(src)) then value = getSourceArgString(src) else value = ""
134115
)
135116
}
136117

118+
predicate hasValueFlow(DataFlow::Node src, DataFlow::Node sink) {
119+
if exists(EnableLegacyConfiguration e)
120+
then getValueFlowConfig().hasFlow(src, sink)
121+
else DefaultValueFlow::hasFlow(src, sink)
122+
}
123+
124+
predicate hasTaintFlow(DataFlow::Node src, DataFlow::Node sink) {
125+
if exists(EnableLegacyConfiguration e)
126+
then getTaintFlowConfig().hasFlow(src, sink)
127+
else DefaultTaintFlow::hasFlow(src, sink)
128+
}
129+
137130
DataFlow::Configuration getValueFlowConfig() { result = any(DefaultValueFlowConf config) }
138131

139132
DataFlow::Configuration getTaintFlowConfig() { result = any(DefaultTaintFlowConf config) }

java/ql/test/library-tests/dataflow/fluent-methods/Test.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,31 +42,31 @@ public static void sink(String s) {}
4242
public static void test1() {
4343
Test t = new Test();
4444
t.fluentNoop().fluentSet(source()).fluentNoop();
45-
sink(t.get()); // $hasTaintFlow
45+
sink(t.get()); // $hasValueFlow
4646
}
4747

4848
public static void test2() {
4949
Test t = new Test();
5050
Test.identity(t).fluentNoop().fluentSet(source()).fluentNoop();
51-
sink(t.get()); // $hasTaintFlow
51+
sink(t.get()); // $hasValueFlow
5252
}
5353

5454
public static void test3() {
5555
Test t = new Test();
5656
t.indirectlyFluentNoop().fluentSet(source()).fluentNoop();
57-
sink(t.get()); // $hasTaintFlow
57+
sink(t.get()); // $hasValueFlow
5858
}
5959

6060
public static void testModel1() {
6161
Test t = new Test();
6262
t.indirectlyFluentNoop().modelledFluentMethod().fluentSet(source()).fluentNoop();
63-
sink(t.get()); // $hasTaintFlow
63+
sink(t.get()); // $hasValueFlow
6464
}
6565

6666
public static void testModel2() {
6767
Test t = new Test();
6868
Test.modelledIdentity(t).indirectlyFluentNoop().modelledFluentMethod().fluentSet(source()).fluentNoop();
69-
sink(t.get()); // $hasTaintFlow
69+
sink(t.get()); // $hasValueFlow
7070
}
7171

7272
}

java/ql/test/library-tests/dataflow/fluent-methods/flow.ql

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,3 @@ class IdentityModel extends ValuePreservingMethod {
1212

1313
override predicate returnsValue(int arg) { arg = 0 }
1414
}
15-
16-
class HasFlowTest extends InlineFlowTest {
17-
override DataFlow::Configuration getValueFlowConfig() { none() }
18-
}

java/ql/test/library-tests/dataflow/inoutbarriers/test.ql

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,54 +14,46 @@ predicate sink0(Node n) {
1414
)
1515
}
1616

17-
class Conf1 extends Configuration {
18-
Conf1() { this = "inoutbarriers1" }
17+
module Conf1 implements ConfigSig {
18+
predicate isSource(Node n) { src0(n) }
1919

20-
override predicate isSource(Node n) { src0(n) }
21-
22-
override predicate isSink(Node n) { sink0(n) }
20+
predicate isSink(Node n) { sink0(n) }
2321
}
2422

25-
class Conf2 extends Configuration {
26-
Conf2() { this = "inoutbarriers2" }
27-
28-
override predicate isSource(Node n) { src0(n) }
23+
module Conf2 implements ConfigSig {
24+
predicate isSource(Node n) { src0(n) }
2925

30-
override predicate isSink(Node n) { sink0(n) }
26+
predicate isSink(Node n) { sink0(n) }
3127

32-
override predicate isBarrierIn(Node n) { src0(n) }
28+
predicate isBarrierIn(Node n) { src0(n) }
3329
}
3430

35-
class Conf3 extends Configuration {
36-
Conf3() { this = "inoutbarriers3" }
31+
module Conf3 implements ConfigSig {
32+
predicate isSource(Node n) { src0(n) }
3733

38-
override predicate isSource(Node n) { src0(n) }
34+
predicate isSink(Node n) { sink0(n) }
3935

40-
override predicate isSink(Node n) { sink0(n) }
41-
42-
override predicate isBarrierOut(Node n) { sink0(n) }
36+
predicate isBarrierOut(Node n) { sink0(n) }
4337
}
4438

45-
class Conf4 extends Configuration {
46-
Conf4() { this = "inoutbarriers4" }
47-
48-
override predicate isSource(Node n) { src0(n) }
39+
module Conf4 implements ConfigSig {
40+
predicate isSource(Node n) { src0(n) }
4941

50-
override predicate isSink(Node n) { sink0(n) }
42+
predicate isSink(Node n) { sink0(n) }
5143

52-
override predicate isBarrierIn(Node n) { src0(n) }
44+
predicate isBarrierIn(Node n) { src0(n) }
5345

54-
override predicate isBarrierOut(Node n) { sink0(n) }
46+
predicate isBarrierOut(Node n) { sink0(n) }
5547
}
5648

5749
predicate flow(Node src, Node sink, string s) {
58-
any(Conf1 c).hasFlow(src, sink) and s = "nobarrier"
50+
Make<Conf1>::hasFlow(src, sink) and s = "nobarrier"
5951
or
60-
any(Conf2 c).hasFlow(src, sink) and s = "srcbarrier"
52+
Make<Conf2>::hasFlow(src, sink) and s = "srcbarrier"
6153
or
62-
any(Conf3 c).hasFlow(src, sink) and s = "sinkbarrier"
54+
Make<Conf3>::hasFlow(src, sink) and s = "sinkbarrier"
6355
or
64-
any(Conf4 c).hasFlow(src, sink) and s = "both"
56+
Make<Conf4>::hasFlow(src, sink) and s = "both"
6557
}
6658

6759
from Node src, Node sink, string s

java/ql/test/library-tests/dataflow/taint-format/A.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,44 @@ public static String source() {
66
return "tainted";
77
}
88

9+
public static void sink(Object o) { }
10+
911
public static void test1() {
10-
String bad = source(); // $ hasTaintFlow
12+
String bad = source();
1113
String good = "hi";
1214

13-
bad.formatted(good); // $ hasTaintFlow
14-
good.formatted("a", bad, "b", good); // $ hasTaintFlow
15-
String.format("%s%s", bad, good); // $ hasTaintFlow
16-
String.format("%s", good);
17-
String.format("%s %s %s %s %s %s %s %s %s %s ", "a", "a", "a", "a", "a", "a", "a", "a", "a", bad); // $ hasTaintFlow
15+
sink(bad.formatted(good)); // $ hasTaintFlow
16+
sink(good.formatted("a", bad, "b", good)); // $ hasTaintFlow
17+
sink(String.format("%s%s", bad, good)); // $ hasTaintFlow
18+
sink(String.format("%s", good));
19+
sink(String.format("%s %s %s %s %s %s %s %s %s %s ", "a", "a", "a", "a", "a", "a", "a", "a", "a", bad)); // $ hasTaintFlow
1820
}
1921

2022
public static void test2() {
21-
String bad = source(); // $ hasTaintFlow
23+
String bad = source();
2224
Formatter f = new Formatter();
2325

24-
f.toString();
25-
f.format("%s", bad); // $ hasTaintFlow
26-
f.toString(); // $ hasTaintFlow
26+
sink(f.toString());
27+
sink(f.format("%s", bad)); // $ hasTaintFlow
28+
sink(f.toString()); // $ hasTaintFlow
2729
}
2830

2931
public static void test3() {
30-
String bad = source(); // $ hasTaintFlow
32+
String bad = source();
3133
StringBuilder sb = new StringBuilder();
3234
Formatter f = new Formatter(sb);
3335

34-
sb.toString(); // $ hasTaintFlow false positive
35-
f.format("%s", bad); // $ hasTaintFlow
36-
sb.toString(); // $ hasTaintFlow
36+
sink(sb.toString()); // $ SPURIOUS: hasTaintFlow
37+
sink(f.format("%s", bad)); // $ hasTaintFlow
38+
sink(sb.toString()); // $ hasTaintFlow
3739
}
3840

3941
public static void test4() {
40-
String bad = source(); // $ hasTaintFlow
42+
String bad = source();
4143
StringBuilder sb = new StringBuilder();
4244

43-
sb.append(bad); // $ hasTaintFlow
45+
sink(sb.append(bad)); // $ hasTaintFlow
4446

45-
new Formatter(sb).format("ok").toString(); // $ hasTaintFlow
47+
sink(new Formatter(sb).format("ok").toString()); // $ hasTaintFlow
4648
}
47-
}
49+
}
Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,2 @@
11
import java
2-
import semmle.code.java.dataflow.DataFlow
3-
import semmle.code.java.dataflow.TaintTracking
42
import TestUtilities.InlineFlowTest
5-
6-
class TaintFlowConf extends DefaultTaintFlowConf {
7-
override predicate isSink(DataFlow::Node n) { n instanceof DataFlow::ExprNode }
8-
}

java/ql/test/library-tests/frameworks/JaxWs/JaxRsFlow.ql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import semmle.code.java.dataflow.TaintTracking
33
import semmle.code.java.dataflow.FlowSources
44
import TestUtilities.InlineFlowTest
55

6+
class EnableLegacy extends EnableLegacyConfiguration {
7+
EnableLegacy() { exists(this) }
8+
}
9+
610
class TaintFlowConf extends DefaultTaintFlowConf {
711
override predicate isSource(DataFlow::Node n) {
812
super.isSource(n)

java/ql/test/library-tests/frameworks/android/content-provider/test.ql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import java
22
import semmle.code.java.dataflow.FlowSources
33
import TestUtilities.InlineFlowTest
44

5+
class EnableLegacy extends EnableLegacyConfiguration {
6+
EnableLegacy() { exists(this) }
7+
}
8+
59
class ProviderTaintFlowConf extends DefaultTaintFlowConf {
610
override predicate isSource(DataFlow::Node n) { n instanceof RemoteFlowSource }
711
}

java/ql/test/library-tests/frameworks/android/slice/test.ql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import java
22
import TestUtilities.InlineFlowTest
33
import semmle.code.java.dataflow.FlowSources
44

5+
class EnableLegacy extends EnableLegacyConfiguration {
6+
EnableLegacy() { exists(this) }
7+
}
8+
59
class SliceValueFlowConf extends DefaultValueFlowConf {
610
override predicate isSource(DataFlow::Node source) {
711
super.isSource(source) or source instanceof RemoteFlowSource

java/ql/test/library-tests/frameworks/android/sources/OnActivityResultSourceTest.ql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import java
22
import semmle.code.java.dataflow.FlowSources
33
import TestUtilities.InlineFlowTest
44

5+
class EnableLegacy extends EnableLegacyConfiguration {
6+
EnableLegacy() { exists(this) }
7+
}
8+
59
class SourceValueFlowConf extends DefaultValueFlowConf {
610
override predicate isSource(DataFlow::Node src) { src instanceof RemoteFlowSource }
711
}

0 commit comments

Comments
 (0)