Files
calendar/packages/lib/getSafeRedirectUrl.ts
T
e2119879ae refactor: apply biome formatting to packages/sms, prisma, emails, lib (#27880)
* refactor: apply biome formatting to small packages + packages/lib

Format packages/sms, packages/prisma, packages/platform/libraries,
packages/platform/examples, packages/platform/types, packages/emails,
and packages/lib.

Excludes packages/platform/examples/base/src/pages/[bookingUid].tsx
due to pre-existing lint errors.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* revert: remove packages/platform formatting changes

Revert biome formatting for packages/platform as requested.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-13 07:39:01 -03:00

51 lines
1.7 KiB
TypeScript

import { CONSOLE_URL, WEBAPP_URL, WEBSITE_URL, EMBED_LIB_URL } from "@calcom/lib/constants";
import { getTldPlus1 } from "@calcom/lib/getTldPlus1";
// It ensures that redirection URL safe where it is accepted through a query params or other means where user can change it.
export const getSafeRedirectUrl = (url = "") => {
if (!url) {
return null;
}
//It is important that this fn is given absolute URL because urls that don't start with HTTP can still deceive browser into redirecting to another domain
if (url.search(/^https?:\/\//) === -1) {
throw new Error("Pass an absolute URL");
}
const urlParsed = new URL(url);
// Avoid open redirection security vulnerability
if (![CONSOLE_URL, WEBAPP_URL, WEBSITE_URL].some((u) => new URL(u).origin === urlParsed.origin)) {
url = `${WEBAPP_URL}/`;
}
return url;
};
// There is a copy of this fn at packages/embed/embed-core/src/preview.ts as that can't import this function. Keep it in sync
export function isSafeUrlToLoadResourceFrom(urlString: string) {
try {
const url = new URL(urlString);
if (url.protocol !== "http:" && url.protocol !== "https:") {
return false;
}
// Allow localhost for development
if (url.hostname === "localhost" || url.hostname === "127.0.0.1") {
return true;
}
const webappUrl = new URL(WEBAPP_URL);
const embedLibUrl = new URL(EMBED_LIB_URL);
const urlTldPlus1 = getTldPlus1(url.hostname);
const webappTldPlus1 = getTldPlus1(webappUrl.hostname);
const embedLibTldPlus1 = getTldPlus1(embedLibUrl.hostname);
// URLs must share the same TLD+1 so that org domains are also allowed.
return [webappTldPlus1, embedLibTldPlus1].includes(urlTldPlus1);
} catch {
return false;
}
}