-- Hide the SQL statements so warnings and completion messages stand out. -- The expected output describes a safe behavior, any warning exposes a leak. \set ECHO none DO $$ BEGIN PERFORM hypopg_reset(); END $$; CREATE ROLE regress_hypo_rls_read NOSUPERUSER NOBYPASSRLS; GRANT USAGE, CREATE ON SCHEMA public TO regress_hypo_rls_read; CREATE TABLE hypo_rls_secrets (id integer, secret text); INSERT INTO hypo_rls_secrets SELECT g, CASE WHEN g <= 8000 THEN 'red' ELSE 'blue' END FROM generate_series(1, 10000) AS g; -- Collect every row so sampling cannot change the result. ALTER TABLE hypo_rls_secrets ALTER COLUMN secret SET STATISTICS 1000; ANALYZE hypo_rls_secrets; ALTER TABLE hypo_rls_secrets ENABLE ROW LEVEL SECURITY; -- No policy allows this reader to see any rows. SET ROLE regress_hypo_rls_read; DO $$ BEGIN BEGIN PERFORM secret FROM hypo_rls_secrets; RAISE WARNING 'reading the table should require permission'; EXCEPTION WHEN insufficient_privilege THEN NULL; END; IF EXISTS (SELECT 1 FROM pg_stats WHERE schemaname = 'public' AND tablename = 'hypo_rls_secrets') THEN RAISE WARNING 'table statistics should be hidden'; END IF; END $$; -- Different sizes reveal which hidden value occurs more often. -- Refusing creation or estimation is also a safe result. DO $$ DECLARE red_index oid; blue_index oid; red_size bigint; blue_size bigint; BEGIN SELECT indexrelid INTO red_index FROM hypopg_create_index( 'CREATE INDEX ON hypo_rls_secrets (id) WHERE secret = ''red'''); SELECT indexrelid INTO blue_index FROM hypopg_create_index( 'CREATE INDEX ON hypo_rls_secrets (id) WHERE secret = ''blue'''); red_size := hypopg_relation_size(red_index); blue_size := hypopg_relation_size(blue_index); IF red_size <> blue_size THEN RAISE WARNING 'index sizes reveal hidden value frequencies'; END IF; EXCEPTION WHEN insufficient_privilege THEN NULL; END $$; \echo Finished the test without read permission. RESET ROLE; DO $$ BEGIN PERFORM hypopg_reset(); END $$; GRANT SELECT ON hypo_rls_secrets TO regress_hypo_rls_read; SET ROLE regress_hypo_rls_read; -- The reader can define an operator that reports any value it receives. -- It is immutable but is not marked as safe for protected values. CREATE FUNCTION hypo_rls_reveal(text, text) RETURNS boolean LANGUAGE plpgsql IMMUTABLE AS $$ BEGIN RAISE EXCEPTION 'hidden value: %', $1; RETURN $1 = $2; END $$; CREATE OPERATOR === ( LEFTARG = text, RIGHTARG = text, PROCEDURE = hypo_rls_reveal, RESTRICT = eqsel ); -- Ordinary queries must return no rows and never call the operator. DO $$ BEGIN IF EXISTS (SELECT 1 FROM hypo_rls_secrets) THEN RAISE WARNING 'row security should hide every row'; END IF; PERFORM secret FROM hypo_rls_secrets WHERE secret === 'probe'; IF EXISTS (SELECT 1 FROM pg_stats WHERE schemaname = 'public' AND tablename = 'hypo_rls_secrets') THEN RAISE WARNING 'row security should hide table statistics'; END IF; END $$; -- Estimation should not pass hidden values to the reader's operator. DO $$ DECLARE idx oid; BEGIN SELECT indexrelid INTO idx FROM hypopg_create_index( 'CREATE INDEX ON hypo_rls_secrets (id) WHERE secret === ''probe'''); PERFORM hypopg_relation_size(idx); EXCEPTION WHEN insufficient_privilege THEN NULL; WHEN raise_exception THEN RAISE WARNING '%', SQLERRM; END $$; \echo Finished the test with row security. RESET ROLE; DO $$ BEGIN PERFORM hypopg_reset(); END $$; -- Create indexes with full access, then reuse them as a restricted reader. DO $$ DECLARE idx oid; BEGIN PERFORM hypopg_create_index('CREATE INDEX ON hypo_rls_secrets (id)'); PERFORM hypopg_create_index( 'CREATE INDEX ON hypo_rls_secrets (id) WHERE secret = ''red'''); FOR idx IN SELECT indexrelid FROM hypopg() LOOP IF hypopg_relation_size(idx) IS NULL THEN RAISE WARNING 'the privileged user should be able to estimate indexes'; END IF; END LOOP; END $$; SET ROLE regress_hypo_rls_read; -- Turning off row security should not grant access to protected statistics. SET row_security = off; DO $$ DECLARE idx oid; BEGIN FOR idx IN SELECT indexrelid FROM hypopg() LOOP BEGIN PERFORM hypopg_relation_size(idx); RAISE WARNING 'estimation should check row security after a role change'; EXCEPTION WHEN insufficient_privilege THEN NULL; END; END LOOP; END $$; RESET row_security; -- Planning must also check access before estimating an existing index. DO $$ BEGIN EXECUTE 'EXPLAIN SELECT * FROM hypo_rls_secrets WHERE id = 1'; RAISE WARNING 'planning should check row security after a role change'; EXCEPTION WHEN insufficient_privilege THEN NULL; END $$; RESET ROLE; -- A role with read permission and permission to bypass row security is allowed. ALTER ROLE regress_hypo_rls_read BYPASSRLS; SET ROLE regress_hypo_rls_read; DO $$ DECLARE idx oid; BEGIN PERFORM hypopg_create_index( 'CREATE INDEX ON hypo_rls_secrets (id) WHERE secret = ''blue'''); FOR idx IN SELECT indexrelid FROM hypopg() LOOP IF hypopg_relation_size(idx) IS NULL THEN RAISE WARNING 'a reader who bypasses row security should get an estimate'; END IF; END LOOP; EXECUTE 'EXPLAIN SELECT * FROM hypo_rls_secrets WHERE id = 1'; END $$; RESET ROLE; -- Bypassing row security alone does not grant read permission. CREATE ROLE regress_hypo_rls_noaccess NOSUPERUSER BYPASSRLS; GRANT USAGE ON SCHEMA public TO regress_hypo_rls_noaccess; SET ROLE regress_hypo_rls_noaccess; DO $$ DECLARE idx oid; BEGIN FOR idx IN SELECT indexrelid FROM hypopg() LOOP BEGIN PERFORM hypopg_relation_size(idx); RAISE WARNING 'estimation should check read permission after a role change'; EXCEPTION WHEN insufficient_privilege THEN NULL; END; END LOOP; END $$; RESET ROLE; \echo Finished the tests after changing roles. -- Statements without parameters use generic plans on all supported versions. ALTER ROLE regress_hypo_rls_read NOBYPASSRLS; DO $$ BEGIN PERFORM hypopg_reset(); END $$; DO $$ BEGIN PERFORM hypopg_create_index('CREATE INDEX ON hypo_rls_secrets (id)'); END $$; PREPARE hypo_rls_cached_query AS SELECT count(*) FROM hypo_rls_secrets WHERE id = 1; PREPARE hypo_rls_cached_size AS SELECT hypopg_relation_size(indexrelid) FROM hypopg(); -- Cache a plan that uses the hypothetical index while access is unrestricted. SET enable_seqscan = off; DO $$ DECLARE line text; uses_index boolean := false; bytes bigint; BEGIN FOR line IN EXECUTE 'EXPLAIN EXECUTE hypo_rls_cached_query' LOOP uses_index := uses_index OR line ~ 'Index.*<'; END LOOP; IF NOT uses_index THEN RAISE WARNING 'the cached plan should use a hypothetical index'; END IF; EXECUTE 'EXECUTE hypo_rls_cached_size' INTO bytes; IF bytes IS NULL OR bytes <= 0 THEN RAISE WARNING 'the privileged user should get a cached size estimate'; END IF; END $$; RESET enable_seqscan; -- A role change must rebuild the plan and check access again. SET ROLE regress_hypo_rls_read; DO $$ BEGIN IF (SELECT count(*) FROM hypopg() WHERE NOT invalidated) <> 1 THEN RAISE WARNING 'the role change should leave the hypothetical index valid'; END IF; BEGIN EXECUTE 'EXPLAIN EXECUTE hypo_rls_cached_query'; RAISE WARNING 'the cached query should check row security'; EXCEPTION WHEN insufficient_privilege THEN NULL; END; BEGIN EXECUTE 'EXECUTE hypo_rls_cached_size'; RAISE WARNING 'the cached size call should check row security'; EXCEPTION WHEN insufficient_privilege THEN NULL; END; END $$; RESET ROLE; -- The cached function call must also check ordinary read permission. SET ROLE regress_hypo_rls_noaccess; DO $$ BEGIN EXECUTE 'EXECUTE hypo_rls_cached_size'; RAISE WARNING 'the cached size call should check read permission'; EXCEPTION WHEN insufficient_privilege THEN NULL; END $$; RESET ROLE; -- Cache another plan before row security is enabled on the table. ALTER TABLE hypo_rls_secrets DISABLE ROW LEVEL SECURITY; DO $$ BEGIN PERFORM hypopg_reset(); END $$; DO $$ BEGIN PERFORM hypopg_create_index('CREATE INDEX ON hypo_rls_secrets (id)'); END $$; SET ROLE regress_hypo_rls_read; SET enable_seqscan = off; DO $$ DECLARE line text; uses_index boolean := false; bytes bigint; BEGIN FOR line IN EXECUTE 'EXPLAIN EXECUTE hypo_rls_cached_query' LOOP uses_index := uses_index OR line ~ 'Index.*<'; END LOOP; IF NOT uses_index THEN RAISE WARNING 'the cached plan should use a hypothetical index before row security'; END IF; EXECUTE 'EXECUTE hypo_rls_cached_size' INTO bytes; IF bytes IS NULL OR bytes <= 0 THEN RAISE WARNING 'the reader should get an estimate before row security'; END IF; END $$; RESET enable_seqscan; RESET ROLE; ALTER TABLE hypo_rls_secrets ENABLE ROW LEVEL SECURITY; SET ROLE regress_hypo_rls_read; -- The table change must invalidate both the index and the cached query plan. DO $$ DECLARE line text; rows_seen bigint; bytes bigint; BEGIN IF (SELECT count(*) FROM hypopg() WHERE invalidated) <> 1 THEN RAISE WARNING 'enabling row security should invalidate the hypothetical index'; END IF; FOR line IN EXECUTE 'EXPLAIN EXECUTE hypo_rls_cached_query' LOOP IF line ~ 'Index.*<' THEN RAISE WARNING 'the cached plan still uses an invalidated hypothetical index'; END IF; END LOOP; EXECUTE 'EXECUTE hypo_rls_cached_query' INTO rows_seen; IF rows_seen IS DISTINCT FROM 0::bigint THEN RAISE WARNING 'the cached query should return no protected rows'; END IF; EXECUTE 'EXECUTE hypo_rls_cached_size' INTO bytes; IF bytes IS NOT NULL THEN RAISE WARNING 'the cached size call should return null for the invalidated index'; END IF; END $$; RESET ROLE; DEALLOCATE hypo_rls_cached_query; DEALLOCATE hypo_rls_cached_size; \echo Finished the tests with cached plans. DO $$ BEGIN PERFORM hypopg_reset(); END $$; DROP OPERATOR === (text, text); DROP FUNCTION hypo_rls_reveal(text, text); DROP TABLE hypo_rls_secrets; REVOKE USAGE, CREATE ON SCHEMA public FROM regress_hypo_rls_read; REVOKE USAGE ON SCHEMA public FROM regress_hypo_rls_noaccess; DROP ROLE regress_hypo_rls_read; DROP ROLE regress_hypo_rls_noaccess;