-
Notifications
You must be signed in to change notification settings - Fork 0
Add functions to ban users (and fixes) #37
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
149 changes: 149 additions & 0 deletions
149
supabase/migrations/20260119090943_ban_user_nuke_content.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,149 @@ | ||
| -- Shadow banning and hard-delete moderation | ||
|
|
||
| create or replace function public.is_banned() | ||
| returns boolean | ||
| language sql | ||
| stable | ||
| as $$ | ||
| -- Access tokens are stateless; this makes bans effective immediately via RLS. | ||
| select exists ( | ||
| select 1 | ||
| from auth.users | ||
| where id = auth.uid() | ||
| and banned_until > now() | ||
| ); | ||
| $$; | ||
|
|
||
| create policy "Deny banned users" on public.accounts | ||
| as restrictive for all | ||
| to authenticated | ||
| using (not public.is_banned()) | ||
| with check (not public.is_banned()); | ||
|
|
||
| create policy "Deny banned users" on public.channels | ||
| as restrictive for all | ||
| to authenticated | ||
| using (not public.is_banned()) | ||
| with check (not public.is_banned()); | ||
|
|
||
| create policy "Deny banned users" on public.tracks | ||
| as restrictive for all | ||
| to authenticated | ||
| using (not public.is_banned()) | ||
| with check (not public.is_banned()); | ||
|
|
||
| create policy "Deny banned users" on public.user_channel | ||
| as restrictive for all | ||
| to authenticated | ||
| using (not public.is_banned()) | ||
| with check (not public.is_banned()); | ||
|
|
||
| create policy "Deny banned users" on public.channel_track | ||
| as restrictive for all | ||
| to authenticated | ||
| using (not public.is_banned()) | ||
| with check (not public.is_banned()); | ||
|
|
||
| create policy "Deny banned users" on public.followers | ||
| as restrictive for all | ||
| to authenticated | ||
| using (not public.is_banned()) | ||
| with check (not public.is_banned()); | ||
|
|
||
| create policy "Deny banned users" on public.broadcast | ||
| as restrictive for all | ||
| to authenticated | ||
| using (not public.is_banned()) | ||
| with check (not public.is_banned()); | ||
|
|
||
| create or replace function public.ban_user( | ||
| target_user_id uuid, | ||
| ban_until timestamp with time zone default now() + interval '4000 years' | ||
| ) | ||
| returns void | ||
| language plpgsql | ||
| security definer | ||
| set search_path = public | ||
| as $$ | ||
| begin | ||
| if auth.role() <> 'service_role' then | ||
| raise exception 'not authorized'; | ||
| end if; | ||
|
|
||
| -- This disables new logins; RLS blocks existing sessions immediately. | ||
| update auth.users | ||
| set banned_until = ban_until | ||
| where id = target_user_id; | ||
|
|
||
| with target_channels as ( | ||
| select uc.channel_id | ||
| from public.user_channel uc | ||
| where uc.user_id = target_user_id | ||
| ), | ||
| tracks_to_delete as ( | ||
| select distinct ct.track_id | ||
| from public.channel_track ct | ||
| where ct.channel_id in (select channel_id from target_channels) | ||
| ) | ||
| delete from public.tracks | ||
| where id in (select track_id from tracks_to_delete); | ||
|
|
||
| with target_channels as ( | ||
| select uc.channel_id | ||
| from public.user_channel uc | ||
| where uc.user_id = target_user_id | ||
| ) | ||
| delete from public.channels | ||
| where id in (select channel_id from target_channels); | ||
|
|
||
| delete from public.user_channel where user_id = target_user_id; | ||
| end; | ||
| $$; | ||
|
|
||
| create or replace function public.ban_user_by_channel_slug( | ||
| channel_slug text, | ||
| ban_until timestamp with time zone default now() + interval '4000 years' | ||
| ) | ||
| returns void | ||
| language plpgsql | ||
| security definer | ||
| set search_path = public | ||
| as $$ | ||
| declare | ||
| target_user_id uuid; | ||
| user_count int; | ||
| begin | ||
| if auth.role() <> 'service_role' then | ||
| raise exception 'not authorized'; | ||
| end if; | ||
|
|
||
| select count(distinct uc.user_id) | ||
| into user_count | ||
| from public.channels c | ||
| join public.user_channel uc on uc.channel_id = c.id | ||
| where c.slug = channel_slug; | ||
|
|
||
| if user_count is null or user_count = 0 then | ||
| raise exception 'no owner found for channel slug %', channel_slug; | ||
| end if; | ||
|
|
||
| if user_count > 1 then | ||
| raise exception 'multiple owners found for channel slug %', channel_slug; | ||
| end if; | ||
|
|
||
| select uc.user_id | ||
| into target_user_id | ||
| from public.channels c | ||
| join public.user_channel uc on uc.channel_id = c.id | ||
| where c.slug = channel_slug | ||
| limit 1; | ||
|
|
||
| perform public.ban_user(target_user_id, ban_until); | ||
| end; | ||
| $$; | ||
|
|
||
| revoke execute on function public.ban_user(uuid, timestamp with time zone) | ||
| from anon, public, authenticated; | ||
|
|
||
| revoke execute on function public.ban_user_by_channel_slug(text, timestamp with time zone) | ||
| from anon, public, authenticated; |
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,4 @@ | ||
| -- Ensure a track can belong to only one channel. | ||
|
|
||
| alter table public.channel_track | ||
| add constraint channel_track_track_id_unique unique (track_id); | ||
33 changes: 33 additions & 0 deletions
33
supabase/migrations/20260119105952_fix_delete_user_search_path.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,33 @@ | ||
| -- Harden delete_user() by pinning search_path. | ||
|
|
||
| create or replace function public.delete_user() | ||
| returns void | ||
| language sql | ||
| security definer | ||
| set search_path = public | ||
| as $$ | ||
| with target_channels as ( | ||
| select channel_id | ||
| from public.user_channel | ||
| where user_id = auth.uid() | ||
| ), | ||
| tracks_to_delete as ( | ||
| select distinct ct.track_id | ||
| from public.channel_track ct | ||
| where ct.channel_id in (select channel_id from target_channels) | ||
| ) | ||
| delete from public.tracks | ||
| where id in (select track_id from tracks_to_delete); | ||
|
|
||
| with target_channels as ( | ||
| select channel_id | ||
| from public.user_channel | ||
| where user_id = auth.uid() | ||
| ) | ||
| delete from public.channels | ||
| where id in (select channel_id from target_channels); | ||
|
|
||
| delete from public.user_channel where user_id = auth.uid(); | ||
| delete from public.accounts where id = auth.uid(); | ||
| delete from auth.users where id = auth.uid(); | ||
| $$; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We had three different firstCREATE or replace function delete_user()
returns void
LANGUAGE SQL SECURITY DEFINER
AS $$
-- delete from channels where user_id = auth.uid();
delete from user_channel where user_id = auth.uid();
delete from auth.users where id = auth.uid();
$$;second-- Overwrite the existing function with new functionality.
-- The two last lines ensure that any orphaned channels and tracks are also deleted.
CREATE or replace function delete_user()
returns void
LANGUAGE SQL SECURITY DEFINER
AS $$
-- Delete user's associations with channels
delete from user_channel where user_id = auth.uid();
-- Delete account and user
delete from accounts where id = auth.uid();
delete from auth.users where id = auth.uid();
-- Delete any orphaned channels
delete from channels where id not in (select channel_id from user_channel);
-- Delete any orphaned tracks
delete from tracks where id not in (select track_id from channel_track);
$$;third version-- Fix for mutable search path
CREATE or replace function delete_user()
returns void
LANGUAGE SQL SECURITY DEFINER
SET search_path TO 'pg_catalog', 'public' -- Explicit search path for security
AS $$
-- Delete user's associations with channels
delete from user_channel where user_id = auth.uid();
-- Delete account and user
delete from accounts where id = auth.uid();
delete from auth.users where id = auth.uid();
-- Delete any orphaned channels
delete from channels where id not in (select channel_id from user_channel);
-- Delete any orphaned tracks
delete from tracks where id not in (select track_id from channel_track);
$$;
fourth(the one in this PR) |
||
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.
Uh oh!
There was an error while loading. Please reload this page.