Files
calendar/packages/trpc/server/routers/viewer/webhook/edit.handler.ts
T
Pedro CastroGitHubDevin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
cb36fc201f fix: add URL validation to webhook endpoints (#26593)
Validates webhook URLs on create and update:
- HTTPS required (HTTP allowed for self-hosted and E2E)
- Blocks private IP ranges and localhost
- Blocks cloud metadata endpoints

Existing webhooks are preserved: validation only applies when URL is created or changed.

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-02-05 16:18:10 -03:00

99 lines
2.6 KiB
TypeScript

import { PermissionCheckService } from "@calcom/features/pbac/services/permission-check.service";
import {
updateTriggerForExistingBookings,
deleteWebhookScheduledTriggers,
cancelNoShowTasksForBooking,
} from "@calcom/features/webhooks/lib/scheduleTrigger";
import { validateUrlForSSRFSync } from "@calcom/lib/ssrfProtection";
import { prisma } from "@calcom/prisma";
import { MembershipRole } from "@calcom/prisma/enums";
import type { TrpcSessionUser } from "@calcom/trpc/server/types";
import { TRPCError } from "@trpc/server";
import type { TEditInputSchema } from "./edit.schema";
type EditOptions = {
ctx: {
user: NonNullable<TrpcSessionUser>;
};
input: TEditInputSchema;
};
export const editHandler = async ({ input, ctx }: EditOptions) => {
const { id, ...data } = input;
const webhook = await prisma.webhook.findUnique({
where: {
id,
},
});
if (!webhook) {
return null;
}
// SSRF validation: only validate if URL is being changed
if (data.subscriberUrl && data.subscriberUrl !== webhook.subscriberUrl) {
const validation = validateUrlForSSRFSync(data.subscriberUrl);
if (!validation.isValid) {
throw new TRPCError({
code: "BAD_REQUEST",
message: `Webhook URL is not allowed: ${validation.error}`,
});
}
}
if (webhook.platform) {
const { user } = ctx;
if (user?.role !== "ADMIN") {
throw new TRPCError({ code: "UNAUTHORIZED" });
}
}
if (webhook.teamId) {
const permissionService = new PermissionCheckService();
const hasPermission = await permissionService.checkPermission({
userId: ctx.user.id,
teamId: webhook.teamId,
permission: "webhook.update",
fallbackRoles: [MembershipRole.ADMIN, MembershipRole.OWNER],
});
if (!hasPermission) {
throw new TRPCError({
code: "UNAUTHORIZED",
});
}
}
const updatedWebhook = await prisma.webhook.update({
where: {
id,
},
data: {
...data,
time: data.time ?? null,
timeUnit: data.timeUnit ?? null,
},
});
if (data.active) {
const activeTriggersBefore = webhook.active ? webhook.eventTriggers : [];
await updateTriggerForExistingBookings(webhook, activeTriggersBefore, updatedWebhook.eventTriggers);
} else if (!data.active && webhook.active) {
await cancelNoShowTasksForBooking({
webhook: {
id: webhook.id,
userId: webhook.userId,
teamId: webhook.teamId,
eventTypeId: webhook.eventTypeId,
},
});
await deleteWebhookScheduledTriggers({ webhookId: webhook.id });
}
return updatedWebhook;
};