From: walther@technowledgy.de
To: Erik Wienhold <ewie@ewie.name>
To: Dominique Devienne <ddevienne@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: DEFINER / INVOKER conundrum
Date: Tue, 4 Apr 2023 07:55:25 +0200
Message-ID: <f82f70fd-665f-6384-5e8a-987ab9e640d3@technowledgy.de> (raw)
In-Reply-To: <478369375.553946.1680533719442@office.mailbox.org>
References: <CAFCRh-86dC08WJ-FW+Hn4i6p1bNuPnSPnaD8PnOZ36siR0VhDw@mail.gmail.com>
<478369375.553946.1680533719442@office.mailbox.org>
Erik Wienhold:
> A single DEFINER function works if you capture current_user with a parameter
> and default value. Let's call it claimed_role. Use pg_has_role[0] to check
> that session_user has the privilege for claimed_role (in case the function is
> called with an explicit value), otherwise raise an exception.
>
> Connect as postgres:
>
> CREATE FUNCTION f(claimed_role text default current_user)
> RETURNS TABLE (claimed_role text, curr_user text, sess_user text)
> SECURITY DEFINER
> LANGUAGE sql
> $$ SELECT claimed_role, current_user, session_user $$;
For me, checking whether session_user has the privilege for claimed_role
is not enough, so I add a DOMAIN to the mix:
CREATE DOMAIN current_user_only AS NAME CHECK (VALUE = CURRENT_USER);
CREATE FUNCTION f(calling_user current_user_only DEFAULT CURRENT_USER)
...
SECURITY DEFINER;
This works, because the domain check is evaluated in the calling context.
Best,
Wolfgang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: walther@technowledgy.de, ewie@ewie.name, ddevienne@gmail.com
Subject: Re: DEFINER / INVOKER conundrum
In-Reply-To: <f82f70fd-665f-6384-5e8a-987ab9e640d3@technowledgy.de>
References: <CAFCRh-86dC08WJ-FW+Hn4i6p1bNuPnSPnaD8PnOZ36siR0VhDw@mail.gmail.com> <478369375.553946.1680533719442@office.mailbox.org> <f82f70fd-665f-6384-5e8a-987ab9e640d3@technowledgy.de>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox