Skip to content

Commit 94f32d2

Browse files
committed
Decouple SpelInjection.qll to reuse the taint tracking configuration
1 parent 569426b commit 94f32d2

File tree

4 files changed

+37
-45
lines changed

4 files changed

+37
-45
lines changed

java/ql/src/Security/CWE/CWE-094/SpelInjection.ql

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,9 @@
1111
*/
1212

1313
import java
14-
import semmle.code.java.security.SpelInjection
14+
import semmle.code.java.security.SpelInjectionQuery
1515
import DataFlow::PathGraph
1616

17-
/**
18-
* A taint-tracking configuration for unsafe user input
19-
* that is used to construct and evaluate a SpEL expression.
20-
*/
21-
class SpELInjectionConfig extends TaintTracking::Configuration {
22-
SpELInjectionConfig() { this = "SpELInjectionConfig" }
23-
24-
override predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource }
25-
26-
override predicate isSink(DataFlow::Node sink) { sink instanceof SpelExpressionEvaluationSink }
27-
28-
override predicate isAdditionalTaintStep(DataFlow::Node node1, DataFlow::Node node2) {
29-
any(SpelExpressionInjectionAdditionalTaintStep c).step(node1, node2)
30-
}
31-
}
32-
33-
from DataFlow::PathNode source, DataFlow::PathNode sink, SpELInjectionConfig conf
17+
from DataFlow::PathNode source, DataFlow::PathNode sink, SpelInjectionConfig conf
3418
where conf.hasFlowPath(source, sink)
3519
select sink.getNode(), source, sink, "SpEL injection from $@.", source.getNode(), "this user input"

java/ql/src/semmle/code/java/security/SpelInjection.qll renamed to java/ql/src/semmle/code/java/security/SpelInjectionQuery.qll

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,11 @@
22

33
import java
44
import semmle.code.java.dataflow.DataFlow
5-
import semmle.code.java.dataflow.ExternalFlow
65
import semmle.code.java.dataflow.FlowSources
76

87
/** A data flow sink for unvalidated user input that is used to construct SpEL expressions. */
98
abstract class SpelExpressionEvaluationSink extends DataFlow::ExprNode { }
109

11-
private class SpelExpressionEvaluationModel extends SinkModelCsv {
12-
override predicate row(string row) {
13-
row =
14-
[
15-
"org.springframework.expression;Expression;true;getValue;;;Argument[-1];spel",
16-
"org.springframework.expression;Expression;true;getValueTypeDescriptor;;;Argument[-1];spel",
17-
"org.springframework.expression;Expression;true;getValueType;;;Argument[-1];spel",
18-
"org.springframework.expression;Expression;true;setValue;;;Argument[-1];spel"
19-
]
20-
}
21-
}
22-
2310
/** Default sink for SpEL injection vulnerabilities. */
2411
private class DefaultSpelExpressionEvaluationSink extends SpelExpressionEvaluationSink {
2512
DefaultSpelExpressionEvaluationSink() {
@@ -53,6 +40,22 @@ private class DefaultSpelExpressionInjectionAdditionalTaintStep extends SpelExpr
5340
}
5441
}
5542

43+
/**
44+
* A taint-tracking configuration for unsafe user input
45+
* that is used to construct and evaluate a SpEL expression.
46+
*/
47+
class SpelInjectionConfig extends TaintTracking::Configuration {
48+
SpelInjectionConfig() { this = "SpelInjectionConfig" }
49+
50+
override predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource }
51+
52+
override predicate isSink(DataFlow::Node sink) { sink instanceof SpelExpressionEvaluationSink }
53+
54+
override predicate isAdditionalTaintStep(DataFlow::Node node1, DataFlow::Node node2) {
55+
any(SpelExpressionInjectionAdditionalTaintStep c).step(node1, node2)
56+
}
57+
}
58+
5659
/**
5760
* A configuration for safe evaluation context that may be used in expression evaluation.
5861
*/
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/** Provides sink models relating to SpEL injection vulnerabilities. */
2+
3+
import semmle.code.java.dataflow.ExternalFlow
4+
5+
private class SpelExpressionEvaluationModel extends SinkModelCsv {
6+
override predicate row(string row) {
7+
row =
8+
[
9+
"org.springframework.expression;Expression;true;getValue;;;Argument[-1];spel",
10+
"org.springframework.expression;Expression;true;getValueTypeDescriptor;;;Argument[-1];spel",
11+
"org.springframework.expression;Expression;true;getValueType;;;Argument[-1];spel",
12+
"org.springframework.expression;Expression;true;setValue;;;Argument[-1];spel"
13+
]
14+
}
15+
}

java/ql/test/query-tests/security/CWE-094/SpelInjectionTest.ql

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,19 @@
11
import java
22
import semmle.code.java.dataflow.TaintTracking
33
import semmle.code.java.dataflow.FlowSources
4-
import semmle.code.java.security.SpelInjection
4+
import semmle.code.java.security.SpelInjectionQuery
55
import TestUtilities.InlineExpectationsTest
66

7-
class Conf extends TaintTracking::Configuration {
8-
Conf() { this = "test:cwe:spel-injection" }
9-
10-
override predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource }
11-
12-
override predicate isSink(DataFlow::Node sink) { sink instanceof SpelExpressionEvaluationSink }
13-
14-
override predicate isAdditionalTaintStep(DataFlow::Node node1, DataFlow::Node node2) {
15-
any(SpelExpressionInjectionAdditionalTaintStep c).step(node1, node2)
16-
}
17-
}
18-
197
class HasSpelInjectionTest extends InlineExpectationsTest {
208
HasSpelInjectionTest() { this = "HasSpelInjectionTest" }
219

2210
override string getARelevantTag() { result = "hasSpelInjection" }
2311

2412
override predicate hasActualResult(Location location, string element, string tag, string value) {
2513
tag = "hasSpelInjection" and
26-
exists(DataFlow::Node src, DataFlow::Node sink, Conf conf | conf.hasFlow(src, sink) |
14+
exists(DataFlow::Node src, DataFlow::Node sink, SpelInjectionConfig conf |
15+
conf.hasFlow(src, sink)
16+
|
2717
sink.getLocation() = location and
2818
element = sink.toString() and
2919
value = ""

0 commit comments

Comments
 (0)