Repository navigation
Backport 4.0.7: reject "$user" as a schema name - #50
Merged
Merged
Conversation
kiwitcms-bot
force-pushed
the
backport/upstream-4.0.7
branch
from
November 23, 2026 17:15
d512d84 to
be95f90
Compare
In a search_path PostgreSQL replaces "$user", even when quoted, with the schema named after the current database role, while CREATE/DROP SCHEMA act on a literal "$user" schema. A tenant named $user therefore ran its migrations and all its queries against the role-named schema, possibly another tenant's, even with TENANT_REJECT_EXISTING_SCHEMA on. Without such a schema, creating it failed and left the tenant row behind. - The schema name validator rejects $user (any case), so it can't be used for new tenants, renames, or switching the connection. - create_schema() always returns the connection to public, also when migrating fails, so the failed-create cleanup deletes the tenant row instead of failing on the half-created schema. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --- https://github.com/django-tenants/django-tenants/releases/tag/v4.0.7 GHSA-rh58-8mmc-gcfw django-tenants@bd512ca
kiwitcms-bot
force-pushed
the
backport/upstream-4.0.7
branch
from
November 23, 2026 17:34
be95f90 to
479c192
Compare
The fork restricts schema names to [a-z0-9]{1,63}, so rename the new
fixtures in test_tenants.py and assert that '$user2', 'my$user' and '$'
are rejected here too. No production code changes; search-path
validation already comes from the upstream refactor in #51.
kiwitcms-bot
force-pushed
the
backport/upstream-4.0.7
branch
from
November 23, 2026 17:36
479c192 to
8b18553
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Backports the upstream django-tenants 4.0.7 security fix for GHSA-rh58-8mmc-gcfw (medium).
In a PostgreSQL
search_path,$useris replaced — even when quoted — with the schema named after the current database role. A tenant named$usertherefore ran migrations and queries against that schema, which could belong to another tenant, even withTENANT_REJECT_EXISTING_SCHEMAon.The fix:
$userin any case.TenantMixin.create_schema()always returns the connection to the public schema, including when migrating fails, so the failed-create cleanup can delete the tenant row and raise the original error.This is a cherry-pick of upstream
bd512ca, preserving the original commit message and authorship. Its regression tests are included in that commit. A second commit adapts the tests to this fork's stricter[a-z0-9]{1,63}rule; search-path validation comes from the upstream refactor merged in #51.Upstream references:
Downstream check: Kiwi TCMS has restricted tenant schema names to
[a-z0-9]{1,63}since the validator tightening, andNewTenantFormuses that validator, so$usercannot be created or renamed to. Neithertcms_tenantsnortcms_enterpriseoverridesPUBLIC_SCHEMA_NAMEorPG_EXTRA_SEARCH_PATHS. An existing$userrow would already be refused when switching schemas by the fork validator, but thecreate_schema()cleanup fix is still relevant for failed tenant creation.Verification:
manage.py test django_tenantswithEXECUTOR=standard: 109 tests, OK (1 skipped)manage.py test django_tenantswithEXECUTOR=multiprocessing: 109 tests, OK (1 skipped)manage.py check: no issues