diff --git a/CLAUDE.md b/CLAUDE.md index 00c0248..83818c0 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -89,4 +89,4 @@ npm run build # build de prod - **Runner Gitea** : `bobinoo` (étiquette `bobinoo`), service `gitea-runner` dans le conteneur LXC `bobinoo` du VPS. S'il est hors ligne, plus rien ne se déploie (voir `deploy/RUNNER.md`). - **Sauvegardes de la base** : faites par Romain sur son VPS chaque soir (la sauvegarde via Gitea Actions a été retirée le 28 nov. 2026). - Leaked Password Protection (Supabase) demande l'offre payante : remplacée par la vérification HIBP côté client (`src/lib/pwned.ts`). -- **Skills de sécurité** (`.claude/skills/README.md`) : 16 skills mukul975 (Apache-2.0) + `sharp-edges` et `differential-review` de Trail of Bits (CC BY-SA 4.0, texte seul). N'en ajouter que de sources connues, après lecture complète (skills piégés : étude Snyk « ToxicSkills »). Audit du 1er déc. 2026 : verrous admin/mail testés en ligne (404 sans secret, 401 sans jeton / clé publique / jeton falsifié `alg=none`), `npm audit --omit=dev` = 0. +- **Skills de sécurité** (`.claude/skills/README.md`) : 16 skills mukul975 (Apache-2.0) + `sharp-edges` et `differential-review` de Trail of Bits (CC BY-SA 4.0, texte seul). N'en ajouter que de sources connues, après lecture complète (skills piégés : étude Snyk « ToxicSkills »). Audit du 1er déc. 2026 : verrous admin/mail testés en ligne (404 sans secret, 401 sans jeton / clé publique / jeton falsifié `alg=none`), `npm audit --omit=dev` = 0. Audit de toute l'app (même jour) : double authentification exigée par toutes les fonctions appelables et le stockage (`20261201130000_mfa_everywhere.sql`, testé en aal1/aal2 dans une transaction annulée) ; `ratings` : plafond commun de recherches Trakt (120/min par instance) et seuls les titres trouvés vont en base. diff --git a/supabase/functions/ratings/index.ts b/supabase/functions/ratings/index.ts index 4c0ce19..02ad117 100644 --- a/supabase/functions/ratings/index.ts +++ b/supabase/functions/ratings/index.ts @@ -163,11 +163,15 @@ Deno.serve(async (req) => { return respond(row) } - // Jamais vue : Trakt maintenant (l'enregistrement en base ne retarde pas la réponse) + // Jamais vue : Trakt maintenant (l'enregistrement en base ne retarde pas la réponse). + // Plafond commun à tous (par instance) : un robot qui change d'adresse IP à chaque appel (la limite par IP + // se contourne) ne peut ni épuiser la clé Trakt, ni remplir la table en essayant tous les numéros. + if (rateLimited('ratings-cold', 120, 60_000)) return json(req, 429, { error: 'rate' }) try { const fresh = await fetchTrakt(ext, kind, id) remember(fresh) - inBackground(saveRow(fresh)) + // Seuls les titres connus de Trakt vont en base (leur nombre est fini) ; « inconnu » reste en mémoire + if (fresh.found) inBackground(saveRow(fresh)) return respond(fresh) } catch (e) { const timeout = e instanceof DOMException && (e.name === 'TimeoutError' || e.name === 'AbortError') diff --git a/supabase/migrations/20261201130000_mfa_everywhere.sql b/supabase/migrations/20261201130000_mfa_everywhere.sql new file mode 100644 index 0000000..145f759 --- /dev/null +++ b/supabase/migrations/20261201130000_mfa_everywhere.sql @@ -0,0 +1,199 @@ +-- 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()) + );