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
2 changes: 1 addition & 1 deletion src/lib/login.functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/routes/auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
55 changes: 55 additions & 0 deletions supabase/migrations/20260927170000_five_attempt_login_lockout.sql
Original file line number Diff line number Diff line change
@@ -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;
Loading