Security Model¶
This page documents the security guarantees of django-rls-tenants, what it protects against, and what falls outside its scope.
Scope of the Guarantee¶
django-rls-tenants protects against omitted tenant scoping: code that reads or
writes tenant data without restricting it to the current tenant. Typical cases are a
forgotten .filter(tenant=...), a raw SQL query, a third-party library that issues its
own SQL, or a background job that runs without tenant context. In every one of those,
PostgreSQL applies the policy anyway, and with no context the query returns zero rows.
It does not protect against an attacker who can execute arbitrary SQL on the application's database connection. The tenant and admin GUCs are ordinary session variables, and any SQL running as the application role can change them:
That includes SQL injection, compromised application code or dependencies, and a
dbshell or psql session opened with the application's credentials. RLS is a
defense against mistakes in correct code, not a sandbox for untrusted SQL. Keep
using parameterized queries, keep dependencies audited, and protect the database
credentials as you would without RLS.
Fail-Closed Design¶
The most important security property: if no GUC variable is set, the RLS policy returns zero rows. This is the fail-closed default.
-- When rls.current_tenant is empty or unset:
CASE WHEN (SELECT current_setting('rls.is_admin', true)) = 'true'
THEN true
ELSE tenant_id = nullif(
(SELECT current_setting('rls.current_tenant', true)), '')::int
END
-- Admin not set → ELSE branch → nullif('', '') → NULL
-- tenant_id = NULL → always false → zero rows
Since v1.3.0 each current_setting() read is wrapped in an uncorrelated scalar
sub-SELECT -- (SELECT current_setting(...)) -- which PostgreSQL evaluates once
per statement (an InitPlan) rather than per row. This is a performance
optimization; the fail-closed semantics are identical. Measured plans are in the
Performance & Indexing guide.
This means:
- Unauthenticated requests see zero rows (middleware does not set GUCs). So do token
and JWT clients of a Django REST Framework view that does not use
RLSTenantAPIViewMixin: DRF authenticates after the middleware, which sees them as anonymous (see Django REST Framework). - Misconfigured middleware results in zero rows, not data leaks.
- Background tasks without explicit context see zero rows.
- Raw SQL queries without GUC context see zero rows.
What RLS Guarantees¶
Database-Level Enforcement¶
RLS policies are enforced by PostgreSQL, not by Django. This means every query is filtered, including:
- ORM queries (
Model.objects.all()) - Raw SQL (
cursor.execute("SELECT * FROM ...")) dbshellsessions (python manage.py dbshell) -- filtered by default, but the user can set the GUCs themselves (see Scope of the Guarantee)- Migration data operations
- Third-party libraries that issue SQL directly
- Database functions and triggers
FORCE ROW LEVEL SECURITY¶
The FORCE keyword ensures RLS applies even to the table owner (the database user
that created the table). Without FORCE, the table owner bypasses all RLS policies.
INSERT/UPDATE Validation¶
The WITH CHECK clause validates writes:
INSERT: the tenant FK must match the GUC value.UPDATE: the updated row must still match the GUC value.
This prevents a tenant from inserting data for another tenant or reassigning rows to a different tenant.
No Silent Fallback¶
Unlike application-level filtering, there is no code path where a developer can accidentally skip the filter. The policy is always active.
What RLS Does Not Guarantee¶
Schema-Level Isolation¶
RLS operates at the row level, not the schema level. All tenants share the same tables, indexes, and sequences. This means:
- Table structure is shared: all tenants see the same columns.
- Sequences are shared: auto-increment IDs are not tenant-specific (tenant A might see order #1, #3, #5 while tenant B sees #2, #4, #6).
- Indexes are shared: a unique constraint applies across all tenants unless it includes the tenant FK.
Referential Integrity and Unique Indexes¶
PostgreSQL exempts referential-integrity checks and unique indexes from RLS: foreign key checks and uniqueness checks always see every row, in every tenant. RLS is therefore not a substitute for tenant-aware FK validation or tenant-aware uniqueness.
- Foreign keys can point across tenants. The
WITH CHECKclause validates the tenant FK of the row being written, not the tenant of the rows it references. A tenant that knows or guesses another tenant's primary key can save anOrderwhosecustomer_idbelongs to someone else, and the FK check accepts it. The library validates this in Python:RLSProtectedModel.clean_fields()and DRF serializers withRLSTenantSerializerMixinreject a foreign key to a row of another tenant (validate_cross_tenant_fks(), see Cross-Tenant Foreign Keys), with the error Django gives a missing row, so it does not reveal that the row exists. That coversfull_clean()paths (ModelForm, the admin) only, notsave(),bulk_create(),update()or raw SQL: callvalidate_cross_tenant_fks()in service code, resolve referenced objects through the tenant-scoped manager before assigning them (an RLS-filtered lookup raisesDoesNotExistfor another tenant's row), or enforce it in the schema with a composite foreign key on(tenant_id, customer_id)that references a unique(tenant_id, id)pair, the only option that also covers bulk writes and raw SQL. Many-to-many through tables are the exception:RLSM2MConstraintpolicies check both ends withEXISTSsubqueries. - Unique indexes span all tenants. A global unique constraint rejects a value
that exists only in another tenant, and the resulting
IntegrityErrortells the writer that the value exists. Django'svalidate_unique()queries through RLS, so it cannot see the conflicting row and lets the save reach the database. System checkW012warns about each such unique field or constraint (see Cross-Tenant Unique Constraints).
Cross-Tenant Unique Constraints¶
If you need uniqueness within a tenant (e.g., unique invoice numbers per tenant), include the tenant FK in the constraint:
class Invoice(RLSProtectedModel):
number = models.CharField(max_length=50)
class Meta(RLSProtectedModel.Meta):
constraints = [
*RLSProtectedModel.Meta.constraints,
models.UniqueConstraint(
fields=["tenant", "number"],
name="unique_invoice_per_tenant",
),
]
Warning
Keep *RLSProtectedModel.Meta.constraints in the list. Assigning constraints
without it replaces the model's RLSConstraint, and the table gets no RLS policy.
A unique=True field (number = models.CharField(unique=True)), a unique_together
entry or a UniqueConstraint that leaves the tenant out is unique across all
tenants. Saving a value that only another tenant holds fails with an IntegrityError,
which tells the writer that the value exists there: a company registration number, a
slug or a tag name becomes an existence oracle. System check W012
(django_rls_tenants.W012) reports every such set on a model that has an
RLSConstraint or an EXTERNAL_MODELS entry, primary keys excepted:
unique=Truefields (aOneToOneFieldis one; a unique tenant field itself, as on a one-row-per-tenant model, is fine),Meta.unique_togetherentries,UniqueConstraint(fields=...), conditional ones included,UniqueConstrainton expressions, which passes when one expression references the tenant field:UniqueConstraint(Lower("slug"), "tenant", name=...).
Fix it by scoping the rule to the tenant, as above. Uniqueness that really is global
(a registration number the business guarantees to be unique, where telling a tenant that
it is taken is acceptable) is recorded in RLS_TENANTS["UNIQUE_ACROSS_TENANTS"], keyed
by model label (case-insensitive):
RLS_TENANTS = {
"TENANT_MODEL": "billing.Tenant",
"UNIQUE_ACROSS_TENANTS": {
"billing.Company": [
"registration_number", # unique=True field
"company_lower_name_uniq", # UniqueConstraint name
("country", "vat_id"), # unique_together / UniqueConstraint(fields=...)
],
},
}
A string is a field name or a UniqueConstraint name (the only way to list an
expression constraint); a tuple of field names matches a unique_together entry or a
UniqueConstraint(fields=...) with exactly those fields, in any order. An entry that
matches nothing, or a label that names no RLS-protected model, is reported as W012
too, so the list cannot go stale. Only a model's own fields are checked: the unique
fields of a multi-table-inheritance parent are reported on the parent. See
UNIQUE_ACROSS_TENANTS.
Models You Do Not Own¶
Added in 1.6.0.
A third-party app's model cannot carry an RLSConstraint. Its table can still get the
same policy from an AddRLSPolicy migration of your project, and RLS_TENANTS["EXTERNAL_MODELS"]
tells the library's runtime about the model (see
Tables You Do Not Own). The
database policy is identical to the one RLSConstraint creates, so the isolation of the
table is as strong as for any other protected table: enabled, forced, admin bypass in
USING and WITH CHECK, fail closed without a context.
What differs is the application layer. The model keeps its own manager, not RLSManager,
so queries on the external model itself have no automatic tenant filter, no
for_user() and no STRICT_MODE guard: the policy is the only thing that filters them,
and a missing context shows up as an empty result, not as NoTenantContextError. Joins
into it from a protected model (select_related(), prefetch_related()) and foreign
keys to it do get the library's tenant filter and the
cross-tenant foreign key check, through the
tenant field the entry names.
Two further limits:
- The policy is created only by the migration. An
EXTERNAL_MODELSentry without anAddRLSPolicyleaves the table unprotected;check_rlsreports it (no RLS policies found) and prints the operation to add. Run it in CI. - The model's many-to-many through tables are not covered:
register_m2m_rls()and thecheck_rlsM2M discovery only look at models that declare their own policy (anRLSProtectedModelsubclass or anRLSConstraint).
The policy's GUC names and tenant type are read from RLS_TENANTS when the migration runs
unless you pass them to the operation (RLSConstraint has the literal defaults instead), and
a later GUC_PREFIX change does not rewrite an applied policy.
Sharing Rows Across Tenants¶
Added in 1.6.0.
RLSConstraint(extra_predicates=[...]) and AddRLSPolicy(extra_predicates=[...]) widen
what a tenant can read (see
Sharing Rows Across Tenants).
The predicates are not part of the tenant policy: they form a second permissive policy,
<table>_tenant_sharing_policy, FOR SELECT only. What that does and does not allow:
- Reads widen only by what you list. A row is also readable when one predicate matches
it: a
SharedWithshare row for the current tenant, aTenantFieldthat is the current tenant, or yourRawPredicate. With no tenant context the tenant value isNULL, soSharedWithandTenantFieldmatch nothing. - Writes do not widen.
UPDATE,DELETEandINSERTapply the tenant policy alone, so a tenant cannot update, delete or insert a shared row, nor take it over withUPDATE ... SET tenant_id.SELECT ... FOR UPDATE/FOR SHAREapplies it too, so a shared row cannot be locked (it is not returned). The tenant policy itself is unchanged. - The ORM does not widen on its own. The default manager still filters on the
tenant. Only
including_shared()leaves the tenant filter out, only on a model with predicates, and it refusesupdate(),delete(),bulk_update(),update_or_create()andselect_for_update(). An instance of another tenant read through it can still be passed toinstance.delete(): the database deletes nothing, but Django's collector first runs the delete signals and cascades on the current tenant's own rows that point at it. Do not call it on shared rows. - A predicate is read access to another tenant's data. Anything on the row is readable: every column, and the foreign keys it holds. Relations of the row are not shared; each related table applies its own policy.
RawPredicateis yours. The library refuses only text that breaks out of the policy statement (;,--,/*,$, NUL, empty) and doubles a%for the schema editor. It does not check what the expression means. One that does not depend on the current tenant opens its rows to every tenant, and to sessions with no context. Review it like the policy it is part of. ASharedWithorTenantFieldwhose foreign key points at another model than the tenant model compares that model's keys with the tenant's; system checkW019reports it.- The share model has a policy too. It applies inside
SharedWith's subquery. A share model protected by the owner's tenant is invisible to the tenant it shares with, so the predicate matches nothing: it fails closed, not open. System checkW018reports it.W017reports a predicate that cannot be built, whichmigratewould fail on.
Aggregate Leaks¶
RLS prevents row-level access, but metadata can leak through side channels:
- Timing: query time may reveal data volume.
- Sequence values: auto-increment gaps reveal other tenants' activity.
- Error messages: unique constraint violations may reveal cross-tenant data (see Referential Integrity and Unique Indexes).
For high-security environments, consider UUIDs instead of auto-increment IDs.
Superuser and BYPASSRLS Roles¶
PostgreSQL superusers bypass all RLS policies, and so does any role created with
the BYPASSRLS attribute, superuser or not. The FORCE keyword applies to the table
owner, not to these roles.
Warning
Never connect Django as a superuser or a BYPASSRLS role in production. Use a
regular role with NOSUPERUSER NOBYPASSRLS that owns the application tables.
System check W005 warns when the role of any alias in RLS_TENANTS["DATABASES"]
has either attribute. With settings.DEBUG = False it is the error E001 instead,
which fails manage.py check and every command that runs the checks (migrate
included). Run migrations as the table owner, an ordinary role. If a migration or
test settings module must use a superuser, silence E001 there only, never in the
runtime settings.
CREATE ROLE app LOGIN PASSWORD '...' NOSUPERUSER NOBYPASSRLS;
-- or fix an existing role:
ALTER ROLE app NOSUPERUSER NOBYPASSRLS;
Connection-Level State¶
GUC variables are connection-level state. If multiple requests share a connection
(via connection pooling or CONN_MAX_AGE), GUCs from one request could leak to
another if cleanup fails.
Mitigations:
- Default behavior: middleware clears GUCs in
process_response(WSGI) or at the end of its async path (ASGI). - Safety net:
request_finishedsignal clears GUCs ifprocess_responseis skipped, on a connection that is still open (with Django's pool the next checkout clears it instead). It logs agucs.clearedaudit record atWARNINGwhen it runs. - Other threads: with session GUCs, the
connection_createdhandler sets the request's GUCs on connect only on connections opened on the thread that set them, where the cleanup above runs. A connection opened on another thread during the request (an ASGI error page,thread_sensitive=Falsework) is cleared instead (#130; see Running under ASGI). - GUC affinity guard: before each statement on a configured alias, the guard applies
the active context's GUCs if the connection holds other values, and clears them if no
context is active. Work on another thread therefore runs with the context it was handed,
only while that context is live, and the guard takes the context off that thread's
connection before the next statement run without one (until then the session still
holds it, which SQL that bypasses Django's cursor, such as
callproc(), would see). A connection whose cleanup failed is corrected before its next statement (#41). The guard only applies contexts the library itself set up (a privateContextVar), never one inferred from the public state functions, and only the admin and tenant GUCs. WithUSE_LOCAL_SET=Trueit applies them transaction-locally (1.4.0+, #96), so the session never holds them. See GUC Affinity Guard. - Django's connection pool: on each checkout from a pool (
OPTIONS["pool"]) on a configured alias, theconnection_createdhandler clears every GUC the policies read, with or without a request context, so a connection that the previous borrower's cleanup did not reach starts clean (#96). The pool'sresetcallbackdjango_rls_tenants.pool.reset_rls_gucsclears them again as the connection goes back, and system checkW010asks for it. GUCs read only by hand-written policies, or byAddM2MRLSPolicywith non-default names, or only by anAddRLSPolicy(its explicit GUC names andextra_bypass_flags), are not cleared. - USE_LOCAL_SET: transaction-local GUCs end with their transaction, so they never stay
on a connection. The transaction is the WSGI middleware's request transaction, one you
open with
transaction.atomic(), or, on the middleware's async path (ASGI) and for code outside a transaction, one the guard opens around each statement run under a context (1.4.0+; before, those cases raised). A few commands PostgreSQL refuses inside a transaction block (VACUUM,REINDEX,CREATE INDEX CONCURRENTLYand similar) run without the context; none of them evaluates an RLS policy. Code inside a context can still write the admin or tenant GUC for the session (set_guc(),rls_context()withoutis_local=True, a function, aDOblock): when a context ends, and on any connection after a statement run under a context's values, the sessions' own values count as unknown, and the next statement run outside any context, in autocommit or in a transaction, clears them first, so such a value does not outlive the context on a persistent or pooled connection. Since 1.7.0 (#173) the guard also flags a session when a statement it sees writes a policy GUC at session level (SET [SESSION], comments in it or not, aset_config()call,set_guc()included, or aDOblock spelling both; a compound statement that fails counts too, since it can commit such a write before it fails), and a context's exit (a block, afor_user()call, a request, therequest_finishedsafety net) clears the admin and tenant GUCs of a flagged session at once, with one statement per flagged alias (at the commit in a transaction; a failed transaction is skipped). A session never seen written gets no statement. A write the guard cannot see stays until that lazy clear: one inside a function or procedure (CALL, a function a statement calls) or inside aDOblock that does not spellset_configand the name in its text, one through a raw driver cursor,callproc()or the driver'scopymethods, a name passed in a way the guard does not read (from a table column, built inside a computed string), or a statement it cannot read. A write on another thread's connection stays too, such asset_guc()run in async_to_async()hop withthread_sensitive=Falseinside a block: the exit clears only the exiting thread's connections, and the worker thread's session keeps the write until its next statement run outside a context. Only the admin and tenant GUCs are cleared: a bypass flag or another policy GUC written for the session stays set, so never write the policy GUCs for the session from application code. Inside a context in autocommit, the guard's values override such a write, so it cannot narrow the context. An external pooler such as PgBouncer in transaction mode still needs this mode: with session GUCs the handler and the guard do not see that pooler hand the server connection to another client. Nor does the clear above reach every server connection there: the client's next statement can run on another one, so a session-level write stays where it was made, for every later client. A PgBouncerserver_reset_querythat clears the policy GUCs after every transaction covers it (see One Session-Level Write Reaches Every Later Client).
See Connection Pooling for details, and Async Deployment for the safe combinations under ASGI.
Django REST Framework¶
DRF authenticates inside the view, so RLSTenantMiddleware cannot set the context for
a token or JWT client. Isolation does not depend on getting this right: a view that
runs without a context reads zero rows (fail-closed). To serve such clients, use
RLSTenantAPIViewMixin, and list RLSTenantPermission in
REST_FRAMEWORK["DEFAULT_PERMISSION_CLASSES"] so that a view which forgot the mixin
answers 401/403 instead of an empty list. See the
Django REST Framework guide.
Properties of the DRF side worth knowing:
RLSTenantModelSerializertakes the tenant FK from the active context, ignores atenantvalue in the payload and removes other writable fields that would write the FK (tenant_id,source="tenant"), so a client cannot write into another tenant; theWITH CHECKclause is the backstop either way.- DRF views are exempt from Django's CSRF middleware; only
SessionAuthenticationenforces CSRF. When DRF does not accept a request's session (a token-only view), the mixin enters nothing and the view keeps the contextRLSTenantMiddlewareset for the session cookie's user.RLSTenantPermissionrefuses such a request; keep it in the default permission classes. RLSTenantPermissionlets a non-admin user through in their own tenant, or in the tenant context that aget_rls_context()override entered for the request (a tenant the user selected, say). It does not check that the user may access that tenant: the override must (see A custom selection rule). For aMultiTenantUserit also passes a tenant context of any tenant the user may select (see Users in Several Tenants). The admin bypass still needsis_tenant_admin.- DRF's
ModelSerializerdoes not callModel.full_clean(), so model-level validation does not run on API writes. The caveats in Referential Integrity and Unique Indexes apply regardless; per-tenant uniqueness belongs in aUniqueConstraintthat includes the tenant FK, which the serializer then validates as a400. RLSTenantSerializerMixin.validate()does run the cross-tenant foreign key check thatfull_clean()would, so a foreign key to another tenant's row is a400(see Serializers). A serializer that overridesvalidate()must callsuper().validate(attrs).
Users in Several Tenants¶
A MultiTenantUser selects the tenant a request runs
in, with a header or a session selection. The selection is input from the client, so the
library treats it as untrusted:
- Every selection is checked against the user's own tenants (
rls_tenant_idandrls_tenant_ids), on every request, by the middleware, the DRF mixin and the admin alike. A forged or stale selection can at worst switch the user to another tenant they already belong to; revoking a membership takes effect on the user's next request. - No fallback. A selection that fails the check is a 403 (
PermissionDenied); the request never runs in another tenant instead. A stale session selection is also removed, so the next request runs inrls_tenant_id. - One message for every refusal, "The selected tenant is not one you can access.", for
a tenant that exists but is not the user's, one that does not exist, and a malformed or
empty value, so a refusal is not an existence oracle. The audit record
(
tenant_not_selectable) holds the raw value for the operator, cut to 64 characters. - The header cannot be forged cross-site. A custom request header cannot be set by a cross-site form or image request, only by same-origin JavaScript or a CORS-allowed origin. What an origin you allow can do with it is bounded by the check above: select a tenant the user may select anyway.
- The session selection is a state change. Call
switch_tenant()from a CSRF-protectedPOSTview, never from aGET. The admin's tenant switcher, aGETlink, applies a member's choice only to a navigation from this site (Sec-Fetch-Site, elseReferer), so a link on another site cannot change which tenant the user works in. A link opened from another application counts as a typed address and is applied; it can only select one of the user's own tenants. - The session selection belongs to the session's user. A request authenticated
otherwise (a DRF token sent with another user's session cookie) neither uses nor drops
it, and
switch_tenant()refuses to store one for such a user. - Shared caches. A
MultiTenantUser's response carriesRLS_TENANTS["TENANT_HEADER"]inVary, so a cache keyed on the URL and the cookie or token does not serve one tenant's response for another tenant of the same user. - The membership table is read before any context exists. Keep it a plain table; if it
is RLS-protected,
rls_tenant_idsmust read it insideadmin_context(). - A tenant admin never selects. The header and the session selection are ignored for
is_tenant_admin=True, who runs inadmin_context()as before. - With no selection and no default the middleware sets no context (fail-closed), as for an anonymous request.
for_user() and @with_rls_context use the user's default tenant (rls_tenant_id), not
the request's selection.
Threat Model¶
Protected Against¶
| Threat | Mitigation |
|---|---|
| Developer forgets ORM filter | RLS policy enforces isolation regardless |
| Raw SQL without tenant filter | RLS policy applies to all SQL |
| Third-party library bypasses ORM | RLS policy applies at database level |
| Missing middleware (misconfiguration) | Fail-closed: zero rows returned |
| Unauthenticated access | No GUCs set → zero rows |
DRF view that forgot RLSTenantAPIViewMixin |
Fail-closed: zero rows; RLSTenantPermission answers 401/403 instead |
| Background job without tenant context | Fail-closed: zero rows returned |
| INSERT/UPDATE to wrong tenant | WITH CHECK clause validates writes |
| M2M link to wrong tenant's records | EXISTS-based subquery policies on M2M through tables |
| A user selecting a tenant they do not belong to (header, session, admin switcher) | The selection is checked against the user's tenants on every request; a refusal is a 403 with a uniform message and never a fallback |
| FK to another tenant's row in a form, the admin or a DRF serializer | validate_cross_tenant_fks() in clean_fields() and RLSTenantSerializerMixin.validate() |
| A tenant changing or deleting a row shared with it | The sharing policy is FOR SELECT; writes and FOR UPDATE use the tenant policy alone |
Not Protected Against¶
| Threat | Explanation |
|---|---|
| Arbitrary SQL on the application connection | SQL injection, compromised code, or a dbshell with the app credentials can call set_config() to change the tenant or admin GUC |
Superuser or BYPASSRLS role |
These roles bypass all RLS (W005 warns; E001 is an error with DEBUG = False) |
Database- or role-level default for a policy GUC (ALTER DATABASE/ROLE ... SET) |
In force in every new session before any context is set, and never cleared by the library (W011 warns on every configured alias) |
| Cross-tenant FK references written without validation | Referential-integrity checks are exempt from RLS. Validated on full_clean() paths and in DRF serializers with the mixin; not on save(), bulk writes or raw SQL (composite FK) |
| Cross-tenant uniqueness probes | Unique indexes are exempt from RLS and span all tenants |
| Schema-level information leaks | Shared tables, sequences, indexes |
| Timing side channels | Query duration may reveal data volume |
| GUC leak via connection pooling | Mitigated but not eliminated (see above): cleanup can fail, and an external pooler in transaction mode needs USE_LOCAL_SET |
A RawPredicate that ignores the tenant |
The expression is yours: it can open rows to every tenant and to sessions with no context (see Sharing Rows Across Tenants) |
| Application-level logic bugs | RLS filters rows, not application behavior |
| Denial of service | RLS does not rate-limit or throttle |
Strict Mode¶
By default, queries without RLS context silently return zero rows (fail-closed). While this is secure, it can mask developer mistakes -- the classic "where did my data go?" debugging experience.
STRICT_MODE=True adds an application-level guard: TenantQuerySet evaluation
methods raise NoTenantContextError before the query reaches the database if no
RLS context is active. This makes missing context failures loud instead of
silent.
What counts as "active context":
tenant_context()oradmin_context()context managersRLSTenantMiddleware(for authenticated requests)for_user()on the queryset
Guarded methods: _fetch_all() (iteration), count(), exists(),
aggregate(), update(), delete(), iterator(), aiterator(), bulk_create(),
bulk_update(), get(), first(), last(). A queryset whose results are already
cached answers iteration, count() and exists() without a query and is not checked,
so results fetched inside a context (including prefetch_related() caches) can be read
after it ends.
Note
Strict mode does not change database-level behavior. RLS policies continue to enforce fail-closed isolation regardless. Strict mode is an additional application-level safety net that surfaces mistakes earlier.
Excluding models¶
Some code legitimately reads an RLS-protected model with no context. The usual case is an
authentication backend that reads the user model under bypass_flag(): the flag is a
GUC, not an RLS context, so strict mode raises there. List such models in
STRICT_MODE_EXCLUDE:
RLS_TENANTS = {
"TENANT_MODEL": "myapp.Tenant",
"STRICT_MODE": True,
"STRICT_MODE_EXCLUDE": ["accounts.User"],
}
The exclusion removes the application-level guard for that model (and its proxies) and
nothing else. Without a context the database still fails closed: the query returns only
the rows the policy lets through, none unless a bypass flag from the constraint's
extra_bypass_flags is set or the connection role bypasses RLS. Do not use it to make an
"empty result" error go away: a query that should see a tenant's rows needs a context, and
the exclusion only turns the loud failure back into a silent one for that model. It
applies to every query on the model, not to one call. System check W015 reports entries
that name no installed RLS-protected model.
For one query, Model.objects.rls_skip_strict() skips the guard for that queryset
instead of the whole model. It is equally Python-side only: the policies, the auto-scope
filter and for_user() are unchanged, so with no context the query still fails closed.
With STRICT_MODE=False, AUDIT_NO_CONTEXT_QUERIES=True logs a context.denied record
(source queryset, reason no_context) for each query on a protected model that runs
with no context, so a code path that lost its context can be alerted on. See
Audit Logging.
See Configuration for setup, and
STRICT_MODE_EXCLUDE.
Recommendations¶
- Connect as a
NOSUPERUSER NOBYPASSRLSrole in production, and keepW005andE001unsilenced in the runtime settings. Treat aW013(a check that could not run) as an open finding, not as a pass. - Enable
STRICT_MODEin development and staging to catch missing context early. - Enable
USE_LOCAL_SETbehind an external pooler in transaction mode (PgBouncer), and setreset_rls_gucsas theresetcallback of Django's connection pool. - Use UUIDs for tenant PKs if sequence-based leaks are a concern.
- Include tenant FK in unique constraints that should be per-tenant, and keep
W012clean (see Cross-Tenant Unique Constraints). - Validate FK targets: forms, the admin and
RLSTenantSerializerMixindo it for you; in service code that usessave(), callvalidate_cross_tenant_fks(), resolve targets through tenant-scoped managers, or add composite FKs. RLS does not check the tenant of referenced rows. - Run
check_rlsin CI to verify policies exist. - Audit bypass usage (
admin_context,bypass_flag) regularly. The audit log records everyadmin_context()entry with its source and user, and every refusal;bypass_flag()andset_guc()are not logged, so review those in code. - Test fail-closed behavior with
assert_rls_blocks_without_context.