Skip to content

Commit 423ab1d

Browse files
committed
Refactor JndiInjection
1 parent 8bf3315 commit 423ab1d

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

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

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import semmle.code.java.frameworks.SpringLdap
77
import semmle.code.java.security.JndiInjection
88

99
/**
10+
* DEPRECATED: Use `JndiInjectionFlow` instead.
11+
*
1012
* A taint-tracking configuration for unvalidated user input that is used in JNDI lookup.
1113
*/
12-
class JndiInjectionFlowConfig extends TaintTracking::Configuration {
14+
deprecated class JndiInjectionFlowConfig extends TaintTracking::Configuration {
1315
JndiInjectionFlowConfig() { this = "JndiInjectionFlowConfig" }
1416

1517
override predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource }
@@ -27,14 +29,32 @@ class JndiInjectionFlowConfig extends TaintTracking::Configuration {
2729
}
2830
}
2931

32+
/**
33+
* A taint-tracking configuration for unvalidated user input that is used in JNDI lookup.
34+
*/
35+
private module JndiInjectionFlowConfig implements DataFlow::ConfigSig {
36+
predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource }
37+
38+
predicate isSink(DataFlow::Node sink) { sink instanceof JndiInjectionSink }
39+
40+
predicate isBarrier(DataFlow::Node node) {
41+
node.getType() instanceof PrimitiveType or node.getType() instanceof BoxedType
42+
}
43+
44+
predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) {
45+
any(JndiInjectionAdditionalTaintStep c).step(node1, node2)
46+
}
47+
}
48+
49+
/** Tracks flow of unvalidated user input that is used in JNDI lookup */
50+
module JndiInjectionFlow = TaintTracking::Make<JndiInjectionFlowConfig>;
51+
3052
/**
3153
* A method that does a JNDI lookup when it receives a `SearchControls` argument with `setReturningObjFlag` = `true`
3254
*/
3355
private class UnsafeSearchControlsSink extends JndiInjectionSink {
3456
UnsafeSearchControlsSink() {
35-
exists(UnsafeSearchControlsConf conf, MethodAccess ma |
36-
conf.hasFlowTo(DataFlow::exprNode(ma.getAnArgument()))
37-
|
57+
exists(MethodAccess ma | UnsafeSearchControlsFlow::hasFlowToExpr(ma.getAnArgument()) |
3858
this.asExpr() = ma.getArgument(0)
3959
)
4060
}
@@ -44,14 +64,14 @@ private class UnsafeSearchControlsSink extends JndiInjectionSink {
4464
* Find flows between a `SearchControls` object with `setReturningObjFlag` = `true`
4565
* and an argument of an `LdapOperations.search` or `DirContext.search` call.
4666
*/
47-
private class UnsafeSearchControlsConf extends DataFlow2::Configuration {
48-
UnsafeSearchControlsConf() { this = "UnsafeSearchControlsConf" }
49-
50-
override predicate isSource(DataFlow::Node source) { source instanceof UnsafeSearchControls }
67+
private module UnsafeSearchControlsConfig implements DataFlow::ConfigSig {
68+
predicate isSource(DataFlow::Node source) { source instanceof UnsafeSearchControls }
5169

52-
override predicate isSink(DataFlow::Node sink) { sink instanceof UnsafeSearchControlsArgument }
70+
predicate isSink(DataFlow::Node sink) { sink instanceof UnsafeSearchControlsArgument }
5371
}
5472

73+
private module UnsafeSearchControlsFlow = DataFlow::Make<UnsafeSearchControlsConfig>;
74+
5575
/**
5676
* An argument of type `SearchControls` of an `LdapOperations.search` or `DirContext.search` call.
5777
*/

java/ql/src/Security/CWE/CWE-074/JndiInjection.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.JndiInjectionQuery
16-
import DataFlow::PathGraph
16+
import JndiInjectionFlow::PathGraph
1717

18-
from DataFlow::PathNode source, DataFlow::PathNode sink, JndiInjectionFlowConfig conf
19-
where conf.hasFlowPath(source, sink)
18+
from JndiInjectionFlow::PathNode source, JndiInjectionFlow::PathNode sink
19+
where JndiInjectionFlow::hasFlowPath(source, sink)
2020
select sink.getNode(), source, sink, "JNDI lookup might include name from $@.", source.getNode(),
2121
"this user input"

java/ql/test/query-tests/security/CWE-074/JndiInjectionTest.ql

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

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

0 commit comments

Comments
 (0)