ENABLE ROW LEVEL SECURITY is a configuration fact, not proof of tenant isolation. The effective result depends on the role that reaches PostgreSQL, the policy attached to the operation, the source of tenant context, and the way connection-pool state is created and cleared. An audit must follow a real application request from authenticated identity to SQL execution and then repeat it when the same physical connection serves another tenant.
The useful outcome is an evidence chain: named build, endpoint, application identity, database role, transaction, context value, policy decision, returned or changed rows, pool mode, and reuse result. A screenshot of an enabled RLS switch cannot provide that chain.
Begin with the Effective Database Role
PostgreSQL 18 applies RLS after ordinary privileges allow an operation. When RLS is enabled and no applicable policy exists, the default is deny. But table owners normally bypass RLS unless the table is forced to apply it to the owner. Superusers and roles with BYPASSRLS always bypass it; FORCE ROW LEVEL SECURITY does not constrain those roles.
Inventory the roles actually used by the web process, background workers, migrations, support jobs, exports, and administrative tooling. Record membership and role switching, table ownership, BYPASSRLS, ordinary grants, and the final role visible on the query path. Testing a restricted role in a SQL console says little if production connects as the table owner or a privileged service role.
Pentest.red can help trace this cross-layer path when database roles, pool configuration, application code, and synthetic tenant accounts are explicitly included in the pentest scope. The deliverable should name the exact role and request that reached each policy, rather than claiming that every ordinary web assessment includes database configuration review.
Read the Policy by Operation
USING controls which existing rows are visible or targetable; a non-matching row is normally filtered without an error. WITH CHECK validates new values created by INSERT or produced by UPDATE, and a false or NULL result rejects the write. Policies can be command-specific and role-specific, and multiple policies can combine. A correct SELECT result therefore does not establish safe writes, and the absence of a SQL error does not establish that the intended row changed.
For each tenant table, build a small policy map:
SELECT: can tenant A read tenant B’s existing row?INSERT: can A create a row labelled as B?UPDATE: can A target its own row but change its tenant key to B?DELETE: can A delete B’s row by ID or through a bulk condition?- indirect paths: do approved functions, jobs, exports, or maintenance roles use another role or policy?
Do not treat row_security=off as a bypass. PostgreSQL documents it as an error-producing mode when a policy would filter rows, useful for detecting incomplete results in some backup contexts. The assessment must verify actual returned and changed rows.
Decide Whether Tenant Context Is Trusted
A policy can be syntactically correct and still trust the wrong input. If it compares tenant_id with a custom PostgreSQL setting supplied directly by the caller, isolation depends on who may set that value. A two-part custom setting is a storage mechanism, not authenticated identity. A request header, JSON field, or SQL argument becomes unsafe tenant authority when an actor can alter it without a server-side authorization decision.
Trace context from an authenticated server-side identity or other approved authority. Record where the tenant is selected, how it is bound to the transaction, whether the application can change it after authorization, and whether arbitrary SQL or a privileged helper can set it. The positive control must first prove tenant B can read B’s synthetic row through the intended path; only then does the negative A-to-B request test isolation rather than missing test data.
The broader data security audit covers access and exports. This article answers the narrower question of whether PostgreSQL receives a trustworthy tenant decision on the actual request path.
- IdentityAuthenticate synthetic tenant A or B and record the server-side account-to-tenant decision
- RoleConfirm the effective database role, ownership, grants, memberships, and bypass attributes
- ContextBind tenant state inside the intended transaction and record its trusted source
- PolicyExercise read and write operations against owned and foreign synthetic rows
- ReuseReturn the connection to the pool, borrow it for the other tenant, and repeat positive and negative controls
Treat Pool Reuse as a State Transition
PgBouncer documents that session pooling preserves a server connection for the client session, while transaction pooling returns it after each transaction; some session-based PostgreSQL features are incompatible with transaction pooling. This is a compatibility boundary, not a promise that every driver resets every custom setting.
PostgreSQL SET LOCAL ends at commit or rollback. That can support transaction-scoped context when the application establishes it inside every transaction, but it does not promise an empty value afterward: an earlier session-level SET becomes effective again after a successful transaction, while a rollback to a savepoint from before either command cancels that command. A successful session-level SET can persist for the session. The test must include the real driver, pooler, autocommit behavior, baseline session value, savepoints, error paths, nested operations, and any code that runs before context is set.
PgBouncer's feature table marks session-style SET/RESET expectations as incompatible with transaction pooling; that is not a ban on a properly scoped SET LOCAL inside one explicit transaction. PgBouncer documents that server_reset_query does not run for transaction pooling by default; server_reset_query_always changes that behavior. Neither setting alone proves tenant isolation. An application pool may retain a database client across many HTTP requests, so client release or disconnect does not necessarily occur after each request.
Never assume a reset from product name alone. Observe it: establish A's context, complete the transaction, return the logical client to the application pool, then make B use the same PostgreSQL backend as proven by a correlated backend identifier in an isolated test. Repeat after commit, rollback, savepoint rollback, statement error, request cancellation, retry, and a request that deliberately omits tenant context. The last case reveals whether a prior session baseline becomes active again.
Use a Matrix That Includes Reuse and Writes
Scroll sideways to compare
| Scenario | Positive control | Isolation probe | Evidence | Failure meaning |
|---|---|---|---|---|
| Read as A | A reads row A-101 | A requests B-202 | Effective role, context, SQL path, row IDs | Foreign read or an invalid test prerequisite |
| Insert as A | A creates a row owned by A | A submits B as the row tenant | Error or affected/returned row, then stored tenant key | Missing or ineffective write check |
| Update as A | A changes an allowed field on A-101 | A changes tenant key or targets B-202 | Affected/returned rows and final stored state | Unsafe target visibility or new-row check |
| Delete as A | A deletes an expendable A row | A targets an expendable B row | Row count and retained B control | Foreign delete or ambiguous missing row |
| Pool A→B | B reads B-202 after borrowing reused connection | B must not see A-101 or inherit A context | Physical connection marker, transaction and context | Leaked session state or wrong context setup |
| Privileged path | Approved job performs its documented task | Ordinary unauthorized web caller cannot reach that role/path | Call chain and role transition | Privilege boundary exposed to that caller |
Every destructive case uses separate disposable rows. A positive update or delete must not consume the object needed for the negative probe. Final server-side row state, not only an HTTP response, decides the result.
Run a Fictional A/B Connection-Reuse Test
Assume a synthetic service has tenant A user Alice and tenant B user Bob. The database contains A-101 and B-202, both verified through their own authorized request paths. The test pool is limited so the team can prove reuse of one PostgreSQL backend with an agreed correlation marker; the application sets tenant context at transaction start.
Alice reads A-101, then the transaction commits and the client returns to the application pool. Bob's request reaches that same backend. The positive control confirms Bob reads B-202. The isolation probe requests A-101 and must return no foreign row. The team repeats after an application error that triggers rollback, after a savepoint rollback, after a cancelled request, and once without tenant context to expose any inherited session baseline.
If Bob sees A-101, the test proves cross-tenant access. Calling leaked context the cause requires the recorded effective role, context value, policy decision, and reuse evidence; a bypass role or overly broad policy could produce the same outcome. If Bob with a correctly established B context cannot read B-202, the positive control has failed and this pair cannot establish isolation. The missing-context variant instead expects a safe denial or no protected rows; a normal B request with context remains a separate positive control. If reads are safe but Alice can insert a B-labelled row, the write policy is a separate finding.
Include Release, Worker, and Export Paths
Tenant isolation can differ across web requests, asynchronous jobs, data exports, and migrations because they use different roles and transaction boundaries. Do not generalize a passing web check to a worker. Map each in-scope path to its build, role, pool mode, and tenant-context source.
Mixed deployments also matter. An older instance may set session-level context while a newer one uses transaction scope. During a rolling release, both may share a pool or database. The guide to pre-release transition-state testing explains why a fixed build must be checked across every active replica.
Keep privileged maintenance paths explicit. A migration role may legitimately bypass RLS, but an ordinary unauthorized application caller must not be able to select that role or invoke an unrestricted helper. The audit should report the boundary and reachability, not label every bypass role a vulnerability. Hosted wrappers must be tested on the actual request: Supabase, for example, documents that a secret key bypasses RLS only when the request has no user access token; with a user token, the signed-in user's RLS policies apply even if the client was initialized with the secret key.
Retest the Named Build, Role, and Pool Configuration
In an authorized cross-layer penetration test, Pentest.red can combine source-assisted tracing with synthetic requests and database evidence when those assets are expressly in scope. Engineers receive the effective role, policy and operation, trusted-context source, pool transition, reproducible A/B sequence, impact, and remediation priority.
After remediation, repeat the same positive and negative matrix on the named application build, database policy revision, role grants, and pool configuration. Include commit, rollback, error, cancellation, and reuse. A defensible closure statement is specific: every tested application path established trusted tenant context before protected SQL, foreign reads and writes were rejected, and reused connections did not carry tenant state across the tested transitions.
The conclusion remains bounded to the roles, tables, paths, pool behavior, and versions examined. RLS is then part of the evidence for isolation, rather than a checkbox standing in for it.
PENTEST.RED
Discuss pentest scope and pricing
We start by agreeing on systems, access, constraints and the expected evidence.
Based on in-depth research, edited for clarity and practical use.