Skip to content

Commit 4f717df

Browse files
committed
Forbid LDAP/RMI from JndiHelper (#2414)
(cherry picked from commit 24e50ff)
1 parent f6c485b commit 4f717df

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/util/JndiHelper.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ public class JndiHelper {
3434
*/
3535
public JndiHelper(Properties environment) {
3636
this.environment = environment;
37+
38+
// Avoid unsafe protocols if they are somehow misconfigured
39+
String providerUrl = environment.getProperty(Context.PROVIDER_URL);
40+
if (providerUrl != null && (providerUrl.startsWith("ldap://")
41+
|| providerUrl.startsWith("rmi://"))) {
42+
throw new IllegalArgumentException("Unsafe protocol in JNDI URL: " + providerUrl);
43+
}
3744
}
3845

3946
@SuppressWarnings("unchecked")

rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/JMSConfigFactoryTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
package org.apache.cxf.transport.jms;
2121

22+
import java.util.Properties;
23+
24+
import javax.naming.Context;
2225
import javax.naming.NamingException;
2326
import javax.transaction.xa.XAException;
2427

@@ -35,6 +38,26 @@
3538

3639
public class JMSConfigFactoryTest extends AbstractJMSTester {
3740

41+
@Test
42+
public void testJndiForbiddenProtocol() throws Exception {
43+
Properties env = new Properties();
44+
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
45+
env.put(Context.PROVIDER_URL, "ldap://127.0.0.1:12345");
46+
// Allow following referrals (important for LDAP injection)
47+
env.put(Context.REFERRAL, "follow");
48+
49+
JMSConfiguration jmsConfig = new JMSConfiguration();
50+
jmsConfig.setJndiEnvironment(env);
51+
jmsConfig.setConnectionFactoryName("objectName");
52+
53+
try {
54+
jmsConfig.getConnectionFactory();
55+
Assert.fail("JNDI lookup should have failed");
56+
} catch (Exception e) {
57+
Assert.assertTrue(e.getMessage().contains("Unsafe protocol in JNDI URL"));
58+
}
59+
}
60+
3861
@Test
3962
public void testUsernameAndPassword() throws Exception {
4063
EndpointInfo ei = setupServiceInfo("HelloWorldService", "HelloWorldPort");

0 commit comments

Comments
 (0)