From: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
---|---|
To: | pgsql-committers(at)lists(dot)postgresql(dot)org |
Subject: | pgsql: Ensure that AFTER triggers run as the instigating user. |
Date: | 2025-01-23 17:26:12 |
Message-ID: | E1tb0y4-0037sb-1e@gemulon.postgresql.org |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-committers |
Ensure that AFTER triggers run as the instigating user.
With deferred triggers, it is possible that the current role changes
between the time when the trigger is queued and the time it is
executed (for example, the triggering data modification could have
been executed in a SECURITY DEFINER function).
Up to now, deferred trigger functions would run with the current role
set to whatever was active at commit time. That does not matter for
foreign-key constraints, whose correctness doesn't depend on the
current role. But for user-written triggers, the current role
certainly can matter.
Hence, fix things so that AFTER triggers are fired under the role
that was active when they were queued, matching the behavior of
BEFORE triggers which would have actually fired at that time.
(If the trigger function is marked SECURITY DEFINER, that of course
overrides this, as it always has.)
This does not create any new security exposure: if you do DML on a
table owned by a hostile user, that user has always had various ways
to exploit your permissions, such as the aforementioned BEFORE
triggers, default expressions, etc. It might remove some security
exposure, because the old behavior could potentially expose some
other role besides the one directly modifying the table.
There was discussion of making a larger change, such as running as
the trigger's owner. However, that would break the common idiom of
capturing the value of CURRENT_USER in a trigger for auditing/logging
purposes. This change will make no difference in the typical scenario
where the current role doesn't change before commit.
Arguably this is a bug fix, but it seems too big a semantic change
to consider for back-patching.
Author: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
Reviewed-by: Joseph Koshakow <koshy44(at)gmail(dot)com>
Reviewed-by: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Discussion: https://postgr.es/m/77ee784cf248e842f74588418f55c2931e47bd78.camel@cybertec.at
Branch
------
master
Details
-------
https://git.postgresql.org/pg/commitdiff/01463e1cccd33fb11b33a4dd6dbebcad3c534102
Modified Files
--------------
doc/src/sgml/trigger.sgml | 4 ++
src/backend/commands/trigger.c | 20 ++++++++++
src/test/regress/expected/triggers.out | 67 ++++++++++++++++++++++++++++++++++
src/test/regress/sql/triggers.sql | 63 ++++++++++++++++++++++++++++++++
4 files changed, 154 insertions(+)
From | Date | Subject | |
---|---|---|---|
Next Message | Tom Lane | 2025-01-23 19:24:24 | pgsql: Don't ask for bug reports about pthread_is_threaded_np() != 0. |
Previous Message | Jeff Davis | 2025-01-23 17:10:58 | pgsql: Add support for Unicode case folding. |