Supabase Service Role Key Exposes Critical Multi-Tenant Data Leak Risk in Clinic Application
A critical security vulnerability has been identified in a multi-tenant application's codebase, where the use of Supabase's Service Role key completely bypasses Row Level Security (RLS). This architectural flaw means that any server-side query missing a manual tenant filter, such as `.eq('clinic_id', clinicId)`, will inadvertently leak sensitive data across different clinics. The risk is not theoretical; it is documented as a known gap in the repository's handover documentation, explicitly warning that a single developer oversight in the actions file could cause a cross-tenant data breach.
The vulnerability stems from the `src/lib/db.ts` file, which utilizes the `SUPABASE_SERVICE_ROLE_KEY`. This key grants unrestricted database access, rendering RLS policies ineffective for all Server Components and Server Actions. In a multi-tenant system where data isolation is paramount, this creates a ticking time bomb. As the codebase grows and more actions are added, the probability of a developer forgetting to manually append the correct clinic ID filter increases exponentially, making a significant data leak a matter of when, not if.
The proposed remediation focuses on hardening tenant isolation. The scope includes creating a clinic-scoped database wrapper or proxy to automatically append tenant filters, refactoring the core database client, or implementing short-lived JWTs to enforce RLS on the server side. Until these structural changes are implemented, the application remains exposed to a severe data integrity failure, placing all tenant data at continuous risk from a simple coding error.