Multi-tenant access control looks simple until you write the check. Here is a mistake we made, and why it is worth knowing about.
The tree
Tenants form a hierarchy: platform, then master partners, then partners, then businesses, then teams. A parent should be able to read its descendants. Nothing should read sideways or upward.
The mistake
The natural way to write it is to check whether the target appears in the caller's ancestry. That is exactly backwards — a caller's ancestry contains its parents, so the check grants a child access to everything above it.
// Wrong — grants a child access to its parent
return scope.ancestry.includes(targetTenantId);
// Right — the TARGET must sit beneath the caller
return targetAncestry.includes(scope.tenantId);What caught it
A lint warning, of all things — an unused parameter. The parameter was unused because the function was not doing what its name claimed. That was the thread worth pulling.
The lesson we took
An unused parameter in an authorisation function is never cosmetic. It means the check is not using something it was given, which usually means it is not checking what you think.
Testing both directions
One assertion is not enough. We now assert that a partner CAN read a tenant beneath it, and that a partner CANNOT read across to another partner's tenant. A check that only tests the denial case passes happily when the function denies everything.