Data authorization and tenancy
What you need to know
Authorization decides whether an already identified caller may perform a particular operation on a particular resource. A multi-tenant application must check the organization boundary as well as the individual owner. A tenant is a group of users whose data must remain isolated from other groups.
By the end, you can: enforce owner scope at the DAL; test another tenant.
Evaluate a tenant-scoped policy
type Principal = {id:string; tenant:string; role:'reader'|'editor'|'admin'};type Draft = {owner:string; tenant:string};function decide(p:Principal|null,d:Draft|null) { if (!p) return 'UNAUTHENTICATED'; if (!d || d.tenant !== p.tenant) return 'NOT_FOUND'; return p.role==='admin' || (p.role==='editor' && p.id===d.owner) ? 'ALLOW' : 'FORBIDDEN';}const a:Principal={id:'a',tenant:'north',role:'editor'};console.log(decide(a,{owner:'a',tenant:'north'}));console.log(decide(a,{owner:'a',tenant:'south'}));Expected behavior: ALLOW, then NOT_FOUND. A matching owner string does not override the tenant boundary.
How it works
A data access layer (DAL) is a server-side module that combines persistence access with access rules. Resolve the caller from a verified session before invoking it; never accept an owner or role from a browser form as proof of identity. For River Books, a draft belongs to one tenant and one editor. The first example isolates the policy from persistence so its truth table is easy to review. A tenant administrator may edit any draft within that tenant, but never another tenant's draft. Ordinary readers may not edit even if they created a record. In the actual persistence layer, constrain the query by tenant and resource ID, and include the owner condition for editors. For writes, include these predicates in the mutation or enforce them inside the transaction; a prior read alone can become stale. Choose whether to conceal the existence of an inaccessible resource: this lab returns the same NOT_FOUND result for missing and cross-tenant records, but a known same-tenant resource may return FORBIDDEN. This is a deliberate policy, not a universal HTTP rule.
Scope a database update to the verified caller
-- PostgreSQL parameterized statement; editor branch onlyUPDATE articles SET title = $1, revision = revision + 1WHERE id = $2 AND tenant_id = $3 AND owner_id = $4 AND revision = $5 AND state = 'draft'RETURNING id, title, revision;Expected behavior: For an existing draft with all matching predicates, one row is returned with the new revision. For a different tenant, owner, state or revision, zero rows are returned. The server supplies parameters 3 and 4 from a verified principal; handle zero rows without leaking a foreign record. This is an illustrative statement, not an executed PostgreSQL test.
Common mistake
Filtering the dashboard list by tenant but allowing an unscoped update by article ID protects only the display. Put the authorization conditions in every data operation, and verify authorization again when a write occurs.
Practice and verification
Create a matrix for anonymous, owner-editor, another editor, same-tenant admin and foreign admin. Include missing records and identical owner IDs in different tenants. In the V8 local repository fixture, inspect actual row state before and after a rejected operation.
Scope: App Router, proposed Next.js 16.x and React 19, Cache Components disabled. Framework snippets are staged, not executed; local policy, SQLite and HTTP transport evidence is separate. Exact patched dependencies and platform contracts are pending.
Further reading: Auth · Security Data · Actions