Scheduling infrastructure for absolutely everyone.
fix: fix email exclusion validation for full emails and and @-prefixed domains (#26727)
Anik Dhabal Babuce997447a4cc
diff --git a/packages/features/bookings/lib/getBookingResponsesSchema.test.ts b/packages/features/bookings/lib/getBookingResponsesSchema.test.tsindex 106dfb9d3e6..d3e820aacdb 100644--- a/packages/features/bookings/lib/getBookingResponsesSchema.test.ts+++ b/packages/features/bookings/lib/getBookingResponsesSchema.test.ts@@ -1536,6 +1536,126 @@ describe("excluded email/domain validation", () => {email: "[email protected]",});});++ test("should block full email match when exact email is excluded", async () => {+ const excludedEmails = "[email protected]";++ const schema = getBookingResponsesSchema({+ bookingFields: [+ {+ name: "name",+ type: "name",+ required: true,+ },+ {+ name: "email",+ type: "email",+ required: true,+ excludeEmails: excludedEmails,+ },+ ] as z.infer<typeof eventTypeBookingFields> & z.BRAND<"HAS_SYSTEM_FIELDS">,+ view: "ALL_VIEWS",+ });++ // [email protected] should be blocked+ const parsedResponses = await schema.safeParseAsync({+ name: "test",+ email: "[email protected]",+ });++ expect(parsedResponses.success).toBe(false);++ if (parsedResponses.success) {+ throw new Error("Should not reach here");+ }++ expect(parsedResponses.error.issues[0]).toEqual(+ expect.objectContaining({+ code: "custom",+ message: `{email}${CUSTOM_EMAIL_EXCLUDED_ERROR_MSG}`,+ })+ );+ });++ test("should block emails with domain when domain starts with @", async () => {+ const excludedEmails = "@gmail.com";++ const schema = getBookingResponsesSchema({+ bookingFields: [+ {+ name: "name",+ type: "name",+ required: true,+ },+ {+ name: "email",+ type: "email",+ required: true,+ excludeEmails: excludedEmails,+ },+ ] as z.infer<typeof eventTypeBookingFields> & z.BRAND<"HAS_SYSTEM_FIELDS">,+ view: "ALL_VIEWS",+ });++ // [email protected] should be blocked when @gmail.com is excluded+ const parsedResponses = await schema.safeParseAsync({+ name: "test",+ email: "[email protected]",+ });++ expect(parsedResponses.success).toBe(false);++ if (parsedResponses.success) {+ throw new Error("Should not reach here");+ }++ expect(parsedResponses.error.issues[0]).toEqual(+ expect.objectContaining({+ code: "custom",+ message: `{email}${CUSTOM_EMAIL_EXCLUDED_ERROR_MSG}`,+ })+ );+ });++ test("should block emails with domain when excluded email is just the domain without @", async () => {+ const excludedEmails = "gmail.com";++ const schema = getBookingResponsesSchema({+ bookingFields: [+ {+ name: "name",+ type: "name",+ required: true,+ },+ {+ name: "email",+ type: "email",+ required: true,+ excludeEmails: excludedEmails,+ },+ ] as z.infer<typeof eventTypeBookingFields> & z.BRAND<"HAS_SYSTEM_FIELDS">,+ view: "ALL_VIEWS",+ });++ // [email protected] should be blocked when gmail.com is excluded+ const parsedResponses = await schema.safeParseAsync({+ name: "test",+ email: "[email protected]",+ });++ expect(parsedResponses.success).toBe(false);++ if (parsedResponses.success) {+ throw new Error("Should not reach here");+ }++ expect(parsedResponses.error.issues[0]).toEqual(+ expect.objectContaining({+ code: "custom",+ message: `{email}${CUSTOM_EMAIL_EXCLUDED_ERROR_MSG}`,+ })+ );+ });});describe("require email/domain validation", () => {diff --git a/packages/features/bookings/lib/getBookingResponsesSchema.ts b/packages/features/bookings/lib/getBookingResponsesSchema.tsindex f858b84203e..41918948bf5 100644--- a/packages/features/bookings/lib/getBookingResponsesSchema.ts+++ b/packages/features/bookings/lib/getBookingResponsesSchema.ts@@ -24,6 +24,28 @@ const ensureValidPhoneNumber = (value: string) => {// Replace the space(s) in the beginning with + as it is supposed to be provided in the beginning onlyreturn value.replace(/^ +/, "+");};++/**+ * Checks if a booker email matches an email/domain entry.+ * Supports three formats:+ * - Full email: "[email protected]" - matches exactly+ * - Domain with @ prefix: "@example.com" - matches any email ending with "@example.com"+ * - Domain without @ prefix: "example.com" - matches any email ending with "@example.com"+ */+const doesEmailMatchEntry = (bookerEmail: string, entry: string): boolean => {+ const bookerEmailLower = bookerEmail.toLowerCase();++ if (entry.startsWith("@")) {+ const domain = entry.slice(1).toLowerCase();+ return bookerEmailLower.endsWith("@" + domain);+ }++ if (entry.includes("@")) {+ return bookerEmailLower === entry.toLowerCase();+ }++ return bookerEmailLower.endsWith("@" + entry.toLowerCase());+};export const getBookingResponsesPartialSchema = ({ bookingFields, view, translateFn }: CommonParams) => {const schema = bookingResponses.unwrap().partial().and(catchAllSchema);@@ -208,7 +230,7 @@ function preprocess<T extends z.ZodType>({const excludedEmails =bookingField.excludeEmails?.split(",").map((domain) => domain.trim()) || [];- const match = excludedEmails.find((email) => bookerEmail.endsWith("@" + email));+ const match = excludedEmails.find((excludedEntry) => doesEmailMatchEntry(bookerEmail, excludedEntry));if (match) {ctx.addIssue({code: z.ZodIssueCode.custom,@@ -220,7 +242,9 @@ function preprocess<T extends z.ZodType>({?.split(",").map((domain) => domain.trim()).filter(Boolean) || [];- const requiredEmailsMatch = requiredEmails.find((email) => bookerEmail.endsWith("@" + email));+ const requiredEmailsMatch = requiredEmails.find((requiredEntry) =>+ doesEmailMatchEntry(bookerEmail, requiredEntry)+ );if (requiredEmails.length > 0 && !requiredEmailsMatch) {ctx.addIssue({code: z.ZodIssueCode.custom,