Skip to content

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

10.4 KB
Raw Blame
import { z } from "zod";

import { WebhooksSchema } from "@app/db/schemas";
import { EventType } from "@app/ee/services/audit-log/audit-log-types";
import { removeTrailingSlash } from "@app/lib/fn";
import { readLimit, writeLimit } from "@app/server/config/rateLimiter";
import { getTelemetryDistinctId } from "@app/server/lib/telemetry";
import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
import { AuthMode } from "@app/services/auth/auth-type";
import { PostHogEventTypes } from "@app/services/telemetry/telemetry-types";
import {
  SUBSCRIBABLE_WEBHOOK_EVENTS,
  TSubscribableWebhookEvent,
  WebhookType
} from "@app/services/webhook/webhook-types";

export const sanitizedWebhookSchema = WebhooksSchema.pick({
  id: true,
  secretPath: true,
  lastStatus: true,
  lastRunErrorMessage: true,
  isDisabled: true,
  createdAt: true,
  updatedAt: true,
  envId: true,
  type: true
}).extend({
  projectId: z.string(),
  environment: z.object({
    id: z.string(),
    name: z.string(),
    slug: z.string()
  }),
  eventsFilter: z.array(
    z.object({
      eventName: z.enum([...SUBSCRIBABLE_WEBHOOK_EVENTS] as [TSubscribableWebhookEvent, ...TSubscribableWebhookEvent[]])
    })
  )
});

export const registerWebhookRouter = async (server: FastifyZodProvider) => {
  server.route({
    method: "POST",
    url: "/",
    config: {
      rateLimit: writeLimit
    },
    onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN]),
    schema: {
      operationId: "createWebhook",
      body: z
        .object({
          type: z.nativeEnum(WebhookType).default(WebhookType.GENERAL),
          projectId: z.string().trim(),
          environment: z.string().trim(),
          webhookUrl: z.string().url().trim(),
          webhookSecretKey: z.string().trim().optional(),
          secretPath: z.string().trim().default("/").transform(removeTrailingSlash),
          eventsFilter: z
            .array(
              z.object({
                eventName: z.enum([...SUBSCRIBABLE_WEBHOOK_EVENTS] as [
                  TSubscribableWebhookEvent,
                  ...TSubscribableWebhookEvent[]
                ])
              })
            )
            .optional()
        })
        .superRefine((data, ctx) => {
          if (data.type === WebhookType.SLACK) {
            const parsed = new URL(data.webhookUrl);
            if (parsed.hostname !== "hooks.slack.com") {
              ctx.addIssue({
                code: z.ZodIssueCode.custom,
                message: "Incoming Webhook URL is invalid.",
                path: ["webhookUrl"]
              });
            }
          }
        }),
      response: {
        200: z.object({
          message: z.string(),
          webhook: sanitizedWebhookSchema
        })
      }
    },
    handler: async (req) => {
      const webhook = await server.services.webhook.createWebhook({
        actor: req.permission.type,
        actorId: req.permission.id,
        actorAuthMethod: req.permission.authMethod,
        actorOrgId: req.permission.orgId,
        ...req.body
      });

      await server.services.auditLog.createAuditLog({
        ...req.auditLogInfo,
        projectId: req.body.projectId,
        event: {
          type: EventType.CREATE_WEBHOOK,
          metadata: {
            environment: webhook.environment.slug,
            webhookId: webhook.id,
            isDisabled: webhook.isDisabled,
            secretPath: webhook.secretPath,
            eventsFilter: webhook.eventsFilter
          }
        }
      });

      await server.services.telemetry.sendPostHogEvents({
        event: PostHogEventTypes.WebhookCreated,
        distinctId: getTelemetryDistinctId(req),
        organizationId: req.permission.orgId,
        properties: {
          projectId: req.body.projectId,
          environment: webhook.environment.slug,
          webhookId: webhook.id,
          type: req.body.type,
          eventTypes: req.body.eventsFilter?.map((e) => e.eventName)
        }
      });

      return { message: "Successfully created webhook", webhook };
    }
  });

  server.route({
    method: "PATCH",
    url: "/:webhookId",
    config: {
      rateLimit: writeLimit
    },
    onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN]),
    schema: {
      operationId: "updateWebhook",
      params: z.object({
        webhookId: z.string().trim()
      }),
      body: z
        .object({
          isDisabled: z.boolean().optional(),
          eventsFilter: z
            .array(
              z.object({
                eventName: z.enum([...SUBSCRIBABLE_WEBHOOK_EVENTS] as [
                  TSubscribableWebhookEvent,
                  ...TSubscribableWebhookEvent[]
                ])
              })
            )
            .optional()
        })
        .refine(({ isDisabled, eventsFilter }) => {
          return isDisabled !== undefined || eventsFilter !== undefined;
        }, "At least one field is required"),
      response: {
        200: z.object({
          message: z.string(),
          webhook: sanitizedWebhookSchema
        })
      }
    },
    handler: async (req) => {
      const webhook = await server.services.webhook.updateWebhook({
        actor: req.permission.type,
        actorId: req.permission.id,
        actorAuthMethod: req.permission.authMethod,
        actorOrgId: req.permission.orgId,
        id: req.params.webhookId,
        isDisabled: req.body.isDisabled,
        eventsFilter: req.body.eventsFilter
      });

      await server.services.auditLog.createAuditLog({
        ...req.auditLogInfo,
        projectId: webhook.projectId,
        event: {
          type: EventType.UPDATE_WEBHOOK_STATUS,
          metadata: {
            environment: webhook.environment.slug,
            webhookId: webhook.id,
            isDisabled: webhook.isDisabled,
            secretPath: webhook.secretPath,
            eventsFilter: webhook.eventsFilter
          }
        }
      });

      void server.services.telemetry
        .sendPostHogEvents({
          event: PostHogEventTypes.WebhookUpdated,
          distinctId: getTelemetryDistinctId(req),
          organizationId: req.permission.orgId,
          properties: { webhookId: webhook.id, projectId: webhook.projectId }
        })
        .catch(() => {});

      return { message: "Successfully updated webhook", webhook };
    }
  });

  server.route({
    method: "DELETE",
    url: "/:webhookId",
    config: {
      rateLimit: writeLimit
    },
    onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN]),
    schema: {
      operationId: "deleteWebhook",
      params: z.object({
        webhookId: z.string().trim()
      }),
      response: {
        200: z.object({
          message: z.string(),
          webhook: sanitizedWebhookSchema
        })
      }
    },
    handler: async (req) => {
      const webhook = await server.services.webhook.deleteWebhook({
        actor: req.permission.type,
        actorId: req.permission.id,
        actorAuthMethod: req.permission.authMethod,
        actorOrgId: req.permission.orgId,
        id: req.params.webhookId
      });

      await server.services.auditLog.createAuditLog({
        ...req.auditLogInfo,
        projectId: webhook.projectId,
        event: {
          type: EventType.DELETE_WEBHOOK,
          metadata: {
            environment: webhook.environment.slug,
            webhookId: webhook.id,
            isDisabled: webhook.isDisabled,
            secretPath: webhook.secretPath
          }
        }
      });

      void server.services.telemetry
        .sendPostHogEvents({
          event: PostHogEventTypes.WebhookDeleted,
          distinctId: getTelemetryDistinctId(req),
          organizationId: req.permission.orgId,
          properties: { webhookId: webhook.id, projectId: webhook.projectId }
        })
        .catch(() => {});

      return { message: "Successfully deleted webhook", webhook };
    }
  });

  server.route({
    method: "POST",
    url: "/:webhookId/test",
    config: {
      rateLimit: writeLimit
    },
    onRequest: verifyAuth([AuthMode.JWT]),
    schema: {
      operationId: "testWebhook",
      params: z.object({
        webhookId: z.string().trim()
      }),
      response: {
        200: z.object({
          message: z.string(),
          webhook: sanitizedWebhookSchema
        })
      }
    },
    handler: async (req) => {
      const webhook = await server.services.webhook.testWebhook({
        actor: req.permission.type,
        actorId: req.permission.id,
        actorAuthMethod: req.permission.authMethod,
        actorOrgId: req.permission.orgId,
        id: req.params.webhookId
      });
      return { message: "Successfully tested webhook", webhook };
    }
  });

  server.route({
    method: "GET",
    url: "/:webhookId",
    config: {
      rateLimit: readLimit
    },
    onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN, AuthMode.OAUTH]),
    schema: {
      operationId: "getWebhookById",
      params: z.object({
        webhookId: z.string().trim()
      }),
      response: {
        200: z.object({
          webhook: sanitizedWebhookSchema.extend({ url: z.string() })
        })
      }
    },
    handler: async (req) => {
      const webhook = await server.services.webhook.getWebhookById({
        actor: req.permission.type,
        actorId: req.permission.id,
        actorAuthMethod: req.permission.authMethod,
        actorOrgId: req.permission.orgId,
        id: req.params.webhookId
      });
      return { webhook };
    }
  });

  server.route({
    method: "GET",
    url: "/",
    config: {
      rateLimit: readLimit
    },
    onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN, AuthMode.OAUTH]),
    schema: {
      operationId: "listWebhooks",
      querystring: z.object({
        projectId: z.string().trim(),
        environment: z.string().trim().optional(),
        secretPath: z
          .string()
          .trim()
          .optional()
          .transform((val) => (val ? removeTrailingSlash(val) : val))
      }),
      response: {
        200: z.object({
          message: z.string(),
          webhooks: sanitizedWebhookSchema.extend({ url: z.string() }).array()
        })
      }
    },
    handler: async (req) => {
      const webhooks = await server.services.webhook.listWebhooks({
        actor: req.permission.type,
        actorId: req.permission.id,
        actorAuthMethod: req.permission.authMethod,
        actorOrgId: req.permission.orgId,
        ...req.query,
        projectId: req.query.projectId
      });
      return { message: "Successfully fetched webhook", webhooks };
    }
  });
};