Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions apps/web/src/__tests__/safe-redirect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { describe, expect, it } from "vitest";
import { safeRedirectPath } from "@/lib/safe-redirect";

describe("safeRedirectPath", () => {
it("preserves internal paths, queries, and fragments", () => {
expect(safeRedirectPath("/usage?range=30d#details")).toBe(
"/usage?range=30d#details"
);
});

it.each([
"https://attacker.example/phish",
"//attacker.example/phish",
"/\\attacker.example/phish",
"javascript:alert(1)",
"account",
])("rejects unsafe redirect %s", (value) => {
expect(safeRedirectPath(value)).toBe("/account");
});

it("uses the requested fallback for missing values", () => {
expect(safeRedirectPath(null, "/")).toBe("/");
});
});
4 changes: 2 additions & 2 deletions apps/web/src/app/api/auth/callback/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { NextRequest, NextResponse } from "next/server";
import { createClient } from "@supabase/supabase-js";
import { safeRedirectPath } from "@/lib/safe-redirect";

const SUPABASE_URL = process.env.NEXT_PUBLIC_SUPABASE_URL;
const SUPABASE_SERVICE_KEY = process.env.SUPABASE_SERVICE_ROLE_KEY;
Expand All @@ -26,8 +27,7 @@ export async function GET(request: NextRequest) {
const { searchParams } = new URL(request.url);
const code = searchParams.get("code");
const ref = searchParams.get("ref") || "";
const nextParam = searchParams.get("next") || "/account";
const nextPath = nextParam.startsWith("/") ? nextParam : "/account";
const nextPath = safeRedirectPath(searchParams.get("next"));

if (!code) {
return NextResponse.redirect(`${APP_URL}/auth/login?error=no_code`);
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/app/auth/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { useMemo, useState } from "react";
import Link from "next/link";
import { setAccessToken } from "@/lib/auth-client";
import { safeRedirectPath } from "@/lib/safe-redirect";

export default function LoginPage() {
const [email, setEmail] = useState("");
Expand All @@ -11,8 +12,7 @@ export default function LoginPage() {
const [error, setError] = useState("");
const nextPath = useMemo(() => {
if (typeof window === "undefined") return "/account";
const raw = new URLSearchParams(window.location.search).get("next") || "/account";
return raw.startsWith("/") ? raw : "/account";
return safeRedirectPath(new URLSearchParams(window.location.search).get("next"));
}, []);

const handleSubmit = async (e: React.FormEvent) => {
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/app/auth/signup/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { useState, useEffect, useMemo } from "react";
import Link from "next/link";
import { safeRedirectPath } from "@/lib/safe-redirect";

export default function SignupPage() {
const [email, setEmail] = useState("");
Expand All @@ -14,8 +15,7 @@ export default function SignupPage() {
const [success, setSuccess] = useState(false);
const nextPath = useMemo(() => {
if (typeof window === "undefined") return "/account";
const raw = new URLSearchParams(window.location.search).get("next") || "/account";
return raw.startsWith("/") ? raw : "/account";
return safeRedirectPath(new URLSearchParams(window.location.search).get("next"));
}, []);

useEffect(() => {
Expand Down
16 changes: 16 additions & 0 deletions apps/web/src/lib/safe-redirect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const REDIRECT_BASE = "https://threatcrush.invalid";

export function safeRedirectPath(
value: string | null | undefined,
fallback = "/account"
): string {
if (!value || !value.startsWith("/")) return fallback;

try {
const url = new URL(value, REDIRECT_BASE);
if (url.origin !== REDIRECT_BASE) return fallback;
return `${url.pathname}${url.search}${url.hash}`;
} catch {
return fallback;
}
}
Loading