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 idea, explained simply
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.
| Level | Time needed |
|---|---|
| Advanced | 4-6 hours |
What you need (tools and setup)
| Tool | What it is | Why you need it | Cost |
|---|---|---|---|
| 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 |
| Git | Version 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
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
- Add a tenant_id column to every tenant-owned table, with an index on tenant_id.
- Build a small shared query-building helper that always requires a tenant_id argument and always includes it in the WHERE clause.
- Audit every existing query in the codebase and migrate it to use the shared helper, removing any direct queries that bypass it.
- Where the database supports it (PostgreSQL), add row-level security policies as a second, defense-in-depth layer beneath the application-level scoping.
- 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
| Check | Why it matters | How to verify |
|---|---|---|
| Tenant scoping on every query | A single missed query can leak another tenant's data | Grep the codebase for any query not using the shared helper |
| Index on tenant_id | Keeps tenant-scoped queries fast at scale | Confirm an index exists on tenant_id (often combined with id) |
| Row-level security (if available) | A second layer beneath application code | Confirm RLS policies exist if using PostgreSQL |
Test what you just read
1. Why centralize tenant scoping in one shared helper?
Centralizing removes reliance on every call site remembering it.