Skip to content

Commit 3a4443c

Browse files
committed
[misc] ensure not loading static class of wrong socketFactory implementation
1 parent 5e825c8 commit 3a4443c

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

src/main/java/org/mariadb/jdbc/client/impl/ConnectionHelper.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,13 @@ public static Socket standardSocket(Configuration conf, HostAddress hostAddress)
8484
if (socketFactoryName != null) {
8585
if (hostAddress == null) throw new SQLException("hostname must be set to connect socket");
8686
try {
87-
@SuppressWarnings("unchecked")
88-
Class<? extends SocketFactory> socketFactoryClass =
89-
(Class<? extends SocketFactory>) Class.forName(socketFactoryName);
87+
Class<SocketFactory> socketFactoryClass =
88+
(Class<SocketFactory>)
89+
Class.forName(socketFactoryName, false, ConnectionHelper.class.getClassLoader());
90+
if (!SocketFactory.class.isAssignableFrom(socketFactoryClass)) {
91+
throw new IOException(
92+
"Wrong Socket factory implementation '" + conf.socketFactory() + "'");
93+
}
9094
Constructor<? extends SocketFactory> constructor = socketFactoryClass.getConstructor();
9195
socketFactory = constructor.newInstance();
9296
if (socketFactory instanceof ConfigurableSocketFactory) {

src/test/java/org/mariadb/jdbc/integration/ConnectionTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,8 @@ public void localSocket() throws Exception {
975975
stmt.execute("DROP USER testSocket");
976976
}
977977

978+
static public int staticTestValue = 0;
979+
978980
@Test
979981
public void socketFactoryTest() throws SQLException {
980982
try (Connection conn = createCon("socketFactory=" + SocketFactoryBasicTest.class.getName())) {
@@ -989,6 +991,12 @@ public void socketFactoryTest() throws SQLException {
989991
SQLNonTransientConnectionException.class,
990992
() -> createCon("socketFactory=wrongClass"),
991993
"Socket factory failed to initialized with option \"socketFactory\" set to \"wrongClass\"");
994+
assertEquals(0, staticTestValue);
995+
Common.assertThrowsContains(
996+
SQLNonTransientConnectionException.class,
997+
() -> createCon("socketFactory=org.mariadb.jdbc.integration.util.WrongSocketFactoryTest"),
998+
"Socket factory failed to initialized with option \"socketFactory\" set to \"org.mariadb.jdbc.integration.util.WrongSocketFactoryTest\"");
999+
assertEquals(0, staticTestValue);
9921000
}
9931001

9941002
@Test
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// SPDX-License-Identifier: LGPL-2.1-or-later
2+
// Copyright (c) 2012-2014 Monty Program Ab
3+
// Copyright (c) 2015-2023 MariaDB Corporation Ab
4+
package org.mariadb.jdbc.integration.util;
5+
6+
import org.mariadb.jdbc.integration.ConnectionTest;
7+
8+
public class WrongSocketFactoryTest {
9+
10+
static {
11+
ConnectionTest.staticTestValue = 50;
12+
}
13+
14+
public WrongSocketFactoryTest() {
15+
ConnectionTest.staticTestValue = 100;
16+
}
17+
}

0 commit comments

Comments
 (0)