$unuseAll() / $unuse('policy') inside $transaction still apply PolicyPlugin
Description and expected behavior
Inside an interactive transaction, a client derived with $unuseAll() or $unuse('policy') still enforces access policies. The same call outside a transaction bypasses them, as documented.
Expected: the derived client stays in the same transaction (as fixed in #2494) but runs without the removed plugins.
plugin policy {
provider = '@zenstackhq/plugin-policy'
}
type AuthUser {
id String @id
organizationId String
@@auth
}
model Organization {
id String @id
name String
@@deny('all', auth() == null || auth().organizationId != id)
@@allow('all', true)
}
const db = new ZenStackClient(schema, { dialect })
.$use(new PolicyPlugin())
.$setAuth({ id: 'u1', organizationId: 'o2' })
// table has one row: { id: 'o1', name: 'O1' }
const outside = await db.$unuseAll().organization.count()
const inside = await db.$transaction(tx => tx.$unuseAll().organization.count())
const unuse = await db.$transaction(tx => tx.$unuse('policy').organization.count())
console.log({ outside, inside, unuse })
// actual: { outside: 1, inside: 0, unuse: 0 }
// expected: { outside: 1, inside: 1, unuse: 1 }
Environment:
- ZenStack version: 3.10.0 (the code below is unchanged on
main) - Database type: PostgreSQL 17
- Node.js version: 24.x
- Package manager: npm
Additional context
Looks like a side effect of #2497 (the fix for #2494). In the ClientImpl constructor:
if (baseClient?.isTransaction && !executor) {
this.kysely = baseClient.$qb;
}
The derived client reuses the transaction's Kysely instance, whose executor is the ZenStackQueryExecutor created for the original client. That executor still runs the original plugin list, so $unuse* has no effect on queries run through the derived client. Keeping the transaction is right; the plugins should come from the derived client.
Impact: code that needs a policy-free client inside a transaction (e.g. an onEntityMutation hook with runAfterMutationWithinTransaction: true, or a procedure called within $transaction) silently gets filtered results instead of an error.
I dug into this. A client derived inside a transaction reuses the transaction's Kysely instance (this.kysely = baseClient.$qb in client-impl.ts, from #2497), and that executor's ZenStackQueryExecutor still points at the original client, so onKyselyQuery hooks come from the original $options.plugins. That's why $unuse('policy') still applies the policy, and a plugin added with $use inside a transaction wouldn't get onKyselyQuery either.
I have a local fix where a derived tx client builds its own ZenStackQueryExecutor using the transaction's executor as its connection provider. Your issue repro and a regression test pass with it, as do the transaction, plugin-infra and client-extensions e2e tests on sqlite. One catch: Kysely's "already committed" guard lives in its internal wrapper, so a derived client kept around after the transaction ends would no longer throw and would run on the released connection.
Would you rather keep that guard through some shared transaction-liveness state passed to derived clients, or is the simpler version OK for a first PR? Happy to do either.
@aliassar thanks for digging into this, your analysis matches what I saw.
I'd prefer keeping the guard. Without it, a loud error becomes a silent one: a derived client that outlives the transaction would run on a connection already returned to the pool, which with pg may be serving another request by then. That's harder to track down than the original bug. A shared liveness flag sounds fine to me, but I'll defer to the maintainers on the exact shape.
Cases I'd like the tests to cover:
tx.$unuseAll()andtx.$unuse('policy')bypass the policy and still see the transaction's uncommitted writes- rolling back the transaction also rolls back writes made through the derived client
- a plugin added with
tx.$use()getsonKyselyQuery - a derived client used after commit or rollback throws, same as the transaction client
For context, my current workaround is raw SQL with dangerouslyAllowRawSql, which I'd like to drop once this lands.