Skip to content

Security

Row level security and least privilege in AI apps

How Postgres and Supabase row level security protects each user's data even when the code or the model gets it wrong, and how to apply least privilege to keys and tools.

Talqeen wrote this article and no editor has reviewed it yet. Search engines do not see this page until it is reviewed.

In a multi-user app the worst bug is one user seeing another's data. Add a model that writes queries or calls tools and the risk grows. Do not rely on every line of code being right: put the rule in the database itself.

What RLS is

Row level security is a Postgres feature Supabase uses heavily. Once enabled on a table, a query sees only the rows a policy allows:

alter table public.projects enable row level security;

create policy "owner_all" on public.projects
  for all to authenticated
  using (owner_id = (select auth.uid()))
  with check (owner_id = (select auth.uid()));

Even if code forgets the owner condition, the database returns the user's rows only.

Practical rules

  • Enable RLS in the same migration that creates the table.
  • Test the policies: sign in as one user, try to read another's row, expect failure. Talqeen does this for every project table, and checks that an admin cannot read any project either.
  • Audit automatically: one command that fails when a table has no RLS keeps you from forgetting.

The service key is too strong

Supabase's service key bypasses RLS. It never reaches the browser, is read in one server file used by admin services only, and is never handed to a tool a model calls.

Least privilege for the model

  • Tools act as the user, so the same RLS applies.
  • Separate read from write: a Talqeen project token is read or read-write, and read is enough most days.
  • Narrow the scope: a Talqeen token reaches one project, and the tools take no project id at all.
  • No delete tool unless truly needed, and then with human confirmation.

Why it matters with models

A model can be tricked by injection or simply be wrong. If protection lives only in the prompt, one mistake is enough; if it lives in the database and permissions, the worst case is a failed query.