Home Projects Portfolio Dashboard Export PDF Log in

Securing Multi-Tenant APIs: Preventing Cross-Tenant Data Tampering

In multi-tenant applications, a critical security concern is ensuring that users can only access and modify data belonging to their own tenant. We recently addressed a subtle yet significant vulnerability in our motos-max-cordialidad project that highlighted the importance of robust input validation and server-side data trust.

The Problem: Client-Supplied Tenant IDs Overriding Security

Imagine an API designed to manage various entities like customer records, vehicle details, or work orders. Each record belongs to a specific "tenant" or workshop, identified by a tenantId. Our application's update and creation handlers were constructing their use-case input by merging user-supplied request body data with trusted information derived from the authenticated user's JSON Web Token (JWT). The common pattern looked something like this:

const useCaseInput = { tenantId: user.tenantId, ...request.body };

The intent was to ensure that the tenantId from the authenticated user's JWT token was always used. However, this spread syntax ...request.body first, followed by tenantId: user.tenantId was problematic. If request.body itself contained a tenantId or a resource id, it would override the value derived from the user's authentication token. This created a severe vulnerability.

An authenticated user, with valid update permissions, could craft a malicious request body containing another tenant's ID or a different resource ID. Since the client controlled both the path parameters and the request body, this allowed them to perform read-modify-write operations on resources belonging to other tenants. This could lead to account takeovers, unauthorized data tampering, and a complete breakdown of multi-tenancy isolation across workshops.

The Journey to the Solution: Prioritizing Server-Derived Identity

The core principle to uphold in such scenarios is that server-derived identity and security-critical fields must always take precedence over anything sent by the client. The fix involved a straightforward but crucial change in the order of operations when constructing the input for our use-cases.

Instead of spreading the client's body first and then applying trusted fields, we reversed the order: apply trusted, server-derived fields first, and then spread the client's body data. This ensures that any tenantId or id present in the client's body will be overwritten by the server's authoritative values.

The Solution: A Simple Order Change

The updated pattern now looks like this:

// Server-derived fields first, then merge client body. Server-derived wins on conflict.
const useCaseInput = { ...request.body, tenantId: user.tenantId, id: request.params.id };

// Example for a creation handler (no path ID)
const creationUseCaseInput = { ...request.body, tenantId: user.tenantId };

This small change was applied to all 11 affected call sites across various entities (customers, users, parts, vehicles, motorcycle-units, service-catalog, tenants, and work-orders). It ensures that the tenantId and resource id provided by the authenticated user's token and the request path are the authoritative sources, irrespective of what the client attempts to send in the request body.

To further solidify this, we implemented a controller regression test. This test specifically simulates a hostile request where a client attempts to inject an unauthorized tenantId or id in the request body, verifying that these values are correctly ignored in favor of the token-derived and path-derived identifiers.

The Takeaway: Trust No Input, Validate Everything

This incident reinforces a fundamental security principle for API design: never implicitly trust client-supplied identifiers, especially when they govern access control or data ownership. Always explicitly define the source of truth for critical fields. Prioritize server-derived and authentication-based data over any client input to prevent unauthorized access and maintain the integrity of your multi-tenant environment. Always validate and sanitize all client input, and construct your internal data models with trusted sources taking precedence.


Generated with Gitvlg.com

Securing Multi-Tenant APIs: Preventing Cross-Tenant Data Tampering
Luis Romero

Luis Romero

Author

Share: