{"success":true,"name":"enforce-tenant-isolation-in-every-api-query-not-in-the-front","description":"One API serves multiple customers' data from shared tables","category":"ai-brimind","path":"ai-brimind/enforce-tenant-isolation-in-every-api-query-not-in-the-front/SKILL.md","file":"SKILL.md","content":"---\nname: enforce-tenant-isolation-in-every-api-query-not-in-the-front\ndescription: \"One API serves multiple customers' data from shared tables\"\n---\n\n# Enforce Tenant Isolation in Every API Query, Not in the Frontend\n\n## When to use\nYour API is multi-tenant (multiple companies/workspaces share the system), and queries can technically reach across tenants if a filter is forgotten.\n\n## Example request\nOne forgotten tenant filter in one query → customer A sees customer B's invoices; that's not a bug class you fix once, it needs a mechanism. — Tenant id taken from the request body → any caller claims any tenant; the tenant must come from the authenticated token, never client input. — Isolation checked in the UI → direct API calls bypass it; the boundary lives at the data layer. — Every developer must remember the filter manually → humans forget; scoped queries by default (middleware, ORM scopes, row-level security) make forgetting impossible.\n\n## Instructions\nAct as a tenancy guardian who assumes one missed filter equals a data breach. For the multi-tenant API: 1) Establish the one rule: tenant identity comes only from the authenticated context (token, session) — never from a body field, query param, or header the client controls; a client-supplied tenant id is an impersonation feature. 2) Make isolation mechanical, not disciplinary: use the platform's existing mechanism — ORM default scopes, query middleware injecting the tenant filter, or database row-level security — so an unscoped query is impossible or loud, instead of relying on every developer remembering. 3) Audit the escape hatches: raw queries, background jobs, admin endpoints, and reports are where scoping mechanisms get bypassed — each needs the tenant story stated explicitly. 4) Test the breach directly: authenticate as tenant A, request tenant B's resource ids, and confirm 403/404 — this test is the isolation's proof, per sensitive endpoint. 5) If you find cross-tenant access is needed (support tools, admin), design it as an explicit privileged path with audit logging, not as a missing filter. Only trust the isolation once the cross-tenant test is confirmed failing correctly, and explain how the mechanism makes leaks structurally hard rather than merely unlikely.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #924\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}