The View That Stopped Checking Who Was Asking
Bakery Buddy keeps a database view called `v_customer_list`. It's the thing behind the duplicate-phone warning when you start a new order, and a few other spots that need a customer's name, contact info, and lifetime spend in one read instead of three. Views are ordinary in Postgres. What happened to this one isn't something a build or a typecheck could ever catch, and it sat open for 41 days before anyone looked for it.
A view has to be told whose rules to follow
Every table a bakery can see, `customers`, `orders`, and the rest, is protected by row-level security: a policy that only lets a request see rows belonging to its own bakery. That's the whole multi-tenant boundary. One bakery's data physically cannot be read by another bakery's login, because Postgres itself checks a `bakery_id` match on every single row before it leaves the table.
A view sits on top of tables like that, and by default a view runs as whoever *owns* it, not as whoever is asking. Here, the owner is the same privileged role the app's own backend connects as, a role that owns every table and isn't subject to row-level security at all. Left at the default, a view built on protected tables quietly ignores the protection.
Postgres has a fix for exactly this: an option called `security_invoker`, set to true, which makes the view run as the caller instead of the owner, so the same row-level policy applies through the view as if you'd queried the tables directly. The view was built with that option set, back in July. Verified, working, invisible, exactly like a security boundary should be.
A rewrite that never touched the part that mattered
Two months in, the view got rewritten for speed. The original version joined in a customer's order history through three separate lateral subqueries; the rewrite flattened that into one aggregated subquery, a real performance win, and nothing about who could see what. Changing a view's definition in Postgres means writing `CREATE OR REPLACE VIEW` and the new query, which is the normal, idiomatic way to do it. The code comment above that rewrite even said it was safe to run again later, because nothing in the query itself had an ordering dependency to break.
That comment was right about the query and wrong about something it never considered. `security_invoker` isn't part of the `SELECT`. It's a separate option attached to the view, and `CREATE OR REPLACE VIEW` resets those options unless the new statement sets them again. The rewrite's own diff shows exactly one thing changed, the shape of the join, and exactly one thing silently reverted that nobody wrote a line to revert: the view went back to running as its owner. Nothing in that diff looked like a security change, because nothing in it was written as one.
What was actually sitting open
With `security_invoker` gone, the view ran as owner and bypassed row-level security entirely, reading straight across every bakery's `customers` and `orders` rows at once. And the view was reachable the same way any Supabase table or view is reachable: over its own REST endpoint, using the anon key, the public API key that ships inside the JavaScript bundle of every page a browser loads. That key is supposed to be safe to expose, because row-level security is the thing standing between "public key" and "everyone's data." For this one view, for 41 days, nothing was standing there.
A plain, unauthenticated request with nothing but that public key returned every tenant's customer rows. Names, emails, phone numbers, lifetime spend, unpaid balances. Not a sample, all of it, 3,764 rows across every bakery on the platform, to anyone who thought to ask.
Why a clean migration and a green build never said a word
The rewrite applied without an error. The app worked exactly the same before and after, because the app's own requests always carry a real signed-in session, so the owner-versus-caller distinction never shows up in normal use. Nothing about a passing `npm run build` or a quiet migration log tests who else, outside the app entirely, can read the same endpoint. Even Supabase's own security advisor, which does flag common misconfigurations, had nothing to flag here: a view with no RLS policies of its own is often completely fine, that's the entire point of `security_invoker`, and the advisor has no way to know this particular view used to have it set and silently didn't anymore.
The only test that actually proves the boundary holds
It was found the only honest way: by sending the exact request a stranger would send. A plain GET to the view's endpoint, carrying only the public anon key, no session, nothing a real attacker wouldn't also have sitting in their browser's dev tools. The response should come back with zero rows for a key with no login behind it. It came back with a row count instead, which was the whole answer.
The fix is one clause, put back in the same statement that could drop it again: `CREATE OR REPLACE VIEW ... WITH (security_invoker = true) AS ...`. And the comment above the original rewrite now says plainly, in the same file, that `CREATE OR REPLACE VIEW` resets this option every time it runs, so the next person who touches this view's definition sees the warning before they repeat the exact same silent step backward.
What generalizes past one view
Some settings don't live inside the part of a definition you'd actually look at when reviewing a change. A view's `security_invoker`, a function's `search_path`, anything attached to a database object as an option rather than written into its body, has to be re-asserted every time that object gets rebuilt. A diff that only touches the query can still revert a setting the query never mentioned, and nothing about that diff will look like a security change, because it wasn't written as one.
And the only check that actually proves a tenant boundary holds is the one run from outside it, as the key a stranger would actually hold, against the exact endpoint a stranger would actually hit. Not the advisor, not the typecheck, not "the migration said it applied." Those all answer "did this run." Only the outside request answers the question that actually mattered here: can anyone who isn't supposed to, read this.
See what else runs behind that same boundary, for every bakery on Bakery Buddy.
Frequently Asked Questions
What was the actual bug?
A database view, v_customer_list, was originally created with Postgres's security_invoker option set to true, which makes a view run as the caller so row-level security still applies through it. An unrelated performance rewrite of the same view, done with the normal CREATE OR REPLACE VIEW statement, reset that option back to its default without anyone touching the query itself. The view started running as its owner, which bypasses row-level security entirely.
What did that actually expose?
The view read across customers and orders for every bakery at once, with no row-level security filtering it by tenant. Because it bypassed RLS, the public anon key, the key every Supabase-backed site ships inside its own browser bundle, could query the view directly over its REST endpoint and get back every bakery's customer rows: names, emails, phone numbers, lifetime spend, and unpaid balances. All 3,764 rows across every tenant, for 41 days before it was caught.
Why didn't a passing build or migration catch this?
The migration applied without error, and the app itself worked identically before and after, since the app's own requests always carry a real signed-in session, so the owner-versus-caller distinction never surfaces in normal use. Even Supabase's own security advisor had nothing to flag, because a view with no RLS policies of its own is ordinarily fine by design; the advisor had no way to know this specific view used to have the setting and silently lost it.
How was it actually found and fixed?
By sending the exact request an outside attacker would send: a plain, unauthenticated GET to the view's endpoint using only the public anon key. It should return zero rows with no session behind it; it returned a full row count instead, which was the evidence. The fix put the clause back explicitly in the same CREATE OR REPLACE VIEW statement, and the file's own comment now warns that this statement resets the option every time it runs.
I build Bakery Buddy for my wife Lindsay's cake studio, Marin Cake Studio, and this is the bug that taught me a clean migration and a passing build prove nothing about who else can read the data.
Ready to put this into practice?
Join the Waitlist