Security release
This release fixes two vulnerabilities in how django-tenants restores the
previous tenant when leaving schema_context(), tenant_context() or
with tenant:. In both, a connection could be left on another tenant, so
later queries read or wrote that tenant's data.
- Context managers on a non-default database alias (GHSA-q7xr-949m-53qg,
medium). WithTENANT_DB_ALIASordatabase=,schema_context()and
tenant_context()restored the tenant of the default connection onto the
alias they had switched. Only affects projects where bothdefaultand the
other alias use the django-tenants backend. Reported by @zerox80. - Concurrent use of
with tenant:and the decorator form
(GHSA-8558-24hp-q3wq, medium).with tenant:kept the tenants to restore in
a list shared by every thread, and@schema_context(...)/
@tenant_context(...)kept them on one instance shared by every call. In
threaded workers, overlapping uses could restore each other's tenant.
Affected: all versions before 4.0.5. All users should upgrade.
See the advisories for full details and workarounds.
Upgrade notes
- All three context managers now keep the tenants to restore on a stack on
the database connection (which is per thread). There is no change for code
that uses them normally. - Subclasses of
schema_contextortenant_contextthat override
__enter__or__exit__must callsuper()in both, so the stack stays
balanced. - The private class attribute
TenantMixin._previous_tenanthas been removed.