Scheduling infrastructure for absolutely everyone.
fix: UID cookie isn't accessible/created when in embed as it is third party context. (#19859)
Gene Parmesan Thomas192238f4bff6
diff --git a/packages/trpc/server/routers/viewer/slots/reserveSlot.handler.test.ts b/packages/trpc/server/routers/viewer/slots/reserveSlot.handler.test.tsnew file mode 100644index 00000000000..f1df4adcc58--- /dev/null+++ b/packages/trpc/server/routers/viewer/slots/reserveSlot.handler.test.ts@@ -0,0 +1,103 @@+import { vi, describe, it, expect, beforeEach, afterEach } from "vitest";++// We want to test that the UID cookie set by reserveSlotHandler is configured with the correct+// SameSite and Secure attributes depending on the environment (http vs https).+// reserveSlotHandler relies on WEBAPP_URL being evaluated at import time, so we need to reset modules+// between tests after tweaking process.env to simulate different deployment environments.+//+// The handler also calls into Prisma and the SelectedSlotsRepository, so we stub those parts out.++// We alias the module path once we know WEBAPP_URL has been configured.+const dynamicImportHandler = async () => await import("./reserveSlot.handler");++// The repository static method is used to check for an existing reservation by someone else.+// To keep this unit test isolated from the database layer, we stub this to always resolve falsey.+vi.mock("@calcom/lib/server/repository/selectedSlots", () => ({+ SelectedSlotsRepository: {+ findReservedByOthers: vi.fn().mockResolvedValue(null),+ },+}));++// A tiny helper to build a canned handler context with stubbed Prisma methods.+const buildContext = () => {+ const prismaStub = {+ eventType: {+ // Return a minimal event type that will exercise the happy path.+ findUnique: vi.fn().mockResolvedValue({ users: [{ id: 1 }], seatsPerTimeSlot: null }),+ },+ booking: {+ findFirst: vi.fn().mockResolvedValue(null),+ },+ selectedSlots: {+ upsert: vi.fn().mockResolvedValue(null),+ },+ } as unknown as any;++ // Capture header values to assert on.+ let cookieHeaderValue: string | null = null;+ const resStub = {+ setHeader: vi.fn((_name: string, value: string) => {+ cookieHeaderValue = value;+ }),+ };++ const reqStub = {+ cookies: {},+ };++ return { prismaStub, resStub, reqStub, getCookieHeader: () => cookieHeaderValue };+};++describe("reserveSlotHandler cookie settings", () => {+ const originalWebappUrl = process.env.NEXT_PUBLIC_WEBAPP_URL;++ afterEach(() => {+ // Reset the module registry and restore the WEBAPP_URL between tests.+ vi.resetModules();+ if (originalWebappUrl === undefined) {+ delete process.env.NEXT_PUBLIC_WEBAPP_URL;+ } else {+ process.env.NEXT_PUBLIC_WEBAPP_URL = originalWebappUrl;+ }+ });++ it("sets SameSite=None and Secure when WEBAPP_URL is https", async () => {+ process.env.NEXT_PUBLIC_WEBAPP_URL = "https://example.com";+ vi.resetModules();+ const { reserveSlotHandler } = await dynamicImportHandler();+ const { prismaStub, reqStub, resStub, getCookieHeader } = buildContext();+ await reserveSlotHandler({+ ctx: { prisma: prismaStub, req: reqStub, res: resStub },+ input: {+ slotUtcStartDate: new Date().toISOString(),+ slotUtcEndDate: new Date(Date.now() + 15 * 60 * 1000).toISOString(),+ eventTypeId: 1,+ bookingUid: undefined,+ _isDryRun: false,+ },+ });+ const cookie = getCookieHeader();+ expect(cookie).toMatch(/SameSite=None/);+ expect(cookie).toMatch(/Secure/);+ });++ it("falls back to SameSite=Lax and no Secure for http WEBAPP_URL", async () => {+ process.env.NEXT_PUBLIC_WEBAPP_URL = "http://localhost:3000";+ vi.resetModules();+ const { reserveSlotHandler } = await dynamicImportHandler();+ const { prismaStub, reqStub, resStub, getCookieHeader } = buildContext();+ await reserveSlotHandler({+ ctx: { prisma: prismaStub, req: reqStub, res: resStub },+ input: {+ slotUtcStartDate: new Date().toISOString(),+ slotUtcEndDate: new Date(Date.now() + 15 * 60 * 1000).toISOString(),+ eventTypeId: 1,+ bookingUid: undefined,+ _isDryRun: false,+ },+ });+ const cookie = getCookieHeader();+ expect(cookie).toMatch(/SameSite=Lax/);+ expect(cookie).not.toMatch(/Secure/);+ });+});diff --git a/packages/trpc/server/routers/viewer/slots/reserveSlot.handler.ts b/packages/trpc/server/routers/viewer/slots/reserveSlot.handler.tsindex 51ecf0cb92c..8ed8ad1bc93 100644--- a/packages/trpc/server/routers/viewer/slots/reserveSlot.handler.ts+++ b/packages/trpc/server/routers/viewer/slots/reserveSlot.handler.ts@@ -3,6 +3,7 @@ import type { NextApiRequest, NextApiResponse } from "next";import { v4 as uuid } from "uuid";import dayjs from "@calcom/dayjs";+import { WEBAPP_URL } from "@calcom/lib/constants";import { MINUTES_TO_BOOK } from "@calcom/lib/constants";import { SelectedSlotsRepository } from "@calcom/lib/server/repository/selectedSlots";import type { PrismaClient } from "@calcom/prisma";@@ -99,7 +100,18 @@ export const reserveSlotHandler = async ({ ctx, input }: ReserveSlotOptions) =>});}}- res?.setHeader("Set-Cookie", serialize("uid", uid, { path: "/", sameSite: "lax" }));+ // We need this cookie to be accessible from embeds where the booking flow is displayed within an iframe on a different origin.+ // For third‑party iframe contexts (embeds on other sites), browsers require SameSite=None and Secure to make the cookie available.+ // For local development on http://localhost we fall back to SameSite=Lax to avoid requiring https during development.+ const useSecureCookies = WEBAPP_URL.startsWith("https://");+ res?.setHeader(+ "Set-Cookie",+ serialize("uid", uid, {+ path: "/",+ sameSite: useSecureCookies ? "none" : "lax",+ secure: useSecureCookies,+ })+ );return {uid: uid,};