Skip to content

Commit 52b2411

Browse files
author
Sauyon Lee
committed
Add tests for Spring validation.Errors
1 parent b76f761 commit 52b2411

File tree

5 files changed

+131
-72
lines changed

5 files changed

+131
-72
lines changed

java/ql/test/library-tests/frameworks/spring/ValidationErrorsTest.java

Lines changed: 0 additions & 72 deletions
This file was deleted.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import org.springframework.validation.Errors;
2+
3+
class ValidationErrorsTest {
4+
Object source() { return null; }
5+
6+
Errors sourceErrs() { return (Errors)source(); }
7+
Errors errors() { return null; }
8+
9+
void sink(Object o) {}
10+
11+
void test() {
12+
Errors es0 = errors();
13+
es0.addAllErrors(sourceErrs());
14+
sink(es0); // $hasTaintFlow
15+
16+
sink(sourceErrs().getAllErrors()); // $hasTaintFlow
17+
18+
sink(sourceErrs().getFieldError()); // $hasTaintFlow
19+
sink(sourceErrs().getFieldError("field")); // $hasTaintFlow
20+
21+
sink(sourceErrs().getGlobalError()); // $hasTaintFlow
22+
sink(sourceErrs().getGlobalErrors()); // $hasTaintFlow
23+
24+
Errors es1 = errors();
25+
es1.reject((String)source());
26+
sink(es1); // $hasTaintFlow
27+
28+
Errors es2 = errors();
29+
es2.reject((String)source(), null, "");
30+
sink(es2); // $hasTaintFlow
31+
32+
Errors es3 = errors();
33+
es3.reject((String)source(), null, "");
34+
sink(es3); // $hasTaintFlow
35+
36+
{
37+
Errors es4 = errors();
38+
Object[] in = { (String)source() };
39+
es4.reject("", in, "");
40+
sink(in); // $hasTaintFlow
41+
}
42+
43+
{
44+
Errors es5 = errors();
45+
es5.reject("", null, (String)source());
46+
sink(es5); // $hasTaintFlow
47+
}
48+
49+
Errors es6 = errors();
50+
es6.reject((String)source(), "");
51+
sink(es6); // $hasTaintFlow
52+
53+
Errors es7 = errors();
54+
es7.reject("", (String)source());
55+
sink(es7); // $hasTaintFlow
56+
57+
Errors es8 = errors();
58+
es8.rejectValue("", (String)source(), null, "");
59+
sink(es8); // $hasTaintFlow
60+
61+
Errors es9 = errors();
62+
Object[] in = {source()};
63+
es9.rejectValue("", "", in, "");
64+
sink(es9); // $hasTaintFlow
65+
66+
Errors es10 = errors();
67+
es10.rejectValue("", "", null, (String)source());
68+
sink(es10); // $hasTaintFlow
69+
70+
Errors es11 = errors();
71+
es11.rejectValue("", (String)source(), "");
72+
sink(es11); // $hasTaintFlow
73+
74+
Errors es12 = errors();
75+
es12.rejectValue("", "", (String)source());
76+
sink(es12); // $hasTaintFlow
77+
}
78+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//semmle-extractor-options: --javac-args -cp ${testdir}/../../../../stubs/springframework-5.3.8

java/ql/test/library-tests/frameworks/spring/validation/test.expected

Whitespace-only changes.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import java
2+
import semmle.code.java.dataflow.DataFlow
3+
import semmle.code.java.dataflow.TaintTracking
4+
import TestUtilities.InlineExpectationsTest
5+
6+
class ValueFlowConf extends DataFlow::Configuration {
7+
ValueFlowConf() { this = "qltest:valueFlowConf" }
8+
9+
override predicate isSource(DataFlow::Node n) {
10+
n.asExpr().(MethodAccess).getMethod().hasName("source")
11+
}
12+
13+
override predicate isSink(DataFlow::Node n) {
14+
n.asExpr().(Argument).getCall().getCallee().hasName("sink")
15+
}
16+
}
17+
18+
class TaintFlowConf extends TaintTracking::Configuration {
19+
TaintFlowConf() { this = "qltest:taintFlowConf" }
20+
21+
override predicate isSource(DataFlow::Node n) {
22+
n.asExpr().(MethodAccess).getMethod().hasName("source")
23+
}
24+
25+
override predicate isSink(DataFlow::Node n) {
26+
n.asExpr().(Argument).getCall().getCallee().hasName("sink")
27+
}
28+
}
29+
30+
class HasFlowTest extends InlineExpectationsTest {
31+
HasFlowTest() { this = "HasFlowTest" }
32+
33+
override string getARelevantTag() { result = ["hasValueFlow", "hasTaintFlow"] }
34+
35+
override predicate hasActualResult(Location location, string element, string tag, string value) {
36+
tag = "hasValueFlow" and
37+
exists(DataFlow::Node src, DataFlow::Node sink, ValueFlowConf conf | conf.hasFlow(src, sink) |
38+
sink.getLocation() = location and
39+
element = sink.toString() and
40+
value = ""
41+
)
42+
or
43+
tag = "hasTaintFlow" and
44+
exists(DataFlow::Node src, DataFlow::Node sink, TaintFlowConf conf |
45+
conf.hasFlow(src, sink) and not any(ValueFlowConf c).hasFlow(src, sink)
46+
|
47+
sink.getLocation() = location and
48+
element = sink.toString() and
49+
value = ""
50+
)
51+
}
52+
}

0 commit comments

Comments
 (0)