@@ -5,31 +5,6 @@ private import semmle.code.java.dataflow.DataFlow
5
5
private import semmle.code.java.frameworks.Networking
6
6
private import semmle.code.java.frameworks.Jndi
7
7
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
-
33
8
/**
34
9
* An expression that represents an insecure (non-SSL, non-private) LDAP URL.
35
10
*/
@@ -54,6 +29,34 @@ class InsecureLdapUrl extends Expr {
54
29
}
55
30
}
56
31
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
+
57
60
/**
58
61
* Holds if `ma` writes the `java.naming.provider.url` (also known as `Context.PROVIDER_URL`) key of a `Hashtable`.
59
62
*/
@@ -71,11 +74,36 @@ predicate isProviderUrlSetter(MethodAccess ma) {
71
74
)
72
75
}
73
76
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
+
74
102
/**
75
103
* Holds if `ma` sets `fieldValue` to `envValue` in some `Hashtable`.
76
104
*/
77
105
bindingset [ fieldValue, envValue]
78
- predicate hasFieldValueEnv ( MethodAccess ma , string fieldValue , string envValue ) {
106
+ private predicate hasFieldValueEnv ( MethodAccess ma , string fieldValue , string envValue ) {
79
107
// environment.put("java.naming.security.authentication", "simple")
80
108
ma .getMethod ( ) .getDeclaringType ( ) .getAnAncestor ( ) instanceof TypeHashtable and
81
109
ma .getMethod ( ) .hasName ( [ "put" , "setProperty" ] ) and
@@ -98,28 +126,3 @@ private predicate hasFieldNameEnv(MethodAccess ma, string fieldName, string envV
98
126
) and
99
127
ma .getArgument ( 1 ) .( CompileTimeConstantExpr ) .getStringValue ( ) = envValue
100
128
}
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