An authenticated user can still request an object owned by another account. If the server checks only that the session exists, changing an identifier can cross the application's tenant boundary. Make the permission decision about the requested object and action, using authoritative membership and ownership context.
For a multi-tenant document API, protect both document contents and the authority to change them. The worked policy uses readers and editors in two synthetic tenants, then follows requests through object lookup, field permissions, caches, and denial tests.
Define the relationship that permits each action
List the actors, objects, and actions before writing middleware. A tenant member may read a document while only its owner or an assigned editor may modify it. An administrator may manage membership without automatically having permission to export all document contents.
Express these distinctions in an access matrix with explicit default denial. The OWASP authorization guidance recommends denying by default and validating permissions on every request. Use those principles to define the application policy, then verify how the chosen framework actually enforces it.
Derive the active tenant from a validated membership relationship. A tenant identifier supplied by the client can help select a context, but it cannot establish membership. Check the server's authoritative state before using that selection to authorize access.
Record exceptional paths such as support access or emergency administration. Give them deliberate scope, audit behavior, and revocation rules. An undocumented bypass inside a general helper makes the apparent tenant policy difficult to reason about and harder to test.
Constrain data access before returning objects
Make the policy concrete with two synthetic tenants, North and South. Alice is an editor in North; Ben is a reader in North; Cara belongs only to South. Document doc-17 belongs to North. Under this example policy, Alice may read and edit its title, Ben may read but not edit, and Cara may do neither. Membership administration is a separate permission and does not grant document access by implication.
Turn those relationships into a small set of regression cases:
- Alice reads
doc-17in her verified North context: return its allowed fields. - Ben changes its title: deny and preserve the title and document version.
- Cara supplies North's tenant ID in a header: reject the selected context because she has no North membership.
- Cara requests
doc-17using her valid South context: return the API's consistent unavailable-object response without document data. - Alice submits an extra
tenant_idorowner_idproperty with a valid title: reject the unsupported mutation shape rather than mass-assigning ownership.
The last case catches a different bug from cross-tenant lookup. A correctly scoped query can still be followed by an overly broad update that moves the object across the boundary.
Design repository or service interfaces that require the relevant access context. For the synthetic API, looking up a document by its identifier alone should not automatically produce a client-visible object. The access path needs the caller's verified tenant and the action being requested.
Where practical, include tenant scope in the authoritative query rather than retrieving a global object and hoping every caller remembers to filter it afterward. This is an engineering recommendation that reduces reliance on repeated caller discipline. It still requires correct policies for shared objects, administrative access, and cross-tenant workflows.
For a simple tenant-owned document, think of the lookup as the conjunction of object ID and verified tenant ID, followed by the action-specific permission. A tenant predicate alone would still let Ben edit if the implementation forgets the reader/editor distinction. Conversely, an editor role checked without object scope could let Alice edit South's documents. Keep both predicates visible in the service contract and the denied tests.
Treat opaque identifiers as identifiers. An unguessable document ID can reduce casual discovery, but it does not grant permission to whoever obtains it. Links, browser history, exports, logs, and legitimate sharing can expose identifiers beyond the original viewer.
Choose a consistent response policy for unavailable and unauthorized objects. Avoid response details that unnecessarily reveal another tenant's object existence. Internally, retain sanitized decision context sufficient for investigation without logging document contents or session material.
Cover every representation and mutation path
Check list endpoints, individual reads, downloads, search results, exports, previews, and attachment routes. A protected document page provides little isolation if a preview endpoint retrieves the same content without an equivalent decision.
For writes, verify both access to the object and authority over the requested fields. A member permitted to edit a title should not be able to replace the owner or tenant through additional properties in the request. Define a narrow validated mutation shape instead of applying all received fields.
Review batch operations as their own policy. Decide whether a mixed-authority batch fails entirely or reports per-item results, and avoid returning unauthorized details in either response. Bound batch size so permission evaluation cannot be used to trigger uncontrolled work.
Background jobs need authoritative context too. Decide whether access is evaluated when a job is accepted, when it executes, or at both boundaries. Document how membership revocation affects a queued export. A job should not silently inherit indefinite authority from an old browser action.
Keep revocation and caching within the policy
Cache identity is part of this boundary. Suppose the response cache uses only doc-17 as its key and returns the entry before authorization. Alice's successful read can then populate content that Cara receives on a later cache hit. Including the tenant in a key can prevent accidental namespace collisions, but it does not replace checking Cara's authority before returning the cached representation. Shared objects and role-specific fields may require additional variation or separate representations.
Test this sequence explicitly: populate the cache as Alice, repeat the request as Cara, then revoke Alice's membership and request again as Alice. A cold-cache test would miss both the populated-cache bypass and stale-grant behavior. Inspect body, headers, download capabilities, and queued effects rather than treating a denial status as sufficient evidence.
Identify cached membership, role, and object-sharing decisions. Define how long a stale grant may remain effective and how the system invalidates it. A fast cache with an unexplained revocation delay can violate the product's access promise.
For a consequential mutation, consider the race between checking a permission and committing the change. Choose transaction or concurrency controls that preserve the required relationship during that operation. The transaction isolation guide provides a way to reason about that schedule without assuming all checks remain true indefinitely.
Do not use a browser's hidden controls as the enforcement boundary. The server must handle a direct request from a caller who never loaded the interface. Keep user-interface feedback helpful, while making the authoritative permission decision independently.
If a policy service is unavailable, specify which operations fail closed and how users recover. Do not convert an unavailable authorization result into a broad temporary grant. Preserve a controlled error and an observable internal classification rather than returning a stack trace.
Test denial as an absence of effects
Create synthetic tenants with an owner, a reader, a nonmember, and a revoked member. Exercise each action across own-tenant and other-tenant objects. Include alternate representations and attempts to change ownership fields.
Assert more than the response status. A denied mutation should leave object state unchanged and should not enqueue an export, send a notification, or create a usable download capability. A denied read should not include protected data in an error body or response metadata.
Add tests for stale cached permissions and revoked access to queued work. These tests expose the lifetime of a grant, which a simple route test may miss. Keep fixtures synthetic and avoid using real tenant records for isolation checks.
If a cross-tenant exposure is discovered, contain the affected access path and revoke related capabilities before performing cleanup. Investigate the affected scope through controlled evidence. Restore the feature when the authoritative boundary and its denial tests agree on who may perform each action.
Was this guide useful?
Your rating helps us prioritize clearer, more practical technical content.
Review diffs, run checks, and prepare the release in Arezgit.
Keep Git review, security scanning, API checks, database inspection, and release preparation together in one local desktop application.
Explore Arezgit