Test a multi-tenant API for data leaks between tenants

A B2B API serves hundreds of companies from one database. Every query is supposed to add WHERE tenant_id = ?. Forget it once, in one search endpoint or one export, and company A sees company B's invoices. It's the most expensive bug a SaaS can ship, and the tests for it are simple.

Two tenants in the environment

Set up once on staging, and save in the environment:

VariableValue
tenantAToken, tenantBTokena user token for each tenant (secret)
tenantAId, tenantBIdthe tenant ids
tenantBInvoiceId, tenantBCustomerIda few of Globex's object ids
tenantBMarkera unique string in Globex's data, such as a customer named GLOBEX-ONLY-7f3a

The marker is the trick: if it ever shows up in a response to Acme, something leaked.

One check for every read

In a Tenant isolation folder, all requests are sent as Acme (Authorization: Bearer {{tenantAToken}} on the folder). The folder's test script checks every response:

JavaScript
const marker = pm.environment.get('tenantBMarker');
const otherTenant = pm.environment.get('tenantBId');
const body = pm.response.text();

pm.test('no data from the other tenant', () => {
  pm.expect(body).to.not.include(marker);
  pm.expect(body).to.not.include(otherTenant);
});

Add every list, search, report and export endpoint to the folder. The check costs nothing per request and catches the forgotten WHERE.

Cross the line on purpose

Ids. Ask for Globex's objects with Acme's token:

Output
GET {{baseUrl}}/invoices/{{tenantBInvoiceId}}
JavaScript
pm.test("another tenant's invoice is not found", () => {
  pm.expect(pm.response.code).to.be.oneOf([403, 404]);
});

Do the same for update and delete. A PATCH that returns 404 for reads but 200 for writes is a real finding.

Tenant headers and parameters. If your API takes the tenant from a header or a path (X-Tenant-Id, /tenants/{id}/…), send Globex's id with Acme's token. The server must take the tenant from the token, not from what the client says:

Output
GET {{baseUrl}}/tenants/{{tenantBId}}/users
X-Tenant-Id: {{tenantBId}}

Search. Search for Globex's marker as Acme. Expect an empty result, not an error that says "1 match, access denied".

Exports and async jobs. Start an export as Acme, download it, and check the file for the marker. Background jobs are where tenant scoping is most often forgotten.

Both directions

Run the folder twice, as Acme and as Globex, with a data file that swaps the roles:

tenants.csv
me,other
A,B
B,A
JavaScript
// Folder pre-request
const me = pm.iterationData.get('me');
const other = pm.iterationData.get('other');
pm.variables.set('token', pm.environment.get('tenant' + me + 'Token'));
pm.variables.set('otherTenantId', pm.environment.get('tenant' + other + 'Id'));
pm.variables.set('otherMarker', pm.environment.get('tenant' + other + 'Marker'));

Then use {{token}}, {{otherTenantId}} and {{otherMarker}} in the folder, and run it with --data tenants.csv.