Skip to content

Commit d9e98ce

Browse files
committed
Consider setSslContextFactory and fix tests
1 parent 4d20710 commit d9e98ce

File tree

5 files changed

+478
-8
lines changed

5 files changed

+478
-8
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,12 @@ private class SafeSetEndpointIdentificationAlgorithm extends MethodAccess {
121121
}
122122

123123
/**
124-
* A call to the method `useSslProtocol` on an instance of `com.rabbitmq.client.ConnectionFactory`
125-
* that doesn't set `enableHostnameVerification`.
124+
* A call to a method that enables SSL (`useSslProtocol` or `setSslContextFactory`)
125+
* on an instance of `com.rabbitmq.client.ConnectionFactory` that doesn't set `enableHostnameVerification`.
126126
*/
127127
class RabbitMQEnableHostnameVerificationNotSet extends MethodAccess {
128128
RabbitMQEnableHostnameVerificationNotSet() {
129-
this.getMethod().hasName("useSslProtocol") and
129+
this.getMethod().hasName(["useSslProtocol", "setSslContextFactory"]) and
130130
this.getMethod().getDeclaringType() instanceof RabbitMQConnectionFactory and
131131
exists(Variable v |
132132
v.getType() instanceof RabbitMQConnectionFactory and

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

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import java.net.Socket;
22
import java.nio.ByteBuffer;
3+
import java.security.NoSuchAlgorithmException;
34
import javax.net.SocketFactory;
45
import javax.net.ssl.SSLContext;
56
import javax.net.ssl.SSLEngine;
67
import javax.net.ssl.SSLParameters;
78
import javax.net.ssl.SSLSocket;
89
import javax.net.ssl.SSLSocketFactory;
910
import com.rabbitmq.client.ConnectionFactory;
11+
import com.rabbitmq.client.SslContextFactory;
1012

1113
public class UnsafeCertTrustTest {
1214

@@ -146,13 +148,39 @@ public void testSocketEndpointIdNotSet() throws Exception {
146148
}
147149

148150
public void testRabbitMQFactoryEnableHostnameVerificationNotSet() throws Exception {
149-
ConnectionFactory connectionFactory = new ConnectionFactory();
150-
connectionFactory.useSslProtocol(); // $hasUnsafeCertTrust
151+
{
152+
ConnectionFactory connectionFactory = new ConnectionFactory();
153+
connectionFactory.useSslProtocol(SSLContext.getDefault()); // $hasUnsafeCertTrust
154+
}
155+
{
156+
ConnectionFactory connectionFactory = new ConnectionFactory();
157+
connectionFactory.setSslContextFactory(new TestSslContextFactory()); // $hasUnsafeCertTrust
158+
}
151159
}
152160

153161
public void testRabbitMQFactorySafe() throws Exception {
154-
ConnectionFactory connectionFactory = new ConnectionFactory();
155-
connectionFactory.useSslProtocol(); // Safe
156-
connectionFactory.enableHostnameVerification();
162+
{
163+
ConnectionFactory connectionFactory = new ConnectionFactory();
164+
connectionFactory.useSslProtocol(SSLContext.getDefault()); // Safe
165+
connectionFactory.enableHostnameVerification();
166+
}
167+
{
168+
ConnectionFactory connectionFactory = new ConnectionFactory();
169+
connectionFactory.setSslContextFactory(new TestSslContextFactory()); // Safe
170+
connectionFactory.enableHostnameVerification();
171+
}
172+
}
173+
174+
static class TestSslContextFactory implements SslContextFactory {
175+
176+
@Override
177+
public SSLContext create(String name) {
178+
try {
179+
return SSLContext.getDefault();
180+
} catch (NoSuchAlgorithmException e) {
181+
return null;
182+
}
183+
}
184+
157185
}
158186
}

java/ql/test/stubs/amqp-client-5.12.0/com/rabbitmq/client/ConnectionFactory.java

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

java/ql/test/stubs/amqp-client-5.12.0/com/rabbitmq/client/SslContextFactory.java

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)