Skip to content

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

fix: correct auth template patch error parsing

Scott Wilson232034e44be4
diff --git a/backend/src/ee/routes/v1/identity-template-router.ts b/backend/src/ee/routes/v1/identity-template-router.ts
index a1454bd2db..606de581ff 100644
--- a/backend/src/ee/routes/v1/identity-template-router.ts
+++ b/backend/src/ee/routes/v1/identity-template-router.ts
@@ -8,13 +8,13 @@ import {
TEMPLATE_VALIDATION_MESSAGES
} from "@app/ee/services/identity-auth-template/identity-auth-template-enums";
import {
- kubernetesTemplateFieldsBaseSchema,
kubernetesTemplateFieldsCreateSchema,
kubernetesTemplateFieldsResponseSchema,
ldapTemplateFieldsResponseSchema,
ldapTemplateFieldsSchema,
oidcTemplateFieldsResponseSchema,
- oidcTemplateFieldsSchema
+ oidcTemplateFieldsSchema,
+ templateFieldsPatchSchema
} from "@app/ee/services/identity-auth-template/identity-auth-template-schemas";
import { readLimit, writeLimit } from "@app/server/config/rateLimiter";
import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
@@ -137,15 +137,7 @@ export const registerIdentityTemplateRouter = async (server: FastifyZodProvider)
}),
body: z.object({
name: templateNameSchema.optional(),
- // strict partials so the union can discriminate by field names; the service
- // validates the patch against the template's actual auth method
- templateFields: z
- .union([
- ldapTemplateFieldsSchema.partial().strict(),
- kubernetesTemplateFieldsBaseSchema.partial().strict(),
- oidcTemplateFieldsSchema.partial().strict()
- ])
- .optional()
+ templateFields: templateFieldsPatchSchema.optional()
}),
response: {
200: sanitizedTemplateSchema
diff --git a/backend/src/ee/services/identity-auth-template/identity-auth-template-schemas.test.ts b/backend/src/ee/services/identity-auth-template/identity-auth-template-schemas.test.ts
index 7c37a82b54..0622fab676 100644
--- a/backend/src/ee/services/identity-auth-template/identity-auth-template-schemas.test.ts
+++ b/backend/src/ee/services/identity-auth-template/identity-auth-template-schemas.test.ts
@@ -1,6 +1,7 @@
import { describe, expect, it, vi } from "vitest";
-import { oidcTemplateFieldsSchema } from "./identity-auth-template-schemas";
+import { TEMPLATE_VALIDATION_MESSAGES } from "./identity-auth-template-enums";
+import { oidcTemplateFieldsSchema, templateFieldsPatchSchema } from "./identity-auth-template-schemas";
vi.mock("@app/lib/config/env", () => ({
getConfig: () => ({ isDevelopmentMode: false, ALLOW_INTERNAL_IP_CONNECTIONS: false })
@@ -50,3 +51,54 @@ describe("oidcTemplateFieldsSchema discovery URL", () => {
expect(result.success && result.data.oidcDiscoveryUrl).toBe("https://idp.example.com/realms/acme");
});
});
+
+// the route cannot see the template's method, so the error must name the offending field
+describe("templateFieldsPatchSchema", () => {
+ it("surfaces the OIDC discovery URL error for an OIDC-only patch", () => {
+ const result = templateFieldsPatchSchema.safeParse({
+ oidcDiscoveryUrl: "https://idp.example.com/.well-known/openid-configuration"
+ });
+
+ expect(result.success).toBe(false);
+ expect(result.success === false && result.error.issues).toEqual([
+ expect.objectContaining({
+ path: ["oidcDiscoveryUrl"],
+ message: TEMPLATE_VALIDATION_MESSAGES.OIDC.DISCOVERY_URL_WELL_KNOWN_SUFFIX
+ })
+ ]);
+ });
+
+ it("names the field for a bad Kubernetes value instead of calling the key unrecognized", () => {
+ const result = templateFieldsPatchSchema.safeParse({ tokenReviewMode: "bogus" });
+
+ expect(result.success).toBe(false);
+ expect(result.success === false && result.error.issues).toEqual([
+ expect.objectContaining({ code: "invalid_enum_value", path: ["tokenReviewMode"] })
+ ]);
+ });
+
+ it("rejects a key that belongs to no auth method", () => {
+ const result = templateFieldsPatchSchema.safeParse({ bindDn: "cn=admin,dc=example,dc=com" });
+
+ expect(result.success).toBe(false);
+ expect(result.success === false && result.error.issues).toEqual([
+ expect.objectContaining({ code: "unrecognized_keys", keys: ["bindDn"] })
+ ]);
+ });
+
+ it("applies the field normalizations to a partial patch", () => {
+ const result = templateFieldsPatchSchema.safeParse({ oidcDiscoveryUrl: "https://idp.example.com/realms/acme/" });
+
+ expect(result.success).toBe(true);
+ expect(result.success && result.data).toEqual({ oidcDiscoveryUrl: "https://idp.example.com/realms/acme" });
+ });
+
+ it("leaves method membership to the service", () => {
+ const result = templateFieldsPatchSchema.safeParse({
+ url: "ldap://idp.example.com",
+ oidcDiscoveryUrl: "https://idp.example.com"
+ });
+
+ expect(result.success).toBe(true);
+ });
+});
diff --git a/backend/src/ee/services/identity-auth-template/identity-auth-template-schemas.ts b/backend/src/ee/services/identity-auth-template/identity-auth-template-schemas.ts
index a2e91cecf3..5b59e5a214 100644
--- a/backend/src/ee/services/identity-auth-template/identity-auth-template-schemas.ts
+++ b/backend/src/ee/services/identity-auth-template/identity-auth-template-schemas.ts
@@ -150,3 +150,16 @@ export const templateFieldPatchKeysByMethod = {
kubernetes: Object.keys(kubernetesTemplateFieldsBaseSchema.shape),
oidc: Object.keys(oidcTemplateFieldsSchema.shape)
} as const;
+
+// not a union of per-method partials: zod reports only the first failing branch, so every
+// bad value surfaced as an LDAP unrecognized-key error. The service checks method membership
+export const templateFieldsPatchSchema = ldapTemplateFieldsSchema
+ .merge(kubernetesTemplateFieldsBaseSchema)
+ .merge(oidcTemplateFieldsSchema)
+ .extend({
+ caCert: oidcTemplateFieldsSchema.shape.caCert.describe(
+ "The PEM-encoded CA certificate used to validate the TLS certificate of the Kubernetes API server or identity provider"
+ )
+ })
+ .partial()
+ .strict();
diff --git a/backend/src/ee/services/identity-auth-template/identity-auth-template-service.test.ts b/backend/src/ee/services/identity-auth-template/identity-auth-template-service.test.ts
index 9017d3efa4..08a860eb10 100644
--- a/backend/src/ee/services/identity-auth-template/identity-auth-template-service.test.ts
+++ b/backend/src/ee/services/identity-auth-template/identity-auth-template-service.test.ts
@@ -370,3 +370,57 @@ describe("identityAuthTemplateServiceFactory oidc templates", () => {
);
});
});
+
+// the route accepts any method's fields, so this guard is the only method-membership check
+describe("identityAuthTemplateServiceFactory field patch method guard", () => {
+ beforeEach(() => vi.clearAllMocks());
+
+ it("rejects a field from another auth method before anything is written", async () => {
+ const { service, identityOidcAuthDAL, identityAuthTemplateDAL, auditLogCreate } = createOidcService();
+
+ const error = await patchTemplate(service, { bindDN: "cn=admin,dc=example,dc=com" }).catch((err: unknown) => err);
+
+ expect(error).toBeInstanceOf(BadRequestError);
+ expect((error as BadRequestError).message).toBe(
+ "Template fields [bindDN] are not valid for a 'oidc' auth template"
+ );
+ expect(identityAuthTemplateDAL.updateById).not.toHaveBeenCalled();
+ expect(identityOidcAuthDAL.updateByTemplateId).not.toHaveBeenCalled();
+ expect(auditLogCreate).not.toHaveBeenCalled();
+ });
+
+ it("rejects the whole patch when a valid field rides along with a foreign one", async () => {
+ const { service, identityOidcAuthDAL, identityAuthTemplateDAL } = createOidcService();
+
+ const error = await patchTemplate(service, {
+ boundIssuer: "https://other-issuer.example.com",
+ bindDN: "cn=admin,dc=example,dc=com"
+ }).catch((err: unknown) => err);
+
+ expect(error).toBeInstanceOf(BadRequestError);
+ expect((error as BadRequestError).message).toBe(
+ "Template fields [bindDN] are not valid for a 'oidc' auth template"
+ );
+ expect(identityAuthTemplateDAL.updateById).not.toHaveBeenCalled();
+ expect(identityOidcAuthDAL.updateByTemplateId).not.toHaveBeenCalled();
+ });
+
+ it("names every foreign field and the template's own method", async () => {
+ const { service, identityAuthTemplateDAL } = createService({
+ authMethod: IdentityAuthTemplateMethod.LDAP,
+ blobFields: { url: "ldap://example.com", bindDN: "cn=admin", bindPass: "pw", searchBase: "dc=example" },
+ gatewayColumns: NO_GATEWAY
+ });
+
+ const error = await patchTemplate(service, {
+ oidcDiscoveryUrl: "https://idp.example.com",
+ tokenReviewMode: IdentityKubernetesAuthTokenReviewMode.Api
+ }).catch((err: unknown) => err);
+
+ expect(error).toBeInstanceOf(BadRequestError);
+ expect((error as BadRequestError).message).toBe(
+ "Template fields [oidcDiscoveryUrl, tokenReviewMode] are not valid for a 'ldap' auth template"
+ );
+ expect(identityAuthTemplateDAL.updateById).not.toHaveBeenCalled();
+ });
+});