-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
looks like db_pre_request is not running on sturage upload request #34
Comments
Oh that's an interesting discovery. How are you calling get_group()? Are you triggering it on insert of one of the storage related tables? I will attempt to replicate in the local dev environment. |
-- Step 2: Override the db_pre_request function
create or REPLACE FUNCTION public.db_pre_request () returns void language plpgsql stable security definer
set
search_path = public as $$
declare
groups jsonb;
default_group_id uuid;
begin
-- get current groups from auth.users
select raw_app_meta_data->'groups' from auth.users into groups where id = auth.uid();
-- store it in the request object
perform set_config('request.groups'::text, groups::text, false /* applies to transaction if true, session if false */);
-- custom logic
SELECT id FROM public.groups into default_group_id WHERE metadata ->> 'groupName' = 'default_group';
RAISE LOG 'group_id: %', default_group_id;
perform set_config('request.default_group_id'::text, default_group_id::text, false /* applies to transaction if true, session if false */);
end;
$$;
create or replace function get_default_group_id()
returns uuid language sql stable
as $function$
select current_setting('request.default_group_id', true)::uuid
$function$;
CREATE OR REPLACE FUNCTION root_admin_check()
RETURNS boolean
AS $$
DECLARE
user_roles jsonb;
BEGIN
RAISE LOG 'root_admin_check: current_user %', to_jsonb(current_user);
-- Extract the roles from the user's 'groups' using the default_group_id function
user_roles := (auth.jwt() -> 'app_metadata' -> 'groups' -> get_default_group_id()::text -> 'roles');
RAISE LOG 'root_admin_check: User roles %', to_jsonb(user_roles);
-- Check if the user_roles is NULL or if 'root_admin' is not in the list of roles
IF user_roles IS NULL OR NOT (user_roles ? 'root_admin') THEN
RAISE LOG 'root_admin_check: Not a root admin %', to_jsonb(auth.jwt());
RETURN FALSE;
END IF;
RETURN TRUE;
END;
$$
LANGUAGE plpgsql;
CREATE POLICY authorize_groups_all ON groups AS PERMISSIVE FOR ALL TO authenticated
using (
root_admin_check()
); currently I am doing something like this It works when i do db actions. I use ts to upload images supabase.storage
.from("u_profiles")
.upload(...) I assume it is trying to write something to "storage"."objects" and failed. because db_pre_request is set to run for |
I added a few RAISE LOG in |
I asked in the Supabase Discord and got the following reply:
For anyone trying to get around it by using a trigger, the trigger uses the same session so the authorization functions (e.g. So the following will not work:
|
@danielbank thank you. If i understand right, we can't use any kind of caching methond in storage RLS. We should use Should we create storage trigger feature request on supabase repo? |
My take on it is that you can only check authentication (who are you?) in Storage calls via |
Thank you since it is not problem of this plugin, closing the issue created feature request here : https://github.com/orgs/supabase/discussions/30286 |
Just getting up to speed on this, is the conclusion that even contents of the user's jwt are inaccessible for storage requests with rls? |
auth.jwt() should work in storage. |
If that's the case then I think this issue should be reopened, @matart15. There definitely seems to be a discrepancy with the following from the README:
The experience we are seeing with storage is that the db_pre_request function is not called and the RLS policy convenience functions are not successfully falling back to the JWT claims |
@danielbank I am not an employee of Supabase, but I believe this is the storage code that sets the claims... https://github.com/supabase/storage/blob/6b052d85b9f0c7eb2442a0377b49ca058a5809af/src/internal/database/connection.ts#L231 And unless something broke auth.jwt(), auth.uid() and auth.role() all work for storage requests. BUT PostgREST is not involved with storage. It goes directly to the database, so db_pre_request would never fire. If that function is used to do some sort of security it would not apply to storage operations. |
I was able to get around the issue by not using the helper functions and instead accessing the Note in my example below, I am assuming the top-level folder (
|
this function returns value if i write to database.
but return null if is upload image
The text was updated successfully, but these errors were encountered: