Re: SQL-standard function bodies and creating SECURITY DEFINER routines securely

From: Erki Eessaar <erki(dot)eessaar(at)taltech(dot)ee>
To: Bruce Momjian <bruce(at)momjian(dot)us>
Cc: "pgsql-docs(at)lists(dot)postgresql(dot)org" <pgsql-docs(at)lists(dot)postgresql(dot)org>, Noah Misch <noah(at)leadboat(dot)com>, Peter Eisentraut <peter(dot)eisentraut(at)enterprisedb(dot)com>
Subject: Re: SQL-standard function bodies and creating SECURITY DEFINER routines securely
Date: 2022-10-07 13:50:14
Message-ID: AM9PR01MB8268561F390A27AE8C56F9B6FE5F9@AM9PR01MB8268.eurprd01.prod.exchangelabs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-docs

Hello

Another example where explicit search path is needed.

CREATE TABLE public.B(b INTEGER);
CREATE TABLE pg_temp.B(b INTEGER);

CREATE OR REPLACE FUNCTION f3 () RETURNS VOID
LANGUAGE sql SECURITY DEFINER
BEGIN ATOMIC
INSERT INTO B(b) VALUES (1);
END;

SELECT f3();

SELECT * FROM public.B;
/*Result has 0 rows.*/

SELECT * FROM pg_temp.B;
/*Result has 1 row. Function f3 was associated with pg_temp.B because
f3() did not have explicitly set search path.*/

I see now that there are multiple reasons why to still use search path. I agree now that this extra paragaraph is perhaps too confusing and is not needed.

Best regards
Erki Eessaar

________________________________
From: Bruce Momjian <bruce(at)momjian(dot)us>
Sent: Friday, October 7, 2022 4:35 PM
To: Erki Eessaar <erki(dot)eessaar(at)taltech(dot)ee>
Cc: pgsql-docs(at)lists(dot)postgresql(dot)org <pgsql-docs(at)lists(dot)postgresql(dot)org>; Noah Misch <noah(at)leadboat(dot)com>; Peter Eisentraut <peter(dot)eisentraut(at)enterprisedb(dot)com>
Subject: Re: SQL-standard function bodies and creating SECURITY DEFINER routines securely

On Fri, Oct 7, 2022 at 08:05:36AM +0000, Erki Eessaar wrote:
> Hello
>
> I confirmed, that setting search_path is indeed sometimes needed in case of
> SECURITY DEFINER routines that have SQL-standard bodies. See an example at the
> end of the letter.
>
> I suggest the following paragraph to the documentation:
>
> Starting from PostgreSQL 14 SQL-standard bodies can be used in SQL-language
> functions. This form tracks dependencies between the function and objects used
> in the function body. However, there is still a possibility that such function
> calls other code that reacts to search path. Thus, as a best practice, SECURITY
> DEFINER functions with SQL-standard bodies should also override search_path.

I think this gets back to what Noah said about this section not needing
to explain all the details but rather give general guidance. I am not
sure adding the reasons for _why_ you should use search path for
SQL-standard bodies is really adding anything. Noah, is that accurate?

--
Bruce Momjian <bruce(at)momjian(dot)us> https://momjian.us
EDB https://enterprisedb.com

Indecision is a decision. Inaction is an action. Mark Batterson

In response to

Responses

Browse pgsql-docs by date

  From Date Subject
Next Message Bruce Momjian 2022-10-07 14:02:22 Re: SQL-standard function bodies and creating SECURITY DEFINER routines securely
Previous Message Bruce Momjian 2022-10-07 13:35:49 Re: SQL-standard function bodies and creating SECURITY DEFINER routines securely