Home / Learn Secure SQL / Project: Secure Multi-Tenant SaaS Data Isolation

Project: Secure Multi-Tenant SaaS Data Isolation

Ensure every query is scoped to the correct tenant.

The gistEnsure every query is scoped to the correct tenant.

The idea, explained simply

Defense in depth for tenant isolationApplication-level scoping: every query includes tenant_idShared query-building helper enforces it centrallyDatabase-level row-level security (if supported)
Defense in depth for tenant isolation

Definition

A multi-tenant application stores every row with a tenant_id, and every single query (read or write) must be scoped to the current tenant — this project builds a small query layer that enforces that scoping centrally rather than trusting every call site to remember it.

Why you need this

Real projects are where secure patterns become habits instead of theory.

How it works

Work through the steps in order; do not skip the checklist.

Where you'll see this

  • Portfolio project
  • Code review practice
  • Interview preparation

What you will build

A multi-tenant application stores every row with a tenant_id, and every single query (read or write) must be scoped to the current tenant — this project builds a small query layer that enforces that scoping centrally rather than trusting every call site to remember it.

LevelTime needed
Advanced4-6 hours

What you need (tools and setup)

ToolWhat it isWhy you need itCost
Code editor (VS Code or similar)The program you write code in.You need somewhere to write and run the example app.free
A language runtime (Node.js, Python, PHP or similar)Installs the interpreter so your code can run on your computer.The project examples run as small scripts.free
SQLite (bundled, no install needed)A tiny file-based database engine.Every project in this course can be tried with SQLite first, with zero setup.free
PostgreSQL (optional, local or Docker)A full production-grade database server.For the parts of a project that use roles, grants or row-level security (SQLite does not have these).free
GitVersion control: tracks every change you make.Lets you compare the insecure version and the fixed version of the code.free

Step 1: see the problem and the fix in the sandbox

Tenant-scoped query, with tenant_id bound alongside every other filterSQL: edit and run

Both tenant_id and id are bound parameters, and the query only ever returns a row when both match together, so a guessed or enumerated id from a different tenant still returns nothing.

Step by step

  1. Add a tenant_id column to every tenant-owned table, with an index on tenant_id.
  2. Build a small shared query-building helper that always requires a tenant_id argument and always includes it in the WHERE clause.
  3. Audit every existing query in the codebase and migrate it to use the shared helper, removing any direct queries that bypass it.
  4. Where the database supports it (PostgreSQL), add row-level security policies as a second, defense-in-depth layer beneath the application-level scoping.
  5. Write a test that, as tenant 1, requests a resource id known to belong to tenant 2, and asserts it returns nothing.

Security checklist before you ship it

CheckWhy it mattersHow to verify
Tenant scoping on every queryA single missed query can leak another tenant's dataGrep the codebase for any query not using the shared helper
Index on tenant_idKeeps tenant-scoped queries fast at scaleConfirm an index exists on tenant_id (often combined with id)
Row-level security (if available)A second layer beneath application codeConfirm RLS policies exist if using PostgreSQL
Tip:Commit the insecure version and the fixed version as two separate commits, so the diff itself becomes a teaching example.

Test what you just read

1. Why centralize tenant scoping in one shared helper?