Prevent xss in preview aand load resources from known URL (#18978)

Co-authored-by: Hariom Balhara <[email protected]>
This commit is contained in:
Hariom BalharaandHariom Balhara authored and GitHub committed 2025-02-05 12:17:56 +05:30
1 parent 1232eefece
commit 288248d94f
8 files changed
+153 -7

No files matched your search

+61 -3
View File
@@ -1,11 +1,72 @@
// We can't import @calcom/lib/constants here yet as this file is compiled using Vite
const WEBAPP_URL = process.env.EMBED_PUBLIC_WEBAPP_URL ?? "";
const EMBED_LIB_URL = process.env.EMBED_PUBLIC_EMBED_LIB_URL ?? WEBAPP_URL;
const IS_E2E = process.env.NEXT_PUBLIC_IS_E2E === "1";
if (!WEBAPP_URL) {
throw new Error("WEBAPP_URL is not set");
}
// Because it is only used in Embed Snippet Generator preview that is accessible through dashboard only which has URL WEBAPP_URL, we are good with this strict restriction
if (!IS_E2E && (window.self === window.top || !document.referrer.startsWith(WEBAPP_URL))) {
throw new Error(`This page can only be accessed within an iframe from ${WEBAPP_URL}`);
}
// It is a copy of isSafeUrlToLoadResourceFrom in packages/lib/getSafeRedirectUrl. Keep it in sync
// We can't import that here has it is loaded in a separate Vanilla JS page
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
return [webappTldPlus1, embedLibTldPlus1].includes(urlTldPlus1);
} catch {
return false;
}
function getTldPlus1(hostname: string) {
// Note: It doesn't support multipart tlds like .co.uk and thus makes only one part tld's safe like .com(and thus cal.com)
// If we want to use it elsewhere as well(apart from embed/preview.ts) we must consider Public Suffix List
return hostname.split(".").slice(-2).join(".");
}
}
const searchParams = new URL(document.URL).searchParams;
const embedType = searchParams.get("embedType");
const calLink = searchParams.get("calLink");
const bookerUrl = searchParams.get("bookerUrl");
const embedLibUrl = searchParams.get("embedLibUrl");
if (!bookerUrl || !embedLibUrl) {
throw new Error('Can\'t Preview: Missing "bookerUrl" or "embedLibUrl" query parameter');
}
if (!isSafeUrlToLoadResourceFrom(embedLibUrl)) {
throw new Error('Invalid "embedLibUrl".');
}
if (!isSafeUrlToLoadResourceFrom(bookerUrl)) {
throw new Error('Invalid "bookerUrl".');
}
if (!calLink) {
throw new Error('Missing "calLink" query parameter');
}
// TODO: Reuse the embed code snippet from the embed-snippet package - Not able to use it because of circular dependency
// Install Cal Embed Code Snippet
(function (C, A, L) {
@@ -56,9 +117,6 @@ previewWindow.Cal("init", {
origin: bookerUrl,
});
if (!calLink) {
throw new Error('Missing "calLink" query parameter');
}
if (embedType === "inline") {
previewWindow.Cal("inline", {
elementOrSelector: "#my-embed",