Repository navigation
fix(settings): restrict organization updates to admins and lock server-managed columns - #755
Merged
Merged
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The
organizationstable carried two permissive UPDATE policies:organizations: admin updateadminUsers cannot update plan fields directlyPostgres 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 CHECKdoes 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 CHECKsees 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 UPDATEtrigger 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 optimisticplanwrite after Stripe accepts a plan changestripe/checkout— persisting a newly createdstripe_customer_idNeither 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/webhookandstripe/successalways have.One thing that turned up on the way
stripe/checkouttakesorganizationIdfrom 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 — a403rather 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 fixHow to test
The guard holds. As a non-admin member,
PATCHyour organization row through the client: expect a permission error rather than a write. As an admin, changeplanthe same way: expect the trigger to refuse it.Renaming still works. As an admin, update
nameon your own org: expect success. This is what #753 will build on.Billing is unaffected.
stripe_customer_id. Expect the customer id to land on the row.403.subscription.updatedwebhook 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
from('organizations')write inweb/andserver/: 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.admin, so no existing owner loses access.yarn typecheck,yarn lint(0 errors) andyarn format:checkpass fromweb/.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 teststeps above are the real check, and they should be run right after it is applied.Checklist
feature/,fix/,chore/,docs/)yarn lintpasses (run fromweb/)yarn typecheckpasses (run fromweb/)yarn format:checkpasses (run fromweb/)