Skip to content

Commit 0eaf222

Browse files
committed
Move public classes/predicates to top of library file
1 parent f28f1af commit 0eaf222

File tree

1 file changed

+54
-51
lines changed

1 file changed

+54
-51
lines changed

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

Lines changed: 54 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,6 @@ private import semmle.code.java.dataflow.DataFlow
55
private import semmle.code.java.frameworks.Networking
66
private import semmle.code.java.frameworks.Jndi
77

8-
/**
9-
* An insecure (non-SSL, non-private) LDAP URL string literal.
10-
*/
11-
private class InsecureLdapUrlLiteral extends StringLiteral {
12-
InsecureLdapUrlLiteral() {
13-
// Match connection strings with the LDAP protocol and without private IP addresses to reduce false positives.
14-
exists(string s | this.getValue() = s |
15-
s.regexpMatch("(?i)ldap://[\\[a-zA-Z0-9].*") and
16-
not s.substring(7, s.length()) instanceof PrivateHostName
17-
)
18-
}
19-
}
20-
21-
/** The class `java.util.Hashtable`. */
22-
private class TypeHashtable extends Class {
23-
TypeHashtable() { this.getSourceDeclaration().hasQualifiedName("java.util", "Hashtable") }
24-
}
25-
26-
/** Get the string value of an expression representing a hostname. */
27-
private string getHostname(Expr expr) {
28-
result = expr.(CompileTimeConstantExpr).getStringValue() or
29-
result =
30-
expr.(VarAccess).getVariable().getAnAssignedValue().(CompileTimeConstantExpr).getStringValue()
31-
}
32-
338
/**
349
* An expression that represents an insecure (non-SSL, non-private) LDAP URL.
3510
*/
@@ -54,6 +29,34 @@ class InsecureLdapUrl extends Expr {
5429
}
5530
}
5631

32+
/**
33+
* A sink representing the construction of a `DirContextEnvironment`.
34+
*/
35+
class InsecureLdapUrlSink extends DataFlow::Node {
36+
InsecureLdapUrlSink() {
37+
exists(ConstructorCall cc |
38+
cc.getConstructedType().getAnAncestor() instanceof TypeDirContext and
39+
this.asExpr() = cc.getArgument(0)
40+
)
41+
}
42+
}
43+
44+
/**
45+
* Holds if `ma` sets `java.naming.security.authentication` (also known as `Context.SECURITY_AUTHENTICATION`) to `simple` in some `Hashtable`.
46+
*/
47+
predicate isBasicAuthEnv(MethodAccess ma) {
48+
hasFieldValueEnv(ma, "java.naming.security.authentication", "simple") or
49+
hasFieldNameEnv(ma, "SECURITY_AUTHENTICATION", "simple")
50+
}
51+
52+
/**
53+
* Holds if `ma` sets `java.naming.security.protocol` (also known as `Context.SECURITY_PROTOCOL`) to `ssl` in some `Hashtable`.
54+
*/
55+
predicate isSslEnv(MethodAccess ma) {
56+
hasFieldValueEnv(ma, "java.naming.security.protocol", "ssl") or
57+
hasFieldNameEnv(ma, "SECURITY_PROTOCOL", "ssl")
58+
}
59+
5760
/**
5861
* Holds if `ma` writes the `java.naming.provider.url` (also known as `Context.PROVIDER_URL`) key of a `Hashtable`.
5962
*/
@@ -71,11 +74,36 @@ predicate isProviderUrlSetter(MethodAccess ma) {
7174
)
7275
}
7376

77+
/**
78+
* An insecure (non-SSL, non-private) LDAP URL string literal.
79+
*/
80+
private class InsecureLdapUrlLiteral extends StringLiteral {
81+
InsecureLdapUrlLiteral() {
82+
// Match connection strings with the LDAP protocol and without private IP addresses to reduce false positives.
83+
exists(string s | this.getValue() = s |
84+
s.regexpMatch("(?i)ldap://[\\[a-zA-Z0-9].*") and
85+
not s.substring(7, s.length()) instanceof PrivateHostName
86+
)
87+
}
88+
}
89+
90+
/** The class `java.util.Hashtable`. */
91+
private class TypeHashtable extends Class {
92+
TypeHashtable() { this.getSourceDeclaration().hasQualifiedName("java.util", "Hashtable") }
93+
}
94+
95+
/** Get the string value of an expression representing a hostname. */
96+
private string getHostname(Expr expr) {
97+
result = expr.(CompileTimeConstantExpr).getStringValue() or
98+
result =
99+
expr.(VarAccess).getVariable().getAnAssignedValue().(CompileTimeConstantExpr).getStringValue()
100+
}
101+
74102
/**
75103
* Holds if `ma` sets `fieldValue` to `envValue` in some `Hashtable`.
76104
*/
77105
bindingset[fieldValue, envValue]
78-
predicate hasFieldValueEnv(MethodAccess ma, string fieldValue, string envValue) {
106+
private predicate hasFieldValueEnv(MethodAccess ma, string fieldValue, string envValue) {
79107
// environment.put("java.naming.security.authentication", "simple")
80108
ma.getMethod().getDeclaringType().getAnAncestor() instanceof TypeHashtable and
81109
ma.getMethod().hasName(["put", "setProperty"]) and
@@ -98,28 +126,3 @@ private predicate hasFieldNameEnv(MethodAccess ma, string fieldName, string envV
98126
) and
99127
ma.getArgument(1).(CompileTimeConstantExpr).getStringValue() = envValue
100128
}
101-
102-
/**
103-
* Holds if `ma` sets `java.naming.security.authentication` (also known as `Context.SECURITY_AUTHENTICATION`) to `simple` in some `Hashtable`.
104-
*/
105-
predicate isBasicAuthEnv(MethodAccess ma) {
106-
hasFieldValueEnv(ma, "java.naming.security.authentication", "simple") or
107-
hasFieldNameEnv(ma, "SECURITY_AUTHENTICATION", "simple")
108-
}
109-
110-
/**
111-
* Holds if `ma` sets `java.naming.security.protocol` (also known as `Context.SECURITY_PROTOCOL`) to `ssl` in some `Hashtable`.
112-
*/
113-
predicate isSslEnv(MethodAccess ma) {
114-
hasFieldValueEnv(ma, "java.naming.security.protocol", "ssl") or
115-
hasFieldNameEnv(ma, "SECURITY_PROTOCOL", "ssl")
116-
}
117-
118-
class InsecureLdapUrlSink extends DataFlow::Node {
119-
InsecureLdapUrlSink() {
120-
exists(ConstructorCall cc |
121-
cc.getConstructedType().getAnAncestor() instanceof TypeDirContext and
122-
this.asExpr() = cc.getArgument(0)
123-
)
124-
}
125-
}

0 commit comments

Comments
 (0)