Skip to content

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

refactor: enhance LDAP authentication error handling and validation

Luiz Filho7723959bb347
diff --git a/backend/src/server/routes/v1/identity-ldap-auth-router.ts b/backend/src/server/routes/v1/identity-ldap-auth-router.ts
index 309b3c7039..ae929bd43e 100644
--- a/backend/src/server/routes/v1/identity-ldap-auth-router.ts
+++ b/backend/src/server/routes/v1/identity-ldap-auth-router.ts
@@ -18,7 +18,7 @@ import { EventType } from "@app/ee/services/audit-log/audit-log-types";
import { isValidLdapFilter } from "@app/ee/services/ldap-config/ldap-fns";
import { ApiDocsTags, LDAP_AUTH } from "@app/lib/api-docs";
import { getConfig } from "@app/lib/config/env";
-import { UnauthorizedError } from "@app/lib/errors";
+import { BadRequestError, UnauthorizedError } from "@app/lib/errors";
import { logger } from "@app/lib/logger";
import { readLimit, writeLimit } from "@app/server/config/rateLimiter";
import { slugSchema } from "@app/server/lib/schemas";
@@ -36,27 +36,34 @@ export const registerIdentityLdapAuthRouter = async (server: FastifyZodProvider)
await server.register(passport.secureSession());
const getLdapPassportOpts = (req: FastifyRequest, done: any) => {
- const { identityId } = req.body as {
- identityId: string;
- };
+ // ldapConfig is already loaded by preValidation hook
+ // This allows proper error handling since passport doesn't propagate errors well
+ const { ldapConfig } = req;
- process.nextTick(async () => {
- try {
- const { ldapConfig, opts } = await server.services.identityLdapAuth.getLdapConfig(identityId);
- req.ldapConfig = {
- ...ldapConfig,
- isActive: true,
- groupSearchBase: "",
- uniqueUserAttribute: "",
- groupSearchFilter: ""
- };
+ if (!ldapConfig) {
+ // This should never happen since preValidation already validated it
+ return done(new BadRequestError({ message: "LDAP configuration not found" }));
+ }
- done(null, opts);
- } catch (err) {
- logger.error(err, "Error in LDAP verification callback");
- done(err);
- }
- });
+ const opts = {
+ server: {
+ url: ldapConfig.url,
+ bindDN: ldapConfig.bindDN,
+ bindCredentials: ldapConfig.bindPass,
+ searchBase: ldapConfig.searchBase,
+ searchFilter: ldapConfig.searchFilter,
+ ...(ldapConfig.caCert
+ ? {
+ tlsOptions: {
+ ca: [ldapConfig.caCert]
+ }
+ }
+ : {})
+ },
+ passReqToCallback: true
+ };
+
+ return done(null, opts);
};
passport.use(
@@ -124,9 +131,9 @@ export const registerIdentityLdapAuthRouter = async (server: FastifyZodProvider)
tags: [ApiDocsTags.LdapAuth],
description: "Login with LDAP Auth for machine identity",
body: z.object({
- identityId: z.string().trim().describe(LDAP_AUTH.LOGIN.identityId),
- username: z.string().describe(LDAP_AUTH.LOGIN.username),
- password: z.string().describe(LDAP_AUTH.LOGIN.password),
+ identityId: z.string().trim().min(1, "Identity ID is required").describe(LDAP_AUTH.LOGIN.identityId),
+ username: z.string().min(1, "Username is required").describe(LDAP_AUTH.LOGIN.username),
+ password: z.string().min(1, "Password is required").describe(LDAP_AUTH.LOGIN.password),
subOrganizationName: slugSchema().optional().describe(LDAP_AUTH.LOGIN.subOrganizationName)
}),
response: {
@@ -139,7 +146,44 @@ export const registerIdentityLdapAuthRouter = async (server: FastifyZodProvider)
}
},
preValidation: [
- (req, res) => {
+ async (req, res) => {
+ // Validate required fields before passport authentication
+ // This is needed because preValidation runs before Fastify's schema validation
+ const body = req.body as { identityId?: string; username?: string; password?: string } | undefined;
+
+ if (!body || typeof body !== "object") {
+ throw new BadRequestError({ message: "Request body is required" });
+ }
+
+ if (!body.identityId || typeof body.identityId !== "string" || body.identityId.trim() === "") {
+ throw new BadRequestError({ message: "Identity ID is required" });
+ }
+
+ // Validate UUID format to prevent database errors
+ const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
+ if (!uuidRegex.test(body.identityId)) {
+ throw new BadRequestError({ message: "Identity ID must be a valid UUID" });
+ }
+
+ if (!body.username || typeof body.username !== "string" || body.username.trim() === "") {
+ throw new BadRequestError({ message: "Username is required" });
+ }
+
+ if (!body.password || typeof body.password !== "string" || body.password.trim() === "") {
+ throw new BadRequestError({ message: "Password is required" });
+ }
+
+ // Pre-validate that the LDAP config exists before calling passport
+ // This allows proper error handling since passport doesn't propagate errors well
+ const { ldapConfig } = await server.services.identityLdapAuth.getLdapConfig(body.identityId);
+ req.ldapConfig = {
+ ...ldapConfig,
+ isActive: true,
+ groupSearchBase: "",
+ uniqueUserAttribute: "",
+ groupSearchFilter: ""
+ };
+
const passportAuth = (request: FastifyRequest, reply: FastifyReply) =>
(
passport.authenticate("ldapauth", {
@@ -148,7 +192,7 @@ export const registerIdentityLdapAuthRouter = async (server: FastifyZodProvider)
}) as any
)(request, reply);
- const { identityId, username } = req.body;
+ const { identityId, username } = body;
return server.services.identityLdapAuth.withLdapLockout(
{
identityId,