Scheduling infrastructure for absolutely everyone.
fix: Exclude the possibility to book events with invalid email addresses (#9898)
jemiluv8bfc2b8df3408
diff --git a/packages/features/bookings/lib/getBookingResponsesSchema.ts b/packages/features/bookings/lib/getBookingResponsesSchema.tsindex adfe196842d..dd61982c77a 100644--- a/packages/features/bookings/lib/getBookingResponsesSchema.ts+++ b/packages/features/bookings/lib/getBookingResponsesSchema.ts@@ -3,7 +3,7 @@ import z from "zod";import type { ALL_VIEWS } from "@calcom/features/form-builder/FormBuilderFieldsSchema";import type { eventTypeBookingFields } from "@calcom/prisma/zod-utils";-import { bookingResponses } from "@calcom/prisma/zod-utils";+import { bookingResponses, emailSchemaRefinement } from "@calcom/prisma/zod-utils";type EventType = Parameters<typeof preprocess>[0]["eventType"];// eslint-disable-next-line @typescript-eslint/ban-types@@ -114,7 +114,7 @@ function preprocess<T extends z.ZodType>({eventType.bookingFields.forEach((bookingField) => {const value = responses[bookingField.name];const stringSchema = z.string();- const emailSchema = isPartialSchema ? z.string() : z.string().email();+ const emailSchema = isPartialSchema ? z.string() : z.string().refine(emailSchemaRefinement);const phoneSchema = isPartialSchema? z.string(): z.string().refine((val) => isValidPhoneNumber(val));diff --git a/packages/prisma/zod-utils.ts b/packages/prisma/zod-utils.tsindex 9baf5f2c05f..b99ee957bca 100644--- a/packages/prisma/zod-utils.ts+++ b/packages/prisma/zod-utils.ts@@ -576,3 +576,11 @@ export const allManagedEventTypeProps: { [k in keyof Omit<Prisma.EventTypeSelectexport const unlockedManagedEventTypeProps = {...pick(allManagedEventTypeProps, ["locations", "scheduleId", "destinationCalendar"]),};++// The PR at https://github.com/colinhacks/zod/pull/2157 addresses this issue and improves email validation+// I introduced this refinement(to be used with z.email()) as a short term solution until we upgrade to a zod+// version that will include updates in the above PR.+export const emailSchemaRefinement = (value: string) => {+ const emailRegex = /^([A-Z0-9_+-]+\.?)*[A-Z0-9_+-]@([A-Z0-9][A-Z0-9-]*\.)+[A-Z]{2,}$/i+ return emailRegex.test(value)+}