Add rls-safe-mode module: Hibernate 8.0.0.CR2 HQL safe mode and PostgreSQL row-level security for @TenantId

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b6ZEVHGaYy15HAxRzy
This commit is contained in:
Ankur Mhatre
2026-12-01 08:08:31 +05:30
co-authored by Claude Sonnet 5.5
parent 54c6d606ff
commit 1c9d9423c0
20 changed files with 817 additions and 1 deletions
+22
View File
@@ -0,0 +1,22 @@
hibernate.query.safe_mode_enabled=false
upper() built in ok: [ALICE, BOB]
initcap() contributed ok: [Alice, Bob]
quote_ident() unregistered ok: [alice, bob]
current_setting() unregistered ok: tenantA
set_config() unregistered ok: tenantB
sql('...') raw SQL ok: [alice, bob]
criteria function() ok: tenantA
criteria sql() ok: [alice, bob]
translate() built in on PostgreSQL ok: [xlice, bob]
hibernate.query.safe_mode_enabled=true
upper() built in ok: [ALICE, BOB]
initcap() contributed ok: [Alice, Bob]
quote_ident() unregistered rejected: SemanticException: Function [quote_ident] is not allowed in safe mode [select quote_ident(i.customer) from Invoice i order by i.id]
current_setting() unregistered rejected: SemanticException: Function [current_setting] is not allowed in safe mode [select current_setting('hibernate.tenant_id')]
set_config() unregistered rejected: SemanticException: Function [set_config] is not allowed in safe mode [select set_config('hibernate.tenant_id', 'tenantB', true)]
sql('...') raw SQL rejected: SemanticException: Function [sql] is not allowed in safe mode [select sql('lower(?)', i.customer) from Invoice i order by i.id]
criteria function() rejected: SemanticException: Function [current_setting] is not allowed in safe mode
criteria sql() rejected: SemanticException: Function [sql] is not allowed in safe mode
translate() built in on PostgreSQL ok: [xlice, bob]
@@ -0,0 +1,11 @@
select relname, relrowsecurity, relforcerowsecurity from pg_class (invoice, receipt)
invoice rowsecurity=true force=true
receipt rowsecurity=true force=true
select tablename, policyname, cmd, qual, with_check from pg_policies
table=invoice policy=hibernate_tenant_isolation cmd=ALL
using (((tenant_id)::text = ((NULLIF(current_setting('hibernate.tenant_id'::text, true), ''::text))::character varying(255))::text) OR COALESCE((NULLIF(current_setting('hibernate.tenant_id_root'::text, true), ''::text))::boolean, false))
with check (((tenant_id)::text = ((NULLIF(current_setting('hibernate.tenant_id'::text, true), ''::text))::character varying(255))::text) OR COALESCE((NULLIF(current_setting('hibernate.tenant_id_root'::text, true), ''::text))::boolean, false))
table=receipt policy=hibernate_tenant_isolation cmd=ALL
using (((tenant_id)::text = ((NULLIF(current_setting('hibernate.tenant_id'::text, true), ''::text))::character varying(255))::text) OR COALESCE((NULLIF(current_setting('hibernate.tenant_id_root'::text, true), ''::text))::boolean, false))
with check (((tenant_id)::text = ((NULLIF(current_setting('hibernate.tenant_id'::text, true), ''::text))::character varying(255))::text) OR COALESCE((NULLIF(current_setting('hibernate.tenant_id_root'::text, true), ''::text))::boolean, false))
@@ -0,0 +1,2 @@
hibernate.multi_tenant.rls_enabled=false
policies=0 tables with row security=0
+20
View File
@@ -0,0 +1,20 @@
rls_enabled=true. Rows: tenantA=2, tenantB=1
tenantA HQL select count(i) from Invoice i -> 2
tenantA native select count(*) from invoice -> 2
SQL that reached PostgreSQL for the native query:
select set_config('hibernate.tenant_id', ?, true), set_config('hibernate.tenant_id_root', ?, true) [tenantA, false]
select set_config('hibernate.tenant_id', ?, true), set_config('hibernate.tenant_id_root', ?, true) [tenantA, false]
select count(*) from invoice []
tenantA native insert for tenantB -> rejected: PSQLException: ERROR: new row violates row-level security policy for table "invoice"
admin (root) HQL count -> 3 native count -> 3
no tenant set -> rejected: HibernateException: SessionFactory configured for multi-tenancy, but no tenant identifier specified
Outside Hibernate, on plain JDBC connections:
role app, no tenant variable set select count(*) -> 0
role app, after set_config('...','tenantB') select count(*) -> 1
role postgres (superuser) select count(*) -> 3
role app superuser=false bypassrls=false
role postgres superuser=true bypassrls=true
rls_enabled=false. Same data.
tenantA HQL count -> 2 native count -> 3
@@ -0,0 +1,8 @@
safe_mode_enabled=false. tenantA runs a hostile HQL fragment, then the app counts rows natively in the same transaction.
hostile set_config(): ran
native count afterwards -> ok: 1
safe_mode_enabled=true. tenantA runs a hostile HQL fragment, then the app counts rows natively in the same transaction.
hostile set_config(): blocked (SemanticException)
native count afterwards -> ok: 2
@@ -0,0 +1,12 @@
rls_enabled=false, so only the SQL in @SQLUpdate and @SQLDelete stands between tenants.
tenantA edits its own receipt:
update receipt set memo=? where id=? and tenant_id=coalesce(?,tenant_id) [a-edited, 1, tenantA]
root edits tenantB's receipt:
update receipt set memo=? where id=? and tenant_id=coalesce(?,tenant_id) [b-edited-by-root, 2, null]
tenantA tries to delete tenantB's receipt through a reference:
-> rejected: ObjectNotFoundException: No row with the given identifier exists for entity [com.ankurm.tenant.Receipt with id '2']
tenantB's receipt still exists: true
tenantA deletes its own receipt:
delete from receipt where id=? and tenant_id=coalesce(?,tenant_id) [1, tenantA]
root deletes tenantB's receipt:
delete from receipt where id=? and tenant_id=coalesce(?,tenant_id) [2, null]