DEV Community

Pablo Ian Laurino
Pablo Ian Laurino

Posted on

I Built a Zero-Credentials Browser Scanner & GitHub Action to Catch Supabase RLS Leaks Before Prod

The Problem: RLS Is Easy to Forget, Hard to Verify

You've seen the pattern. A migration adds a table:

create table public.invoices (
  id uuid primary key default gen_random_uuid(),
  user_id uuid not null references auth.users(id),
  amount_cents integer not null,
  created_at timestamptz default now()
);
Enter fullscreen mode Exit fullscreen mode

No alter table ... enable row level security. No policies. The table is world-readable to anyone with an anon key. In Supabase, that's everyone.

Or an AI assistant generates a policy:

create policy "users can read own invoices"
  on public.invoices
  for select
  using (true);  -- 💀
Enter fullscreen mode Exit fullscreen mode

using (true) isn't a policy. It's a press release.

Or a SECURITY DEFINER function forgets set search_path:

create function public.get_invoice_total(p_user_id uuid)
returns integer
language sql
security definer
as $$
  select sum(amount_cents) from public.invoices
  where user_id = p_user_id;
$$;
Enter fullscreen mode Exit fullscreen mode

An attacker who can create a malicious public.invoices view (or any object earlier in search_path) hijacks execution context. This is CVE-class stuff.

These aren't theoretical. They show up in real codebases weekly. I know because I've reviewed them.


What This Tool Actually Does

Supabase RLS Guard is a static analyzer for Postgres/Supabase SQL migrations. It runs:

  • 100% in your browser — zero credentials, zero network calls (connect-src 'none' CSP), your SQL never leaves the tab
  • As a GitHub Action — drops into CI, fails the build on high-severity findings
  • Open source — MIT licensed, regex/parser-based, no LLM hallucinations

Browser scanner: https://pabloian92.github.io/supabase-rls-auditor/

Repo + Action: https://github.com/PabloIan92/supabase-rls-auditor


The Five Failure Modes It Catches

RLS-001: Tables Missing ENABLE ROW LEVEL SECURITY

Before:

create table public.invoices (
  id uuid primary key default gen_random_uuid(),
  user_id uuid not null references auth.users(id),
  amount_cents integer not null
);
-- RLS never enabled. anon key reads all rows.
Enter fullscreen mode Exit fullscreen mode

After:

create table public.invoices (
  id uuid primary key default gen_random_uuid(),
  user_id uuid not null references auth.users(id),
  amount_cents integer not null
);

alter table public.invoices enable row level security;

create policy "users read own invoices"
  on public.invoices
  for select
  using (auth.uid() = user_id);
Enter fullscreen mode Exit fullscreen mode

The scanner flags any create table in public (or custom schemas you configure) without a matching enable row level security before the next DDL statement.


RLS-002: USING (true) Policies

Before:

create policy "ai generated this"
  on public.invoices
  for select
  using (true);  -- bypasses RLS entirely
Enter fullscreen mode Exit fullscreen mode

After:

create policy "users read own invoices"
  on public.invoices
  for select
  using (auth.uid() = user_id);
Enter fullscreen mode Exit fullscreen mode

using (true) is functionally equivalent to no policy. The scanner treats it as a high-severity finding. It also catches with check (true) on insert/update policies.


RPC-001: SECURITY DEFINER Without SET search_path = ''

Before:

create function public.get_invoice_total(p_user_id uuid)
returns integer
language sql
security definer
as $$
  select sum(amount_cents) from public.invoices
  where user_id = p_user_id;
$$;
Enter fullscreen mode Exit fullscreen mode

After:

create function public.get_invoice_total(p_user_id uuid)
returns integer
language sql
security definer
set search_path = ''  -- critical
as $$
  select sum(amount_cents) from public.invoices
  where user_id = p_user_id;
$$;
Enter fullscreen mode Exit fullscreen mode

Without set search_path = '', the function resolves unqualified names against the caller's search_path. A malicious public.invoices view created by an attacker (or a sloppy migration) executes with the function's privileges — typically postgres or a privileged role.

The scanner flags any security definer function lacking an explicit set search_path clause.


RLS-003 / RLS-005: Policies Defaulting to PUBLIC (Including anon)

Before:

create policy "anyone can read"
  on public.invoices
  for select
  to public  -- implicit, but explicit is worse
  using (true);
Enter fullscreen mode Exit fullscreen mode

After:

create policy "authenticated users read own"
  on public.invoices
  for select
  to authenticated  -- not public, not anon
  using (auth.uid() = user_id);
Enter fullscreen mode Exit fullscreen mode

Supabase's anon role maps to public. A policy granted to public or anon exposes data to unauthenticated requests. The scanner flags any policy where the to clause includes public, anon, or is omitted (defaults to public).

RLS-005 specifically targets storage.objects — open bucket policies that allow unauthenticated uploads/downloads.


SEC-001: Leaked Secrets in Migrations

Before:

-- migration 20240115_add_webhook.sql
create function public.stripe_webhook()
returns trigger
language plpgsql
security definer
as $$
begin
  -- hardcoded service_role JWT 💀
  perform net.http_post(
    url := 'https://api.stripe.com/v1/webhook_endpoints',
    headers := '{"Authorization": "Bearer sk_live_abc123..."}'::jsonb,
    body := to_jsonb(new)
  );
  return new;
end;
$$;
Enter fullscreen mode Exit fullscreen mode

After:

-- migration 20240115_add_webhook.sql
create function public.stripe_webhook()
returns trigger
language plpgsql
security definer
set search_path = ''
as $$
begin
  -- secret fetched from vault / env at runtime
  perform net.http_post(
    url := 'https://api.stripe.com/v1/webhook_endpoints',
    headers := jsonb_build_object(
      'Authorization', 'Bearer ' || current_setting('app.stripe_secret_key')
    ),
    body := to_jsonb(new)
  );
  return new;
end;
$$;
Enter fullscreen mode Exit fullscreen mode

The scanner uses entropy-based regexes to detect:

  • service_role JWTs (base64url, 3 segments, high entropy)
  • postgresql:// DSNs with passwords
  • Stripe/GitHub/AWS key prefixes (sk_live_, ghp_, AKIA)
  • Generic high-entropy strings in string literals

False positives happen. You can suppress with --ignore-secret comments, but the default is noisy on purpose.


GitHub Action: Drop-In CI Integration

- name: Audit Supabase SQL migrations
  uses: PabloIan92/supabase-rls-auditor@v1
  with:
    path: supabase/migrations
    fail-on-high: 'true'
Enter fullscreen mode Exit fullscreen mode

Inputs:
| Input | Required | Default | Description |
|-------|----------|---------|-------------|
| path | yes | — | Directory or glob of .sql files |
| fail-on-high | no | 'true' | Exit non-zero on HIGH severity |
| fail-on-medium | no | 'false' | Exit non-zero on MEDIUM severity |
| config-file | no | — | Path to .rls-guard.yml for custom rules |
| format | no | 'github' | Output format: github, json, sarif |

Outputs: findings-count, high-count, medium-count, low-count — usable in subsequent steps.

Example SARIF upload:

- name: Upload SARIF
  uses: github/codeql-action/upload-sarif@v3
  if: always()
  with:
    sarif_file: rls-guard-results.sarif
Enter fullscreen mode Exit fullscreen mode

What Static Analysis Cannot Catch (Be Honest)

This tool parses SQL text. It does not execute against a database. That means:

Limitation Example
Runtime policy evaluation A policy using (auth.uid() = user_id) passes static analysis but fails if user_id is nullable and rows have NULL
Policy gaps across operations select policy exists but update missing — scanner catches missing policies per table, but not "this table has select but no insert policy for the same role"
Function body semantics SECURITY DEFINER function calling current_user vs session_user — static analysis sees the clause, not the logic
Dynamic SQL / EXECUTE execute format('select * from %I', user_input) — injection risk invisible to text search
Grants / default privileges grant select on all tables in schema public to anon — not in migration DDL, applied separately

Complement, don't replace: Run pgTAP tests against a real database in CI. Test that anon actually cannot select from invoices. Test that authenticated can select only their rows. Static analysis catches the structural omissions; integration tests catch the logic bugs.


Architecture: Why Zero-Credentials Matters

The browser scanner is a single HTML file (~180 KB gzipped) containing:

  • A TypeScript-compiled parser (Postgres grammar subset via nearley)
  • Rule engine (50+ rules, each a pure function AST → Finding[])
  • Web Worker for non-blocking parsing of large migration sets
  • Zero dependencies on external CDNs — all bundled, CSP default-src 'self'; connect-src 'none'

Your SQL is parsed in a Web Worker, findings rendered to DOM. No fetch, no XMLHttpRequest, no WebSockets. Works offline.

The GitHub Action wraps the same core (compiled to a standalone Node binary via esbuild) in a Docker container (node:20-alpine, ~45 MB). Runs in ~2 seconds for 200 migration files.


Configuration: Custom Rulesets

Create .rls-guard.yml at repo root:

# .rls-guard.yml
rules:
  RLS-001:
    severity: high
    enabled: true
  RLS-002:
    severity: high
    enabled: true
  RPC-001:
    severity: critical
    enabled: true
  SEC-001:
    severity: high
    enabled: true
    # suppress known false positives
    ignore-patterns:
      - "example_secret_do_not_use"
      - "test_.*_key"

# custom schema allowlist (default: public)
schemas:
  - public
  - auth
  - storage

# custom rule: flag any function without SET search_path
custom-rules:
  - id: CUSTOM-001
    name: "Function missing search_path"
    pattern: "CREATE\\s+(OR\\s+REPLACE\\s+)?FUNCTION\\s+[^;]+SECURITY\\s+DEFINER(?!.*SET\\s+SEARCH_PATH)"
    severity: high
    message: "SECURITY DEFINER function must set search_path"
Enter fullscreen mode Exit fullscreen mode

Pass to Action:

with:
  config-file: .rls-guard.yml
Enter fullscreen mode Exit fullscreen mode

Closing Resources

Free Browser Scanner — paste migrations, get instant findings, zero setup:

https://pabloian92.github.io/supabase-rls-auditor/

Pro CI Kit + Custom Ruleset ($49) — SARIF upload, custom rule DSL, Slack/Discord webhooks, priority support:

https://pablian.gumroad.com/l/scdaox

Hosted Apify MCP Server ($0.05/audit) — call from AI agents (Cursor, Claude Code, custom bots) via MCP protocol:

https://apify.com/pabloian92/mcp-supabase-rls-auditor


Final Thought

RLS isn't optional. It's the difference between "my app has a bug" and "my user's data is on Pastebin." Static analysis catches the mistakes you know you'll make — the missing enable, the using (true), the forgotten search_path — before they hit a database.

Run it locally. Add it to CI. Write the pgTAP tests anyway.

Your future self (and your users) will notice the absence of incidents.

Top comments (0)