@@ -97,20 +97,26 @@ static void supautils_fmgr_hook(FmgrHookEventType event, FmgrInfo *flinfo, Datum
97
97
// we only need to change behavior before the function gets executed
98
98
case FHET_START : {
99
99
if (is_event_trigger_function (flinfo -> fn_oid )){ // recheck the function is an event trigger in case another extension need_fmgr_hook passed our supautils_needs_fmgr_hook
100
- const char * current_role_name = GetUserNameFromId (GetUserId (), false);
101
- const bool role_is_super = superuser ();
100
+ func_attrs fattrs = get_function_attrs ((func_search ){ .as = FO_SEARCH_FINFO , .val .finfo = flinfo });
101
+ const Oid current_role_oid =
102
+ fattrs .is_security_definer ?
103
+ // when the function is security definer, we need to get the session user id otherwise it will fire for superusers or reserved roles.
104
+ // See https://github.com/supabase/supautils/issues/140.
105
+ GetOuterUserId ():
106
+ GetUserId ();
107
+ const char * current_role_name = GetUserNameFromId (current_role_oid , false);
108
+ const bool role_is_super = superuser_arg (current_role_oid );
102
109
const bool role_is_reserved = is_reserved_role (current_role_name , false);
103
110
if (role_is_super || role_is_reserved ) {
104
- Oid func_owner = get_function_owner ((func_owner_search ){ .as = FO_SEARCH_FINFO , .val .finfo = flinfo });
105
- bool function_is_owned_by_super = superuser_arg (func_owner );
111
+ bool function_is_owned_by_super = superuser_arg (fattrs .owner );
106
112
if (!function_is_owned_by_super ){
107
113
if (log_skipped_evtrigs ){
108
114
char * func_name = get_func_name (flinfo -> fn_oid );
109
115
ereport (
110
116
NOTICE ,
111
117
errmsg ("Skipping event trigger function \"%s\" for user \"%s\"" , func_name , current_role_name ),
112
118
errdetail ("\"%s\" %s and the function \"%s\" is not superuser-owned, it's owned by \"%s\"" ,
113
- current_role_name , role_is_super ?"is a superuser" :"is a reserved role" , func_name , GetUserNameFromId (func_owner , false))
119
+ current_role_name , role_is_super ?"is a superuser" :"is a reserved role" , func_name , GetUserNameFromId (fattrs . owner , false))
114
120
);
115
121
}
116
122
// we can't skip execution directly inside the fmgr_hook (although we can abort it with ereport)
@@ -814,8 +820,8 @@ static void supautils_hook(PROCESS_UTILITY_PARAMS) {
814
820
const char * current_role_name = GetUserNameFromId (current_user_id , false);
815
821
816
822
bool current_user_is_super = superuser_arg (current_user_id );
817
- Oid function_owner = get_function_owner (( func_owner_search ){FO_SEARCH_NAME , {stmt -> funcname }});
818
- bool function_is_owned_by_super = superuser_arg (function_owner );
823
+ func_attrs fattrs = get_function_attrs (( func_search ){FO_SEARCH_NAME , {stmt -> funcname }});
824
+ bool function_is_owned_by_super = superuser_arg (fattrs . owner );
819
825
820
826
if (!current_user_is_super && function_is_owned_by_super ){
821
827
ereport (ERROR , (
0 commit comments