Make sure that absolute URL is of WEBAPP only (#2624)

This commit is contained in:
Hariom Balhara authored and Omar López committed 2022-05-02 16:21:11 -06:00
1 parent 59c0784cd6
commit a224a46654
6 files changed
+29 -7

No files matched your search

@@ -2,6 +2,7 @@ import { google } from "googleapis";
import type { NextApiRequest, NextApiResponse } from "next";
import { WEBAPP_URL } from "@calcom/lib/constants";
import { getSafeRedirectUrl } from "@calcom/lib/getSafeRedirectUrl";
import prisma from "@calcom/prisma";
import { decodeOAuthState } from "../../_utils/decodeOAuthState";
@@ -10,7 +11,6 @@ const credentials = process.env.GOOGLE_API_CREDENTIALS;
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const { code } = req.query;
if (code && typeof code !== "string") {
res.status(400).json({ message: "`code` must be a string" });
return;
@@ -19,7 +19,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
res.status(400).json({ message: "There are no Google Credentials installed." });
return;
}
const { client_secret, client_id } = JSON.parse(credentials).web;
const redirect_uri = WEBAPP_URL + "/api/integrations/googlecalendar/callback";
@@ -41,5 +40,5 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
},
});
const state = decodeOAuthState(req);
res.redirect(state?.returnTo ?? "/apps/installed");
res.redirect(getSafeRedirectUrl(state?.returnTo) ?? "/apps/installed");
}
@@ -3,6 +3,7 @@ import { TokenResponseIF } from "@hubspot/api-client/lib/codegen/oauth/models/To
import type { NextApiRequest, NextApiResponse } from "next";
import { WEBAPP_URL } from "@calcom/lib/constants";
import { getSafeRedirectUrl } from "@calcom/lib/getSafeRedirectUrl";
import prisma from "@calcom/prisma";
import { decodeOAuthState } from "../../_utils/decodeOAuthState";
@@ -52,5 +53,5 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
});
const state = decodeOAuthState(req);
res.redirect(state?.returnTo ?? "/apps/installed");
res.redirect(getSafeRedirectUrl(state?.returnTo) ?? "/apps/installed");
}
@@ -1,6 +1,7 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { BASE_URL } from "@calcom/lib/constants";
import { getSafeRedirectUrl } from "@calcom/lib/getSafeRedirectUrl";
import prisma from "@calcom/prisma";
import { decodeOAuthState } from "../../_utils/decodeOAuthState";
@@ -62,5 +63,5 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
});
const state = decodeOAuthState(req);
return res.redirect(state?.returnTo ?? "/apps/installed");
return res.redirect(getSafeRedirectUrl(state?.returnTo) ?? "/apps/installed");
}
@@ -1,6 +1,7 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { BASE_URL } from "@calcom/lib/constants";
import { getSafeRedirectUrl } from "@calcom/lib/getSafeRedirectUrl";
import prisma from "@calcom/prisma";
import { decodeOAuthState } from "../../_utils/decodeOAuthState";
@@ -63,5 +64,5 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
});
const state = decodeOAuthState(req);
return res.redirect(state?.returnTo ?? "/apps/installed");
return res.redirect(getSafeRedirectUrl(state?.returnTo) ?? "/apps/installed");
}
+16
View File
@@ -0,0 +1,16 @@
import { WEBAPP_URL, WEBSITE_URL } from "@calcom/lib/constants";
// 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: string | undefined) => {
url = url || "";
if (url.search(/^https?:\/\//) === -1) {
throw new Error("Pass an absolute URL");
}
// Avoid open redirection security vulnerability
if (!url.startsWith(WEBAPP_URL) && !url.startsWith(WEBSITE_URL)) {
url = `${WEBAPP_URL}/`;
}
return url;
};