From 90e40105a3df69bbb0c41e1a6fa07c9d675cbf84 Mon Sep 17 00:00:00 2001 From: B SIMAK AHMED <1NZ24CS030@newhorizonindia.edu> Date: Mon, 23 Nov 2026 19:14:36 +0530 Subject: [PATCH 1/2] Restrict password reset requests to admin email --- src/lib/password-reset.functions.ts | 33 ++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/lib/password-reset.functions.ts b/src/lib/password-reset.functions.ts index 7eb4e05..ebc4168 100644 --- a/src/lib/password-reset.functions.ts +++ b/src/lib/password-reset.functions.ts @@ -18,12 +18,40 @@ export const requestPasswordReset = createServerFn({ method: "POST" }) const verified = await verifyTurnstile(data.turnstileToken, ip, "password_reset"); if (!verified) throw new Error("Human verification failed. Please try again."); - const { createClient } = await import("@supabase/supabase-js"); + // Resolve the administrator email server-side from the role assignment. + // Never trust the submitted address or expose service-role credentials. + const { supabaseAdmin } = await import("@/integrations/supabase/client.server"); + const { data: adminRoles, error: roleError } = await supabaseAdmin + .from("user_roles") + .select("user_id") + .eq("role", "admin"); + + if (roleError) { + console.error("[password-reset] admin role lookup failed", roleError); + throw new Error("Unable to process the request."); + } + + let adminEmailMatches = false; + for (const role of adminRoles ?? []) { + const { data: userData, error: userError } = await supabaseAdmin.auth.admin.getUserById(role.user_id); + if (userError) { + console.error("[password-reset] admin user lookup failed", userError); + throw new Error("Unable to process the request."); + } + if (userData.user?.email?.trim().toLowerCase() === data.email) { + adminEmailMatches = true; + break; + } + } + + if (!adminEmailMatches) return { success: false as const, reason: "invalid_email" as const }; + const supabaseUrl = process.env.VITE_SUPABASE_URL || process.env.SUPABASE_URL; const publishableKey = process.env.VITE_SUPABASE_PUBLISHABLE_KEY || process.env.SUPABASE_PUBLISHABLE_KEY; if (!supabaseUrl || !publishableKey) throw new Error("Supabase server configuration is incomplete."); + const { createClient } = await import("@supabase/supabase-js"); const userClient = createClient(supabaseUrl, publishableKey, { auth: { autoRefreshToken: false, persistSession: false, detectSessionInUrl: false }, }); @@ -34,6 +62,5 @@ export const requestPasswordReset = createServerFn({ method: "POST" }) if (error) throw new Error("Unable to process the request."); - // Deliberately do not reveal whether the submitted address exists. - return { success: true }; + return { success: true as const }; }); From 76fbdc920f14537331b7aa7377267ccf511fa50f Mon Sep 17 00:00:00 2001 From: B SIMAK AHMED <1NZ24CS030@newhorizonindia.edu> Date: Mon, 23 Nov 2026 19:14:55 +0530 Subject: [PATCH 2/2] Show red invalid email toast for non-admin reset requests --- src/routes/forgot-password.tsx | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/routes/forgot-password.tsx b/src/routes/forgot-password.tsx index e4daf26..85d3443 100644 --- a/src/routes/forgot-password.tsx +++ b/src/routes/forgot-password.tsx @@ -1,9 +1,8 @@ /** * /forgot-password — Admin password recovery request. * - * Supabase sends the recovery email. The page deliberately returns the same - * success message regardless of whether the address exists to avoid account - * enumeration. + * Supabase sends the recovery email. The server validates the address against + * the administrator role before allowing the reset request. */ import { createFileRoute, Link, useNavigate } from "@tanstack/react-router"; import { useState } from "react"; @@ -46,13 +45,22 @@ function ForgotPasswordPage() { setBusy(true); try { - await requestPasswordReset({ + const result = await requestPasswordReset({ data: { email: normalizedEmail, turnstileToken, }, }); + if (!result.success) { + toast.error("Invalid email address", { + icon: , + }); + setTurnstileToken(""); + setResetKey((key) => key + 1); + return; + } + setSent(true); setTurnstileToken(""); setResetKey((key) => key + 1); @@ -60,6 +68,8 @@ function ForgotPasswordPage() { toast.error("Unable to process the request. Please try again.", { icon: , }); + setTurnstileToken(""); + setResetKey((key) => key + 1); } finally { setBusy(false); }