Leakproof enum equality and like/ilike, so RLS queries can use their indexes #54373
Unanswered
martijn00
asked this question in
Feature Requests
Replies: 1 comment
|
One more workaround for the enum case that keeps a closed list of values: a lookup table instead of an enum. create table public.ticket_status (code text primary key);
insert into public.ticket_status values ('open'), ('closed');
alter table public.tickets
add column status_code text references public.ticket_status (code);
For anyone checking their own project, this shows which comparison functions are leakproof on the instance: select proname, proleakproof
from pg_proc
where proname in ('texteq', 'uuid_eq', 'int4eq', 'enum_eq', 'enum_ne', 'textlike', 'texticlike')
order by 1;On the |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Under RLS, Postgres only uses a filter as an index condition (or evaluates it before the policies) when its operator function is
LEAKPROOF. Two common filters aren't, and on Supabase the project can't change that, becauseALTER FUNCTION ... LEAKPROOFneeds a superuser:enum_eq(andenum_ne,enum_lt, ...) has noproleakproofinsrc/include/catalog/pg_proc.dat, whiletexteqdoes. On an RLS table,where status = 'open'with an index on(organization_id, status)reads every row of the organization and filters after the policies.likeandilike.textlikeandtexticlikearen't leakproof, so apg_trgmindex never serves?name=ilike.*foo*from PostgREST, or a search in asecurity invokerfunction, for a user subject to RLS.The same mechanism is behind supabase/storage#1678 (
lower(name)instorage.search).Workarounds today
textwith acheckconstraint instead of an enum, which loses the type.security definerhelper that checks the permission itself, matches with the index, and returns ids for the invoker query to filter on (id in (select helper(...))). Each searchable table needs one, and each is a place where the permission check can go wrong.Ask
On the platform image, mark the comparison functions of these operators
LEAKPROOF, at leastenum_eqandenum_ne, which only compare two enum sort orders and raise no data-dependent errors. Whethertextlikeandtexticlikequalify needs a closer look (they can raise on a malformed pattern, but the pattern comes from the query, not the row). If changing built-ins is out of the question, a supported way for the project owner to request it (an extension, or asupabase_adminfunction that marks an allowlisted set) would do.All reactions