pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Erik Wienhold <ewie@ewie.name>
To: walther@technowledgy.de, Dominique Devienne <ddevienne@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: DEFINER / INVOKER conundrum
Date: Tue, 4 Apr 2023 10:46:49 +0200 (CEST)
Message-ID: <1333359937.578652.1680598009141@office.mailbox.org> (raw)
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>

> On 04/04/2023 07:55 CEST walther@technowledgy.de wrote:
>
> 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.

Nice.  It's equivalent to my version without the domain if the client can
execute SET ROLE before calling f, thereby injecting any role for which
pg_has_role(session_user, calling_user, 'MEMBER') returns true.

Dominique did not say whether he controls the clients or not.

--
Erik





view thread (6+ messages)  latest in thread

Message-ID: <1333359937.578652.1680598009141@office.mailbox.org>
Permalink:  ../1333359937.578652.1680598009141@office.mailbox.org/
Also on:    postgresql.org/message-id/1333359937.578652.1680598009141@office.mailbox.org

 · 

reply

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: ewie@ewie.name, ddevienne@gmail.com
  Subject: Re: DEFINER / INVOKER conundrum
  In-Reply-To: <1333359937.578652.1680598009141@office.mailbox.org>
  References: <CAFCRh-86dC08WJ-FW+Hn4i6p1bNuPnSPnaD8PnOZ36siR0VhDw@mail.gmail.com> <478369375.553946.1680533719442@office.mailbox.org> <f82f70fd-665f-6384-5e8a-987ab9e640d3@technowledgy.de> <1333359937.578652.1680598009141@office.mailbox.org>

* 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