Skip to content

fix(settings): restrict organization updates to admins and lock server-managed columns - #755

Merged
gkhngyk merged 2 commits into
mainfrom
fix/organization-update-policy
Aug 20, 2026
Merged

gkhngyk merged 2 commits into
mainfrom
fix/organization-update-policy

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Summary

The organizations table carried two permissive UPDATE policies:

Policy Reaches
organizations: admin update rows whose org has the caller as admin
Users cannot update plan fields directly rows whose org has the caller as a member — no column restriction

Postgres ORs permissive policies together, so the wider one decided every case and the admin restriction never applied. The second policy's name states an intent its WITH CHECK does not carry out: it names no columns, so the row's whole shape — plan, subscription_status, plan_overrides, the Stripe ids, the encrypted API key — was writable through PostgREST by anyone in the org, whatever their role.

This came up while looking at the inert Project settings tab (#753): a rename UI would rely on these policies, and they do not hold.

Where the rule belongs

Column-level intent cannot be expressed in a policy at all. An UPDATE policy's WITH CHECK sees only the new row, so "this column may not change" has nothing to compare against — which is why the original attempt names columns in its title and nowhere in its body.

A BEFORE UPDATE trigger sees both rows, so that is where it goes. The policy is now purely about who may update; the trigger is about what may change.

The trigger exempts roles that already bypass RLS. Naming the client roles instead (authenticated, anon) would fail open on any role nobody thought of; asking about the privilege fails closed.

Two writes this would have broken

Both went through the user's own client and touch guarded columns:

  • stripe/subscription — the optimistic plan write after Stripe accepts a plan change
  • stripe/checkout — persisting a newly created stripe_customer_id

Neither is the caller writing their own billing state; both are the server mirroring a decision Stripe already made. They now use the service role, as stripe/webhook and stripe/success always have.

One thing that turned up on the way

stripe/checkout takes organizationId from the request body and never checked that the caller belongs to it. Row-level security was the only thing stopping a request from naming someone else's org. Moving that write to the service role removes the backstop, so the membership check is now explicit — a 403 rather than an implicit no-op.

Related issue

Adjacent to #753, which is the UI half. This is the part that has to be right before a rename form exists.

Type of change

  • fix — Bug fix

How to test

The guard holds. As a non-admin member, PATCH your organization row through the client: expect a permission error rather than a write. As an admin, change plan the same way: expect the trigger to refuse it.

Renaming still works. As an admin, update name on your own org: expect success. This is what #753 will build on.

Billing is unaffected.

  1. Start a checkout on an org with no stripe_customer_id. Expect the customer id to land on the row.
  2. Post a checkout with another org's id in the body. Expect 403.
  3. Change plan from the billing page. Expect the plan to update as before — the write is now server-side, so the optimistic update lands rather than silently failing.
  4. Let a subscription.updated webhook arrive. Expect it to write as it always has; the trigger does not apply to it.

Migration

00062_organization_client_write_guard.sql. Apply after this merges and Vercel has deployed, not before — until the new code is live, the two writes above still go through the user's client and the trigger would reject them. Both fail silently today (neither checks the error) and the webhook repairs the state afterwards, so the ordering is about avoiding noise rather than damage, but there is no reason to invite it.

Verification already done

  • Confirmed against the hosted database that both UPDATE policies are live and permissive, and read the column list the wider one leaves open.
  • Checked every from('organizations') write in web/ and server/: four already use the service role, two used the user's client, and both are changed here. The remaining references are reads and one insert, neither affected.
  • Checked role distribution before making updates admin-only: org creators are already admin, so no existing owner loses access.
  • yarn typecheck, yarn lint (0 errors) and yarn format:check pass from web/.

Not verified by execution: the migration itself. I could not exercise the trigger against a scratch copy, so it has not run anywhere yet. The How to test steps above are the real check, and they should be run right after it is applied.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/)
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

…r-managed columns

The organizations table carried two permissive UPDATE policies. Postgres ORs
permissive policies together, so the wider of the two decided every case and
the admin restriction alongside it never applied. The wider policy names no
columns, which left the row's whole shape -- plan, subscription state, Stripe
ids, the encrypted API key -- writable by any member through PostgREST.

Column-level intent cannot live in a policy: WITH CHECK sees only the new row,
so "this column may not change" has nothing to compare against. The rule moves
to a BEFORE UPDATE trigger, which sees both rows. It exempts roles that already
bypass RLS, so the webhook and the migrations pass through untouched, and it
fails closed on any role nobody anticipated rather than open.

Two legitimate writes went through the user's own client and would have started
failing. Both are the server mirroring a decision Stripe already made, so both
now use the service role, as the webhook and the success route always have.

The checkout route takes its organization id from the request body and never
checked that the caller belongs to that org -- row-level security was the only
thing standing between it and another org's row. Writing with the service role
removes that backstop, so the check is now explicit.
@gkhngyk gkhngyk added bug Something isn't working needs-migration Adds or edits a migration — apply it to the hosted database labels Aug 20, 2026
@gkhngyk
gkhngyk merged commit d73c6f8 into main Aug 20, 2026
5 checks passed
@gkhngyk
gkhngyk deleted the fix/organization-update-policy branch August 20, 2026 17:18
@gkhngyk gkhngyk mentioned this pull request Aug 24, 2026
7 of 12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working needs-migration Adds or edits a migration — apply it to the hosted database

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant