* chore: CSRF protect forgot-password functionality * feat: implement conditional sameSite cookie setting for CSRF tokens - Use WEBAPP_URL to determine secure cookie settings - Set sameSite to 'none' for HTTPS environments to support cross-origin scenarios - Fall back to 'lax' for HTTP development environments - Follows patterns from PRs #23439 and #23556 Co-Authored-By: alex@cal.com <me@alexvanandel.com> * update * change * NIT * minor --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: unknown <adhabal2002@gmail.com> Co-authored-by: Anik Dhabal Babu <81948346+anikdhabal@users.noreply.github.com>
30 lines
801 B
TypeScript
30 lines
801 B
TypeScript
import { randomBytes } from "crypto";
|
|
import { NextResponse } from "next/server";
|
|
|
|
import { WEBAPP_URL } from "@calcom/lib/constants";
|
|
|
|
export async function GET(req: Request) {
|
|
const url = new URL(req.url);
|
|
const sameSiteParam = url.searchParams.get("sameSite");
|
|
|
|
const useSecureCookies = WEBAPP_URL.startsWith("https://");
|
|
|
|
// Validate the param, default to "lax"
|
|
let sameSite: "lax" | "strict" | "none" = "lax";
|
|
if (sameSiteParam === "strict" || (sameSiteParam === "none" && useSecureCookies)) {
|
|
sameSite = sameSiteParam;
|
|
}
|
|
|
|
const token = randomBytes(32).toString("hex");
|
|
const res = NextResponse.json({ csrfToken: token });
|
|
|
|
res.cookies.set("calcom.csrf_token", token, {
|
|
httpOnly: true,
|
|
secure: useSecureCookies,
|
|
sameSite,
|
|
path: "/",
|
|
});
|
|
|
|
return res;
|
|
}
|