Skip to content

Commit 00ee3d2

Browse files
committed
Query to detect LDAP injections in Java
Cleanup
1 parent 95723b0 commit 00ee3d2

File tree

5 files changed

+18
-22
lines changed

5 files changed

+18
-22
lines changed

java/ql/src/Security/CWE/CWE-90/LdapInjectionLib.qll

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import semmle.code.java.frameworks.UnboundId
66
import semmle.code.java.frameworks.SpringLdap
77
import semmle.code.java.frameworks.ApacheLdap
88

9-
109
/** Holds if the parameter of `c` at index `paramIndex` is varargs. */
1110
bindingset[paramIndex]
1211
predicate isVarargs(Callable c, int paramIndex) {
@@ -20,8 +19,8 @@ abstract class LdapInjectionSource extends DataFlow::Node { }
2019
abstract class LdapInjectionSink extends DataFlow::ExprNode { }
2120

2221
/**
23-
* A taint-tracking configuration for unvalidated user input that is used to construct LDAP queries.
24-
*/
22+
* A taint-tracking configuration for unvalidated user input that is used to construct LDAP queries.
23+
*/
2524
class LdapInjectionFlowConfig extends TaintTracking::Configuration {
2625
LdapInjectionFlowConfig() { this = "LdapInjectionFlowConfig" }
2726

@@ -79,7 +78,7 @@ class JndiLdapInjectionSink extends LdapInjectionSink {
7978
|
8079
m.getDeclaringType().getAnAncestor() instanceof TypeDirContext and
8180
m.hasName("search") and
82-
index in [0..1]
81+
index in [0 .. 1]
8382
)
8483
}
8584
}
@@ -129,16 +128,13 @@ class SpringLdapInjectionSink extends LdapInjectionSink {
129128
) and
130129
(
131130
// Parameter index is 1 (DN or query) or 2 (filter) if method is not authenticate
132-
(
133-
index in [0..1] and
134-
not m instanceof MethodSpringLdapTemplateAuthenticate
135-
) or
131+
index in [0 .. 1] and
132+
not m instanceof MethodSpringLdapTemplateAuthenticate
133+
or
136134
// But it's not the last parameter in case of authenticate method (last param is password)
137-
(
138-
index in [0..1] and
139-
index < m.getNumberOfParameters() - 1 and
140-
m instanceof MethodSpringLdapTemplateAuthenticate
141-
)
135+
index in [0 .. 1] and
136+
index < m.getNumberOfParameters() - 1 and
137+
m instanceof MethodSpringLdapTemplateAuthenticate
142138
)
143139
)
144140
}
@@ -442,4 +438,4 @@ predicate apacheLdapDnGetStep(ExprNode n1, ExprNode n2) {
442438
m.getDeclaringType().getAnAncestor() instanceof TypeApacheDn and
443439
(m.hasName("getName") or m.hasName("getNormName") or m.hasName("toString"))
444440
)
445-
}
441+
}

java/ql/src/semmle/code/java/frameworks/ApacheLdap.qll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,5 @@ class TypeApacheSearchRequest extends Interface {
2323

2424
/** The class `org.apache.directory.api.ldap.model.name.Dn`. */
2525
class TypeApacheDn extends Class {
26-
TypeApacheDn() {
27-
this.hasQualifiedName("org.apache.directory.api.ldap.model.name", "Dn")
28-
}
29-
}
26+
TypeApacheDn() { this.hasQualifiedName("org.apache.directory.api.ldap.model.name", "Dn") }
27+
}

java/ql/src/semmle/code/java/frameworks/Jndi.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,4 @@ class MethodLdapNameToString extends Method {
5656
getDeclaringType() instanceof TypeLdapName and
5757
hasName("toString")
5858
}
59-
}
59+
}

java/ql/src/semmle/code/java/frameworks/SpringLdap.qll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import semmle.code.java.Member
99
/*--- Types ---*/
1010
/** The class `org.springframework.ldap.core.LdapTemplate`. */
1111
class TypeSpringLdapTemplate extends Class {
12-
TypeSpringLdapTemplate() { this.hasQualifiedName("org.springframework.ldap.core", "LdapTemplate") }
12+
TypeSpringLdapTemplate() {
13+
this.hasQualifiedName("org.springframework.ldap.core", "LdapTemplate")
14+
}
1315
}
1416

1517
/** The class `org.springframework.ldap.query.LdapQueryBuilder`. */
@@ -188,4 +190,4 @@ class MethodSpringLdapUtilsNewLdapName extends Method {
188190
getDeclaringType() instanceof TypeSpringLdapUtils and
189191
hasName("newLdapName")
190192
}
191-
}
193+
}

java/ql/src/semmle/code/java/frameworks/UnboundId.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,4 @@ class MethodUnboundIdLDAPConnectionSearchForEntry extends Method {
110110
getDeclaringType() instanceof TypeUnboundIdLDAPConnection and
111111
hasName("searchForEntry")
112112
}
113-
}
113+
}

0 commit comments

Comments
 (0)