fix: add extra security layers for sending messages to attendees (#10636)
Co-authored-by: CarinaWolli <[email protected]>
This commit is contained in:
1 parent
633d85e81b
commit
c7dfa7bc89
33 files changed
+690
-101
No files matched your search
@@ -40,6 +40,7 @@ const ENDPOINTS = [
|
||||
"workflows",
|
||||
"appsRouter",
|
||||
"googleWorkspace",
|
||||
"kycVerification",
|
||||
] as const;
|
||||
export type Endpoint = (typeof ENDPOINTS)[number];
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ import { bookingsRouter } from "./bookings/_router";
|
||||
import { deploymentSetupRouter } from "./deploymentSetup/_router";
|
||||
import { eventTypesRouter } from "./eventTypes/_router";
|
||||
import { googleWorkspaceRouter } from "./googleWorkspace/_router";
|
||||
import { kycVerificationRouter } from "./kycVerification/_router";
|
||||
import { viewerOrganizationsRouter } from "./organizations/_router";
|
||||
import { paymentsRouter } from "./payments/_router";
|
||||
import { slotsRouter } from "./slots/_router";
|
||||
@@ -50,5 +51,6 @@ export const viewerRouter = mergeRouters(
|
||||
users: userAdminRouter,
|
||||
googleWorkspace: googleWorkspaceRouter,
|
||||
admin: adminRouter,
|
||||
kycVerification: kycVerificationRouter,
|
||||
})
|
||||
);
|
||||
@@ -0,0 +1,42 @@
|
||||
import { authedAdminProcedure } from "../../../procedures/authedProcedure";
|
||||
import { router } from "../../../trpc";
|
||||
import { ZVerifyInputSchema } from "./verify.schema";
|
||||
|
||||
type KYCVerificationRouterHandlerCache = {
|
||||
isVerified?: typeof import("./isVerified.handler").isVerifiedHandler;
|
||||
verify?: typeof import("./verify.handler").verifyHandler;
|
||||
};
|
||||
|
||||
const UNSTABLE_HANDLER_CACHE: KYCVerificationRouterHandlerCache = {};
|
||||
|
||||
export const kycVerificationRouter = router({
|
||||
isVerified: authedAdminProcedure.query(async ({ ctx }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.isVerified) {
|
||||
UNSTABLE_HANDLER_CACHE.isVerified = await import("./isVerified.handler").then(
|
||||
(mod) => mod.isVerifiedHandler
|
||||
);
|
||||
}
|
||||
// Unreachable code but required for type safety
|
||||
if (!UNSTABLE_HANDLER_CACHE.isVerified) {
|
||||
throw new Error("Failed to load handler");
|
||||
}
|
||||
|
||||
return UNSTABLE_HANDLER_CACHE.isVerified({
|
||||
ctx,
|
||||
});
|
||||
}),
|
||||
verify: authedAdminProcedure.input(ZVerifyInputSchema).mutation(async ({ ctx, input }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.verify) {
|
||||
UNSTABLE_HANDLER_CACHE.verify = await import("./verify.handler").then((mod) => mod.verifyHandler);
|
||||
}
|
||||
// Unreachable code but required for type safety
|
||||
if (!UNSTABLE_HANDLER_CACHE.verify) {
|
||||
throw new Error("Failed to load handler");
|
||||
}
|
||||
|
||||
return UNSTABLE_HANDLER_CACHE.verify({
|
||||
ctx,
|
||||
input,
|
||||
});
|
||||
}),
|
||||
});
|
||||
@@ -0,0 +1,44 @@
|
||||
import hasKeyInMetadata from "@calcom/lib/hasKeyInMetadata";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
|
||||
|
||||
type IsKYCVerifiedOptions = {
|
||||
ctx: {
|
||||
user: NonNullable<TrpcSessionUser>;
|
||||
};
|
||||
};
|
||||
|
||||
export const isVerifiedHandler = async ({ ctx }: IsKYCVerifiedOptions) => {
|
||||
const user = ctx.user;
|
||||
|
||||
const memberships = await prisma.membership.findMany({
|
||||
where: {
|
||||
accepted: true,
|
||||
userId: user.id,
|
||||
team: {
|
||||
slug: {
|
||||
not: null,
|
||||
},
|
||||
},
|
||||
},
|
||||
select: {
|
||||
team: {
|
||||
select: {
|
||||
metadata: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
let isKYCVerified = user && hasKeyInMetadata(user, "kycVerified") ? !!user.metadata.kycVerified : false;
|
||||
|
||||
if (!isKYCVerified) {
|
||||
//check if user is part of a team that is KYC verified
|
||||
isKYCVerified = !!memberships.find(
|
||||
(membership) =>
|
||||
hasKeyInMetadata(membership.team, "kycVerified") && !!membership.team.metadata.kycVerified
|
||||
);
|
||||
}
|
||||
|
||||
return { isKYCVerified };
|
||||
};
|
||||
@@ -0,0 +1 @@
|
||||
export {};
|
||||
@@ -0,0 +1,76 @@
|
||||
import hasKeyInMetadata from "@calcom/lib/hasKeyInMetadata";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
|
||||
|
||||
import type { TVerifyInputSchema } from "./verify.schema";
|
||||
|
||||
type VerifyOptions = {
|
||||
ctx: {
|
||||
user: NonNullable<TrpcSessionUser>;
|
||||
};
|
||||
input: TVerifyInputSchema;
|
||||
};
|
||||
|
||||
export const verifyHandler = async ({ ctx, input }: VerifyOptions) => {
|
||||
const { name, isTeam } = input;
|
||||
if (isTeam) {
|
||||
const team = await prisma.team.findFirst({
|
||||
where: {
|
||||
slug: name,
|
||||
},
|
||||
});
|
||||
if (!team) {
|
||||
throw new Error("Team not found");
|
||||
}
|
||||
|
||||
if (hasKeyInMetadata(team, "kycVerified") && !!team.metadata.kycVerified) {
|
||||
throw new Error("Team already verified");
|
||||
}
|
||||
|
||||
const metadata = typeof team.metadata === "object" ? team.metadata : {};
|
||||
const updatedMetadata = { ...metadata, kycVerified: true };
|
||||
|
||||
const updatedTeam = await prisma.team.update({
|
||||
where: {
|
||||
id: team.id,
|
||||
},
|
||||
data: {
|
||||
metadata: updatedMetadata,
|
||||
},
|
||||
});
|
||||
return {
|
||||
name: updatedTeam.slug,
|
||||
isTeam: true,
|
||||
};
|
||||
}
|
||||
|
||||
const user = await prisma.user.findFirst({
|
||||
where: {
|
||||
username: name,
|
||||
},
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new Error("User not found");
|
||||
}
|
||||
|
||||
if (hasKeyInMetadata(user, "kycVerified") && !!user.metadata.kycVerified) {
|
||||
throw new Error("User already verified");
|
||||
}
|
||||
|
||||
const metadata = typeof user.metadata === "object" ? user.metadata : {};
|
||||
const updatedMetadata = { ...metadata, kycVerified: true };
|
||||
|
||||
const updatedUser = await prisma.user.update({
|
||||
where: {
|
||||
id: user.id,
|
||||
},
|
||||
data: {
|
||||
metadata: updatedMetadata,
|
||||
},
|
||||
});
|
||||
return {
|
||||
name: updatedUser.username,
|
||||
isTeam: false,
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,8 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const ZVerifyInputSchema = z.object({
|
||||
name: z.string(),
|
||||
isTeam: z.boolean(),
|
||||
});
|
||||
|
||||
export type TVerifyInputSchema = z.infer<typeof ZVerifyInputSchema>;
|
||||
@@ -4,6 +4,7 @@ import hasKeyInMetadata from "@calcom/lib/hasKeyInMetadata";
|
||||
import { getTranslation } from "@calcom/lib/server/i18n";
|
||||
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
|
||||
|
||||
import { isVerifiedHandler } from "../kycVerification/isVerified.handler";
|
||||
import { hasTeamPlanHandler } from "../teams/hasTeamPlan.handler";
|
||||
|
||||
type GetWorkflowActionOptionsOptions = {
|
||||
@@ -25,6 +26,13 @@ export const getWorkflowActionOptionsHandler = async ({ ctx }: GetWorkflowAction
|
||||
const { hasTeamPlan } = await hasTeamPlanHandler({ ctx });
|
||||
isTeamsPlan = !!hasTeamPlan;
|
||||
}
|
||||
|
||||
const { isKYCVerified } = await isVerifiedHandler({ ctx });
|
||||
|
||||
const t = await getTranslation(ctx.user.locale, "common");
|
||||
return getWorkflowActionOptions(t, IS_SELF_HOSTED || isCurrentUsernamePremium || isTeamsPlan);
|
||||
return getWorkflowActionOptions(
|
||||
t,
|
||||
IS_SELF_HOSTED || isCurrentUsernamePremium || isTeamsPlan,
|
||||
isKYCVerified
|
||||
);
|
||||
};
|
||||
@@ -1,6 +1,9 @@
|
||||
import type { Prisma } from "@prisma/client";
|
||||
|
||||
import { isSMSOrWhatsappAction } from "@calcom/features/ee/workflows/lib/actionHelperFunctions";
|
||||
import {
|
||||
isSMSOrWhatsappAction,
|
||||
isTextMessageToAttendeeAction,
|
||||
} from "@calcom/features/ee/workflows/lib/actionHelperFunctions";
|
||||
import {
|
||||
deleteScheduledEmailReminder,
|
||||
scheduleEmailReminder,
|
||||
@@ -22,6 +25,7 @@ import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
|
||||
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
import { isVerifiedHandler } from "../kycVerification/isVerified.handler";
|
||||
import { hasTeamPlanHandler } from "../teams/hasTeamPlan.handler";
|
||||
import type { TUpdateInputSchema } from "./update.schema";
|
||||
import {
|
||||
@@ -80,6 +84,9 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
|
||||
}
|
||||
const hasPaidPlan = IS_SELF_HOSTED || isCurrentUsernamePremium || isTeamsPlan;
|
||||
|
||||
const kycVerified = await isVerifiedHandler({ ctx });
|
||||
const isKYCVerified = kycVerified.isKYCVerified;
|
||||
|
||||
const activeOnEventTypes = await ctx.prisma.eventType.findMany({
|
||||
where: {
|
||||
id: {
|
||||
@@ -421,6 +428,9 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
|
||||
if (!hasPaidPlan && !isSMSOrWhatsappAction(oldStep.action) && isSMSOrWhatsappAction(newStep.action)) {
|
||||
throw new TRPCError({ code: "UNAUTHORIZED" });
|
||||
}
|
||||
if (!isKYCVerified && isTextMessageToAttendeeAction(newStep.action)) {
|
||||
throw new TRPCError({ code: "UNAUTHORIZED", message: "Account needs to be verified" });
|
||||
}
|
||||
const requiresSender =
|
||||
newStep.action === WorkflowActions.SMS_NUMBER || newStep.action === WorkflowActions.WHATSAPP_NUMBER;
|
||||
await ctx.prisma.workflowStep.update({
|
||||
@@ -593,6 +603,9 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
|
||||
if (isSMSOrWhatsappAction(s.action) && !hasPaidPlan) {
|
||||
throw new TRPCError({ code: "UNAUTHORIZED" });
|
||||
}
|
||||
if (isTextMessageToAttendeeAction(s.action)) {
|
||||
throw new TRPCError({ code: "UNAUTHORIZED", message: "Account needs to be verified" });
|
||||
}
|
||||
const { id: _stepId, ...stepToAdd } = s;
|
||||
return stepToAdd;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user