Skip to content

Commit 26911a1

Browse files
JoelRabinovitchkwwall
authored andcommitted
* Issue ESAPI#389 Overloaded the encodeForLDAP method to provide the option of not encoding wildcard (*) characters. This would be used when doing queries against an LDAP directory using wildcards, while at the same time, encoding other potentially dangerous characters. * Issue ESAPI#389 Renamed the escapeWildcards variable to encodeWildcards to be consistent with the interface definition.
1 parent 8ae840f commit 26911a1

File tree

3 files changed

+52
-21
lines changed

3 files changed

+52
-21
lines changed

src/main/java/org/owasp/esapi/Encoder.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ public interface Encoder {
369369
String encodeForOS(Codec codec, String input);
370370

371371
/**
372-
* Encode data for use in LDAP queries.
372+
* Encode data for use in LDAP queries. Wildcard (*) characters will be encoded.
373373
*
374374
* @param input
375375
* the text to encode for LDAP
@@ -378,6 +378,18 @@ public interface Encoder {
378378
*/
379379
String encodeForLDAP(String input);
380380

381+
/**
382+
* Encode data for use in LDAP queries. You have the option whether or not to encode wildcard (*) characters.
383+
*
384+
* @param input
385+
* the text to encode for LDAP
386+
* @param encodeWildcards
387+
* whether or not wildcard (*) characters will be encoded.
388+
*
389+
* @return input encoded for use in LDAP
390+
*/
391+
String encodeForLDAP(String input, boolean encodeWildcards);
392+
381393
/**
382394
* Encode data for use in an LDAP distinguished name.
383395
*

src/main/java/org/owasp/esapi/reference/DefaultEncoder.java

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -281,32 +281,39 @@ public String encodeForOS(Codec codec, String input) {
281281
* {@inheritDoc}
282282
*/
283283
public String encodeForLDAP(String input) {
284+
return encodeForLDAP(input, true);
285+
}
286+
287+
/**
288+
* {@inheritDoc}
289+
*/
290+
public String encodeForLDAP(String input, boolean encodeWildcards) {
284291
if( input == null ) {
285292
return null;
286293
}
287294
// TODO: replace with LDAP codec
288295
StringBuilder sb = new StringBuilder();
289296
for (int i = 0; i < input.length(); i++) {
290297
char c = input.charAt(i);
291-
switch (c) {
292-
case '\\':
293-
sb.append("\\5c");
294-
break;
295-
case '*':
296-
sb.append("\\2a");
297-
break;
298-
case '(':
299-
sb.append("\\28");
300-
break;
301-
case ')':
302-
sb.append("\\29");
303-
break;
304-
case '\0':
305-
sb.append("\\00");
306-
break;
307-
default:
308-
sb.append(c);
309-
}
298+
299+
if (c == '\\') {
300+
sb.append("\\5c");
301+
}
302+
else if ((c == '*') && encodeWildcards) {
303+
sb.append("\\2a");
304+
}
305+
else if (c == '(') {
306+
sb.append("\\28");
307+
}
308+
else if (c == ')') {
309+
sb.append("\\29");
310+
}
311+
else if (c == '\0') {
312+
sb.append("\\00");
313+
}
314+
else {
315+
sb.append(c);
316+
}
310317
}
311318
return sb.toString();
312319
}

src/test/java/org/owasp/esapi/reference/EncoderTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,19 @@ public void testEncodeForLDAP() {
471471
}
472472

473473
/**
474-
* Test of encodeForLDAP method, of class org.owasp.esapi.Encoder.
474+
* Test of encodeForLDAP method with without encoding wildcard characters, of class org.owasp.esapi.Encoder.
475+
*/
476+
public void testEncodeForLDAPWithoutEncodingWildcards() {
477+
System.out.println("encodeForLDAPWithoutEncodingWildcards");
478+
Encoder instance = ESAPI.encoder();
479+
assertEquals(null, instance.encodeForLDAP(null, false));
480+
assertEquals("No special characters to escape", "Hi This is a test #��", instance.encodeForLDAP("Hi This is a test #��", false));
481+
assertEquals("Zeros", "Hi \\00", instance.encodeForLDAP("Hi \u0000", false));
482+
assertEquals("LDAP Christams Tree", "Hi \\28This\\29 = is * a \\5c test # � � �", instance.encodeForLDAP("Hi (This) = is * a \\ test # � � �", false));
483+
}
484+
485+
/**
486+
* Test of encodeForDN method, of class org.owasp.esapi.Encoder.
475487
*/
476488
public void testEncodeForDN() {
477489
System.out.println("encodeForDN");

0 commit comments

Comments
 (0)