You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by jo...@apache.org on 2020/03/31 01:22:26 UTC

[druid] 01/03: Improve search string handling

This is an automated email from the ASF dual-hosted git repository.

jonwei pushed a commit to branch 0.17.1
in repository https://gitbox.apache.org/repos/asf/druid.git

commit 57e40e27884c905e91378e8e4ef34b48ce6add84
Author: jon-wei <jo...@imply.io>
AuthorDate: Mon Mar 23 18:59:07 2020 -0700

    Improve search string handling
---
 LICENSE                                            |  7 ++++
 .../validator/LDAPCredentialsValidator.java        | 47 +++++++++++++++++++++-
 .../validator/LDAPCredentialsValidatorTest.java    | 47 ++++++++++++++++++++++
 licenses.yaml                                      | 11 +++++
 licenses/src/esapi.BSD3                            | 11 +++++
 5 files changed, 122 insertions(+), 1 deletion(-)

diff --git a/LICENSE b/LICENSE
index c747d40..820819d 100644
--- a/LICENSE
+++ b/LICENSE
@@ -307,6 +307,13 @@ SOURCE/JAVA-CORE
      which is available under a BSD-3-Clause License. For details, see licenses/src/porter-stemmer.BSD3.
       * processing/src/test/java/org/apache/druid/query/extraction/JavaScriptExtractionFnTest.java
 
+SOURCE/EXTENSIONS/druid-basic-security
+    This product contains an LDAP string encoding function from OWASP ESAPI, copyright The OWASP Foundation
+     (https://github.com/ESAPI/esapi-java-legacy) which is available under the BSD-3-Clause License. For details, see
+     licenses/src/esapi.BSD3.
+      * extensions-core/druid-basic-security/src/main/java/org/apache/druid/security/basic/authentication/validator/LDAPCredentialsValidator.java
+
+
 Public Domain
 ================================
 
diff --git a/extensions-core/druid-basic-security/src/main/java/org/apache/druid/security/basic/authentication/validator/LDAPCredentialsValidator.java b/extensions-core/druid-basic-security/src/main/java/org/apache/druid/security/basic/authentication/validator/LDAPCredentialsValidator.java
index 9f75d37..7a9bc0c 100644
--- a/extensions-core/druid-basic-security/src/main/java/org/apache/druid/security/basic/authentication/validator/LDAPCredentialsValidator.java
+++ b/extensions-core/druid-basic-security/src/main/java/org/apache/druid/security/basic/authentication/validator/LDAPCredentialsValidator.java
@@ -189,9 +189,10 @@ public class LDAPCredentialsValidator implements CredentialsValidator
       SearchControls sc = new SearchControls();
       sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
       sc.setReturningAttributes(new String[] {ldapConfig.getUserAttribute(), "memberOf" });
+      String encodedUsername = encodeForLDAP(username, true);
       NamingEnumeration<SearchResult> results = context.search(
           ldapConfig.getBaseDn(),
-          StringUtils.format(ldapConfig.getUserSearch(), username),
+          StringUtils.format(ldapConfig.getUserSearch(), encodedUsername),
           sc);
       try {
         if (!results.hasMore()) {
@@ -293,4 +294,48 @@ public class LDAPCredentialsValidator implements CredentialsValidator
       }
     }
   }
+
+  /**
+   * This code is adapted from DefaultEncoder from version 2.2.0.0 of ESAPI (https://github.com/ESAPI/esapi-java-legacy)
+   */
+  public static String encodeForLDAP(String input, boolean encodeWildcards)
+  {
+    if (input == null) {
+      return null;
+    }
+    StringBuilder sb = new StringBuilder();
+    for (int i = 0; i < input.length(); i++) {
+      char c = input.charAt(i);
+
+      switch (c) {
+        case '\\':
+          sb.append("\\5c");
+          break;
+        case '/':
+          sb.append(("\\2f"));
+          break;
+        case '*':
+          if (encodeWildcards) {
+            sb.append("\\2a");
+          } else {
+            sb.append(c);
+          }
+
+          break;
+        case '(':
+          sb.append("\\28");
+          break;
+        case ')':
+          sb.append("\\29");
+          break;
+        case '\0':
+          sb.append("\\00");
+          break;
+        default:
+          sb.append(c);
+      }
+    }
+    return sb.toString();
+  }
+
 }
diff --git a/extensions-core/druid-basic-security/src/test/java/org/apache/druid/security/authentication/validator/LDAPCredentialsValidatorTest.java b/extensions-core/druid-basic-security/src/test/java/org/apache/druid/security/authentication/validator/LDAPCredentialsValidatorTest.java
new file mode 100644
index 0000000..56aee16
--- /dev/null
+++ b/extensions-core/druid-basic-security/src/test/java/org/apache/druid/security/authentication/validator/LDAPCredentialsValidatorTest.java
@@ -0,0 +1,47 @@
+/*
+ * 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.druid.security.authentication.validator;
+
+import org.apache.druid.security.basic.authentication.validator.LDAPCredentialsValidator;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class LDAPCredentialsValidatorTest
+{
+  @Test
+  public void testEncodeForLDAP_noSpecialChars()
+  {
+    String input = "user1";
+    String encoded = LDAPCredentialsValidator.encodeForLDAP(input, true);
+    Assert.assertEquals(input, encoded);
+  }
+
+  @Test
+  public void testEncodeForLDAP_specialChars()
+  {
+    String input = "user1\\*()\0/user1";
+    String encodedWildcardTrue = LDAPCredentialsValidator.encodeForLDAP(input, true);
+    String encodedWildcardFalse = LDAPCredentialsValidator.encodeForLDAP(input, false);
+    String expectedWildcardTrue = "user1\\5c\\2a\\28\\29\\00\\2fuser1";
+    String expectedWildcardFalse = "user1\\5c*\\28\\29\\00\\2fuser1";
+    Assert.assertEquals(expectedWildcardTrue, encodedWildcardTrue);
+    Assert.assertEquals(expectedWildcardFalse, encodedWildcardFalse);
+  }
+}
diff --git a/licenses.yaml b/licenses.yaml
index cc0a403..4c35679 100644
--- a/licenses.yaml
+++ b/licenses.yaml
@@ -136,6 +136,17 @@ source_paths:
 
 ---
 
+name: LDAP string encoding function from OWASP ESAPI
+license_category: source
+module: extensions/druid-basic-security
+license_name: BSD-3-Clause License
+copyright: The OWASP Foundation (https://github.com/ESAPI/esapi-java-legacy)
+license_file_path: licenses/src/esapi.BSD3
+source_paths:
+  - extensions-core/druid-basic-security/src/main/java/org/apache/druid/security/basic/authentication/validator/LDAPCredentialsValidator.java
+
+---
+
 name: AWS SDK for Java
 license_category: binary
 module: java-core
diff --git a/licenses/src/esapi.BSD3 b/licenses/src/esapi.BSD3
new file mode 100644
index 0000000..fdbcc85
--- /dev/null
+++ b/licenses/src/esapi.BSD3
@@ -0,0 +1,11 @@
+The BSD License
+
+Copyright (c) 2007, The OWASP Foundation
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+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.
+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.
+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 PROFI [...]


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org