Commit 63e9c7f
committed
fix(edge): accept never demotes, the migration splits into safe-now functions and apply-last lockdown
Round-2 security review fixes:
N1 (Important) — accept_invite could DEMOTE: invites only ever carry role
admin|member, so an owner accepting a stray member-invite went
owner->member, potentially zeroing the account's owners (an admin could
engineer this to bypass the owner-removal rule). Replaced the blind
role-overwriting upsert with a read-then-branch: after the atomic claim,
read the signer's existing membership (.ilike, case-insensitive); no row
inserts at invite.role; existing role >= invite role leaves the row
untouched (invite still consumed); invite role higher updates only the
role column. Reverts the invite to 'pending' on any write failure, same
as before.
N2 (Important) — deploy-order hard dependency: leave/remove_member call
delete_owner_guarded, which lived in the migration gated "APPLY ONLY
AFTER the edge function is live" — so those two actions would 500 for
the entire rollout window. Split the migration:
- 20260801_membership_functions.sql: get_invite_by_token +
delete_owner_guarded + all revokes/grants + the new unique index
(N3). Additive only, safe to apply immediately, BEFORE the edge
function deploys.
- 20260802_account_membership_lockdown.sql: only the 8 drop-policy
statements, keeping the "APPLY ONLY AFTER" gate.
Deleted the old combined 20260801_account_membership_lockdown.sql.
N3 (Minor) — added uq_account_owners_account_lower_wallet, a unique index
on (account_id, lower(wallet_address)), to the functions migration. Closes
the latent trap where the one known checksummed production row could gain
a lowercase sibling (e.g. via accept_invite's insert path) and make
.ilike()/.maybeSingle() lookups start matching 2 rows and 500 forever.
Verified zero duplicate-case pairs exist in prod today; existing data is
NOT normalized (no FK cascade from users to make that safe here).
N4 (Minor) — the ERC-1271 verifyMessage catch-all was returning 503 for
malformed signatures too, not just RPC outages. Added isWellFormedSignature
(0x + even-length hex, >=132 chars) checked before any verifier call;
malformed signatures now fail fast as 401 BAD_SIGNATURE. 503
VERIFY_UNAVAILABLE is now reserved for errors thrown by the ERC-1271 RPC
call path after shape validation already passed.
pnpm test:web stays green (271/271).1 parent bac90ee commit 63e9c7f
4 files changed
Lines changed: 168 additions & 84 deletions
File tree
- apps/expo/supabase/functions/org-membership
- supabase/migrations
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | | - | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
10 | 12 | | |
11 | 13 | | |
12 | 14 | | |
| |||
59 | 61 | | |
60 | 62 | | |
61 | 63 | | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
62 | 72 | | |
63 | 73 | | |
64 | 74 | | |
| |||
164 | 174 | | |
165 | 175 | | |
166 | 176 | | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
167 | 185 | | |
168 | 186 | | |
169 | 187 | | |
| |||
339 | 357 | | |
340 | 358 | | |
341 | 359 | | |
342 | | - | |
343 | | - | |
344 | | - | |
345 | | - | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
346 | 380 | | |
347 | 381 | | |
348 | 382 | | |
349 | 383 | | |
350 | | - | |
351 | | - | |
352 | | - | |
353 | | - | |
354 | | - | |
355 | | - | |
356 | | - | |
357 | | - | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
358 | 407 | | |
359 | 408 | | |
360 | 409 | | |
| |||
759 | 808 | | |
760 | 809 | | |
761 | 810 | | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
762 | 817 | | |
763 | 818 | | |
764 | 819 | | |
| |||
Lines changed: 0 additions & 67 deletions
This file was deleted.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
Lines changed: 22 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
0 commit comments