From demo to production · Carousel

RLS in Supabase: the last wall between the data and everyone

In every app built with AI, Lovable, Bolt or Supabase directly, the database is the last line of defense. The million-dollar question: who really sees the data?

The carousel as it was published, slide after slide. Below, the same content as text.

Why it's critical

The key everyone sees

In apps built with AI, much of the access to data happens with a public key that's visible in the browser. And even when there's a server layer, the database itself is the last line of defense.

  • SPA, SSR or a server function: the rule is enforced in the database, beneath all of them.
  • So what really protects the tables? Only one thing: RLS.
  • In Lovable, new apps (since May 2026) run on TanStack Start with SSR, and RLS is still the layer that protects the data.

In Supabase, RLS isn't an extra wall. It's the only wall.

The AI writes your RLS

And that's exactly the trap

The part that catches builders unprepared: the tool that built your app also generates the security rules from the prompt. Sounds great, but it means rules you never read are guarding all your data.

  • Sometimes an excellent rule, sometimes too permissive, and sometimes a table is forgotten completely.
  • "The code works" doesn't mean "the code is secure". You need to read what was generated.
  • In Lovable, the AI generates the schema, RLS, authentication and server functions automatically, and that's exactly what needs going over by hand.

The AI generates your RLS in a second, but the responsibility for checking it's right stays with you.

The big idea

Every row is asked "who are you?"

The model is simple once you get it: for each table you write one rule or several, and the database runs it on every row. "Is this user allowed to touch this row?" That's it. That's all of RLS.

The rule
USING ( auth.uid() = user_id )
In words
auth.uid() is the logged-in user, user_id is the row's owner. Everyone sees only their own rows.

You don't need to remember syntax. You need to remember the question: "who is supposed to see this row?"

Who sees what

The first and most important rule

The rule you always start from: a user sees only their own rows. A row that doesn't belong to them simply doesn't exist for them. It isn't hidden, it isn't returned at all.

  • Without this rule: one request returns the data of every user.
  • With the rule: the same request returns only what belongs to whoever asked.
  • This doesn't happen in the interface. It happens in the database itself, before anything goes out.

You don't hide rows. You define which rows exist at all for this user.

Who writes what

Not only reading, creating too

Reading is half the story. Without a rule on writing, a user can create an order and register it in someone else's name, or edit a record that isn't theirs.

  • The classic: creating a record with another user's ID.
  • When editing you need to check both what's being touched, and how it stays after the change.
  • A rule for reading and a rule for writing are two separate decisions. You need both.

Protecting reading alone leaves a door open. Someone can still write in your name.

Identity doesn't lie

Who you are is set in the database

Why can't these rules be fooled? Because the question "who is the user?" isn't asked in your code. It's asked inside the database, based on the identity signed at login.

  • The identity comes from the verified login, not from a parameter that was sent.
  • Even if an attacker changes the request in the browser, the identity doesn't move.
  • That's exactly why RLS counts as really secure, and not just "looks secure".

In RLS, identity is set in one place the attacker can't touch: inside the database.

The key that bypasses everything

service_role

There's a second key that bypasses all the rules and sees everything. In Lovable Cloud it's managed for you, and logic that needs it runs in a server function. If you connected your own Supabase, the key is in your hands, and it must never reach the browser.

  • The strong key lives only on the server side, not in the client code.
  • In the browser you use only the public key, which is protected by the rules.
  • If the key leaked into git, it's as if there's no protection.

Perfect RLS is worth zero if the strong key leaked. Guard it like an admin password.

The classic mistake

"I checked that he's an admin"

The code checks "if the user is an admin, show the button", and the button disappears for everyone else. Feels secure. But it's a check in the browser. Anyone opens a console, calls the data directly, and bypasses it.

  • Hiding a button is experience, not security.
  • Admin permission is checked inside the rule in the database, not in the client code.
  • Rule of thumb: everything that runs in the browser, the attacker can change.

A permission check on the client side is a lock whose key is with the user.

Performance too

Not only security

The rule runs on every relevant row in every request. When it's written carelessly, the table that worked fast at 100 rows gets stuck at 100,000. This is exactly where security and performance are the same decision.

  • A rule written right: identity is checked once, not once per row.
  • On the column the rule checks, like ownership, put an index.
  • The same layer that guards the data also decides how fast it arrives.

A well-written rule protects the data. A badly written rule protects it, and slows it down.

How to check

Two minutes that save a disaster

Protection the AI wrote and you didn't check is a guess. The check is simple: log in as two different users, and make sure one doesn't see the other's data. And if you want to check for real, try to break it like an attacker, not like a polite user.

  • Two users, the same action: does each one see only their own?
  • In Lovable Cloud you check the tables and rules straight from the Cloud screen.
  • Every new table: "who is supposed to see this?" And did you read what the AI wrote?

If you didn't check from an attacker's angle, you didn't check at all.

RLS isn't a feature. It's your permission model.

Every table without a rule is an open door. And in a Supabase app, it's the only door.

What closes the chapter

RLS isn't a feature. It's your permission model.

Did a post meet you in your system?

Half an hour, the system open, free. You leave with your open decisions.

or on WhatsApp, if it's a single question. Nothing is stored on the site.