Skip to content

Commit 787b733

Browse files
committed
Refactor TemplateInjection
1 parent 7e1c424 commit 787b733

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

java/ql/lib/semmle/code/java/security/TemplateInjectionQuery.qll

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ import semmle.code.java.dataflow.TaintTracking
55
import semmle.code.java.dataflow.FlowSources
66
import semmle.code.java.security.TemplateInjection
77

8-
/** A taint tracking configuration to reason about server-side template injection (SST) vulnerabilities */
9-
class TemplateInjectionFlowConfig extends TaintTracking::Configuration {
8+
/**
9+
* DEPRECATED: Use `TemplateInjectionFlow` instead.
10+
*
11+
* A taint tracking configuration to reason about server-side template injection (SST) vulnerabilities
12+
*/
13+
deprecated class TemplateInjectionFlowConfig extends TaintTracking::Configuration {
1014
TemplateInjectionFlowConfig() { this = "TemplateInjectionFlowConfig" }
1115

1216
override predicate isSource(DataFlow::Node source, DataFlow::FlowState state) {
@@ -36,3 +40,35 @@ class TemplateInjectionFlowConfig extends TaintTracking::Configuration {
3640
any(TemplateInjectionAdditionalTaintStep a).isAdditionalTaintStep(node1, state1, node2, state2)
3741
}
3842
}
43+
44+
/** A taint tracking configuration to reason about server-side template injection (SST) vulnerabilities */
45+
private module TemplateInjectionFlowConfig implements DataFlow::StateConfigSig {
46+
class FlowState = DataFlow::FlowState;
47+
48+
predicate isSource(DataFlow::Node source, FlowState state) {
49+
source.(TemplateInjectionSource).hasState(state)
50+
}
51+
52+
predicate isSink(DataFlow::Node sink, FlowState state) {
53+
sink.(TemplateInjectionSink).hasState(state)
54+
}
55+
56+
predicate isBarrier(DataFlow::Node sanitizer) { sanitizer instanceof TemplateInjectionSanitizer }
57+
58+
predicate isBarrier(DataFlow::Node sanitizer, FlowState state) {
59+
sanitizer.(TemplateInjectionSanitizerWithState).hasState(state)
60+
}
61+
62+
predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) {
63+
any(TemplateInjectionAdditionalTaintStep a).isAdditionalTaintStep(node1, node2)
64+
}
65+
66+
predicate isAdditionalFlowStep(
67+
DataFlow::Node node1, FlowState state1, DataFlow::Node node2, FlowState state2
68+
) {
69+
any(TemplateInjectionAdditionalTaintStep a).isAdditionalTaintStep(node1, state1, node2, state2)
70+
}
71+
}
72+
73+
/** Tracks server-side template injection (SST) vulnerabilities */
74+
module TemplateInjectionFlow = TaintTracking::MakeWithState<TemplateInjectionFlowConfig>;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
import java
1515
import semmle.code.java.security.TemplateInjectionQuery
16-
import DataFlow::PathGraph
16+
import TemplateInjectionFlow::PathGraph
1717

18-
from TemplateInjectionFlowConfig config, DataFlow::PathNode source, DataFlow::PathNode sink
19-
where config.hasFlowPath(source, sink)
18+
from TemplateInjectionFlow::PathNode source, TemplateInjectionFlow::PathNode sink
19+
where TemplateInjectionFlow::hasFlowPath(source, sink)
2020
select sink.getNode(), source, sink, "Template, which may contain code, depends on a $@.",
2121
source.getNode(), "user-provided value"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class TemplateInjectionTest extends InlineExpectationsTest {
99

1010
override predicate hasActualResult(Location location, string element, string tag, string value) {
1111
tag = "hasTemplateInjection" and
12-
exists(DataFlow::Node sink, TemplateInjectionFlowConfig conf | conf.hasFlowTo(sink) |
12+
exists(DataFlow::Node sink | TemplateInjectionFlow::hasFlowTo(sink) |
1313
sink.getLocation() = location and
1414
element = sink.toString() and
1515
value = ""

0 commit comments

Comments
 (0)