Skip to content

Commit 232c27d

Browse files
authored
Merge pull request ESAPI#712 from noloader/develop
Add forward slash encoding to DefaultEncoder's encodeForLDAP and encodeForDN
2 parents 284066b + 0078dc0 commit 232c27d

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,13 +305,22 @@ public String encodeForLDAP(String input, boolean encodeWildcards) {
305305
}
306306
// TODO: replace with LDAP codec
307307
StringBuilder sb = new StringBuilder();
308+
// According to Microsoft docs [1,2], the forward slash ('/') MUST be escaped.
309+
// According to RFC 4513 Section 3 [3], the forward slash (and other characters) MAY be escaped.
310+
// Since Microsoft is a MUST, escape forward slash for all implementations. Also see discussion at [4].
311+
// [1] https://docs.microsoft.com/en-us/windows/win32/adsi/search-filter-syntax
312+
// [2] https://social.technet.microsoft.com/wiki/contents/articles/5312.active-directory-characters-to-escape.aspx
313+
// [3] https://tools.ietf.org/search/rfc4515#section-3
314+
// [4] https://lists.openldap.org/hyperkitty/list/[email protected]/thread/3QPDDLO356ONSJM3JUKD7NMPOOIKIQ5T/
308315
for (int i = 0; i < input.length(); i++) {
309316
char c = input.charAt(i);
310-
311317
switch (c) {
312318
case '\\':
313319
sb.append("\\5c");
314320
break;
321+
case '/':
322+
sb.append("\\2f");
323+
break;
315324
case '*':
316325
if (encodeWildcards) {
317326
sb.append("\\2a");
@@ -349,12 +358,16 @@ public String encodeForDN(String input) {
349358
if ((input.length() > 0) && ((input.charAt(0) == ' ') || (input.charAt(0) == '#'))) {
350359
sb.append('\\'); // add the leading backslash if needed
351360
}
361+
// See discussion of forward slash ('/') in encodeForLDAP()
352362
for (int i = 0; i < input.length(); i++) {
353363
char c = input.charAt(i);
354364
switch (c) {
355365
case '\\':
356366
sb.append("\\\\");
357367
break;
368+
case '/':
369+
sb.append("\\/");
370+
break;
358371
case ',':
359372
sb.append("\\,");
360373
break;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,7 @@ public void testEncodeForLDAP() {
535535
assertEquals("Zeros", "Hi \\00", instance.encodeForLDAP("Hi \u0000"));
536536
assertEquals("LDAP Christams Tree", "Hi \\28This\\29 = is \\2a a \\5c test # � � �", instance.encodeForLDAP("Hi (This) = is * a \\ test # � � �"));
537537
assertEquals("Hi \\28This\\29 =", instance.encodeForLDAP("Hi (This) ="));
538+
assertEquals("Forward slash for \\2fMicrosoft\\2f \\2fAD\\2f", instance.encodeForLDAP("Forward slash for /Microsoft/ /AD/"));
538539
}
539540

540541
/**
@@ -547,6 +548,7 @@ public void testEncodeForLDAPWithoutEncodingWildcards() {
547548
assertEquals("No special characters to escape", "Hi This is a test #��", instance.encodeForLDAP("Hi This is a test #��", false));
548549
assertEquals("Zeros", "Hi \\00", instance.encodeForLDAP("Hi \u0000", false));
549550
assertEquals("LDAP Christams Tree", "Hi \\28This\\29 = is * a \\5c test # � � �", instance.encodeForLDAP("Hi (This) = is * a \\ test # � � �", false));
551+
assertEquals("Forward slash for \\2fMicrosoft\\2f \\2fAD\\2f", instance.encodeForLDAP("Forward slash for /Microsoft/ /AD/"));
550552
}
551553

552554
/**
@@ -563,6 +565,7 @@ public void testEncodeForDN() {
563565
assertEquals("less than greater than", "Hello\\<\\>", instance.encodeForDN("Hello<>"));
564566
assertEquals("only 3 spaces", "\\ \\ ", instance.encodeForDN(" "));
565567
assertEquals("Christmas Tree DN", "\\ Hello\\\\ \\+ \\, \\\"World\\\" \\;\\ ", instance.encodeForDN(" Hello\\ + , \"World\" ; "));
568+
assertEquals("Forward slash for \\/Microsoft\\/ \\/AD\\/", instance.encodeForDN("Forward slash for /Microsoft/ /AD/"));
566569
}
567570

568571
/**

0 commit comments

Comments
 (0)