Summary
A SQL injection vulnerability in django-tenants' PostgreSQL backend arises when tenant schema names are interpolated into SQL without proper escaping. If an attacker can choose or influence a schema name used by affected operations, they may read data belonging to other tenants or execute SQL with the application's database privileges.
Affected context
The vulnerability affects django-tenants deployments using the PostgreSQL backend when tenant schema names are not safely handled in SQL statements. The risk applies to schema-related operations such as setting the search path or creating, dropping, or altering schemas, when an attacker can influence the name supplied to those operations.
Exploit preconditions
- Attack surface: PostgreSQL backend operations that use tenant schema names in SQL.
- Required access: The ability to choose or influence a schema name that reaches an affected operation; this may depend on application access controls.
- Trigger point: A schema-related statement such as
SET search_pathor aCREATE,DROP, orALTER SCHEMAoperation. - Why exploitation is possible: Unescaped schema names can be interpreted as SQL syntax rather than only as identifiers.
Why this happens
The underlying issue is SQL injection (CWE-89): data derived from a tenant schema name is inserted into SQL statements without correctly preserving its status as an identifier. Because SQL identifiers cannot generally be protected by value parameterization in the same way as ordinary data values, unsafe string construction can allow quote characters to alter the meaning of a statement.
Potential impact
- An attacker may read data belonging to other tenants.
- An attacker could execute SQL using the application's database privileges.
- Schema operations could be altered, potentially affecting tenant data or database structure.
- The impact may extend to other database-accessible resources, depending on the application's privileges.
General mitigation guidance
Defensive patterns include treating schema names as untrusted input and ensuring they remain identifiers, not executable SQL. Where possible, derive names from trusted server-side values and enforce a strict allowlist or schema-name format. Use database-aware identifier quoting for dynamic identifiers, and parameterized queries for ordinary data values. Limit the database account to the privileges needed by the application, and apply tenant isolation controls as defense in depth.
How teams detect issues like this
Review code paths that build SQL statements using tenant or schema names, especially search-path changes and schema creation, deletion, or alteration. Static analysis and code review can flag string concatenation or formatting in SQL construction and check whether dynamic identifiers use safe, database-aware handling. Policy checks can require allowlisted identifier sources, appropriate quoting, and least-privilege database roles; automated review tools may help identify risky patterns.

