From b178eb783e2845a1febd814cac195694f6aab86c Mon Sep 17 00:00:00 2001 From: B SIMAK AHMED <1NZ24CS030@newhorizonindia.edu> Date: Sun, 27 Sep 2026 16:57:40 +0530 Subject: [PATCH 1/3] fix: lock admin login after five failed attempts --- src/lib/login.functions.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/login.functions.ts b/src/lib/login.functions.ts index a3d3f84..f32e34c 100644 --- a/src/lib/login.functions.ts +++ b/src/lib/login.functions.ts @@ -3,7 +3,7 @@ import { createHash } from "node:crypto"; import { verifyTurnstile, getClientIp } from "@/lib/turnstile.server"; const LOCKOUT_MINUTES = 30; -const MAX_FAILED_ATTEMPTS = 4; +const MAX_FAILED_ATTEMPTS = 5; type LockoutRow = { failed_attempts: number; From f49f56074884ab7c3cef3f09472b677ad6b57ecb Mon Sep 17 00:00:00 2001 From: B SIMAK AHMED <1NZ24CS030@newhorizonindia.edu> Date: Sun, 27 Sep 2026 16:57:43 +0530 Subject: [PATCH 2/3] docs: reflect five-attempt admin login lockout --- src/routes/auth.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/auth.tsx b/src/routes/auth.tsx index 8dd78d1..d9d0985 100644 --- a/src/routes/auth.tsx +++ b/src/routes/auth.tsx @@ -3,7 +3,7 @@ * * Flow after successful password login: * - Authorization is role-based on the server; no admin email is hard-coded here - * - Four invalid login attempts per client IP trigger a 30-minute server-side lockout + * - Five invalid login attempts per client IP trigger a 30-minute server-side lockout * - If the admin has a verified TOTP factor → /mfa-verify * - Otherwise → /mfa-setup */ From bc0574ac8a3f32603fc8d7dc6b1f68c93588f5fa Mon Sep 17 00:00:00 2001 From: B SIMAK AHMED <1NZ24CS030@newhorizonindia.edu> Date: Sun, 27 Sep 2026 16:57:46 +0530 Subject: [PATCH 3/3] fix: require five failed attempts before login lockout --- ...60927170000_five_attempt_login_lockout.sql | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 supabase/migrations/20260927170000_five_attempt_login_lockout.sql diff --git a/supabase/migrations/20260927170000_five_attempt_login_lockout.sql b/supabase/migrations/20260927170000_five_attempt_login_lockout.sql new file mode 100644 index 0000000..843d73b --- /dev/null +++ b/supabase/migrations/20260927170000_five_attempt_login_lockout.sql @@ -0,0 +1,55 @@ +-- Set password-login lockout to five failed attempts followed by a 30-minute lock. +-- Keep the database threshold authoritative; the application reads locked_until +-- from this function and blocks subsequent attempts until that timestamp expires. + +ALTER TABLE public.login_lockouts + DROP CONSTRAINT IF EXISTS login_lockouts_failed_attempts_check; + +ALTER TABLE public.login_lockouts + ADD CONSTRAINT login_lockouts_failed_attempts_check + CHECK (failed_attempts >= 0 AND failed_attempts <= 5); + +CREATE OR REPLACE FUNCTION public.login_lockout_record_failure(p_lockout_key text) +RETURNS TABLE (failed_attempts smallint, locked_until timestamptz) +LANGUAGE plpgsql +SECURITY DEFINER +SET search_path = public +AS $$ +BEGIN + IF p_lockout_key IS NULL OR p_lockout_key !~ '^admin_password:[0-9a-f]{64}$' THEN + RAISE EXCEPTION 'Invalid lockout key'; + END IF; + + PERFORM pg_advisory_xact_lock(hashtext(p_lockout_key)); + + INSERT INTO public.login_lockouts (lockout_key, failed_attempts, locked_until, updated_at) + VALUES (p_lockout_key, 1, NULL, now()) + ON CONFLICT (lockout_key) DO UPDATE + SET + failed_attempts = CASE + WHEN public.login_lockouts.locked_until IS NOT NULL + AND public.login_lockouts.locked_until > now() + THEN public.login_lockouts.failed_attempts + WHEN public.login_lockouts.locked_until IS NOT NULL + AND public.login_lockouts.locked_until <= now() + THEN 1 + ELSE LEAST(public.login_lockouts.failed_attempts + 1, 5) + END, + locked_until = CASE + WHEN public.login_lockouts.locked_until IS NOT NULL + AND public.login_lockouts.locked_until > now() + THEN public.login_lockouts.locked_until + WHEN public.login_lockouts.locked_until IS NOT NULL + AND public.login_lockouts.locked_until <= now() + THEN NULL + WHEN public.login_lockouts.failed_attempts + 1 >= 5 + THEN now() + interval '30 minutes' + ELSE NULL + END, + updated_at = now() + RETURNING public.login_lockouts.failed_attempts, public.login_lockouts.locked_until; +END; +$$; + +REVOKE ALL ON FUNCTION public.login_lockout_record_failure(text) FROM PUBLIC, anon, authenticated; +GRANT EXECUTE ON FUNCTION public.login_lockout_record_failure(text) TO service_role;