From: Erik Wienhold <ewie@ewie.name>
To: Dominique Devienne <ddevienne@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: DEFINER / INVOKER conundrum
Date: Mon, 3 Apr 2023 16:55:19 +0200 (CEST)
Message-ID: <478369375.553946.1680533719442@office.mailbox.org> (raw)
In-Reply-To: <CAFCRh-86dC08WJ-FW+Hn4i6p1bNuPnSPnaD8PnOZ36siR0VhDw@mail.gmail.com>
References: <CAFCRh-86dC08WJ-FW+Hn4i6p1bNuPnSPnaD8PnOZ36siR0VhDw@mail.gmail.com>
> On 03/04/2023 13:18 CEST Dominique Devienne <ddevienne@gmail.com> wrote:
>
> My goal is to have clients connect to PostgreSQL,
> and call a function that return a JWT token.
>
> The JWT is supposed to capture the user (login role),
> and the current_role (which has meaning in our app),
> and sign it using a secret corresponding to a mid-tier
> service the client will connect to later.
>
> I've foundĀ https://github.com/michelp/pgjwt which seems
> perfect for my use case, but I'm struggling with something.
>
> On the one hand, I want a INVOKER security function,
> to be able to capture the login and current ROLEs.
>
> On the other hand, I want a DEFINER security function,
> to be able to access the secret to sign the JWT with.
>
> That secret will be in a table that regular users of our DB
> do NOT have access to, of course. But that the function
> doing the JWT signing does need access to, of course (again).
>
> I thought I'd have two layers of functions, one INVOKER
> that captures the ROLEs, which then calls the DEFINER one,
> passing the ROLEs captured, but since the INVOKER function
> must also be able to call the DEFINER function, what prevents
> the client from calling it directly, with different (spoofed) ROLEs?
>
> Is there a way out of that conundrum?
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 $$;
Connect as alice:
SELECT * FROM f();
claimed_role | curr_user | sess_user
--------------+-----------+-----------
alice | postgres | alice
(1 row)
[0] https://www.postgresql.org/docs/current/functions-info.html#FUNCTIONS-INFO-ACCESS-TABLE
--
Erik
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: <478369375.553946.1680533719442@office.mailbox.org>
References: <CAFCRh-86dC08WJ-FW+Hn4i6p1bNuPnSPnaD8PnOZ36siR0VhDw@mail.gmail.com> <478369375.553946.1680533719442@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