Skip to content

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

Address codex comments

Carlos Monastyrski7c8d6df72cf4
diff --git a/backend/src/lib/ldap/ldap-search-fns.test.ts b/backend/src/lib/ldap/ldap-search-fns.test.ts
index 60b1e446ae..85a80e942f 100644
--- a/backend/src/lib/ldap/ldap-search-fns.test.ts
+++ b/backend/src/lib/ldap/ldap-search-fns.test.ts
@@ -100,7 +100,7 @@ describe("searchLdap", () => {
const strict = buildClient((res) => res.emit("error", sizeLimitError), captured);
await expect(
searchLdap(strict, { baseDN: "DC=corp", filter: "(cn=*)", attributes: [], timeLimitSeconds: 30 })
- ).rejects.toThrow(/Size Limit Exceeded/);
+ ).rejects.toThrow("Size Limit Exceeded");
const lenient = buildClient((res) => {
res.emit("searchEntry", entry([{ type: "cn", values: ["web01"], buffers: [] }]));
@@ -127,7 +127,7 @@ describe("searchLdap", () => {
timeLimitSeconds: 15,
acceptSizeLimitExceeded: true
})
- ).rejects.toThrow(/Busy/);
+ ).rejects.toThrow("Busy");
});
test("collects entries and lets a caller map failures into its own error type", async () => {
diff --git a/backend/src/lib/validator/validate-hostname.ts b/backend/src/lib/validator/validate-hostname.ts
index 4abcb290ed..d1154a11a0 100644
--- a/backend/src/lib/validator/validate-hostname.ts
+++ b/backend/src/lib/validator/validate-hostname.ts
@@ -3,7 +3,7 @@ import RE2 from "re2";
const HOSTNAME_MAX_LENGTH = 253;
const HOSTNAME_REGEX = new RE2(
- /^[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
+ "^[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"
);
export const isValidHostname = (value: string) => value.length <= HOSTNAME_MAX_LENGTH && HOSTNAME_REGEX.test(value);
diff --git a/backend/src/services/app-connection/ldap/ldap-directory-fns.test.ts b/backend/src/services/app-connection/ldap/ldap-directory-fns.test.ts
index d619dd9386..e932e17cb6 100644
--- a/backend/src/services/app-connection/ldap/ldap-directory-fns.test.ts
+++ b/backend/src/services/app-connection/ldap/ldap-directory-fns.test.ts
@@ -30,7 +30,7 @@ describe("parseLdapBindIdentity", () => {
});
test("rejects a DN with no DC components, because the domain cannot be derived", () => {
- expect(() => parseLdapBindIdentity("CN=svc-certs,OU=Service Accounts")).toThrow(/Unable to determine the domain/);
+ expect(() => parseLdapBindIdentity("CN=svc-certs,OU=Service Accounts")).toThrow("Unable to determine the domain");
});
test("treats a DN containing @ inside an RDN value as a DN, not a user principal name", () => {
@@ -48,8 +48,8 @@ describe("parseLdapBindIdentity", () => {
});
test("rejects a value that is neither a user principal name nor a DN with DC components", () => {
- expect(() => parseLdapBindIdentity("@corp.example.com")).toThrow(/Unable to determine the domain/);
- expect(() => parseLdapBindIdentity("svc-certs@")).toThrow(/Unable to determine the domain/);
+ expect(() => parseLdapBindIdentity("@corp.example.com")).toThrow("Unable to determine the domain");
+ expect(() => parseLdapBindIdentity("svc-certs@")).toThrow("Unable to determine the domain");
});
});
diff --git a/backend/src/services/app-connection/ldap/ldap-directory-fns.ts b/backend/src/services/app-connection/ldap/ldap-directory-fns.ts
index 5a7ae82ebb..60e8ba3322 100644
--- a/backend/src/services/app-connection/ldap/ldap-directory-fns.ts
+++ b/backend/src/services/app-connection/ldap/ldap-directory-fns.ts
@@ -91,6 +91,19 @@ const readNetbiosDomainName = async (client: ldap.Client, baseDN: string): Promi
return entries.length ? getLdapAttribute(entries[0], "nETBIOSName") || null : null;
};
+const readDefaultNamingContext = async (client: ldap.Client): Promise<string | null> => {
+ const entries = await searchDirectory(client, "", {
+ filter: "(objectClass=*)",
+ attributes: ["defaultNamingContext"],
+ scope: "base",
+ sizeLimit: 1
+ });
+
+ return entries.length ? getLdapAttribute(entries[0], "defaultNamingContext") || null : null;
+};
+
+const netbiosFromBaseDN = (baseDN: string): string => baseDN.split(",")[0]?.split("=")[1]?.toUpperCase() || "";
+
const readAccountName = async (client: ldap.Client, dn: string): Promise<string | null> => {
const entries = await searchDirectory(client, dn, {
filter: "(objectClass=*)",
@@ -122,12 +135,18 @@ const resolveHostLoginFromDirectory = async (
gatewayServices: TLdapGatewayServices
): Promise<TLdapHostLogin> => {
const { domainFqdn, accountName } = parseLdapBindIdentity(config.credentials.dn);
- const baseDN = buildDomainBaseDN(domainFqdn);
+ const fallbackBaseDN = buildDomainBaseDN(domainFqdn);
const resolved = await executeWithPotentialGateway(
config,
gatewayServices.gatewayV2Service,
async (client) => {
+ const baseDN =
+ (await readDefaultNamingContext(client).catch((err: unknown) => {
+ logger.warn({ err }, `Unable to read the directory's naming context [domain=${domainFqdn}]`);
+ return null;
+ })) ?? fallbackBaseDN;
+
const [resolvedAccountName, netbiosDomainName] = await Promise.all([
accountName
? readAccountNameByUpn(client, baseDN, config.credentials.dn).catch((err: unknown) => {
@@ -146,7 +165,7 @@ const resolveHostLoginFromDirectory = async (
return null;
})
]);
- return { netbiosDomainName, resolvedAccountName };
+ return { netbiosDomainName, resolvedAccountName, baseDN };
},
gatewayServices.gatewayPoolService
);
@@ -160,7 +179,7 @@ const resolveHostLoginFromDirectory = async (
}
return {
- username: `${resolved.netbiosDomainName ?? netbiosFromDomainFqdn(domainFqdn)}\\${loginAccountName}`,
+ username: `${resolved.netbiosDomainName ?? netbiosFromBaseDN(resolved.baseDN) ?? netbiosFromDomainFqdn(domainFqdn)}\\${loginAccountName}`,
isGuessed: !resolved.netbiosDomainName || !resolved.resolvedAccountName
};
};
@@ -235,7 +254,7 @@ export const listDirectoryMachines = async (
gatewayServices: TLdapGatewayServices
): Promise<TDirectoryMachine[]> => {
const { domainFqdn } = parseLdapBindIdentity(config.credentials.dn);
- const baseDN = buildDomainBaseDN(domainFqdn);
+ const fallbackBaseDN = buildDomainBaseDN(domainFqdn);
const cappedLimit = Math.min(limit, DIRECTORY_MACHINE_LIST_MAX_LIMIT);
const escapedSearch = search ? escapeLdapFilterValue(search) : undefined;
@@ -246,12 +265,19 @@ export const listDirectoryMachines = async (
const entries = await executeWithPotentialGateway(
config,
gatewayServices.gatewayV2Service,
- (client) =>
- searchDirectory(client, baseDN, {
+ async (client) => {
+ const baseDN =
+ (await readDefaultNamingContext(client).catch((err: unknown) => {
+ logger.warn({ err }, `Unable to read the directory's naming context [domain=${domainFqdn}]`);
+ return null;
+ })) ?? fallbackBaseDN;
+
+ return searchDirectory(client, baseDN, {
filter,
attributes: ["cn", "dNSHostName"],
sizeLimit: cappedLimit
- }),
+ });
+ },
gatewayServices.gatewayPoolService
);
diff --git a/backend/src/services/pki-sync/pki-sync-service.ts b/backend/src/services/pki-sync/pki-sync-service.ts
index 9a13f3320b..37a45582a1 100644
--- a/backend/src/services/pki-sync/pki-sync-service.ts
+++ b/backend/src/services/pki-sync/pki-sync-service.ts
@@ -944,7 +944,12 @@ export const pkiSyncServiceFactory = ({
throw new NotFoundError({ message: `PKI sync with id "${args.syncId}" not found` });
}
- return { projectId: pkiSync.projectId, applicationId: pkiSync.applicationId, name: pkiSync.name };
+ return {
+ projectId: pkiSync.projectId,
+ applicationId: pkiSync.applicationId,
+ name: pkiSync.name,
+ destinationConfig: pkiSync.destinationConfig as Record<string, unknown> | undefined
+ };
}
if (!args.applicationId) {
@@ -954,7 +959,7 @@ export const pkiSyncServiceFactory = ({
});
}
- return { projectId: args.projectId, applicationId: args.applicationId, name: "" };
+ return { projectId: args.projectId, applicationId: args.applicationId, name: "", destinationConfig: undefined };
};
const testPkiSyncHealthCheckCommand = async (
@@ -971,10 +976,12 @@ export const pkiSyncServiceFactory = ({
actor: OrgServiceActor,
auditLogInfo?: AuditLogInfo
) => {
+ const testTarget = await $resolveHealthCheckTestTarget(args);
+
await $assertSyncAction(
ProjectPermissionPkiSyncActions.SetHealthCheckCommand,
ResourcePermissionPkiSyncActions.SetHealthCheckCommand,
- await $resolveHealthCheckTestTarget(args),
+ testTarget,
undefined,
actor
);
@@ -1000,6 +1007,14 @@ export const pkiSyncServiceFactory = ({
destinationConfig: args.destinationConfig
});
+ await $assertMaySetTargetHost({
+ nextConfig: args.destinationConfig,
+ currentConfig: testTarget.destinationConfig,
+ pkiSync: testTarget,
+ subscriberName: undefined,
+ actor
+ });
+
$assertHostCommandsAreSupported(args.destination, args.syncOptions, connection);
const result = await pkiSyncHealthCheckQueue.testHealthCheckCommand({
diff --git a/backend/src/services/pki-sync/pki-sync-target-host-fns.test.ts b/backend/src/services/pki-sync/pki-sync-target-host-fns.test.ts
index 1c697b3f65..b0b7548a52 100644
--- a/backend/src/services/pki-sync/pki-sync-target-host-fns.test.ts
+++ b/backend/src/services/pki-sync/pki-sync-target-host-fns.test.ts
@@ -26,7 +26,7 @@ describe("assertTargetHostMatchesConnection", () => {
connection: sshConnection,
destinationConfig: { destinationPath: "/etc/ssl/certs", host: "server01.corp.example.com" }
})
- ).toThrow(/cannot be set when using the 'prod-web-01' connection/);
+ ).toThrow("cannot be set when using the 'prod-web-01' connection");
});
test("rejects a target host on a WinRM connection", () => {
@@ -36,7 +36,7 @@ describe("assertTargetHostMatchesConnection", () => {
connection: winrmConnection,
destinationConfig: { destinationPath: "C:\\certs", host: "server01.corp.example.com" }
})
- ).toThrow(/cannot be set when using the 'prod-dc-01' connection/);
+ ).toThrow("cannot be set when using the 'prod-dc-01' connection");
});
test("rejects an LDAP connection with no target host", () => {
@@ -46,7 +46,7 @@ describe("assertTargetHostMatchesConnection", () => {
connection: ldapConnection,
destinationConfig: { destinationPath: "/etc/ssl/certs" }
})
- ).toThrow(/A target host is required when using the LDAP connection 'corp-directory'/);
+ ).toThrow("A target host is required when using the LDAP connection 'corp-directory'");
});
test("rejects an empty-string host on an LDAP connection", () => {
@@ -56,7 +56,7 @@ describe("assertTargetHostMatchesConnection", () => {
connection: ldapConnection,
destinationConfig: { destinationPath: "/etc/ssl/certs", host: "" }
})
- ).toThrow(/A target host is required/);
+ ).toThrow("A target host is required");
});
test("accepts an LDAP connection with a target host", () => {
@@ -86,7 +86,7 @@ describe("assertTargetHostMatchesConnection", () => {
connection: ldapConnectionWithoutGateway,
destinationConfig: { destinationPath: "C:\\certs", host: "server01.corp.example.com" }
})
- ).toThrow(/requires a Gateway/);
+ ).toThrow("requires a Gateway");
});
test("accepts an LDAP-backed Windows sync reached through a Gateway pool", () => {
@@ -117,7 +117,7 @@ describe("assertTargetHostMatchesConnection", () => {
connection: { app: AppConnection.WinRM, name: "prod-web-01" },
destinationConfig: { [field]: field === "port" ? 5986 : false }
})
- ).toThrow(new RegExp(`${field} cannot be set when using the 'prod-web-01' connection`));
+ ).toThrow(`${field} cannot be set when using the 'prod-web-01' connection`);
}
});