@@ -4288,4 +4288,114 @@ alter_policy_table_locked CREATE TABLE public.alter_policy_table_locked (
42884288 ) WITH (schema_locked = true);
42894289 CREATE POLICY p_sel ON public.alter_policy_table_locked AS PERMISSIVE FOR ALL TO public WITH CHECK (false)
42904290
4291+ subtest bug_fix_policy_respects_inheritance_#144780
4292+
4293+ statement ok
4294+ CREATE ROLE parent_role
4295+
4296+ statement ok
4297+ CREATE ROLE child_role
4298+
4299+ statement ok
4300+ GRANT parent_role TO child_role
4301+
4302+ statement ok
4303+ CREATE TABLE employees (id SERIAL PRIMARY KEY, name TEXT, department TEXT)
4304+
4305+ statement ok
4306+ INSERT INTO employees VALUES (1, 'Alice', 'Engineering')
4307+
4308+ statement ok
4309+ ALTER TABLE employees ENABLE ROW LEVEL SECURITY
4310+
4311+ statement ok
4312+ CREATE POLICY parent_policy_select ON employees FOR SELECT TO parent_role USING (true)
4313+
4314+ statement ok
4315+ CREATE POLICY parent_policy_insert ON employees FOR INSERT TO parent_role WITH CHECK (department IN ('Engineering', 'Sales'))
4316+
4317+ statement ok
4318+ CREATE POLICY parent_policy_update ON employees FOR UPDATE TO parent_role USING (false)
4319+
4320+ statement ok
4321+ GRANT SELECT, INSERT, UPDATE ON employees TO parent_role
4322+
4323+ # Test with role inheritance - child_role should inherit parent_role's permissions.
4324+ statement ok
4325+ SET ROLE child_role
4326+
4327+ query ITT
4328+ SELECT * FROM employees
4329+ ----
4330+ 1 Alice Engineering
4331+
4332+ # Test INSERT with role inheritance - should succeed for allowed department.
4333+ statement ok
4334+ INSERT INTO employees (id, name, department) VALUES (2, 'Bob', 'Engineering')
4335+
4336+ # Test INSERT with role inheritance - should succeed for another allowed department.
4337+ statement ok
4338+ INSERT INTO employees (id, name, department) VALUES (3, 'Carol', 'Sales')
4339+
4340+ # Test INSERT with role inheritance - should fail for disallowed department.
4341+ statement error pq: new row violates row-level security policy for table "employees"
4342+ INSERT INTO employees (id, name, department) VALUES (4, 'Dave', 'Finance')
4343+
4344+ statement ok
4345+ UPDATE employees SET name = 'Robert' WHERE name = 'Bob'
4346+
4347+ query ITT
4348+ SELECT * FROM employees ORDER BY id
4349+ ----
4350+ 1 Alice Engineering
4351+ 2 Bob Engineering
4352+ 3 Carol Sales
4353+
4354+ statement ok
4355+ RESET ROLE
4356+
4357+ # Now revoke the parent role and ensure permissions don't apply anymore.
4358+ statement ok
4359+ REVOKE parent_role FROM child_role
4360+
4361+ statement ok
4362+ GRANT ALL ON employees TO child_role
4363+
4364+ statement ok
4365+ SET ROLE child_role
4366+
4367+ # Should see no rows because the policy doesn't apply anymore.
4368+ query ITT
4369+ SELECT * FROM employees
4370+ ----
4371+
4372+ # All write operations should fail now.
4373+ statement error pq: new row violates row-level security policy for table "employees"
4374+ INSERT INTO employees (id, name, department) VALUES (3, 'Eve', 'Engineering')
4375+
4376+ # Test UPDATE with role inheritance - should succeed,
4377+ # but the update will not go as the default with ENABLED ROW LEVEL SECURITY.
4378+ statement ok
4379+ UPDATE employees SET name = 'Alice 2.0' WHERE name = 'Alice'
4380+
4381+ statement ok
4382+ RESET ROLE
4383+
4384+ # Can see that no change was made by the update.
4385+ query ITT
4386+ SELECT * FROM employees ORDER BY id
4387+ ----
4388+ 1 Alice Engineering
4389+ 2 Bob Engineering
4390+ 3 Carol Sales
4391+
4392+ statement ok
4393+ DROP TABLE employees CASCADE
4394+
4395+ statement ok
4396+ DROP ROLE child_role
4397+
4398+ statement ok
4399+ DROP ROLE parent_role
4400+
42914401subtest end
0 commit comments