BUILD LOG · COMPANION POST · EPISODE 04

The bug my agents caught.

Any signed in user could have made themselves the owner of someone else's property records by guessing an id. Nobody ever hit it. This is the policy that allowed it, the policy that replaced it, and why shipping the fix still took three builds.

analogtoagents.com/the-bug-my-agents-caught

What the hole actually was

CasaScribe stores a property's members in a property_members table. Your membership row is what grants you access to a property's records, and it carries a role: owner or member.

The original policy governing who may insert into that table was written in September, when the app was local only and the table barely mattered. It checked one thing:

THE POLICY THAT ALLOWED IT
create policy "property_members_insert_self" on property_members
  for insert with check (user_id = auth.uid());

Read that check closely. It verifies that the row you are inserting is about you. It never verifies that you are allowed to join the property you are inserting yourself into.

So a signed in user could insert a membership row naming any property_id they could guess, with role set to owner, and the database would accept it. The row is about them, which is all the policy ever asked.

The same gap existed on update, which is arguably worse: an ordinary member could promote themselves.

The policy that replaced it

THE FIX
create policy "property_members_insert_owner" on property_members
  for insert with check (is_property_owner(property_id));

create policy "property_members_update_owner" on property_members
  for update using (is_property_owner(property_id))
  with check (is_property_owner(property_id));

The question changed from "is this row about me" to "do I already own this property". Only an existing owner can add, change or remove members. Anyone can still remove themselves, which is the one self-service action that is safe.

Why it needed a helper function

The obvious fix is to have the policy query property_members directly to see whether you are an owner. That does not work. A policy on property_members that reads property_members recurses forever, and Postgres aborts with error 42P17, infinite recursion detected in policy.

That is why the check goes through a security definer helper instead:

THE HELPER
create or replace function is_property_owner(p_property_id uuid)
returns boolean as $$
  select exists (
    select 1 from public.property_members pm
    where pm.property_id = p_property_id
      and pm.user_id = auth.uid() and pm.role = 'owner'
  );
$$ language sql stable security definer set search_path = public;

Security definer means the function runs with its owner's rights, so the lookup is not itself subject to the policy that called it. The recursion is what makes this bug interesting: the naive fix fails loudly, and the shape of the workaround is the thing worth copying.

Being precise about the timeline

The episode compresses this, so here is the version you can check. The vulnerable policy dates from 28307a8 on 20 September, a commit called "Prepare CasaScribe for EAS build." It was closed on 3 October at 21:24 in d40935e, which is the first commit of the run shown in the video.

A second commit, 5be4460 at 21:39, is the one captioned "Harden schema after database and security review." It did not close the ownership hole, which was already shut fifteen minutes earlier. It added the guarantees around it.

VERIFIED · git log, 3 October
d40935e  21:24  fix property_members RLS recursion, add per-property tables
f106b95  21:29  Supabase sync engine: row mapping, offline queue, flush and pull
0c4cbb3  21:35  wire real Supabase sign in and data sync into the app
5be4460  21:39  harden schema after database and security review
04291d5  21:43  fix sync and session issues found in review
cee84c1  21:45  only sync with a live session, tighten offline restore
564201b  21:46  add BACKEND_BUILDOUT_SUMMARY.md

That is seven commits in twenty two minutes. Six are code. The seventh is the summary document the agents wrote about their own work, which is where most of the quotes in the episode come from.

What the hardening commit added

Closing the policy is not sufficient on its own, because ownership can still be lost or moved sideways. 5be4460 added the guarantees that make the fix hold:

GUARANTEES
protect_last_owner() · a property always keeps at least one owner. The last owner cannot leave, be removed, or be demoted. Transfer first.
prevent_property_move() · a row's property_id is immutable after insert, so a member of two properties cannot re-parent records between them.
property_members.user_id is immutable, so a membership row cannot be reassigned to someone else.
delete_my_account() revoked from anon, and trigger functions revoked from the API roles.

The re-parenting one is the sort of thing I would never have thought of. If you belong to two properties, and a row's property can be edited, you can move someone else's records into a property you control.

The tests

The suite went from 55 to 58 over the course of this work. The three added for the deep link fix are the ones that matter most, because they exist specifically so the exact failure cannot come back quietly.

What I did not expect: the schema tests run against a real Postgres, not a mock. @electric-sql/pglite runs an in-process Postgres, and the tests act as Supabase's authenticated and anon roles in turn. They cover no recursion, no cross user read or write, self promotion blocked, member versus owner rights, and the cascade behaviour.

A mocked test would have happily passed the vulnerable policy, because the vulnerability was in the policy itself and a mock does not evaluate policies.

VERIFIED · review passes
Database, security and correctness reviewers each checked the work from a fresh context, plus a second pass over the fixes.
npm test · 58 of 58 passing, about 25 seconds

Then shipping it took three builds

This is the half of the episode that is not about security at all.

Build 14 installed and opened. The demo worked. Creating a real account failed with "CasaScribe cloud is not configured for this build." The EAS production environment was missing the Supabase public variables, so the binary shipped without the configuration it needed. Every automated check had passed, because none of them run against a production build.

Build 15 fixed the configuration and proved it: account creation reached Supabase Auth and the verification email arrived. The failure then moved one step later. Tapping the link opened the app and did nothing else. The app never consumed the incoming auth URL and turned it into a session, so it sat on the auth screen.

Build 16 added a native deep link handler, routed the callback through casascribe://auth-callback, handled the implicit, PKCE and token hash payload shapes, and added a regression test for the seam. It passes every automated gate.

I have not tapped that link on my own phone yet. So it is not done.

What the agents said they had not verified

The summary document has a section titled "Not verified or not done," written at the same time as the part that reported the work finished. Nobody asked for it.

QUOTED · BACKEND_BUILDOUT_SUMMARY.md
"A real signed-in device / test-account pass is still required before declaring cloud sync fully operational."

"Photos are not synced. Item, document, property and avatar photos are stored as on-device file URIs, so they will not show on another device."

"When two devices edit the same record, the last write wins."

"The sign-up hint on AuthScreen.js still says data is stored privately on this device. I left it unchanged as instructed, but it is now inaccurate."

"There is still no UI to share a property or to delete an account. The database and AppStore support both."

The last one is my favourite, because it is the agent flagging copy that it was explicitly told to leave alone, and noting that following my instruction made the app say something untrue.

What I would take from this

The policy was not wrong when it was written. It was written for a local only app where the table was decoration, and it became wrong the moment the data moved to a server. Nothing changed in that file between September and October. The context around it changed.

That is the argument for a review pass that reads the whole surface from a fresh context rather than reviewing the diff. A diff review on the sync work would never have opened a policy that nobody touched.

And the second half stands on its own. Three builds, two of them failing after every check went green, is the normal shape of shipping. The checks tell you the code is right. They cannot tell you the build is configured, that the email link works, or that the thing does what you wanted on the device in your hand.

Watch on YouTube   Back to the journal