Goal
Allow single web service instance to serve different tenants on a per request basis.
Design
In order to allow a single web service instance to handle requests for multiple tenants there exists a tenant context service
which maps a JWT to database credentials (simplified for the purpose of this question). These credentials can then be used to retrieve only the authorized tenants data from the database.
Problem
There seem to be several ways in which a small and non-malicious change to the codebase by a developer could lead to data-leaks between tenants down the road. Example scenarios:
- Any use of the singleton pattern that effectively caches constructed components between requests could lead to implicitly cached credentials/session. This could for example be introduced by changing a flag in the dependency injection container. I could see both developer and reviewer being unaware of the consequences here as this is not entirely obvious.
- Storing the JWT, the db credentials or the session created from these credentials in a static property.
- Storing results from the database in-memory in any components which might leak them between tenants.
Questions
- Is there any best practice on how to handle this problem? Does it require changes to the design?
- Is there a way to reliably test against such a bug (it seems non-trivial as it could be edge cases in large scale deployments leading to this issue)
- Would it make sense to establish some kind of memory sandbox per request in order to avoid this and if yes, is there any documentation or example on how this can be done or is usually done?