Supabase PostgreSQL Next.js Lovable #supabase #rls #vibe-coding #secrets

Zero-Vulnerability Supabase Auth + RLS for Vibe-Coded Apps

Prevent CVE-2025-48757 style data leaks: strict PostgreSQL Row Level Security policies, never exposing service_role in Vite/Next bundles, and safe PostgREST schemas.

View /llms.txt View /llms-full.txt

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

  1. Never expose service_role to the browser — never name it NEXT_PUBLIC_SUPABASE_SERVICE_ROLE_KEY or VITE_SUPABASE_SERVICE_ROLE_KEY. Keep it strictly in server-side Edge Functions or backend API routes.
  2. Enable RLS on EVERY table in public (ALTER TABLE ... ENABLE ROW LEVEL SECURITY;) immediately upon creation.
  3. Force auth.uid() = user_id on SELECT, INSERT, UPDATE, and DELETE — avoid USING (true) on user data tables.
  4. Add WITH CHECK (auth.uid() = user_id) on INSERT and UPDATE so users cannot spoof another user_id or escalate role columns.
  5. Revoke anon grants on internal/admin tables and audit GET /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

View raw SKILL.md markdown
## 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

1. **Never expose `service_role` to the browser** — never name it `NEXT_PUBLIC_SUPABASE_SERVICE_ROLE_KEY` or `VITE_SUPABASE_SERVICE_ROLE_KEY`. Keep it strictly in server-side Edge Functions or backend API routes.
2. **Enable RLS on EVERY table in `public`** (`ALTER TABLE ... ENABLE ROW LEVEL SECURITY;`) immediately upon creation.
3. **Force `auth.uid() = user_id` on `SELECT`, `INSERT`, `UPDATE`, and `DELETE`** — avoid `USING (true)` on user data tables.
4. **Add `WITH CHECK (auth.uid() = user_id)`** on `INSERT` and `UPDATE` so users cannot spoof another `user_id` or escalate `role` columns.
5. **Revoke `anon` grants on internal/admin tables** and audit `GET /rest/v1/` OpenAPI schema output to ensure no sensitive tables appear unauthenticated.

## Reference SQL Migration (`supabase/migrations/001_rls.sql`)

```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 the `anon` key — verify private tables are absent or return `0` rows when queried unauthenticated.