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
feat: add modifications into webhook to allow users to define which events should be triggered
adilsonf2abae6a07f0
diff --git a/backend/src/db/migrations/20260409120000_add-webhook-event-toggles.ts b/backend/src/db/migrations/20260409120000_add-webhook-event-toggles.tsnew file mode 100644index 0000000000..cd5ae67d80--- /dev/null+++ b/backend/src/db/migrations/20260409120000_add-webhook-event-toggles.ts@@ -0,0 +1,48 @@+import { Knex } from "knex";++import { TableName } from "../schemas";++const SECRET_MODIFIED_EVENT_COLUMN = "isSecretModifiedEventEnabled";+const SECRET_ROTATION_FAILED_EVENT_COLUMN = "isSecretRotationFailedEventEnabled";++export async function up(knex: Knex): Promise<void> {+ const hasWebhookTable = await knex.schema.hasTable(TableName.Webhook);+ if (!hasWebhookTable) return;++ const hasSecretModifiedEventColumn = await knex.schema.hasColumn(TableName.Webhook, SECRET_MODIFIED_EVENT_COLUMN);+ const hasSecretRotationFailedEventColumn = await knex.schema.hasColumn(+ TableName.Webhook,+ SECRET_ROTATION_FAILED_EVENT_COLUMN+ );++ await knex.schema.alterTable(TableName.Webhook, (table) => {+ if (!hasSecretModifiedEventColumn) {+ table.boolean(SECRET_MODIFIED_EVENT_COLUMN).notNullable().defaultTo(true);+ }++ if (!hasSecretRotationFailedEventColumn) {+ table.boolean(SECRET_ROTATION_FAILED_EVENT_COLUMN).notNullable().defaultTo(true);+ }+ });+}++export async function down(knex: Knex): Promise<void> {+ const hasWebhookTable = await knex.schema.hasTable(TableName.Webhook);+ if (!hasWebhookTable) return;++ const hasSecretModifiedEventColumn = await knex.schema.hasColumn(TableName.Webhook, SECRET_MODIFIED_EVENT_COLUMN);+ const hasSecretRotationFailedEventColumn = await knex.schema.hasColumn(+ TableName.Webhook,+ SECRET_ROTATION_FAILED_EVENT_COLUMN+ );++ await knex.schema.alterTable(TableName.Webhook, (table) => {+ if (hasSecretModifiedEventColumn) {+ table.dropColumn(SECRET_MODIFIED_EVENT_COLUMN);+ }++ if (hasSecretRotationFailedEventColumn) {+ table.dropColumn(SECRET_ROTATION_FAILED_EVENT_COLUMN);+ }+ });+}diff --git a/backend/src/db/schemas/webhooks.ts b/backend/src/db/schemas/webhooks.tsindex 60f031ffff..ca0fe47484 100644--- a/backend/src/db/schemas/webhooks.ts+++ b/backend/src/db/schemas/webhooks.ts@@ -28,6 +28,8 @@ export const WebhooksSchema = z.object({urlIV: z.string().nullable().optional(),urlTag: z.string().nullable().optional(),type: z.string().default("general").nullable().optional(),+ isSecretModifiedEventEnabled: z.boolean().default(true),+ isSecretRotationFailedEventEnabled: z.boolean().default(true),encryptedPassKey: zodBuffer.nullable().optional(),encryptedUrl: zodBuffer});diff --git a/backend/src/server/routes/v1/webhook-router.ts b/backend/src/server/routes/v1/webhook-router.tsindex f3b8804f1a..210a72789e 100644--- a/backend/src/server/routes/v1/webhook-router.ts+++ b/backend/src/server/routes/v1/webhook-router.ts@@ -19,7 +19,9 @@ export const sanitizedWebhookSchema = WebhooksSchema.pick({createdAt: true,updatedAt: true,envId: true,- type: true+ type: true,+ isSecretModifiedEventEnabled: true,+ isSecretRotationFailedEventEnabled: true}).extend({projectId: z.string(),environment: z.object({@@ -46,7 +48,9 @@ export const registerWebhookRouter = async (server: FastifyZodProvider) => {environment: z.string().trim(),webhookUrl: z.string().url().trim(),webhookSecretKey: z.string().trim().optional(),- secretPath: z.string().trim().default("/").transform(removeTrailingSlash)+ secretPath: z.string().trim().default("/").transform(removeTrailingSlash),+ isSecretModifiedEventEnabled: z.boolean().default(true),+ isSecretRotationFailedEventEnabled: z.boolean().default(true)}).superRefine((data, ctx) => {if (data.type === WebhookType.SLACK && !data.webhookUrl.includes("hooks.slack.com")) {@@ -115,9 +119,19 @@ export const registerWebhookRouter = async (server: FastifyZodProvider) => {params: z.object({webhookId: z.string().trim()}),- body: z.object({- isDisabled: z.boolean().default(false)- }),+ body: z+ .object({+ isDisabled: z.boolean().optional(),+ isSecretModifiedEventEnabled: z.boolean().optional(),+ isSecretRotationFailedEventEnabled: z.boolean().optional()+ })+ .refine(({ isDisabled, isSecretModifiedEventEnabled, isSecretRotationFailedEventEnabled }) => {+ return (+ isDisabled !== undefined ||+ isSecretModifiedEventEnabled !== undefined ||+ isSecretRotationFailedEventEnabled !== undefined+ );+ }, "At least one field is required"),response: {200: z.object({message: z.string(),@@ -132,7 +146,9 @@ export const registerWebhookRouter = async (server: FastifyZodProvider) => {actorAuthMethod: req.permission.authMethod,actorOrgId: req.permission.orgId,id: req.params.webhookId,- isDisabled: req.body.isDisabled+ isDisabled: req.body.isDisabled,+ isSecretModifiedEventEnabled: req.body.isSecretModifiedEventEnabled,+ isSecretRotationFailedEventEnabled: req.body.isSecretRotationFailedEventEnabled});await server.services.auditLog.createAuditLog({diff --git a/backend/src/services/webhook/webhook-fns.ts b/backend/src/services/webhook/webhook-fns.tsindex 05fa9114ef..62ed03607b 100644--- a/backend/src/services/webhook/webhook-fns.ts+++ b/backend/src/services/webhook/webhook-fns.ts@@ -362,8 +362,23 @@ export const fnTriggerWebhook = async ({}: TFnTriggerWebhookDTO) => {const webhooks = await webhookDAL.findAllWebhooks(projectId, environment);const toBeTriggeredHooks = webhooks.filter(- ({ secretPath: hookSecretPath, isDisabled }) =>- !isDisabled && picomatch.isMatch(secretPath, hookSecretPath, { strictSlashes: false })+ ({ secretPath: hookSecretPath, isDisabled, isSecretModifiedEventEnabled, isSecretRotationFailedEventEnabled }) => {+ const isEventSubscribed = (() => {+ switch (event.type) {+ case WebhookEvents.SecretModified:+ return isSecretModifiedEventEnabled;+ case WebhookEvents.SecretRotationFailed:+ return isSecretRotationFailedEventEnabled;+ default:+ // all other events are subscribed by default+ return true;+ }+ })();++ return (+ !isDisabled && picomatch.isMatch(secretPath, hookSecretPath, { strictSlashes: false }) && isEventSubscribed+ );+ });if (!toBeTriggeredHooks.length) return;logger.info({ environment, secretPath, projectId }, "Secret webhook job started");diff --git a/backend/src/services/webhook/webhook-service.ts b/backend/src/services/webhook/webhook-service.tsindex 90187dda66..4005596fc4 100644--- a/backend/src/services/webhook/webhook-service.ts+++ b/backend/src/services/webhook/webhook-service.ts@@ -48,7 +48,9 @@ export const webhookServiceFactory = ({environment,secretPath,webhookSecretKey,- type+ type,+ isSecretModifiedEventEnabled,+ isSecretRotationFailedEventEnabled}: TCreateWebhookDTO) => {const { permission } = await permissionService.getProjectPermission({actor,@@ -75,7 +77,9 @@ export const webhookServiceFactory = ({isDisabled: false,secretPath: secretPath || "/",type,- encryptedUrl: secretManagerEncryptor({ plainText: Buffer.from(webhookUrl) }).cipherTextBlob+ encryptedUrl: secretManagerEncryptor({ plainText: Buffer.from(webhookUrl) }).cipherTextBlob,+ ...(isSecretModifiedEventEnabled !== undefined ? { isSecretModifiedEventEnabled } : {}),+ ...(isSecretRotationFailedEventEnabled !== undefined ? { isSecretRotationFailedEventEnabled } : {})};if (webhookSecretKey) {@@ -86,7 +90,16 @@ export const webhookServiceFactory = ({return { ...webhook, projectId, environment: env };};- const updateWebhook = async ({ actorId, actor, actorOrgId, actorAuthMethod, id, isDisabled }: TUpdateWebhookDTO) => {+ const updateWebhook = async ({+ actorId,+ actor,+ actorOrgId,+ actorAuthMethod,+ id,+ isDisabled,+ isSecretModifiedEventEnabled,+ isSecretRotationFailedEventEnabled+ }: TUpdateWebhookDTO) => {const webhook = await webhookDAL.findById(id);if (!webhook) throw new NotFoundError({ message: `Webhook with ID '${id}' not found` });@@ -100,7 +113,17 @@ export const webhookServiceFactory = ({});ForbiddenError.from(permission).throwUnlessCan(ProjectPermissionActions.Edit, ProjectPermissionSub.Webhooks);- const updatedWebhook = await webhookDAL.updateById(id, { isDisabled });+ const updateData: {+ isDisabled?: boolean;+ isSecretModifiedEventEnabled?: boolean;+ isSecretRotationFailedEventEnabled?: boolean;+ } = {+ ...(isDisabled !== undefined ? { isDisabled } : {}),+ ...(isSecretModifiedEventEnabled !== undefined ? { isSecretModifiedEventEnabled } : {}),+ ...(isSecretRotationFailedEventEnabled !== undefined ? { isSecretRotationFailedEventEnabled } : {})+ };++ const updatedWebhook = await webhookDAL.updateById(id, updateData);return { ...webhook, ...updatedWebhook };};diff --git a/backend/src/services/webhook/webhook-types.ts b/backend/src/services/webhook/webhook-types.tsindex 7de8473c9c..cc7387991e 100644--- a/backend/src/services/webhook/webhook-types.ts+++ b/backend/src/services/webhook/webhook-types.ts@@ -8,11 +8,15 @@ export type TCreateWebhookDTO = {webhookUrl: string;webhookSecretKey?: string;type: string;+ isSecretModifiedEventEnabled?: boolean;+ isSecretRotationFailedEventEnabled?: boolean;} & TProjectPermission;export type TUpdateWebhookDTO = {id: string;isDisabled?: boolean;+ isSecretModifiedEventEnabled?: boolean;+ isSecretRotationFailedEventEnabled?: boolean;} & Omit<TProjectPermission, "projectId">;export type TTestWebhookDTO = {