Skip to content

Commit 0779aab

Browse files
committed
Clean up the QL code
1 parent f8c4947 commit 0779aab

File tree

3 files changed

+53
-26
lines changed

3 files changed

+53
-26
lines changed

java/ql/src/experimental/Security/CWE/CWE-273/UnsafeCertTrust.ql

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @name Unsafe implementation of trusting any certificate or missing hostname verification in SSL configuration
2+
* @name Unsafe certificate trust and improper hostname verification
33
* @description Unsafe implementation of the interface X509TrustManager, HostnameVerifier, and SSLSocket/SSLEngine ignores all SSL certificate validation errors when establishing an HTTPS connection, thereby making the app vulnerable to man-in-the-middle attacks.
44
* @kind problem
55
* @id java/unsafe-cert-trust
@@ -39,13 +39,14 @@ class X509TrustAllManagerInit extends MethodAccess {
3939
this.getMethod().getDeclaringType() instanceof SSLContext and //init method of SSLContext
4040
(
4141
exists(ArrayInit ai |
42+
this.getArgument(1).(ArrayCreationExpr).getInit() = ai and
4243
ai.getInit(0).(VarAccess).getVariable().getInitializer().getType().(Class).getASupertype*()
4344
instanceof X509TrustAllManager //Scenario of context.init(null, new TrustManager[] { TRUST_ALL_CERTIFICATES }, null);
4445
)
4546
or
4647
exists(Variable v, ArrayInit ai |
4748
this.getArgument(1).(VarAccess).getVariable() = v and
48-
ai.getParent() = v.getAnAccess().getVariable().getAnAssignedValue() and
49+
ai.getParent() = v.getAnAssignedValue() and
4950
ai.getInit(0).getType().(Class).getASupertype*() instanceof X509TrustAllManager //Scenario of context.init(null, serverTMs, null);
5051
)
5152
)
@@ -81,7 +82,7 @@ class TrustAllHostnameVerify extends MethodAccess {
8182
)
8283
or
8384
exists(Variable v |
84-
this.getArgument(0).(VarAccess).getVariable() = v.getAnAccess().getVariable() and
85+
this.getArgument(0).(VarAccess).getVariable() = v and
8586
v.getInitializer().getType() instanceof TrustAllHostnameVerifier //Scenario of HttpsURLConnection.setDefaultHostnameVerifier(verifier);
8687
)
8788
)
@@ -96,6 +97,10 @@ class Socket extends RefType {
9697
Socket() { this.hasQualifiedName("java.net", "Socket") }
9798
}
9899

100+
class SocketFactory extends RefType {
101+
SocketFactory() { this.hasQualifiedName("javax.net", "SocketFactory") }
102+
}
103+
99104
class SSLSocket extends RefType {
100105
SSLSocket() { this.hasQualifiedName("javax.net.ssl", "SSLSocket") }
101106
}
@@ -110,9 +115,9 @@ predicate setEndpointIdentificationAlgorithm(MethodAccess createSSL) {
110115
createSSL = sslo.getAnAssignedValue() and
111116
ma.getQualifier() = sslo.getAnAccess() and
112117
ma.getMethod().hasName("setSSLParameters") and
113-
ma.getArgument(0).(VarAccess) = sslparams.getAnAccess() and
118+
ma.getArgument(0) = sslparams.getAnAccess() and
114119
exists(MethodAccess setepa |
115-
setepa.getQualifier().(VarAccess) = sslparams.getAnAccess() and
120+
setepa.getQualifier() = sslparams.getAnAccess() and
116121
setepa.getMethod().hasName("setEndpointIdentificationAlgorithm") and
117122
not setepa.getArgument(0) instanceof NullLiteral
118123
)
@@ -128,15 +133,26 @@ predicate hasEndpointIdentificationAlgorithm(Variable ssl) {
128133
|
129134
ma.getQualifier() = ssl.getAnAccess() and
130135
ma.getMethod().hasName("setSSLParameters") and
131-
ma.getArgument(0).(VarAccess) = sslparams.getAnAccess() and
136+
ma.getArgument(0) = sslparams.getAnAccess() and
132137
exists(MethodAccess setepa |
133-
setepa.getQualifier().(VarAccess) = sslparams.getAnAccess() and
138+
setepa.getQualifier() = sslparams.getAnAccess() and
134139
setepa.getMethod().hasName("setEndpointIdentificationAlgorithm") and
135140
not setepa.getArgument(0) instanceof NullLiteral
136141
)
137142
)
138143
}
139144

145+
/**
146+
* Cast of Socket to SSLSocket
147+
*/
148+
predicate sslCast(MethodAccess createSSL) {
149+
exists(Variable ssl, CastExpr ce |
150+
ce.getExpr() = createSSL and
151+
ce.getControlFlowNode().getASuccessor().(VariableAssign).getDestVar() = ssl and
152+
ssl.getType() instanceof SSLSocket //With a type cast `SSLSocket socket = (SSLSocket) socketFactory.createSocket("www.example.com", 443)`
153+
)
154+
}
155+
140156
/**
141157
* SSL object is created in a separate method call or in the same method
142158
*/
@@ -145,13 +161,13 @@ predicate hasFlowPath(MethodAccess createSSL, Variable ssl) {
145161
createSSL = ssl.getAnAssignedValue()
146162
or
147163
exists(CastExpr ce |
148-
ce.getExpr().(MethodAccess) = createSSL and
164+
ce.getExpr() = createSSL and
149165
ce.getControlFlowNode().getASuccessor().(VariableAssign).getDestVar() = ssl //With a type cast like SSLSocket socket = (SSLSocket) socketFactory.createSocket("www.example.com", 443);
150166
)
151167
)
152168
or
153169
exists(MethodAccess tranm |
154-
createSSL.getEnclosingCallable().(Method) = tranm.getMethod() and
170+
createSSL.getEnclosingCallable() = tranm.getMethod() and
155171
tranm.getControlFlowNode().getASuccessor().(VariableAssign).getDestVar() = ssl and
156172
not setEndpointIdentificationAlgorithm(createSSL) //Check the scenario of invocation before used in the current method
157173
)
@@ -183,7 +199,9 @@ class SSLEndpointIdentificationNotSet extends MethodAccess {
183199
this.getMethod().getDeclaringType() instanceof SSLContext //createEngine method of SSLContext
184200
or
185201
this.getMethod().hasName("createSocket") and
186-
this.getMethod().getReturnType() instanceof Socket //createSocket method of SSLSocketFactory
202+
this.getMethod().getDeclaringType() instanceof SocketFactory and
203+
this.getMethod().getReturnType() instanceof Socket and
204+
sslCast(this) //createSocket method of SocketFactory
187205
) and
188206
exists(Variable ssl |
189207
hasNoEndpointIdentificationSet(this, ssl) and //Not set in itself
@@ -208,12 +226,12 @@ class RabbitMQEnableHostnameVerificationNotSet extends MethodAccess {
208226
RabbitMQEnableHostnameVerificationNotSet() {
209227
this.getMethod().hasName("useSslProtocol") and
210228
this.getMethod().getDeclaringType() instanceof RabbitMQConnectionFactory and
211-
exists(VarAccess va |
212-
va.getVariable().getType() instanceof RabbitMQConnectionFactory and
213-
this.getQualifier() = va.getVariable().getAnAccess() and
229+
exists(Variable v |
230+
v.getType() instanceof RabbitMQConnectionFactory and
231+
this.getQualifier() = v.getAnAccess() and
214232
not exists(MethodAccess ma |
215233
ma.getMethod().hasName("enableHostnameVerification") and
216-
ma.getQualifier() = va.getVariable().getAnAccess()
234+
ma.getQualifier() = v.getAnAccess()
217235
)
218236
)
219237
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
| UnsafeCertTrustTest.java:26:4:26:74 | init(...) | Unsafe configuration of trusted certificates |
2-
| UnsafeCertTrustTest.java:41:4:41:38 | init(...) | Unsafe configuration of trusted certificates |
3-
| UnsafeCertTrustTest.java:54:3:59:4 | setDefaultHostnameVerifier(...) | Unsafe configuration of trusted certificates |
4-
| UnsafeCertTrustTest.java:72:3:72:57 | setDefaultHostnameVerifier(...) | Unsafe configuration of trusted certificates |
5-
| UnsafeCertTrustTest.java:123:25:123:52 | createSSLEngine(...) | Unsafe configuration of trusted certificates |
6-
| UnsafeCertTrustTest.java:134:25:134:52 | createSSLEngine(...) | Unsafe configuration of trusted certificates |
7-
| UnsafeCertTrustTest.java:143:34:143:83 | createSocket(...) | Unsafe configuration of trusted certificates |
1+
| UnsafeCertTrustTest.java:27:4:27:74 | init(...) | Unsafe configuration of trusted certificates |
2+
| UnsafeCertTrustTest.java:42:4:42:38 | init(...) | Unsafe configuration of trusted certificates |
3+
| UnsafeCertTrustTest.java:55:3:60:4 | setDefaultHostnameVerifier(...) | Unsafe configuration of trusted certificates |
4+
| UnsafeCertTrustTest.java:73:3:73:57 | setDefaultHostnameVerifier(...) | Unsafe configuration of trusted certificates |
5+
| UnsafeCertTrustTest.java:124:25:124:52 | createSSLEngine(...) | Unsafe configuration of trusted certificates |
6+
| UnsafeCertTrustTest.java:135:25:135:52 | createSSLEngine(...) | Unsafe configuration of trusted certificates |
7+
| UnsafeCertTrustTest.java:144:34:144:83 | createSocket(...) | Unsafe configuration of trusted certificates |

java/ql/test/experimental/query-tests/security/CWE-273/UnsafeCertTrustTest.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import javax.net.ssl.X509TrustManager;
1111

1212
import java.net.Socket;
13+
import javax.net.SocketFactory;
1314
import java.security.cert.CertificateException;
1415
import java.security.cert.X509Certificate;
1516

@@ -126,7 +127,7 @@ public void testSSLEngineEndpointIdSetNull() {
126127
sslEngine.setSSLParameters(sslParameters);
127128
}
128129

129-
/**
130+
/**
130131
* Test the endpoint identification of SSL engine is not set
131132
*/
132133
public void testSSLEngineEndpointIdNotSet() {
@@ -143,11 +144,19 @@ public void testSSLSocketEndpointIdNotSet() {
143144
SSLSocket socket = (SSLSocket) socketFactory.createSocket("www.example.com", 443);
144145
}
145146

147+
/**
148+
* Test the endpoint identification of regular socket is not set
149+
*/
150+
public void testSocketEndpointIdNotSet() {
151+
SocketFactory socketFactory = SocketFactory.getDefault();
152+
Socket socket = socketFactory.createSocket("www.example.com", 80);
153+
}
154+
146155
// /**
147-
// * Test the enableHostnameVerification of RabbitMQConnectionFactory is not set
148-
// */
156+
// * Test the enableHostnameVerification of RabbitMQConnectionFactory is not set
157+
// */
149158
// public void testEnableHostnameVerificationOfRabbitMQFactoryNotSet() {
150-
// ConnectionFactory connectionFactory = new ConnectionFactory();
151-
// connectionFactory.useSslProtocol();
159+
// ConnectionFactory connectionFactory = new ConnectionFactory();
160+
// connectionFactory.useSslProtocol();
152161
// }
153162
}

0 commit comments

Comments
 (0)