deploy / deploy (push) Successful in 1m17s
- Un compte protégé par un code dont seul le mot de passe aurait fuité (jeton aal1) pouvait encore : bloquer / débloquer, supprimer un commentaire, retirer une réaction, supprimer une liste partagée ou en retirer un membre, la quitter, refuser une invitation, lire une carte de profil, envoyer / supprimer ses photos de profil. Tout exige maintenant le code (20261201130000_mfa_everywhere.sql, vérifié en aal1 et aal2 dans une transaction annulée). - ratings : un robot qui change d'adresse IP pouvait remplir la table title_ratings et épuiser la clé Trakt. Plafond commun de 120 recherches par minute (par instance) ; seuls les titres trouvés sont enregistrés. - Vérifié sans changement : règles RLS et gardes (abonnements, profils, suspension), 32 fonctions RPC, accès anonyme (tout refusé), proxy TMDB, suppression de compte, PKCE, en-têtes et CSP, CI, service worker (données publiques seulement), aucun HTML injecté. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YVo1qxKoBQCaMd7Ue8PGbe
200 lines
8.8 KiB
PL/PgSQL
200 lines
8.8 KiB
PL/PgSQL
-- Double authentification exigée partout (audit de sécurité du 01/12/2026).
|
|
--
|
|
-- Un compte protégé par un code (TOTP) dont seul le mot de passe aurait fuité obtient un jeton « aal1 ».
|
|
-- Les lectures et la plupart des écritures le refusaient déjà (azuu_mfa_ok, azuu_me_active, can_view), mais pas :
|
|
-- - bloquer / débloquer, retirer sa réaction, supprimer un commentaire ;
|
|
-- - supprimer une liste partagée, en retirer un membre, la quitter, refuser une invitation, chercher qui inviter ;
|
|
-- - lire la carte d'un profil (get_profile) ;
|
|
-- - envoyer / supprimer / lister ses photos de profil (stockage).
|
|
-- Ces fonctions sont redéfinies à l'identique, avec la vérification en plus. Idempotent.
|
|
|
|
-- Propriétaire d'une liste partagée : aussi soumis au code (couvre delete_shared_list, remove_shared_list_member,
|
|
-- rename_shared_list, invite_to_shared_list, shared_list_candidates)
|
|
create or replace function public.azuu_sl_owner(p_list uuid)
|
|
returns boolean language sql stable security definer set search_path = '' as $$
|
|
select auth.uid() is not null and public.azuu_mfa_ok()
|
|
and exists (select 1 from public.shared_lists l where l.id = p_list and l.owner_id = auth.uid())
|
|
$$;
|
|
|
|
create or replace function public.block_user(p_user uuid)
|
|
returns jsonb language plpgsql security definer set search_path = '' as $$
|
|
declare me uuid := auth.uid();
|
|
begin
|
|
if me is null then raise exception 'auth'; end if;
|
|
if not public.azuu_mfa_ok() then raise exception 'mfa'; end if;
|
|
if p_user is null or p_user = me then raise exception 'bad target'; end if;
|
|
if not exists (select 1 from public.profiles p where p.id = p_user) then raise exception 'not found'; end if;
|
|
if (select count(*) from public.blocks b where b.blocker_id = me) >= 1000 then raise exception 'too many blocks'; end if;
|
|
insert into public.blocks (blocker_id, blocked_id) values (me, p_user) on conflict do nothing;
|
|
delete from public.follows f
|
|
where (f.follower_id = me and f.following_id = p_user) or (f.follower_id = p_user and f.following_id = me);
|
|
delete from public.shared_list_members m using public.shared_lists l
|
|
where m.list_id = l.id and m.user_id <> l.owner_id
|
|
and ((l.owner_id = me and m.user_id = p_user) or (l.owner_id = p_user and m.user_id = me));
|
|
return jsonb_build_object('ok', true);
|
|
end;
|
|
$$;
|
|
|
|
create or replace function public.unblock_user(p_user uuid)
|
|
returns jsonb language plpgsql security definer set search_path = '' as $$
|
|
begin
|
|
if auth.uid() is null then raise exception 'auth'; end if;
|
|
if not public.azuu_mfa_ok() then raise exception 'mfa'; end if;
|
|
delete from public.blocks b where b.blocker_id = auth.uid() and b.blocked_id = p_user;
|
|
return jsonb_build_object('ok', true);
|
|
end;
|
|
$$;
|
|
|
|
create or replace function public.unreact_review(p_author uuid, p_item text)
|
|
returns jsonb language plpgsql security definer set search_path = '' as $$
|
|
begin
|
|
if auth.uid() is null then raise exception 'auth'; end if;
|
|
if not public.azuu_mfa_ok() then raise exception 'mfa'; end if;
|
|
delete from public.review_reactions where author_id = p_author and item_id = p_item and user_id = auth.uid();
|
|
return jsonb_build_object('ok', true);
|
|
end;
|
|
$$;
|
|
|
|
create or replace function public.delete_review_comment(p_id bigint)
|
|
returns jsonb language plpgsql security definer set search_path = '' as $$
|
|
declare
|
|
me uuid := auth.uid();
|
|
n integer;
|
|
begin
|
|
if me is null then raise exception 'auth'; end if;
|
|
if not public.azuu_mfa_ok() then raise exception 'mfa'; end if;
|
|
delete from public.review_comments c
|
|
where c.id = p_id and (c.user_id = me or c.author_id = me);
|
|
get diagnostics n = row_count;
|
|
return jsonb_build_object('ok', n > 0);
|
|
end;
|
|
$$;
|
|
|
|
create or replace function public.leave_shared_list(p_list uuid)
|
|
returns jsonb language plpgsql security definer set search_path = '' as $$
|
|
declare me uuid := auth.uid();
|
|
begin
|
|
if me is null then raise exception 'auth'; end if;
|
|
if not public.azuu_mfa_ok() then raise exception 'mfa'; end if;
|
|
if public.azuu_sl_owner(p_list) then raise exception 'owner cannot leave'; end if;
|
|
delete from public.shared_list_members where list_id = p_list and user_id = me;
|
|
return jsonb_build_object('ok', true);
|
|
end;
|
|
$$;
|
|
|
|
create or replace function public.respond_shared_list_invite(p_list uuid, p_accept boolean)
|
|
returns jsonb language plpgsql security definer set search_path = '' as $$
|
|
declare me uuid := auth.uid();
|
|
begin
|
|
if me is null then raise exception 'auth'; end if;
|
|
if not public.azuu_mfa_ok() then raise exception 'mfa'; end if;
|
|
if not exists (select 1 from public.shared_list_members where list_id = p_list and user_id = me and status = 'pending') then
|
|
raise exception 'invite not found';
|
|
end if;
|
|
if p_accept then
|
|
if not public.azuu_me_active() then raise exception 'forbidden'; end if;
|
|
if (select count(*) from public.shared_list_members where user_id = me and status = 'accepted') >= 100 then
|
|
raise exception 'too many lists';
|
|
end if;
|
|
update public.shared_list_members set status = 'accepted' where list_id = p_list and user_id = me;
|
|
else
|
|
delete from public.shared_list_members where list_id = p_list and user_id = me;
|
|
end if;
|
|
return jsonb_build_object('ok', true);
|
|
end;
|
|
$$;
|
|
|
|
create or replace function public.get_profile(p_username text)
|
|
returns jsonb language plpgsql stable security definer set search_path = '' as $$
|
|
declare
|
|
p public.profiles;
|
|
me uuid := auth.uid();
|
|
res jsonb;
|
|
begin
|
|
if me is null or not public.azuu_mfa_ok() then
|
|
return null;
|
|
end if;
|
|
select * into p from public.profiles where username = lower(trim(p_username));
|
|
if not found then
|
|
return null;
|
|
end if;
|
|
if p.suspended and p.id <> me then
|
|
return null;
|
|
end if;
|
|
if p.id <> me and exists (select 1 from public.blocks b where b.blocker_id = p.id and b.blocked_id = me) then
|
|
return null;
|
|
end if;
|
|
res := public.profile_card(p) || jsonb_build_object(
|
|
'bio', p.bio,
|
|
'banner_url', p.banner_url,
|
|
'is_me', p.id = me,
|
|
'follows_me', exists (select 1 from public.follows where follower_id = p.id and following_id = me and status = 'accepted'),
|
|
'followers', (select count(*) from public.follows where following_id = p.id and status = 'accepted'),
|
|
'following', (select count(*) from public.follows where follower_id = p.id and status = 'accepted'),
|
|
'visible', public.can_view(p.id)
|
|
);
|
|
if public.can_view(p.id) then
|
|
res := res || jsonb_build_object(
|
|
'stats', jsonb_build_object(
|
|
'finished', (select count(*) from public.items where user_id = p.id and not deleted and data->>'status' = 'termine')
|
|
),
|
|
'top', (
|
|
select coalesce(jsonb_agg(public.public_item(data) order by data->'top'->>'category', (data->'top'->>'rank')), '[]'::jsonb)
|
|
from public.items i
|
|
where i.user_id = p.id and not i.deleted and jsonb_typeof(i.data->'top') = 'object'
|
|
and coalesce(
|
|
(select s.data->'topCategories' ? (i.data->'top'->>'category')
|
|
from public.settings s where s.user_id = p.id and jsonb_typeof(s.data->'topCategories') = 'array'),
|
|
true)
|
|
),
|
|
'recent', (
|
|
select coalesce(jsonb_agg(public.public_item(s.data) order by s.k desc nulls last), '[]'::jsonb)
|
|
from (
|
|
select data, coalesce(data->>'endDate', data->>'startDate') as k
|
|
from public.items
|
|
where user_id = p.id and not deleted and data->>'status' = 'termine'
|
|
order by k desc nulls last limit 5
|
|
) s
|
|
),
|
|
'watching', (
|
|
select coalesce(jsonb_agg(public.public_item(s.data) order by s.k desc nulls last), '[]'::jsonb)
|
|
from (
|
|
select data, coalesce(data->>'startDate', left(data->>'updatedAt', 10)) as k
|
|
from public.items
|
|
where user_id = p.id and not deleted and data->>'status' = 'en_cours'
|
|
order by k desc nulls last limit 8
|
|
) s
|
|
)
|
|
);
|
|
end if;
|
|
return res;
|
|
end;
|
|
$$;
|
|
|
|
-- Photos de profil : envoyer, lister, supprimer demandent aussi le code (si le compte en a un)
|
|
drop policy if exists "profile media upload own" on storage.objects;
|
|
create policy "profile media upload own" on storage.objects for insert to authenticated
|
|
with check (
|
|
bucket_id = 'profile-media'
|
|
and (storage.foldername(name))[1] = (select auth.uid())::text
|
|
and name ~ '^[0-9a-f-]{36}/(avatar|banner)-[0-9]{1,15}\.(jpg|webp|png)$'
|
|
and public.azuu_media_count() < 6
|
|
and (select public.azuu_mfa_ok())
|
|
);
|
|
|
|
drop policy if exists "profile media read own" on storage.objects;
|
|
create policy "profile media read own" on storage.objects for select to authenticated
|
|
using (
|
|
bucket_id = 'profile-media'
|
|
and (storage.foldername(name))[1] = (select auth.uid())::text
|
|
and (select public.azuu_mfa_ok())
|
|
);
|
|
|
|
drop policy if exists "profile media delete own" on storage.objects;
|
|
create policy "profile media delete own" on storage.objects for delete to authenticated
|
|
using (
|
|
bucket_id = 'profile-media'
|
|
and (storage.foldername(name))[1] = (select auth.uid())::text
|
|
and (select public.azuu_mfa_ok())
|
|
);
|