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
refactor(ldap): enhance error handling for LDAP referrals
Victor Hugo dos Santosc0dc3f4ca118
diff --git a/backend/src/ee/services/secret-rotation-v2/ldap-password/ldap-password-rotation-fns.ts b/backend/src/ee/services/secret-rotation-v2/ldap-password/ldap-password-rotation-fns.tsindex 092324a76a..d5f954033b 100644--- a/backend/src/ee/services/secret-rotation-v2/ldap-password/ldap-password-rotation-fns.ts+++ b/backend/src/ee/services/secret-rotation-v2/ldap-password/ldap-password-rotation-fns.ts@@ -14,8 +14,10 @@ import {buildReferralUrl,executeWithPotentialGateway,extractDomainFromDN,+ isLdapReferral,isLdapReferralError,LdapProvider,+ LdapReferralError,TLdapConnection} from "@app/services/app-connection/ldap";@@ -45,11 +47,32 @@ const getDN = async (dn: string, client: Client): Promise<string> => {.join(",");return new Promise((resolve, reject) => {- // Perform the search+ const handleSearchError = (error: Error) => {+ // Search referrals (code 10) have err.dn === null, unlike modify referrals.+ // We attach the search base DN so the upstream referral chase loop can+ // extract the target domain and redirect to the correct DC.+ if (isLdapReferral(error)) {+ const ldapErr = error as Partial<LdapReferralError> & Error;+ if (!ldapErr.dn || typeof ldapErr.dn !== "string") {+ ldapErr.dn = base;+ }+ ldapErr.referralSource = "search";+ logger.info(+ { searchBase: base, upn: dn, referralDn: ldapErr.dn },+ "LDAP DN resolution received referral — propagating for upstream chase"+ );+ reject(error);+ return;+ }++ logger.error(error, "LDAP Failed to get DN");+ reject(new Error(`Provider Resolve DN Error: ${error.message}`));+ };+client.search(base, opts, (err, res) => {if (err) {- logger.error(err, "LDAP Failed to get DN");- reject(new Error(`Provider Resolve DN Error: ${err.message}`));+ handleSearchError(err);+ return;}let userDn: string | null;@@ -59,8 +82,7 @@ const getDN = async (dn: string, client: Client): Promise<string> => {});res.on("error", (error) => {- logger.error(error, "LDAP Failed to get DN");- reject(new Error(`Provider Resolve DN Error: ${error.message}`));+ handleSearchError(error);});res.on("end", () => {@@ -241,15 +263,16 @@ export const ldapPasswordRotationFactory: TRotationFactory<throw new Error(`${prefix} — ${errObj.message}`);}- const referralDn = String((caughtErr as { dn: string })?.dn);- const referralCode = Number((caughtErr as { code: number })?.code);- const referralName = String((caughtErr as { name: string })?.name);+ const referralErr = caughtErr;+ const referralSource = referralErr.referralSource === "search" ? "search" : "modify";+ const { dn: referralDn, code: referralCode, name: referralName } = referralErr;if (hop === MAX_REFERRAL_HOPS) {logger.error({...rotationContext,maxHops: MAX_REFERRAL_HOPS,+ referralSource,lastReferralDn: referralDn,lastUrl: currentCredentials.url},@@ -267,13 +290,14 @@ export const ldapPasswordRotationFactory: TRotationFactory<{...rotationContext,hop: hop + 1,+ referralSource,rawErrorDn: referralDn,rawErrorCode: referralCode,rawErrorName: referralName,extractedReferralDomain: referralDomain,currentUrl: currentCredentials.url},- "LDAP referral received — inspecting error details"+ `LDAP referral received during ${referralSource} — inspecting error details`);if (!referralDomain) {@@ -291,12 +315,13 @@ export const ldapPasswordRotationFactory: TRotationFactory<{...rotationContext,hop: hop + 1,+ referralSource,referralDomain,referredUrl,matchedDn: referralDn,originalUrl: credentials.url},- "LDAP referral detected — chasing to referred domain controller"+ `LDAP referral detected during ${referralSource} — chasing to referred domain controller`);currentCredentials = { ...connectionCredentials, url: referredUrl };diff --git a/backend/src/services/app-connection/ldap/ldap-connection-fns.ts b/backend/src/services/app-connection/ldap/ldap-connection-fns.tsindex a4ee17e945..fb25667e84 100644--- a/backend/src/services/app-connection/ldap/ldap-connection-fns.ts+++ b/backend/src/services/app-connection/ldap/ldap-connection-fns.ts@@ -33,15 +33,30 @@ export const extractDomainFromDN = (dn: string): string | null => {return dcComponents.length > 0 ? dcComponents.join(".") : null;};+export type LdapReferralError = Error & {+ dn: string;+ code: number;+ referralSource?: "search" | "modify";+};+/**- * Checks if an error is an LDAP Referral (code 10).- * ldapjs surfaces this as a ReferralError with `dn` set to the matched DN.+ * Checks if an error is an LDAP Referral (code 10) without requiring `dn` to be populated.+ * Useful for search referrals where ldapjs sets `dn` to null.*/-export const isLdapReferralError = (err: unknown): err is Error & { dn: string; code: number } => {+export const isLdapReferral = (err: unknown): boolean => {if (!(err instanceof Error)) return false;- const ldapErr = err as Error & { name?: string; code?: number; dn?: unknown };- const isReferral = ldapErr.name === "ReferralError" || ldapErr.code === 10;- return isReferral && typeof ldapErr.dn === "string";+ const ldapErr = err as Error & { name?: string; code?: number };+ return ldapErr.name === "ReferralError" || ldapErr.code === 10;+};++/**+ * Checks if an error is an LDAP Referral (code 10) with a populated `dn`.+ * ldapjs surfaces this as a ReferralError with `dn` set to the matched DN.+ */+export const isLdapReferralError = (err: unknown): err is LdapReferralError => {+ if (!isLdapReferral(err)) return false;+ const ldapErr = err as Error & { dn?: unknown };+ return typeof ldapErr.dn === "string";};const parseLdapUrl = (url: string): { protocol: string; host: string; port: number } => {@@ -62,7 +77,7 @@ const constructLdapUrl = (protocol: string, host: string, port: number): string/*** Extracts the root domain (last two labels) from a hostname.- * e.g. "dc1.gap.com" → "gap.com", "americas.infisical.local" → "infisical.local"+ * e.g. "dc1.test.com" → "test.com", "americas.infisical.local" → "infisical.local"* Returns null for IPs or single-label hostnames where a suffix check isn't meaningful.*/const getRootDomain = (hostname: string): string | null => {