NARCISO
04/06/2022, 4:19 PMALTER FUNCTION public.myinternalfunc(jsonb) OWNER TO postgres;
GRANT EXECUTE ON FUNCTION public.myinternalfunc(jsonb) TO PUBLIC;
GRANT EXECUTE ON FUNCTION public.myinternalfunc(jsonb) TO anon;
GRANT EXECUTE ON FUNCTION public.myinternalfunc(jsonb) TO authenticated;
GRANT EXECUTE ON FUNCTION public.myinternalfunc(jsonb) TO postgres;
GRANT EXECUTE ON FUNCTION public.myinternalfunc(jsonb) TO service_role;
Needle
04/06/2022, 4:19 PM/title
command!
We have solved your problem?
Click the button below to archive it.Needle
04/06/2022, 4:20 PMgaryaustin
04/06/2022, 7:18 PMNeedle
04/06/2022, 7:18 PMNARCISO
04/07/2022, 10:02 AMafter inserting
a row in a certain table. My concern at the moment is that the user could add the role he wants to himself calling the function as "rpc". And that's something should not happen.
What do you think would be the best way to deny users to execute the function?
Is there a way to permit to execute this function just to the "database" itself?
The function and trigger looks like this:
-- Add worker role to user
create function public.add_worker_role()
returns trigger as $$
begin
-- Check if the user is the owner of the user profile
if (new.user_id <> auth.uid()) then
raise exception 'not_owner';
end if;
-- Check if the user profile just created exists
if not exists(select 1 from public.user_profile where user_id = new.user_id) then
raise exception 'user_profile_not_exists';
end if;
-- Add worker role to the user
insert into public.user_role (user_id, role_id) values (new.user_id, 'WORKER'::role);
return null;
end;
$$ language plpgsql security definer set search_path = public, pg_temp;
create trigger update_user_profile_trigger
after insert on public.user_profile
for each row
execute procedure public.add_worker_role();
Thank you so much in advance!garyaustin
04/07/2022, 2:14 PMNARCISO
04/07/2022, 3:00 PMpgsql
ALTER FUNCTION public.add_worker_role() OWNER TO postgres;
GRANT EXECUTE ON FUNCTION public.add_worker_role() TO PUBLIC;
GRANT EXECUTE ON FUNCTION public.add_worker_role() TO anon;
GRANT EXECUTE ON FUNCTION public.add_worker_role() TO authenticated;
GRANT EXECUTE ON FUNCTION public.add_worker_role() TO postgres;
GRANT EXECUTE ON FUNCTION public.add_worker_role() TO service_role;
Do you think it will work even if I'll just REVOKE the execution from PUBLIC, anon and authenticated? to deny the users the direct invocation of the function?garyaustin
04/07/2022, 3:08 PMNARCISO
04/07/2022, 3:12 PMafter insert
on the table public.user_profile
, if a user from the js-client insert
some data in the table, it would not work revoking the execution?
But if I would have a function (as security definer) that inserts the data, that would work?garyaustin
04/07/2022, 3:13 PMgaryaustin
04/07/2022, 3:14 PMNARCISO
04/07/2022, 3:15 PMNARCISO
04/07/2022, 3:16 PMCREATE SCHEMA security
and then creating the functions with security.add_worker_role would work?garyaustin
04/07/2022, 3:17 PMNARCISO
04/07/2022, 3:18 PMNARCISO
04/07/2022, 3:18 PMgaryaustin
04/07/2022, 3:20 PMNARCISO
04/07/2022, 3:23 PMgaryaustin
04/07/2022, 3:34 PMNARCISO
04/08/2022, 7:36 AMprivate
schema and put there all my private functions, but I was wondering about the grants.
Which grants should I give to it?
Just the postgres
one? and REVOKE all the others PostgREST
roles?
ALTER FUNCTION private.handle_new_user() OWNER TO postgres;
GRANT EXECUTE ON FUNCTION private.handle_new_user() TO PUBLIC;
GRANT EXECUTE ON FUNCTION private.handle_new_user() TO anon;
GRANT EXECUTE ON FUNCTION private.handle_new_user() TO authenticated;
GRANT EXECUTE ON FUNCTION private.handle_new_user() TO postgres;
GRANT EXECUTE ON FUNCTION private.handle_new_user() TO service_role;
NARCISO
04/08/2022, 8:23 AMprivate
schema too?garyaustin
04/08/2022, 12:42 PMNARCISO
04/08/2022, 1:16 PMNARCISO
04/08/2022, 1:18 PMprivate.function()
has none of the above GRANTS the RLS would not be able to use that in the policy?garyaustin
04/08/2022, 1:23 PMNARCISO
04/08/2022, 1:30 PMtrigger function
handle_new_user
to the private schema
that populate the user profile table based on after insert
on auth.users
. As you said, I could leave that in the public
schema because it cannot be called as rpc
, right?
pgsql
CREATE TRIGGER handle_new_user_trigger
AFTER INSERT
ON auth.users
FOR EACH ROW
EXECUTE FUNCTION private.handle_new_user();
garyaustin
04/08/2022, 3:04 PMNARCISO
04/08/2022, 4:16 PM