Skip to main content
Home/Case Study/Multi-Tenant PostgreSQL RLS Leak
SECURITY & ROW-LEVEL AUDIT

Plugging the Multi-Tenant Leak: Enforcing PostgreSQL RLS to Prevent Cross-Account Data Access

How a missing tenant_id filter in a Supabase/PostgreSQL SaaS application allowed Company A users to manipulate API URLs (IDOR Vulnerability) to inspect Company B’s confidential invoices, and how we enforced database-level Row Level Security (RLS).

DATA LEAK RISK0 VulnerabilitiesCross-Account Blocked
ENTERPRISE REVIEWPassedTenant Isolation Verified
SECURITY AUDIT SPEEDRLS + TestsPolicy & Test Suite Deployed
HawkInspect Pro Security Audit Team
•10 min read•Technical Case Study

1. Executive Summary & Vulnerability Discovery

A B2B SaaS company offering invoicing and financial workflow software built on Supabase & PostgreSQL contacted HawkInspect Pro while a large enterprise prospect was running a security review before signing.

The prospect's penetration testers had found an Insecure Direct Object Reference (IDOR) flaw: authenticated users from Company A could change the invoice ID in the API URL (e.g. from /api/invoices/inv_1001 to /api/invoices/inv_1002) and inspect private billing records, customer names, and bank details belonging to Company B.

Multi-Tenant Database Reality:Filtering by tenant_id in JavaScript/TypeScript API routes works until a developer forgets the WHERE clause in a single endpoint. Database-level PostgreSQL Row-Level Security (RLS) keeps enforcing tenant isolation when a route forgets the filter, as long as queries run with the user's JWT rather than the service-role key.

Concerned about multi-tenant data leaks in your SaaS?

We perform line-by-line PostgreSQL RLS policy audits and API middleware tenant isolation testing.

2. Quantifying the Cross-Account Data Leak Risk

Impact of the IDOR Multi-Tenant Leak

  • Cross-Tenant Data Exposure: 100% of tenant tables lacked database-level Row Level Security (RLS) policies.
  • Compliance & Contract Risk: Cross-tenant access broke the confidentiality commitments behind SOC 2 (Trust Services Criteria) and GDPR's security requirements, and it was blocking the enterprise deal.
  • Systemic Human Error Risk: Relying on application-level WHERE tenant_id = req.user.tenantId logic meant any new developer could accidentally introduce a data leak.

3. Vulnerable API Endpoint & Missing RLS

The original Node.js / Supabase route queried the database directly by invoice ID without enforcing or verifying tenant ownership at either the route or database engine level:

VULNERABLE ROUTE // MISSING TENANT ISOLATION
app/api/invoices/[id]/route.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import { NextResponse } from 'next/server';
import { supabase } from '@/lib/supabaseAdmin'; // 🚨 service_role key: ignores RLS
export async function GET(req: Request, { params }: { params: { id: string } }) {
const invoiceId = params.id;
// 🚨 VULNERABILITY: Missing tenant_id verification and database RLS!
// Any authenticated user can read invoiceId belonging to Company B!
const { data, error } = await supabase
.from('invoices')
.select('*')
.eq('id', invoiceId) // ❌ Missing .eq('tenant_id', currentTenantId)
.single();
if (error) {
return NextResponse.json({ error: error.message }, { status: 404 });
}
return NextResponse.json({ invoice: data });
}

4. Anatomy of the IDOR Cross-Tenant Exploitation

An attacker authenticated as User X from Tenant A sends a legitimate HTTP GET request. By substituting the invoice ID in the URL with one belonging to Tenant B, the Supabase client returns Tenant B's raw database record because the table lacked PostgreSQL RLS policies and the endpoint lacked tenant verification.

5. Enforcing PostgreSQL Row Level Security (RLS)

HawkInspect Pro deployed a mandatory PostgreSQL Migration script enforcing RLS policies across all tenant tables. PostgreSQL now extracts tenant_id directly from the cryptographically signed JWT token:

HARDENED // POSTGRESQL RLS ENFORCED
supabase/migrations/20260911_enforce_tenant_rls.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
-- 1. Enable Row Level Security on the Invoices Table
ALTER TABLE invoices ENABLE ROW LEVEL SECURITY;
ALTER TABLE invoices FORCE ROW LEVEL SECURITY;
-- 2. Tenant comes from app_metadata (set server-side; users cannot edit it)
CREATE POLICY tenant_isolation_select_policy
ON invoices
FOR SELECT
USING (
tenant_id = ((select auth.jwt()) -> 'app_metadata' ->> 'tenant_id')::uuid
);
-- 3. INSERT / UPDATE / DELETE: check rows read AND rows written
CREATE POLICY tenant_isolation_modification_policy
ON invoices
FOR ALL
USING (
tenant_id = ((select auth.jwt()) -> 'app_metadata' ->> 'tenant_id')::uuid
) WITH CHECK (
tenant_id = ((select auth.jwt()) -> 'app_metadata' ->> 'tenant_id')::uuid
);
-- 4. Keep the policy filter fast
CREATE INDEX IF NOT EXISTS invoices_tenant_id_idx ON invoices (tenant_id);

Two details made the policy actually hold. First, the tenant claim lives in app_metadata, which only the server can set (we added it with a Supabase custom access token hook); a claim in user_metadata can be edited by the user and would let anyone switch tenants. Second, the API route previously used a client built with the service_role key, which bypasses RLS entirely. We replaced it with a per-request Supabase server client that runs every query as the signed-in user, so the database sees their JWT.

6. Automated API Middleware Tenant Isolation Testing

To catch regressions in future deployments, we built an automated API integration test suite that runs in CI. The test runner signs in mock users from Tenant A and Tenant B and sends cross-tenant requests to every API route, expecting 404 Not Found: under RLS the other tenant's row is simply invisible, so the API cannot even confirm it exists.

7. Business & Compliance Impact

Zero Vulnerabilities

All multi-tenant API routes verified with 0 cross-tenant data leaks.

Enterprise Security Review Passed

The prospect's retest confirmed tenant isolation, unblocking the contract.

Rapid Turnaround

Complete database migration, RLS policies, and test suite shipped into production.

HAWKINSPECT PRO EMERGENCY SHIELD

8. Multi-Tenant Security & PostgreSQL RLS Audit

Protect your SaaS from cross-tenant data leaks, IDOR flaws, and compliance failures. Our principal security engineers audit your database policies and API routes with zero downtime.

9. Interactive Multi-Tenant Security Diagnostic

MULTI-TENANT SECURITY CHECKLIST WE AUDIT

SCORE:0 / 5
Q1:Are PostgreSQL Row Level Security (RLS) policies enabled on all multi-tenant tables?
Q2:Does your database automatically reject queries when tenant_id context is missing?
Q3:Do you have automated integration tests simulating cross-tenant API data requests?
Q4:Are customer invoices and billing payloads isolated by verified JWT tenant claims?
Q5:Has an external security review or pen test specifically checked cross-tenant data isolation?
ASSESSMENT RESULT:0 / 5 PASSED

CRITICAL MULTI-TENANT LEAK RISK

High vulnerability to IDOR cross-tenant data access. Urgent PostgreSQL RLS policy enforcement recommended.

10 // FORWARD TO YOUR ENGINEERING TEAM1-CLICK SHARE

Non-coder founder? Forward this article link directly to your CTO, Tech Lead, or Dev Agency to verify if your PostgreSQL tables have Row-Level Security (RLS) policies enabled:

11. Multi-Tenant Security FAQ

An IDOR occurs when an application exposes database records via URL parameters or API inputs (e.g. /api/invoices/inv_9921) without verifying if the requesting user belongs to the target tenant organization. Attackers simply change the ID in the API request to view other companies’ confidential data.
Relying solely on developers to remember WHERE tenant_id = x in every single API endpoint leads to human error. A single forgotten WHERE clause in an obscure endpoint leaks private data across tenants. PostgreSQL Row Level Security (RLS) guarantees tenant isolation at the database engine layer, enforcing isolation automatically even if application code omits the filter.
Supabase injects the authenticated user’s JWT token into PostgreSQL session context (auth.jwt()). By defining an RLS policy like (tenant_id = (auth.jwt() ->> 'tenant_id')::uuid), PostgreSQL automatically filters every query, insert, and update to match the user's tenant ID. Put the claim in app_metadata (which users cannot edit), never user_metadata, and index tenant_id so the extra filter stays cheap.
Our principal security engineers immediately review your database isolation rules, deploy verified Row-Level Security (RLS) policies, and set up automated tests that catch cross-tenant leaks before they reach production.
12 // FINAL POSTMORTEM

12. What Was Actually Wrong & The Question Worth Asking

Not PostgreSQL engine speed. Not Supabase API limits. Not Next.js router performance.

The vulnerability was a Missing PostgreSQL Row Level Security (RLS) Policy that allowed cross-account data access (IDOR) via simple URL parameter edits.

What Un-Audited SaaS Code Does:

WHERE id = req.params.id (No RLS)

Company A user can edit the ID in URL and inspect Company B private invoices.

What HawkInspect Pro Enforces:

CREATE POLICY ... USING (tenant_id = (auth.jwt()->'app_metadata'->>'tenant_id')::uuid)

The database enforces tenant isolation even when a route forgets the filter.

01

Database-Level RLS Policy Enforcement

Never rely solely on application-level JavaScript WHERE tenant_id = x filters. Enforce PostgreSQL RLS policies at the database engine layer.
02

JWT Tenant Claim Verification

Extract tenant claims directly from cryptographically signed JWT tokens in Supabase/PostgreSQL sessions.
03

Automated Isolation Test Suite

Run automated API middleware integration tests that simulate cross-tenant data requests on every CI/CD deployment.
THE QUESTION WORTH ASKING FOR YOUR MULTI-TENANT SAAS:

If an authenticated user changes tenant_id or resource ID in an API URL today, does PostgreSQL block the request with RLS or leak private customer data?

If the answer is “We don't have RLS policies enabled,” you don't just have a minor bug — you are exposed to cross-tenant data leaks and failed enterprise security reviews.
IS YOUR MULTI-TENANT SAAS VULNERABLE TO CROSS-ACCOUNT DATA LEAKS?

Get Your Multi-Tenant Database Security Audited

Talk directly with our Principal Security Auditor. We'll inspect your PostgreSQL RLS policies, Supabase claims, and API endpoints with zero downtime.