postgresqltriggersprivileges

Is there a way to disable updates/deletes but still allow triggers to perform them?


Basically, I want to be able to use the REVOKE command to disable UPDATE and DELETE, but I still want the triggers on a table to update my rows.

My triggers perform on newly inserted rows, and update a specific field. So I still want this behaviour, but wouldn't they be disabled with REVOKE or with a RULE. (I saw an SO post)

Is there a way to keep using the UPDATE/INSERT commands in TRIGGERS but disabling the rest?


Solution

  • Yes, this is possible.

    Triggers are run with the privileges of the trigger function at creation time, defaulting to SECURITY INVOKER which means, the trigger function is effectively executed with the privileges of the current_user, in your case the one inserting rows.

    Note that privileges on the function itself are only checked at creation time of the trigger. See:

    If the current user does not have the required privileges for the tables your trigger function operates on, your original operation in the underlying table will error out.

    However, you can use SECURITY DEFINER for the trigger function to have this function run with the privileges of the OWNER of the function.

    If you have a superuser own the trigger function, it can do everything - which is a potential security hazard. Consider instructions in the manual for Writing SECURITY DEFINER Functions Safely".

    It's wiser to make a non-superuser role with just the necessary privileges OWNER of the trigger function. You can create a "daemon" role without login to act as privilege bundle. Grant only the needed privileges (on schemas, tables, sequences ...) to this daemon role. For more sophisticated designs bundle privileges in "group roles" (again, no login) and grant these group roles to roles that need it (to the daemon role in this example), effectively making them "member of the group". I do that a lot.