Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(web): validate next redirect in login flow

Richie McIlroyd32dd8944e88
diff --git a/apps/web/app/(org)/login/form.tsx b/apps/web/app/(org)/login/form.tsx
index 0ca73f7d6..c9a491527 100644
--- a/apps/web/app/(org)/login/form.tsx
+++ b/apps/web/app/(org)/login/form.tsx
@@ -21,6 +21,7 @@ import { getOrganizationSSOData } from "@/actions/organization/get-organization-
import { trackEvent } from "@/app/utils/analytics";
import { usePublicEnv } from "@/utils/public-env";
import { getEmailCodeCooldownSeconds, requestEmailCode } from "../auth-email";
+import { getSafeNextPath } from "../safe-next";
const MotionInput = motion(Input);
const MotionLogoBadge = motion(LogoBadge);
@@ -42,6 +43,8 @@ export function LoginForm() {
null,
);
const theme = Cookies.get("theme") || "light";
+ const getNextPath = () =>
+ next ? getSafeNextPath(next, window.location.origin) : null;
useEffect(() => {
document.body.className = theme === "dark" ? "dark" : "light";
@@ -103,13 +106,14 @@ export function LoginForm() {
}, [emailSent]);
const handleGoogleSignIn = () => {
+ const nextPath = getNextPath();
trackEvent("auth_started", {
method: "google",
is_signup: false,
auth_surface: "login",
});
signIn("google", {
- ...(next && next.length > 0 ? { callbackUrl: next } : {}),
+ ...(nextPath ? { callbackUrl: nextPath } : {}),
});
};
@@ -261,9 +265,10 @@ export function LoginForm() {
setLoading(true);
try {
+ const nextPath = getNextPath();
const normalizedEmail = await requestEmailCode({
email,
- next,
+ next: nextPath,
isSignup: false,
authSurface: "login",
});
@@ -274,7 +279,7 @@ export function LoginForm() {
setLastEmailSentTime(sentAt);
const params = new URLSearchParams({
email: normalizedEmail,
- ...(next && { next }),
+ ...(nextPath && { next: nextPath }),
lastSent: sentAt.toString(),
});
router.push(`/verify-otp?${params.toString()}`);
diff --git a/apps/web/app/(org)/login/page.tsx b/apps/web/app/(org)/login/page.tsx
index 596de8b44..195cd182a 100644
--- a/apps/web/app/(org)/login/page.tsx
+++ b/apps/web/app/(org)/login/page.tsx
@@ -1,17 +1,26 @@
import { getCurrentUser } from "@cap/database/auth/session";
+import { serverEnv } from "@cap/env";
import { faArrowLeft } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import Link from "next/link";
import { redirect } from "next/navigation";
+import { getSafeNextPath } from "../safe-next";
import { LoginForm } from "./form";
export const dynamic = "force-dynamic";
-export default async function LoginPage() {
- const session = await getCurrentUser();
+export default async function LoginPage(props: {
+ searchParams: Promise<{ next?: string | string[] }>;
+}) {
+ const [searchParams, session] = await Promise.all([
+ props.searchParams,
+ getCurrentUser(),
+ ]);
+
if (session) {
- redirect("/dashboard");
+ redirect(getSafeNextPath(searchParams.next, serverEnv().WEB_URL));
}
+
return (
<div className="flex relative justify-center items-center w-full h-screen bg-gray-2">
<div className="flex absolute top-10 left-10 gap-2 justify-center items-center transition-opacity hover:opacity-75">