Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore escaped commas when parsing active directory DNs #245

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public static String getRelativeDN(String distinguishedName,
}

public static String getParentDN(String distinguishedName) {
var dnParts = Arrays.stream(distinguishedName.split(",")).toList();
var commasIgnoringEscapedRegex = "(?<!\\\\),";
var dnParts = Arrays.stream(distinguishedName.split(commasIgnoringEscapedRegex)).toList();
if (dnParts.size() == 1) {
return "";
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ private static String getRDN(SearchResult object) {
}

private static String getRDN(String distinguishedName) {
return distinguishedName.split(",", 2)[0];
var commasIgnoringEscapedRegex = "(?<!\\\\),";
return distinguishedName.split(commasIgnoringEscapedRegex, 2)[0];
}

public List<SearchResult> getDirectChildrenObjects() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ private Optional<ActiveDirectoryNode> searchNode(String distinguishedName) {
if (relativeDn.isBlank()) {
return Optional.of(root);
}
var dnParts = Arrays.stream(relativeDn.split(",")).toList();
var commasIgnoringEscapedRegex = "(?<!\\\\),";
var dnParts = Arrays.stream(relativeDn.split(commasIgnoringEscapedRegex)).toList();
return searchNode(root, dnParts);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ private User getUserLeaderUnsafe(User user) throws NamingException {
private List<String> extractOrganizationalUnitsDNs(String distinguishedName) {
var result = new ArrayList<String>();

var components = distinguishedName.split(",");
var commasIgnoringEscapedRegex = "(?<!\\\\),";
var components = distinguishedName.split(commasIgnoringEscapedRegex);

for (var i = 0; i < components.length; i++) {
var component = components[i].trim();
if (component.startsWith("OU=")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ class ActiveDirectoryUtilsTest extends Specification {
result == parentDN

where:
actualDN | parentDN
"OU=SomeNestedTeam,OU=SomeTeam,OU=Teams,DC=fingo,DC=pl" | "OU=SomeTeam,OU=Teams,DC=fingo,DC=pl"
"OU=SomeTeam,OU=Teams,DC=fingo,DC=pl" | "OU=Teams,DC=fingo,DC=pl"
"OU=Teams" | ""
"" | ""
actualDN | parentDN
"OU=Some name with\\, comma,OU=SomeTeam,OU=Teams,DC=fingo,DC=pl" | "OU=SomeTeam,OU=Teams,DC=fingo,DC=pl"
"OU=SomeNestedTeam,OU=SomeTeam,OU=Teams,DC=fingo,DC=pl" | "OU=SomeTeam,OU=Teams,DC=fingo,DC=pl"
"OU=SomeTeam,OU=Teams,DC=fingo,DC=pl" | "OU=Teams,DC=fingo,DC=pl"
"OU=Teams" | ""
"" | ""
}

def "getRelativeDN WHEN base matches, ends with or differs SHOULD return correct relative DN"() {
Expand All @@ -28,14 +29,14 @@ class ActiveDirectoryUtilsTest extends Specification {
result == expectedRelativeDN

where:
distinguishedName | base | expectedRelativeDN
"OU=SomeNestedTeam,OU=SomeTeam,OU=Teams,DC=fingo,DC=pl"| "OU=SomeTeam,OU=Teams,DC=fingo,DC=pl"| "OU=SomeNestedTeam"
"OU=SomeTeam,OU=Teams,DC=fingo,DC=pl" | "OU=Teams,DC=fingo,DC=pl" | "OU=SomeTeam"
"OU=Teams,DC=fingo,DC=pl" | "OU=Teams,DC=fingo,DC=pl" | ""
"OU=RandomTeam,OU=SomeTeam,OU=Teams,DC=fingo,DC=pl" | "OU=SomeTeam,OU=Teams,DC=fingo,DC=pl"| "OU=RandomTeam"
"OU=Teams,DC=fingo,DC=pl" | "OU=DifferentBase,DC=fingo,DC=pl" | "OU=Teams,DC=fingo,DC=pl"
"" | "" | ""
"OU=SomeTeam,OU=Teams,DC=fingo,DC=pl" | "" | "OU=SomeTeam,OU=Teams,DC=fingo,DC=pl"
distinguishedName | base | expectedRelativeDN
"OU=SomeNestedTeam,OU=SomeTeam,OU=Teams,DC=fingo,DC=pl" | "OU=SomeTeam,OU=Teams,DC=fingo,DC=pl" | "OU=SomeNestedTeam"
"OU=SomeTeam,OU=Teams,DC=fingo,DC=pl" | "OU=Teams,DC=fingo,DC=pl" | "OU=SomeTeam"
"OU=Teams,DC=fingo,DC=pl" | "OU=Teams,DC=fingo,DC=pl" | ""
"OU=RandomTeam,OU=SomeTeam,OU=Teams,DC=fingo,DC=pl" | "OU=SomeTeam,OU=Teams,DC=fingo,DC=pl" | "OU=RandomTeam"
"OU=Teams,DC=fingo,DC=pl" | "OU=DifferentBase,DC=fingo,DC=pl" | "OU=Teams,DC=fingo,DC=pl"
"" | "" | ""
"OU=SomeTeam,OU=Teams,DC=fingo,DC=pl" | "" | "OU=SomeTeam,OU=Teams,DC=fingo,DC=pl"
}


Expand Down
Loading