Dev.to Security ๐Ÿ” Cybersecurity ๐Ÿ‘ 2 ๐Ÿ“– 5 min read

Static Auditing Supabase Migrations: Catching Missing RLS & SECURITY DEFINER Bugs Before Prod

The Vibe-Coding Security Gap You scaffolded a Supabase project. Enabled RLS on users. Added a policy. Shipped. Three months later, a pen test finds: analytics_events table: no RLS enabled (RLS-001, Critical) get_u

The Vibe-Coding Security Gap

You scaffolded a Supabase project. Enabled RLS on users. Added a policy. Shipped.

Three months later, a pen test finds:

  • analytics_events table: no RLS enabled (RLS-001, Critical)
  • get_user_stats RPC: SECURITY DEFINER without fixed search_path (RPC-001, High)
  • Frontend calls .rpc('deleted_function') โ€” function dropped in migration 047 (RPC-002, High)
  • .env.example has VITE_SUPABASE_SERVICE_ROLE=eyJ... real JWT, not placeholder (SEC-001, Critical)

None of these throw errors. None show up in logs. They're silent misconfigurations that turn into data breaches.

Enter: Supabase RLS & Vibe-Code Security Auditor

A single-file, zero-dependency Python CLI that reads your SQL migrations (in filename order) and client source, then reports findings with file:line locations and concrete fixes.

# Scan a project
python -m auditor ./my-supabase-project

# JSON for CI, fail on high+critical
python -m auditor ./my-supabase-project --format json --out audit.json --fail-on high

# Or install the CLI entry point
pip install .
supabase-rls-auditor ./my-supabase-project --format md

No Node. No Docker. No database connection. Pure static analysis on your repo.

The Five Rules It Enforces

Rule Severity What It Catches
RLS-001 Critical CREATE TABLE public.xyz without ENABLE ROW LEVEL SECURITY anywhere in migrations
RLS-002 High Policy with USING (true) / WITH CHECK (true) exposed to anon/public, or not explicitly targeting authenticated
RPC-001 High SECURITY DEFINER function missing SET search_path = ... or explicit auth guard (auth.uid() IS NOT NULL)
RPC-002 High Client .rpc('fn_name') calls a function that doesn't exist in final migration state (dropped later)
SEC-001 Critical Service role keys, VITE_* secrets, or real JWTs in client code or .env.example (non-placeholder)

How It Works: Migration-Order State Machine

The scanner parses *.sql files in filename order (lexicographic), building a running schema state:

migrations/
  001_create_users.sql      โ†’ CREATE TABLE users... ; ALTER TABLE users ENABLE RLS;
  002_create_policies.sql   โ†’ CREATE POLICY... USING (auth.uid() = user_id);
  003_add_rpc.sql           โ†’ CREATE FUNCTION get_stats() SECURITY DEFINER SET search_path = public...;
  047_cleanup.sql           โ†’ DROP FUNCTION get_stats();  โ† RPC-002 triggered for any .rpc('get_stats')

This matters because a later migration can invalidate an earlier one. Most linters miss this.

RLS-001: The Silent Table

-- migrations/005_analytics.sql
CREATE TABLE public.analytics_events (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id uuid REFERENCES auth.users(id),
  event_name text,
  props jsonb,
  created_at timestamptz DEFAULT now()
);
-- Missing: ALTER TABLE public.analytics_events ENABLE ROW LEVEL SECURITY;

Finding:

RLS-001 ยท CRITICAL ยท Public table has RLS disabled
Location: migrations/005_analytics.sql:1
Snippet: CREATE TABLE public.analytics_events (
Fix: ALTER TABLE public.analytics_events ENABLE ROW LEVEL SECURITY;

Why Critical: anon role can SELECT * FROM analytics_events by default. No policy can protect what RLS doesn't gate.

RLS-002: The "Wide Open" Policy

-- migrations/006_policies.sql
CREATE POLICY "allow_all" ON public.posts
  FOR ALL USING (true) WITH CHECK (true);  -- ๐Ÿšจ

Finding:

RLS-002 ยท HIGH ยท Policy uses USING (true) exposed to anon/public
Location: migrations/006_policies.sql:2
Fix: Restrict to authenticated: CREATE POLICY "auth_only" ON public.posts FOR ALL TO authenticated USING (auth.uid() = user_id) WITH CHECK (auth.uid() = user_id);

Why High: USING (true) + no TO authenticated = anon can read/write. The scanner flags any policy where the USING/WITH CHECK expression is literally true and the policy isn't explicitly scoped to authenticated.

RPC-001: The Search Path Trap

-- migrations/007_rpcs.sql
CREATE FUNCTION public.get_user_dashboard() 
RETURNS jsonb LANGUAGE plpgsql SECURITY DEFINER AS $$
BEGIN
  RETURN (SELECT jsonb_build_object(...) FROM private.user_secrets WHERE user_id = auth.uid());
END;
$$;
-- Missing: SET search_path = public, pg_catalog

Finding:

RPC-001 ยท HIGH ยท SECURITY DEFINER function lacks fixed search_path
Location: migrations/007_rpcs.sql:1
Fix: Add SET search_path = public, pg_catalog to the function definition.

Why High: Without fixed search_path, a malicious SET search_path = attacker_schema before calling the RPC can hijack unqualified references. Supabase docs require this. The scanner also checks for an explicit auth.uid() IS NOT NULL guard in the function body.

RPC-002: The Stale Client Call

// src/features/dashboard/api.ts
const { data } = await supabase.rpc('get_user_dashboard');  // Function dropped in 047_cleanup.sql

Finding:

RPC-002 ยท HIGH ยท Client RPC calls dropped function 'get_user_dashboard'
Location: src/features/dashboard/api.ts:12
Fix: Remove the call or restore the function in a new migration.

The scanner tracks every CREATE FUNCTION and DROP FUNCTION across migrations, computes the final function set, then cross-references every .rpc('...') call in src/**/*.{ts,tsx,js,jsx}.

SEC-001: The Leaked Secret

# .env.example
VITE_SUPABASE_URL=https://xyz.supabase.co
VITE_SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...  # Real JWT, not placeholder
SUPABASE_SERVICE_ROLE_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...  # Service role in client-accessible file

Finding:

SEC-001 ยท CRITICAL ยท Client-accessible secret detected
Location: .env.example:3
Snippet: VITE_SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Fix: Replace with placeholder: VITE_SUPABASE_ANON_KEY=your-anon-key-here

The scanner checks .env.example at repo root for non-placeholder values (heuristic: base64url-encoded JWTs, keys >32 chars that aren't obvious placeholders like your-..., changeme, xxx).

CI Integration: Gate Your Merges

# .github/workflows/supabase-audit.yml
name: Supabase Security Audit
on: [pull_request]
jobs:
  audit:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with: { python-version: '3.11' }
      - run: pip install -e .
      - run: supabase-rls-auditor . --fail-on high

Exit codes:

  • 0 โ€” scan successful, no findings above --fail-on threshold
  • 1 โ€” findings at/above threshold (CI fails)
  • 2 โ€” invalid path or unreadable files

Library API for Custom Workflows

from auditor import scan_project
from auditor.reporter import render_json, render_markdown

findings = scan_project("./my-project")

# Custom: only RLS findings
rls_findings = [f for f in findings if f.rule.startswith("RLS")]
print(render_markdown(rls_findings))

# Custom: JSON for SIEM ingestion
print(render_json(findings))

What It Doesn't Do (By Design)

  • โŒ Connect to a live database (no credentials, no network)
  • โŒ Validate policy logic correctness (only static patterns)
  • โŒ Parse complex dynamic SQL (raw EXECUTE strings)
  • โŒ Replace supabase db lint or pg_rls โ€” complementary, not competitive

Philosophy: Fast, local, zero-config, catches the class of mistakes that vibe-coding + AI-generated migrations produce most often.

Real-World Catch

Ran this on a production multi-tenant workshop SaaS codebase (production Supabase, 50+ migrations). 17 findings including:

  • RPC-002 at src/features/billing/api.ts:95 โ€” calling process_invoice_atomic dropped in migration 042
  • RPC-001 โ€” three SECURITY DEFINER functions missing SET search_path
  • RLS-002 โ€” two policies with USING (true) on anon-accessible tables

All fixed before next deploy. All fixed before production deploy.

Repository

GitHub: https://github.com/PabloIan92/supabase-rls-auditor

License: MIT. Tests: 7/7 passing (pytest -q --basetemp=".pytest_tmp" -p no:cacheprovider)

If you're shipping Supabase apps without a static RLS/RPC audit in CI, you're guessing. This tool makes it a pass/fail gate. Takes 2 minutes to add.

Apify MCP Server & Pro CI Gate

๐Ÿ“ฐ Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes โ€” full credit and traffic to the original publisher.