Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

fix(ldap): return false on test connection when the host does not resolve

Vlad Ligai82ad0b06969a
diff --git a/backend/src/ee/services/ldap-config/ldap-config-service.test.ts b/backend/src/ee/services/ldap-config/ldap-config-service.test.ts
index 87a944fb9b..2e2b89f2a3 100644
--- a/backend/src/ee/services/ldap-config/ldap-config-service.test.ts
+++ b/backend/src/ee/services/ldap-config/ldap-config-service.test.ts
@@ -1,3 +1,5 @@
+import dns from "node:dns/promises";
+
import { createMongoAbility } from "@casl/ability";
import { beforeEach, describe, expect, test, vi } from "vitest";
@@ -47,6 +49,15 @@ describe("testLDAPConnection", () => {
expect(testLDAPConfig).not.toHaveBeenCalled();
});
+ test("returns false without connecting when the hostname does not resolve", async () => {
+ vi.spyOn(dns, "lookup").mockRejectedValueOnce(
+ Object.assign(new Error("getaddrinfo ENOTFOUND ldap.example.com"), { code: "ENOTFOUND" })
+ );
+
+ await expect(testConnection("ldap://ldap.example.com:389")).resolves.toBe(false);
+ expect(testLDAPConfig).not.toHaveBeenCalled();
+ });
+
test("connects to a public address", async () => {
await expect(testConnection("ldap://8.8.8.8:389")).resolves.toBe(true);
expect(testLDAPConfig).toHaveBeenCalledWith(expect.objectContaining({ url: "ldap://8.8.8.8:389" }));
diff --git a/backend/src/ee/services/ldap-config/ldap-config-service.ts b/backend/src/ee/services/ldap-config/ldap-config-service.ts
index 7c0e3e9bf0..e96fce6336 100644
--- a/backend/src/ee/services/ldap-config/ldap-config-service.ts
+++ b/backend/src/ee/services/ldap-config/ldap-config-service.ts
@@ -1043,7 +1043,13 @@ export const ldapConfigServiceFactory = ({
message: "Failed to test LDAP connection due to plan restriction. Upgrade plan to test the LDAP connection."
});
- await blockLocalAndPrivateIpAddresses(url);
+ try {
+ await blockLocalAndPrivateIpAddresses(url);
+ } catch (error) {
+ if (error instanceof BadRequestError) throw error;
+ // an unresolvable host fails the same way the connection itself would
+ return false;
+ }
if (Boolean(clientCertificate) !== Boolean(clientKeyCertificate)) {
throw new BadRequestError({