KEYCLOAK-19183

LDAPDn should use a static Pattern instead calling String.split with a regex
This commit is contained in:
bohmber 2021-09-03 12:49:25 +02:00 committed by Marek Posolda
parent c7f8544b0c
commit 0c64d32b9b

View file

@ -22,6 +22,7 @@ import java.util.Collection;
import java.util.Deque; import java.util.Deque;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -29,6 +30,10 @@ import java.util.stream.Collectors;
*/ */
public class LDAPDn { public class LDAPDn {
private static final Pattern DN_PATTERN = Pattern.compile("(?<!\\\\),");
private static final Pattern ENTRY_PATTERN = Pattern.compile("(?<!\\\\)\\+");
private static final Pattern SUB_ENTRY_PATTERN = Pattern.compile("(?<!\\\\)=");
private final Deque<RDN> entries; private final Deque<RDN> entries;
private LDAPDn() { private LDAPDn() {
@ -49,7 +54,7 @@ public class LDAPDn {
if(dnString.trim().isEmpty()) if(dnString.trim().isEmpty())
return dn; return dn;
String[] rdns = dnString.split("(?<!\\\\),"); String[] rdns = DN_PATTERN.split(dnString);
for (String entryStr : rdns) { for (String entryStr : rdns) {
if (entryStr.indexOf('+') == -1) { if (entryStr.indexOf('+') == -1) {
// This is 99.9% of cases where RDN consists of single key-value pair // This is 99.9% of cases where RDN consists of single key-value pair
@ -57,7 +62,7 @@ public class LDAPDn {
dn.addLast(new RDN(subEntry)); dn.addLast(new RDN(subEntry));
} else { } else {
// This is 0.1% of cases where RDN consists of more key-value pairs like "uid=foo+cn=bar" // This is 0.1% of cases where RDN consists of more key-value pairs like "uid=foo+cn=bar"
String[] subEntries = entryStr.split("(?<!\\\\)\\+"); String[] subEntries = ENTRY_PATTERN.split(entryStr);
RDN entry = new RDN(); RDN entry = new RDN();
for (String subEntryStr : subEntries) { for (String subEntryStr : subEntries) {
SubEntry subEntry = parseSingleSubEntry(dn, subEntryStr); SubEntry subEntry = parseSingleSubEntry(dn, subEntryStr);
@ -72,7 +77,7 @@ public class LDAPDn {
// parse single sub-entry and add it to the "dn" . Assumption is that subentry is something like "uid=bar" and does not contain + character // parse single sub-entry and add it to the "dn" . Assumption is that subentry is something like "uid=bar" and does not contain + character
private static SubEntry parseSingleSubEntry(LDAPDn dn, String subEntryStr) { private static SubEntry parseSingleSubEntry(LDAPDn dn, String subEntryStr) {
String[] rdn = subEntryStr.split("(?<!\\\\)="); String[] rdn = SUB_ENTRY_PATTERN.split(subEntryStr);
if (rdn.length >1) { if (rdn.length >1) {
return new SubEntry(rdn[0].trim(), rdn[1].trim()); return new SubEntry(rdn[0].trim(), rdn[1].trim());
} else { } else {