Group
  • Database

    Make sure tenants can't see each other's data

    Audits every query in a multi-tenant app to confirm it is scoped by tenant or organization ID, and adds the missing filters.

    • Improve quality
    • Plan & validate
    • Advanced
    • General

Free Prompt

My app is multi-tenant: users belong to organizations/workspaces and must never see another organization's data. Audit my data access layer and enforce tenant isolation everywhere. 1. Inventory: list every table/collection that holds tenant-owned data and confirm it has a tenant/organization ID column. Add it where missing, including a foreign key to the organizations table. 2. Query audit: find every read, update, and delete query and confirm it filters by the current tenant's ID, derived from the authenticated session, never from a client-supplied organization ID without a membership check. Pay special attention to joins, aggregate queries, search endpoints, exports, and background jobs, which are the places scoping usually gets dropped. 3. Write paths: confirm every insert stamps the tenant ID server-side from the session. 4. Membership checks: anywhere a user can switch or specify an organization, verify the user is actually a member before the query runs. 5. Defense in depth: if I am on Postgres, consider row-level security policies as a backstop. Otherwise, centralize tenant scoping in the data access layer so new queries cannot forget it. Do not break legitimate cross-tenant admin tooling; gate it behind an explicit admin role instead of leaving queries unscoped. Do not trust any tenant identifier sent from the client without verifying membership. Give me a table of every endpoint/query, whether it was scoped, and what you changed. Include a test that logs in as a user from tenant A and tries to read tenant B's records by ID, and show it failing.

What This Does / How This Helps

Checks every query in your app for tenant scoping and closes the ones that leak across organizations. The classic multi-tenant bug is a single missing WHERE clause. One endpoint forgets to filter by organization ID and suddenly any logged-in user can read another company's invoices, customers, or messages by guessing IDs. It is the worst kind of breach: quiet, undetectable from the outside, and usually found by a customer, not by you. AI-generated queries almost never scope by tenant because the model has no idea your app is multi-tenant unless you say so. This prompt audits the whole data layer, fixes the gaps, centralizes scoping so future queries inherit it, and gives you the cross-tenant test to prove isolation holds.

Want to skip doing this by hand?

Fortivibe audits your app for all of the areas these prompts cover (and more).

See What We Check

Related Prompts