Skip to content

Backport 4.0.7: reject "$user" as a schema name - #50

Merged
atodorov merged 2 commits into
kiwitcms-v3.8.0from
backport/upstream-4.0.7
Nov 23, 2026
Merged

atodorov merged 2 commits into
kiwitcms-v3.8.0from
backport/upstream-4.0.7

Conversation

@kiwitcms-bot

@kiwitcms-bot kiwitcms-bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Backports the upstream django-tenants 4.0.7 security fix for GHSA-rh58-8mmc-gcfw (medium).

In a PostgreSQL search_path, $user is replaced — even when quoted — with the schema named after the current database role. A tenant named $user therefore ran migrations and queries against that schema, which could belong to another tenant, even with TENANT_REJECT_EXISTING_SCHEMA on.

The fix:

  • The schema-name validator rejects $user in 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, and NewTenantForm uses that validator, so $user cannot be created or renamed to. Neither tcms_tenants nor tcms_enterprise overrides PUBLIC_SCHEMA_NAME or PG_EXTRA_SEARCH_PATHS. An existing $user row would already be refused when switching schemas by the fork validator, but the create_schema() cleanup fix is still relevant for failed tenant creation.

Verification:

  • manage.py test django_tenants with EXECUTOR=standard: 109 tests, OK (1 skipped)
  • manage.py test django_tenants with EXECUTOR=multiprocessing: 109 tests, OK (1 skipped)
  • manage.py check: no issues

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
kiwitcms-bot force-pushed the backport/upstream-4.0.7 branch from be95f90 to 479c192 Compare November 23, 2026 17:34
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
kiwitcms-bot force-pushed the backport/upstream-4.0.7 branch from 479c192 to 8b18553 Compare November 23, 2026 17:36
@atodorov
atodorov merged commit 6cfcf51 into kiwitcms-v3.8.0 Nov 23, 2026
98 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants