Skip to content

fix(db): harden RLS — close anon exposure and public-row mutation - #196

Merged
SebastienGosa merged 2 commits into
mainfrom
fix/rls-user-data-exposure
Jul 1, 2026
Merged

SebastienGosa merged 2 commits into
mainfrom
fix/rls-user-data-exposure

Conversation

@SebastienGosa

@SebastienGosa SebastienGosa commented Jul 1, 2026 •

Copy link
Copy Markdown
Owner

Contexte

Vérifié en conditions réelles (Postgres + pgvector local, rôles anon et non-propriétaire simulés). packages/utils/src/supabase.ts expose un browserClient() avec la clé anon → l'API PostgREST anon est ouverte côté navigateur, donc les policies RLS sont la seule barrière.

Lot 1 — exposition en lecture via anon

  • users lisible publiquement : users_self_select utilisait USING (id = current_auth_uid() OR true). Le OR true rendait toute la table (email, reader_prefs, location, stats) lisible par tout client anon. → SELECT propriétaire uniquement + vue public_user_profiles (colonnes profil sûres, sans email).
  • donations : OR anonymous = FALSE exposait les dons non anonymes (ids Stripe, messages). → SELECT propriétaire uniquement ; transparence publique via v_transparency_monthly.
  • Trigger auth SECURITY DEFINER : search_path épinglé à public, pg_temp.
  • Tables internes (search_queries, affiliate_clicks, system_jobs, affiliate_retailers) : RLS deny-all, accès serveur via service_role.

Lot 2 — mutation de lignes publiques (audit de suivi)

reading_lists, reading_list_books et annotations étaient en une seule policy FOR ALL dont le USING autorisait les lignes publiques. Comme DELETE n'évalue que USING, n'importe qui pouvait supprimer une liste de lecture publique ou une annotation non privée, et UPDATE pouvait réassigner l'ownership. → Split en une policy FOR SELECT publique + INSERT/UPDATE/DELETE propriétaire uniquement.

Validation (rôle simulé)

Test Résultat
users brut lu par anon 0 ligne, 0 email
public_user_profiles par anon ligne renvoyée, colonne email inexistante
donations brut par anon (don non anonyme seedé) 0
Lecture liste publique par un tiers autorisée
Suppression liste publique d'autrui bloquée (0)
Vol via UPDATE (réassignation ownership) bloqué (0), liste conservée au propriétaire
Propriétaire supprime sa liste autorisé

Schéma rechargé sans erreur (ON_ERROR_STOP=1). Couvre le fond réel des PR bots fermées (#188, #195) avec un correctif unique testé.

The users RLS SELECT policy used `OR true`, so any browser holding the
public Supabase anon key could read the whole users table (email, reader
prefs, location, stats). Restrict direct reads to the owner and add a
public_user_profiles view exposing only profile-safe columns for public
rendering.

Also make donations SELECT owner-only (public transparency already comes
from v_transparency_monthly, not raw rows carrying Stripe ids and donor
messages), pin search_path on the SECURITY DEFINER auth trigger, and
enable RLS (deny-all, service_role only) on the internal search_queries,
affiliate_clicks, system_jobs and affiliate_retailers tables.
reading_lists, reading_list_books and annotations used a single FOR ALL
policy whose USING clause allowed public rows. Since DELETE only evaluates
USING, any user could delete a public reading list or non-private annotation,
and UPDATE could reassign ownership. Split each into a public FOR SELECT
policy plus owner-only INSERT/UPDATE/DELETE policies. Verified with a
simulated non-owner role: public read allowed, delete/steal blocked.
@SebastienGosa SebastienGosa changed the title fix(db): close anon RLS exposure on users and donations fix(db): harden RLS — close anon exposure and public-row mutation Jul 1, 2026
@SebastienGosa
SebastienGosa merged commit 1d09024 into main Jul 1, 2026
2 checks passed
@SebastienGosa
SebastienGosa deleted the fix/rls-user-data-exposure branch July 1, 2026 19:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant