Why This Matters
In public audits of vibe-coded Supabase apps, ~30% exposed user emails or private records through the public anon key because tables had Row Level Security (RLS) disabled or overly permissive USING (true) policies, or because SUPABASE_SERVICE_ROLE_KEY was prefixed with NEXT_PUBLIC_ / VITE_.
Hardening Checklist for AI Agents
- Never expose
service_roleto the browser — never name itNEXT_PUBLIC_SUPABASE_SERVICE_ROLE_KEYorVITE_SUPABASE_SERVICE_ROLE_KEY. Keep it strictly in server-side Edge Functions or backend API routes. - Enable RLS on EVERY table in
public(ALTER TABLE ... ENABLE ROW LEVEL SECURITY;) immediately upon creation. - Force
auth.uid() = user_idonSELECT,INSERT,UPDATE, andDELETE— avoidUSING (true)on user data tables. - Add
WITH CHECK (auth.uid() = user_id)onINSERTandUPDATEso users cannot spoof anotheruser_idor escalaterolecolumns. - Revoke
anongrants on internal/admin tables and auditGET /rest/v1/OpenAPI schema output to ensure no sensitive tables appear unauthenticated.
Reference SQL Migration (supabase/migrations/001_rls.sql)
CREATE TABLE IF NOT EXISTS public.profiles (
id UUID PRIMARY KEY REFERENCES auth.users(id) ON DELETE CASCADE,
email TEXT NOT NULL,
display_name TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
ALTER TABLE public.profiles ENABLE ROW LEVEL SECURITY;
-- Restrictive policies scoped strictly to authenticated owner
CREATE POLICY "profiles_select_own"
ON public.profiles
FOR SELECT
TO authenticated
USING (auth.uid() = id);
CREATE POLICY "profiles_update_own"
ON public.profiles
FOR UPDATE
TO authenticated
USING (auth.uid() = id)
WITH CHECK (auth.uid() = id);
-- Explicitly deny anon access to private tables
REVOKE ALL ON public.profiles FROM anon;
Verification Steps
- Run
grep -rn "service_role" dist/ .next/static/— must return 0 matches. - Query
GET https://<project>.supabase.co/rest/v1/with only theanonkey — verify private tables are absent or return0rows when queried unauthenticated.