{"success":true,"name":"scope-every-query-by-tenant-one-club-s-data-must-never-leak-","description":"App serves multiple clubs/organizations whose data lives in shared collections","category":"ai-brimind","path":"ai-brimind/scope-every-query-by-tenant-one-club-s-data-must-never-leak-/SKILL.md","file":"SKILL.md","content":"---\nname: scope-every-query-by-tenant-one-club-s-data-must-never-leak-\ndescription: \"App serves multiple clubs/organizations whose data lives in shared collections\"\n---\n\n# Scope Every Query by Tenant: One Club's Data Must Never Leak Into Another's Screen\n\n## When to use\nMultiple tenants (clubs, companies, teams) share the same database tables/collections, and queries filter by tenant ID only when someone remembers to add it.\n\n## Example request\nA members query forgets the clubId filter → club A's admin sees club B's members; one missing WHERE clause is a data breach between customers, not a bug. The tenant ID comes from the request body → any caller can claim any club; the tenant must derive from the authenticated session, like identity itself. Every new endpoint re-remembers to add clubId by hand → the tenth one forgets; a shared query helper or middleware that injects the tenant filter makes forgetting impossible. A truly global table (list of countries) → no tenant column needed; the discipline applies to data OWNED by someone.\n\n## Instructions\nAct as an architect who treats tenant isolation as the load-bearing wall of a multi-club app — every query touching owned data must answer 'whose?' before 'what?', because a single unscoped query shows one customer another customer's private world, and that is the kind of bug that ends trust in one screenshot. Do this: (1) Classify every collection/table once, in writing: TENANT-OWNED (members, payments, bookings — has a clubId/tenant field, always) or GLOBAL (countries, plans — shared by design). If a tenant-owned collection lacks the tenant field on some old records, that is a migration to run before anything else. (2) Derive the tenant from the verified session on the server — the logged-in user's club membership — never from a clubId the client sent in a body or query param; the client may choose WITHIN its rights (which of MY clubs), but the server verifies that membership before using it, exactly like your authorization discipline: this is authorization applied to every read. (3) Make scoping structural, not remembered: one shared query layer per entity (getMembers(tenantId, filters)) or middleware that injects the tenant filter, so raw unscoped model calls in route code become a smell you can grep for. In Mongoose, schema-level query helpers or a mandatory base filter serve this; the mechanism matters less than there being ONE place that guarantees it. (4) Extend the wall to every side door: aggregations ($match on tenant FIRST — also your index discipline: compound indexes start with the tenant field), background jobs iterating 'all records' (per tenant, scoped), file access (ownership check includes tenant), and ID-based lookups (find by id AND tenant, so guessing another club's record ID returns 404, not their data). (5) Test like the rival club: log in as tenant A, request tenant B's record IDs directly via the API — every attempt must 404. Only call the app multi-tenant once you can point at the single mechanism that scopes reads and writes, and answer for any endpoint: 'where exactly does this query learn whose data it may touch?' — if the answer is 'the developer remembered', the wall is made of memory, and memory leaks.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #234\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}