Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
fix(ldap): match group CNs case-insensitively per LDAP semantics
Scott Wilson2b73532bc660
diff --git a/backend/src/ee/services/ldap-config/ldap-config-service.ts b/backend/src/ee/services/ldap-config/ldap-config-service.tsindex f860cbcd09..fd726d6102 100644--- a/backend/src/ee/services/ldap-config/ldap-config-service.ts+++ b/backend/src/ee/services/ldap-config/ldap-config-service.ts@@ -646,14 +646,15 @@ export const ldapConfigServiceFactory = ({let user = await userDAL.transaction(async (tx) => {const newUser = await userDAL.findOne({ id: userAlias.userId }, tx);if (groups && !isStaleAlias) {- const ldapGroupIdsToBePartOf = (- await ldapGroupMapDAL.find({- ldapConfigId,- $in: {- ldapGroupCN: groups.map((group) => group.cn)- }- })- ).map((groupMap) => groupMap.groupId);+ const allLdapGroupMaps = await ldapGroupMapDAL.find({+ ldapConfigId+ });++ // cn equality in LDAP is case-insensitive (caseIgnoreMatch).+ const userLdapGroupCns = new Set(groups.map((group) => group.cn.toLowerCase()));+ const ldapGroupIdsToBePartOf = allLdapGroupMaps+ .filter((groupMap) => userLdapGroupCns.has(groupMap.ldapGroupCN.toLowerCase()))+ .map((groupMap) => groupMap.groupId);const groupsToBePartOf = await groupDAL.find({orgId,@@ -663,10 +664,6 @@ export const ldapConfigServiceFactory = ({});const toBePartOfGroupIdsSet = new Set(groupsToBePartOf.map((groupToBePartOf) => groupToBePartOf.id));- const allLdapGroupMaps = await ldapGroupMapDAL.find({- ldapConfigId- });-const ldapGroupIdsCurrentlyPartOf = (await userGroupMembershipDAL.find({userId: newUser.id,@@ -887,7 +884,9 @@ export const ldapConfigServiceFactory = ({const groupSearchFilter = `(cn=${ldapGroupCN})`;const groups = await searchGroups(ldapConfig, groupSearchFilter, ldapConfig.groupSearchBase);- if (!groups.some((g) => g.cn === ldapGroupCN)) {+ // cn equality in LDAP is case-insensitive (caseIgnoreMatch).+ const matchedGroup = groups.find((g) => g.cn.toLowerCase() === ldapGroupCN.toLowerCase());+ if (!matchedGroup) {throw new NotFoundError({message: "Failed to find LDAP Group CN"});@@ -902,7 +901,7 @@ export const ldapConfigServiceFactory = ({const groupMap = await ldapGroupMapDAL.create({ldapConfigId,- ldapGroupCN,+ ldapGroupCN: matchedGroup.cn,groupId: group.id});diff --git a/backend/src/ee/services/ldap-config/ldap-fns.test.ts b/backend/src/ee/services/ldap-config/ldap-fns.test.tsnew file mode 100644index 0000000000..2b1e82fb7e--- /dev/null+++ b/backend/src/ee/services/ldap-config/ldap-fns.test.ts@@ -0,0 +1,53 @@+import { describe, expect, test } from "vitest";++import { extractCnFromDn } from "./ldap-fns";++describe("extractCnFromDn", () => {+ test("extracts CN from an Active Directory style DN (uppercase attribute types)", () => {+ expect(extractCnFromDn("CN=Infisical-Admins,OU=Groups,DC=corp,DC=example,DC=com")).toBe("Infisical-Admins");+ });++ test("extracts cn from an OpenLDAP style DN (lowercase attribute types)", () => {+ expect(extractCnFromDn("cn=engineering,ou=groups,dc=example,dc=com")).toBe("engineering");+ });++ test("extracts Cn regardless of mixed casing", () => {+ expect(extractCnFromDn("Cn=Team,DC=example,DC=com")).toBe("Team");+ });++ test("preserves the original casing of the CN value", () => {+ expect(extractCnFromDn("CN=MiXeDCaSe,OU=Groups,DC=example,DC=com")).toBe("MiXeDCaSe");+ });++ test("returns undefined when the DN has no CN RDN", () => {+ expect(extractCnFromDn("OU=Groups,DC=example,DC=com")).toBeUndefined();+ });++ test("extracts CN when it is not the leading RDN", () => {+ expect(extractCnFromDn("OU=Groups,CN=Nested,DC=example,DC=com")).toBe("Nested");+ });++ test("handles a single-RDN DN with no trailing comma", () => {+ expect(extractCnFromDn("cn=solo")).toBe("solo");+ });++ test("tolerates spaces after RDN separators", () => {+ expect(extractCnFromDn("CN=Team, OU=Groups, DC=example, DC=com")).toBe("Team");+ });++ test("does not match cn= inside another RDN's value", () => {+ expect(extractCnFromDn("OU=acn=trap,DC=example,DC=com")).toBeUndefined();+ });++ test("keeps ldapjs hex-escaped commas within the CN value intact", () => {+ // ldapjs DN.toString() serializes an escaped comma as \2c, so the value+ // contains no raw comma and the RDN split cannot cut it in half+ expect(extractCnFromDn("CN=Smith\\2c John,OU=Groups,DC=example,DC=com")).toBe("Smith\\2c John");+ });++ test("does not corrupt offsets when a preceding value contains uppercase Unicode that expands under case folding", () => {+ // "İ" (U+0130) lowercases to two code units, so whole-string toLowerCase()+ // approaches shift indexes and would extract a corrupted value here+ expect(extractCnFromDn("OU=İstanbul,CN=Admins,DC=example,DC=com")).toBe("Admins");+ });+});diff --git a/backend/src/ee/services/ldap-config/ldap-fns.ts b/backend/src/ee/services/ldap-config/ldap-fns.tsindex c1798c18bd..e1061b196c 100644--- a/backend/src/ee/services/ldap-config/ldap-fns.ts+++ b/backend/src/ee/services/ldap-config/ldap-fns.ts@@ -106,6 +106,19 @@ export const testLDAPConfig = async (ldapConfig: TTestLDAPConfigDTO): Promise<bo});};+/**+ * Extract the value of the first CN RDN from an LDAP DN string.+ * RFC 4514 attribute types are case-insensitive.+ */+export const extractCnFromDn = (dn: string): string | undefined => {+ const cnRdn = dn+ .split(",")+ .map((rdn) => rdn.trim())+ .find((rdn) => rdn.slice(0, 3).toLowerCase() === "cn=");++ return cnRdn?.substring(3);+};+/*** Search for groups in the LDAP server* @param ldapConfig - The LDAP configuration to use@@ -143,12 +156,9 @@ export const searchGroups = async (res.on("searchEntry", (entry) => {const dn = entry.dn.toString();- const cnStartIndex = dn.indexOf("cn=");+ const cn = extractCnFromDn(dn);- if (cnStartIndex !== -1) {- const valueStartIndex = cnStartIndex + 3;- const commaIndex = dn.indexOf(",", valueStartIndex);- const cn = dn.substring(valueStartIndex, commaIndex === -1 ? undefined : commaIndex);+ if (cn !== undefined) {groups.push({ dn, cn });}});