PL-041critical (high if signed-in only)Code

AI document search skips row level security

Your AI search function (match_documents) runs as its owner, so it returns every user's chunks to whoever calls it.

Severity
Critical (high if signed-in only)
Who can exploit it
Anyone with your public key, or any signed-in user
Found by
the free RLS check (static, from 30 Nov 2026) and patchlabs-cli 0.7.0 (from 1 Dec 2026)
Lab proof
Lab-proven on the match_documents template, 30 Nov 2026; per app: file and line in the report

What it is

AI document search (“chat with your files”, a support bot over your help centre, RAG) on Supabase stores text chunks and their embeddings in a table with a vector column, then calls a function such as match_documents() to find the closest chunks. The function is what your app calls with supabase.rpc(), so it is a public API endpoint.

PL-041 means that function is security definer: it runs as the user who created it, which skips every row level security policy on the table. If its query does not filter by auth.uid() itself, it returns the closest chunks from every user. A filter argument (for example metadata @> filter) does not help: the caller sends it and can send {}.

Why AI builders produce it

The table is usually created first with row level security on and an owner-only policy. Then search returns nothing (often because the chunks were written by a background job with the service key and no owner, or because an HNSW index returns too few rows once a policy filters them). Asked to make the chatbot find documents again, an AI builder often adds security definer “so the function can read the table”. Search works, and the person testing only ever sees their own files, so nothing looks wrong.

Example of vulnerable code

Simplified from the patterns we see in AI-built apps. Synthetic: not taken from any real app.

supabase/migrations/…_match_documents.sql (vulnerable)
create policy "Owners read their chunks" on public.documents
  for select to authenticated using ((select auth.uid()) = user_id);   -- skipped by the function below

create or replace function public.match_documents(
  query_embedding vector(1536), match_count int, filter jsonb default '{}')
returns table (id bigint, content text, similarity float)
language plpgsql security definer set search_path = '' as $$
begin
  return query select d.id, d.content, 1 - (d.embedding <=> query_embedding)
  from public.documents d
  where d.metadata @> filter                    -- the caller decides the scope
  order by d.embedding <=> query_embedding limit match_count;
end; $$;

Check it yourself

check-pl-041
# the free RLS check reads supabase/migrations in a public repo: patchlabs.dev/tools/rls-check
# or on your own machine, private repos included (reads the files as text, sends nothing):
uvx --from git+https://github.com/manra399/patchlabs-cli patchlabs scan .

-- or in the SQL editor (read-only): tables with embeddings, and the functions that read them
select c.relname as table_name, c.relrowsecurity as rls_on,
       p.proname as function_name, p.prosecdef as runs_as_owner,
       has_function_privilege('anon', p.oid, 'execute') as anon_can_call
from pg_attribute a
join pg_class c on c.oid = a.attrelid and c.relnamespace = 'public'::regnamespace
join pg_type t on t.oid = a.atttypid and t.typname in ('vector', 'halfvec', 'sparsevec')
left join pg_proc p on p.pronamespace = 'public'::regnamespace and p.prosrc ilike '%' || c.relname || '%'
where a.attnum > 0;
-- runs_as_owner = true on a search function is PL-041 unless its body checks auth.uid()

The fix

Let the table's policy do the scoping: make the function run as the caller, give each chunk an owner, and stop anonymous calls.

supabase/migrations/…_fix_match_documents.sql
language plpgsql security definer set search_path = '' as $$
language plpgsql security invoker set search_path = '' as $$
-- (re-run the full create or replace function with this line)
revoke execute on function public.match_documents(vector, int, jsonb) from public, anon;
grant execute on function public.match_documents(vector, int, jsonb) to authenticated;
create policy "Owners add their chunks" on public.documents
  for insert to authenticated with check ((select auth.uid()) = user_id);

If chunks are written by an Edge Function or a background job with the service key, set user_id from the verified token, not from the request body. If owner-only search returns too few rows on a large table with an HNSW index, add set hnsw.iterative_scan = relaxed_order to the function (pgvector 0.8+) instead of going back to security definer. The pgvector guide covers the other ways this leaks (RLS off, using (true), the service key in an Edge Function).

Code findings come with the file, the line and a one-line fix in the report, for your engineer to make. PatchLabs never pushes to your repo.

Ask your AI builder to fix it

Built with Lovable, Bolt, Replit or Cursor? Paste this into the chat. It asks for this one fix and nothing else, and to see the change before it’s applied. Read the change, or have your engineer read it, before you accept.

Prompt for your AI builder
My Supabase AI document search function (for example match_documents) is SECURITY DEFINER and does not filter by auth.uid(), so it returns every user's chunks. Change it to SECURITY INVOKER so the documents table's row level security decides what each caller sees, make sure the table has RLS on with owner-only select and insert policies (user_id = auth.uid()), revoke execute on the function from public and anon, grant it to authenticated, and set user_id from the verified token wherever chunks are written. If owner-only search returns too few rows with an HNSW index, add set hnsw.iterative_scan = relaxed_order to the function. Show me the exact change before you apply it, and don't change anything else.

Then check again with the free RLS check, or run the full free check on your repo.

How PatchLabs finds it

our Postgres 17 + pgvector 0.8.7 test · fake rows
[BEFORE]  match_documents is security definer
          alice   rpc  → alice's chunk + 2 of bob's   LEAK
          anon    rpc  → 3 chunks, 2 users           LEAK
          anon    GET /documents → []  (the table itself is guarded)
[FIX]     security invoker, revoke anon, owner insert policy
[PROVE]   alice   rpc filter {}        → own chunk only  OK
          alice   rpc filter {bob}     → 0 rows          DENIED
          anon    rpc                  → 401             DENIED
          alice   insert as bob        → 403             DENIED
Run on Postgres 17, pgvector 0.8.7, PostgREST 12.2.12 with Supabase default grants, 30 Nov 2026 · fake rows

The free RLS check finds tables with a vector, halfvec or sparsevec column (in create table or a later add column), whose rows belong to someone (an owner or team column, or a policy that checks the caller). It flags a security definer function that runs a similarity search (<=>, <#>, <->) on one of them and never checks auth.uid() in its body. auth.uid() used only as a parameter default (p_user_id uuid default auth.uid()) does not count, because the caller can pass any id. Critical if anon can call it (Supabase grants that by default), high if execute was revoked from anon only, not flagged if only the service role can call it. Shared knowledge bases with no owner column are not flagged. On 30 Nov 2026 it ran on Supabase's chatgpt-your-files example (clean; flagged once security definer was added) and on six public repos with a security definer match_documents: four had locked it down or scoped it, one used it for shared reference data, and one took the user id from the caller. It reads SQL text and never connects to your database.

When the pattern is intended

A search over content that is public on purpose (your public docs, a product catalogue) can run as its owner. That is fine when no row in the table belongs to a user. Say so in a migration comment, and keep user uploads in a separate table.

Why it’s rated critical (high if signed-in only)

Critical: the chunks are the text of users' files (contracts, health letters, support tickets), and one call with the public key returns them. High when execute was revoked from anon, because any account that can sign up still reads everyone's chunks.

Free check

Does your app have PL-041?

Paste your public repo. PatchLabs Code reads it for this and every other code finding, and the Lab rebuilds your database to test who can read what.

First app free, fixes includedReads your public repo onlyOr run the free static RLS check

Last reviewed 10 Nov 2026. Finding IDs are stable: link to them freely. Spotted a mistake? Tell us.