-
Notifications
You must be signed in to change notification settings - Fork 4
ENG-1426 Bug in permission functions #778
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
maparent
wants to merge
5
commits into
main
Choose a base branch
from
eng-1426-bug-in-permission-functions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+109
−9
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
10e2212
eng-1426 bug in public.upsert_account_in_space, still using editor
maparent c2e851e
Do not change value when null update params
maparent eca7a8d
typing
maparent ba78794
use greatest, not aggregate max
maparent 0c8847b
least, not greatest!
maparent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
77 changes: 77 additions & 0 deletions
77
packages/database/supabase/migrations/20260213155959_upsert_user_permissions.sql
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| ALTER TYPE public.account_local_input ADD ATTRIBUTE permissions public."SpaceAccessPermissions"; | ||
|
|
||
| CREATE OR REPLACE FUNCTION public.my_permissions_in_space( | ||
| space_id_ BIGINT | ||
| ) RETURNS public."SpaceAccessPermissions" | ||
| SET search_path = '' | ||
| LANGUAGE sql | ||
| AS $$ | ||
| SELECT permissions FROM public."SpaceAccess" WHERE space_id=space_id_ AND account_uid = auth.uid(); | ||
| $$; | ||
|
|
||
| CREATE OR REPLACE FUNCTION public.upsert_account_in_space( | ||
| space_id_ BIGINT, | ||
| local_account public.account_local_input | ||
| ) RETURNS BIGINT | ||
| SECURITY DEFINER | ||
| SET search_path = '' | ||
| LANGUAGE plpgsql | ||
| AS $$ | ||
| DECLARE | ||
| platform_ public."Platform"; | ||
| account_id_ BIGINT; | ||
| user_uid UUID; | ||
| permissions_ public."SpaceAccessPermissions"; | ||
| BEGIN | ||
| SELECT platform INTO STRICT platform_ FROM public."Space" WHERE id = space_id_; | ||
| INSERT INTO public."PlatformAccount" AS pa ( | ||
| account_local_id, name, platform | ||
| ) VALUES ( | ||
| local_account.account_local_id, local_account.name, platform_ | ||
| ) ON CONFLICT (account_local_id, platform) DO UPDATE SET | ||
| name = COALESCE(NULLIF(TRIM(EXCLUDED.name), ''), pa.name) | ||
| RETURNING id, dg_account INTO STRICT account_id_, user_uid; | ||
| IF user_uid IS NOT NULL THEN | ||
| -- is any permission specified in the input? | ||
| permissions_ := COALESCE( | ||
| local_account.permissions, | ||
| CASE WHEN local_account.space_editor IS true THEN 'editor' -- legacy | ||
| WHEN local_account.space_editor IS false THEN 'reader' END); | ||
| INSERT INTO public."SpaceAccess" as sa (space_id, account_uid, permissions) | ||
| VALUES (space_id_, user_uid, least(my_permissions_in_space(space_id_), COALESCE(permissions_, 'editor'))) | ||
| ON CONFLICT (space_id, account_uid) | ||
| DO UPDATE SET permissions = CASE | ||
| WHEN permissions_ IS NULL THEN permissions | ||
| ELSE least(my_permissions_in_space(space_id_), permissions_) | ||
| END; | ||
| END IF; | ||
| INSERT INTO public."LocalAccess" (space_id, account_id) values (space_id_, account_id_) | ||
| ON CONFLICT (space_id, account_id) | ||
| DO NOTHING; | ||
| IF local_account.email IS NOT NULL THEN | ||
| -- TODO: how to distinguish basic untrusted from platform placeholder email? | ||
| INSERT INTO public."AgentIdentifier" as ai (account_id, value, identifier_type, trusted) VALUES (account_id_, local_account.email, 'email', COALESCE(local_account.email_trusted, false)) | ||
| ON CONFLICT (value, identifier_type, account_id) | ||
| DO UPDATE SET trusted = COALESCE(local_account.email_trusted, ai.trusted, false); | ||
| END IF; | ||
| RETURN account_id_; | ||
| END; | ||
| $$; | ||
|
|
||
|
|
||
| DROP FUNCTION create_account_in_space; | ||
|
|
||
| CREATE OR REPLACE FUNCTION create_account_in_space( | ||
| space_id_ BIGINT, | ||
| account_local_id_ varchar, | ||
| name_ varchar, | ||
| email_ varchar = null, | ||
| email_trusted boolean = true, | ||
| permissions_ public."SpaceAccessPermissions" = 'editor' | ||
| ) RETURNS BIGINT | ||
| SECURITY DEFINER | ||
| SET search_path = '' | ||
| LANGUAGE sql | ||
| AS $$ | ||
| SELECT public.upsert_account_in_space(space_id_, ROW(name_, account_local_id_ ,email_, email_trusted, null, permissions_)::public.account_local_input); | ||
| $$; |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔴
least()with NULL frommy_permissions_in_spacecauses NOT NULL constraint violation on INSERTWhen
my_permissions_in_space(space_id_)returns NULL (which happens when the calling user accesses the space through a group rather than having a directSpaceAccessentry),least(NULL, COALESCE(permissions_, 'editor'))evaluates to NULL in PostgreSQL. This NULL is then used as thepermissionsvalue in the INSERT intoSpaceAccess, which has aNOT NULLconstraint on that column, causing the INSERT to fail with a constraint violation error.Root Cause and Impact
The
my_permissions_in_spacefunction atpackages/database/supabase/schemas/account.sql:192-193only checks for direct user access:But other permission-checking functions like
in_spaceandmy_space_idsusemy_user_accounts()which includes group memberships. When a user accesses a space through a group,my_permissions_in_spacereturns NULL.In PostgreSQL,
least(NULL, 'editor')returns NULL. The INSERT at line 225 becomes:which violates the
NOT NULLconstraint onpermissions(packages/database/supabase/schemas/account.sql:108).The same issue affects the ON CONFLICT UPDATE path at line 229:
which would also set permissions to NULL.
Impact: Any user who accesses a space through a group membership and triggers
upsert_account_in_space(e.g., during Roam sync) will get a database error, preventing account upsert operations from completing.Prompt for agents
Was this helpful? React with 👍 or 👎 to provide feedback.