Skip to content

Commit 57e40e2

Browse files
committed
Improve search string handling
1 parent e2487bc commit 57e40e2

File tree

5 files changed

+122
-1
lines changed

5 files changed

+122
-1
lines changed

LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,13 @@ SOURCE/JAVA-CORE
307307
which is available under a BSD-3-Clause License. For details, see licenses/src/porter-stemmer.BSD3.
308308
* processing/src/test/java/org/apache/druid/query/extraction/JavaScriptExtractionFnTest.java
309309

310+
SOURCE/EXTENSIONS/druid-basic-security
311+
This product contains an LDAP string encoding function from OWASP ESAPI, copyright The OWASP Foundation
312+
(https://github.com/ESAPI/esapi-java-legacy) which is available under the BSD-3-Clause License. For details, see
313+
licenses/src/esapi.BSD3.
314+
* extensions-core/druid-basic-security/src/main/java/org/apache/druid/security/basic/authentication/validator/LDAPCredentialsValidator.java
315+
316+
310317
Public Domain
311318
================================
312319

extensions-core/druid-basic-security/src/main/java/org/apache/druid/security/basic/authentication/validator/LDAPCredentialsValidator.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,10 @@ SearchResult getLdapUserObject(BasicAuthLDAPConfig ldapConfig, DirContext contex
189189
SearchControls sc = new SearchControls();
190190
sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
191191
sc.setReturningAttributes(new String[] {ldapConfig.getUserAttribute(), "memberOf" });
192+
String encodedUsername = encodeForLDAP(username, true);
192193
NamingEnumeration<SearchResult> results = context.search(
193194
ldapConfig.getBaseDn(),
194-
StringUtils.format(ldapConfig.getUserSearch(), username),
195+
StringUtils.format(ldapConfig.getUserSearch(), encodedUsername),
195196
sc);
196197
try {
197198
if (!results.hasMore()) {
@@ -293,4 +294,48 @@ public LdapUserPrincipal put(String key, LdapUserPrincipal value)
293294
}
294295
}
295296
}
297+
298+
/**
299+
* This code is adapted from DefaultEncoder from version 2.2.0.0 of ESAPI (https://github.com/ESAPI/esapi-java-legacy)
300+
*/
301+
public static String encodeForLDAP(String input, boolean encodeWildcards)
302+
{
303+
if (input == null) {
304+
return null;
305+
}
306+
StringBuilder sb = new StringBuilder();
307+
for (int i = 0; i < input.length(); i++) {
308+
char c = input.charAt(i);
309+
310+
switch (c) {
311+
case '\\':
312+
sb.append("\\5c");
313+
break;
314+
case '/':
315+
sb.append(("\\2f"));
316+
break;
317+
case '*':
318+
if (encodeWildcards) {
319+
sb.append("\\2a");
320+
} else {
321+
sb.append(c);
322+
}
323+
324+
break;
325+
case '(':
326+
sb.append("\\28");
327+
break;
328+
case ')':
329+
sb.append("\\29");
330+
break;
331+
case '\0':
332+
sb.append("\\00");
333+
break;
334+
default:
335+
sb.append(c);
336+
}
337+
}
338+
return sb.toString();
339+
}
340+
296341
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.druid.security.authentication.validator;
21+
22+
import org.apache.druid.security.basic.authentication.validator.LDAPCredentialsValidator;
23+
import org.junit.Assert;
24+
import org.junit.Test;
25+
26+
public class LDAPCredentialsValidatorTest
27+
{
28+
@Test
29+
public void testEncodeForLDAP_noSpecialChars()
30+
{
31+
String input = "user1";
32+
String encoded = LDAPCredentialsValidator.encodeForLDAP(input, true);
33+
Assert.assertEquals(input, encoded);
34+
}
35+
36+
@Test
37+
public void testEncodeForLDAP_specialChars()
38+
{
39+
String input = "user1\\*()\0/user1";
40+
String encodedWildcardTrue = LDAPCredentialsValidator.encodeForLDAP(input, true);
41+
String encodedWildcardFalse = LDAPCredentialsValidator.encodeForLDAP(input, false);
42+
String expectedWildcardTrue = "user1\\5c\\2a\\28\\29\\00\\2fuser1";
43+
String expectedWildcardFalse = "user1\\5c*\\28\\29\\00\\2fuser1";
44+
Assert.assertEquals(expectedWildcardTrue, encodedWildcardTrue);
45+
Assert.assertEquals(expectedWildcardFalse, encodedWildcardFalse);
46+
}
47+
}

licenses.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,17 @@ source_paths:
136136

137137
---
138138

139+
name: LDAP string encoding function from OWASP ESAPI
140+
license_category: source
141+
module: extensions/druid-basic-security
142+
license_name: BSD-3-Clause License
143+
copyright: The OWASP Foundation (https://github.com/ESAPI/esapi-java-legacy)
144+
license_file_path: licenses/src/esapi.BSD3
145+
source_paths:
146+
- extensions-core/druid-basic-security/src/main/java/org/apache/druid/security/basic/authentication/validator/LDAPCredentialsValidator.java
147+
148+
---
149+
139150
name: AWS SDK for Java
140151
license_category: binary
141152
module: java-core

licenses/src/esapi.BSD3

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
The BSD License
2+
3+
Copyright (c) 2007, The OWASP Foundation
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7+
8+
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9+
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
10+
Neither the name of the OWASP Foundation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

0 commit comments

Comments
 (0)