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
improvement: add discriminated response for auth templates
Scott Wilsond67c77a965ae
diff --git a/backend/src/ee/routes/v1/identity-template-router.ts b/backend/src/ee/routes/v1/identity-template-router.tsindex 2ded410fe0..b97c7736fd 100644--- a/backend/src/ee/routes/v1/identity-template-router.ts+++ b/backend/src/ee/routes/v1/identity-template-router.ts@@ -10,6 +10,8 @@ import {import {kubernetesTemplateFieldsBaseSchema,kubernetesTemplateFieldsCreateSchema,+ kubernetesTemplateFieldsResponseSchema,+ ldapTemplateFieldsResponseSchema,ldapTemplateFieldsSchema} from "@app/ee/services/identity-auth-template/identity-auth-template-schemas";import { readLimit, writeLimit } from "@app/server/config/rateLimiter";@@ -22,9 +24,20 @@ const templateNameSchema = z.min(1, TEMPLATE_VALIDATION_MESSAGES.TEMPLATE_NAME_REQUIRED).max(64, TEMPLATE_VALIDATION_MESSAGES.TEMPLATE_NAME_MAX_LENGTH);-// template credential fields (LDAP bindPass, Kubernetes tokenReviewerJwt) are stripped-// server-side and never appear in responses-const templateFieldsResponseSchema = z.record(z.string(), z.unknown());+// one response shape per auth method, discriminated the same way the create body is.+// Credential fields are stripped server-side by $sanitizeTemplate; declaring the+// exact shape here documents each method's fields and makes the response serializer a+// second barrier that drops anything undeclared+const sanitizedTemplateSchema = z.discriminatedUnion("authMethod", [+ IdentityAuthTemplatesSchema.extend({+ authMethod: z.literal(IdentityAuthTemplateMethod.LDAP),+ templateFields: ldapTemplateFieldsResponseSchema+ }),+ IdentityAuthTemplatesSchema.extend({+ authMethod: z.literal(IdentityAuthTemplateMethod.KUBERNETES),+ templateFields: kubernetesTemplateFieldsResponseSchema+ })+]);export const registerIdentityTemplateRouter = async (server: FastifyZodProvider) => {server.route({@@ -55,9 +68,7 @@ export const registerIdentityTemplateRouter = async (server: FastifyZodProvider)})]),response: {- 200: IdentityAuthTemplatesSchema.extend({- templateFields: z.record(z.string(), z.unknown())- })+ 200: sanitizedTemplateSchema}},handler: async (req) => {@@ -114,9 +125,7 @@ export const registerIdentityTemplateRouter = async (server: FastifyZodProvider).optional()}),response: {- 200: IdentityAuthTemplatesSchema.extend({- templateFields: z.record(z.string(), z.unknown())- })+ 200: sanitizedTemplateSchema}},handler: async (req) => {@@ -214,9 +223,7 @@ export const registerIdentityTemplateRouter = async (server: FastifyZodProvider)templateId: z.string().min(1, TEMPLATE_VALIDATION_MESSAGES.TEMPLATE_ID_REQUIRED)}),response: {- 200: IdentityAuthTemplatesSchema.extend({- templateFields: templateFieldsResponseSchema- })+ 200: sanitizedTemplateSchema}},handler: async (req) => {@@ -254,9 +261,7 @@ export const registerIdentityTemplateRouter = async (server: FastifyZodProvider)}),response: {200: z.object({- templates: IdentityAuthTemplatesSchema.extend({- templateFields: templateFieldsResponseSchema- }).array(),+ templates: sanitizedTemplateSchema.array(),totalCount: z.number()})}@@ -295,9 +300,7 @@ export const registerIdentityTemplateRouter = async (server: FastifyZodProvider)authMethod: z.nativeEnum(IdentityAuthTemplateMethod)}),response: {- 200: IdentityAuthTemplatesSchema.extend({- templateFields: templateFieldsResponseSchema- }).array()+ 200: sanitizedTemplateSchema.array()}},handler: async (req) => {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.tsindex 203b26d2f0..4f4e722537 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@@ -62,6 +62,20 @@ export const kubernetesTemplateFieldsCreateSchema = kubernetesTemplateFieldsBasesuperRefineKubernetesConnectionFields);+// response shapes: each write-only credential is replaced by a boolean presence flag,+// mirroring $redactTemplateSecrets. Derived from the request schemas so a new template+// field cannot reach the API undocumented, and so the response serializer drops anything+// the redaction step misses+export const ldapTemplateFieldsResponseSchema = ldapTemplateFieldsSchema.omit({ bindPass: true }).extend({+ hasBindPass: z.boolean().describe("Whether a bind password is stored for this template")+});++export const kubernetesTemplateFieldsResponseSchema = kubernetesTemplateFieldsBaseSchema+ .omit({ tokenReviewerJwt: true })+ .extend({+ hasTokenReviewerJwt: z.boolean().describe("Whether a token reviewer JWT is stored for this template")+ });+export const templateFieldPatchKeysByMethod = {ldap: Object.keys(ldapTemplateFieldsSchema.shape),kubernetes: Object.keys(kubernetesTemplateFieldsBaseSchema.shape)diff --git a/backend/src/ee/services/identity-auth-template/identity-auth-template-service.ts b/backend/src/ee/services/identity-auth-template/identity-auth-template-service.tsindex bde0667776..2b771ba80a 100644--- a/backend/src/ee/services/identity-auth-template/identity-auth-template-service.ts+++ b/backend/src/ee/services/identity-auth-template/identity-auth-template-service.ts@@ -1,6 +1,7 @@import { ForbiddenError } from "@casl/ability";import { OrganizationActionScope, TIdentityKubernetesAuthsUpdate } from "@app/db/schemas";+import { TIdentityAuthTemplates } from "@app/db/schemas/identity-auth-templates";import { EventType, TAuditLogServiceFactory } from "@app/ee/services/audit-log/audit-log-types";import { TGatewayDALFactory } from "@app/ee/services/gateway/gateway-dal";import { TGatewayPoolDALFactory } from "@app/ee/services/gateway-pool/gateway-pool-dal";@@ -36,6 +37,7 @@ import {TKubernetesTemplateFields,TLdapTemplateFields,TListIdentityAuthTemplatesDTO,+ TSanitizedIdentityAuthTemplate,TUnlinkTemplateUsageDTO} from "./identity-auth-template-types";@@ -83,15 +85,20 @@ export const identityAuthTemplateServiceFactory = ({// reach the client (the edit UI keeps a stored secret by omitting the field). A// has<Field> presence flag replaces each secret so the edit UI can offer an explicit// "clear stored value" action without ever reading the credential- const $redactTemplateSecrets = (authMethod: string, fields: Record<string, unknown>) => {- const secretFields = TEMPLATE_SECRET_FIELDS_BY_METHOD[authMethod as IdentityAuthTemplateMethod] ?? [];+ const $sanitizeTemplate = (+ template: TIdentityAuthTemplates,+ fields: Record<string, unknown>+ ): TSanitizedIdentityAuthTemplate => {+ const secretFields = TEMPLATE_SECRET_FIELDS_BY_METHOD[template.authMethod as IdentityAuthTemplateMethod] ?? [];const redacted = { ...fields };secretFields.forEach((key) => {const value = redacted[key];delete redacted[key];redacted[`has${key.charAt(0).toUpperCase()}${key.slice(1)}`] = typeof value === "string" && value.length > 0;});- return redacted;+ // authMethod is a plain string column; every row is written through the create route's+ // discriminated body, so it is always one of the supported methods here+ return { ...template, templateFields: redacted } as TSanitizedIdentityAuthTemplate;};// audit the platform-driven rewrite of linked identities; runs after the propagation@@ -293,7 +300,7 @@ export const identityAuthTemplateServiceFactory = ({orgId: actorOrgId});- return { ...template, templateFields: $redactTemplateSecrets(authMethod, fieldsToPersist) };+ return $sanitizeTemplate(template, fieldsToPersist);};const updateTemplate = async ({@@ -496,7 +503,7 @@ export const identityAuthTemplateServiceFactory = ({orgId: template.orgId});- return { ...updatedTemplate, templateFields: $redactTemplateSecrets(template.authMethod, mergedTemplateFields) };+ return $sanitizeTemplate(updatedTemplate, mergedTemplateFields);};const deleteTemplate = async ({@@ -603,10 +610,7 @@ export const identityAuthTemplateServiceFactory = ({const decryptedTemplateFields = JSON.parse(decryptor({ cipherTextBlob: template.templateFields }).toString()) as Record<string, unknown>;- return {- ...template,- templateFields: $redactTemplateSecrets(template.authMethod, decryptedTemplateFields)- };+ return $sanitizeTemplate(template, decryptedTemplateFields);};const listTemplates = async ({@@ -645,10 +649,7 @@ export const identityAuthTemplateServiceFactory = ({string,unknown>;- return {- ...doc,- templateFields: $redactTemplateSecrets(doc.authMethod, parsedTemplateFields)- };+ return $sanitizeTemplate(doc, parsedTemplateFields);})};};@@ -685,10 +686,7 @@ export const identityAuthTemplateServiceFactory = ({string,unknown>;- return {- ...doc,- templateFields: $redactTemplateSecrets(doc.authMethod, parsedTemplateFields)- };+ return $sanitizeTemplate(doc, parsedTemplateFields);});};diff --git a/backend/src/ee/services/identity-auth-template/identity-auth-template-types.ts b/backend/src/ee/services/identity-auth-template/identity-auth-template-types.tsindex 149bdecb65..1b93eda398 100644--- a/backend/src/ee/services/identity-auth-template/identity-auth-template-types.ts+++ b/backend/src/ee/services/identity-auth-template/identity-auth-template-types.ts@@ -1,7 +1,28 @@+import { z } from "zod";++import { TIdentityAuthTemplates } from "@app/db/schemas/identity-auth-templates";import { TProjectPermission } from "@app/lib/types";import { IdentityKubernetesAuthTokenReviewMode } from "@app/services/identity-kubernetes-auth/identity-kubernetes-auth-types";import { IdentityAuthTemplateMethod } from "./identity-auth-template-enums";+import {+ kubernetesTemplateFieldsResponseSchema,+ ldapTemplateFieldsResponseSchema+} from "./identity-auth-template-schemas";++// what every read path returns: the row with its credentials replaced by presence flags.+// Derived from the response schemas so the service and the route contract cannot drift+export type TSanitizedIdentityAuthTemplate = Omit<TIdentityAuthTemplates, "templateFields" | "authMethod"> &+ (+ | {+ authMethod: IdentityAuthTemplateMethod.LDAP;+ templateFields: z.infer<typeof ldapTemplateFieldsResponseSchema>;+ }+ | {+ authMethod: IdentityAuthTemplateMethod.KUBERNETES;+ templateFields: z.infer<typeof kubernetesTemplateFieldsResponseSchema>;+ }+ );// Method-specific template field typesexport type TLdapTemplateFields = {