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): improve error handling and domain validation in LDAP functions
Victor Hugo dos Santos6c37d19aacad
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 5c3e21aae1..092324a76a 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@@ -241,12 +241,9 @@ export const ldapPasswordRotationFactory: TRotationFactory<throw new Error(`${prefix} — ${errObj.message}`);}- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access- const referralDn = String((caughtErr as { dn: string }).dn);- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access- const referralCode = Number((caughtErr as { code: number }).code);- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access- const referralName = String((caughtErr as { name: string }).name);+ const referralDn = String((caughtErr as { dn: string })?.dn);+ const referralCode = Number((caughtErr as { code: number })?.code);+ const referralName = String((caughtErr as { name: string })?.name);if (hop === MAX_REFERRAL_HOPS) {logger.error(diff --git a/backend/src/services/app-connection/ldap/ldap-connection-fns.ts b/backend/src/services/app-connection/ldap/ldap-connection-fns.tsindex 99b4a829d5..a4ee17e945 100644--- a/backend/src/services/app-connection/ldap/ldap-connection-fns.ts+++ b/backend/src/services/app-connection/ldap/ldap-connection-fns.ts@@ -1,5 +1,6 @@+import { isIP } from "node:net";+import ldap from "ldapjs";-import RE2 from "re2";import { TGatewayServiceFactory } from "@app/ee/services/gateway/gateway-service";import { TGatewayV2ServiceFactory } from "@app/ee/services/gateway-v2/gateway-v2-service";@@ -59,8 +60,6 @@ const constructLdapUrl = (protocol: string, host: string, port: number): stringreturn `${protocol}://${host}:${port}`;};-const IPV4_REGEX = new RE2(/^\d+\.\d+\.\d+\.\d+$/);-/*** Extracts the root domain (last two labels) from a hostname.* e.g. "dc1.gap.com" → "gap.com", "americas.infisical.local" → "infisical.local"@@ -69,7 +68,7 @@ const IPV4_REGEX = new RE2(/^\d+\.\d+\.\d+\.\d+$/);const getRootDomain = (hostname: string): string | null => {const parts = hostname.split(".");if (parts.length < 2) return null;- if (IPV4_REGEX.test(hostname)) return null;+ if (isIP(hostname) !== 0) return null;return parts.slice(-2).join(".").toLowerCase();};@@ -84,7 +83,7 @@ const getRootDomain = (hostname: string): string | null => {export const buildReferralUrl = (originalUrl: string, targetDomain: string): string => {const { protocol, host, port } = parseLdapUrl(originalUrl);- if (IPV4_REGEX.test(targetDomain)) {+ if (isIP(targetDomain) !== 0) {throw new Error(`Referral target '${targetDomain}' is an IP address — legitimate AD referrals use FQDNs. Refusing to forward credentials`);