Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g6otN-00084k-5A for pgsql-general@arkaria.postgresql.org; Mon, 01 Oct 2018 03:29:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g6otH-0004ht-Km for pgsql-general@arkaria.postgresql.org; Mon, 01 Oct 2018 03:28:59 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g6otH-0004hm-2R for pgsql-general@lists.postgresql.org; Mon, 01 Oct 2018 03:28:59 +0000 Received: from 9v6.net-wizard.org ([2a01:4f8:172:2a5c::9] helo=gandalf.net-wizard.org) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g6ot8-0007bq-9C for pgsql-general@lists.postgresql.org; Mon, 01 Oct 2018 03:28:56 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.net-wizard.org (Postfix) with ESMTP id 10D9680229; Mon, 1 Oct 2018 05:28:47 +0200 (CEST) X-Virus-Scanned: Gentoo amavisd-new at aragorn Received: from aragorn.net-wizard.org ([127.0.0.1]) by localhost (aragorn.net-wizard.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ssf3NiL2PQg0; Mon, 1 Oct 2018 05:28:46 +0200 (CEST) Received: from [82.130.89.233] (mtec-lee-dock-3-233.ethz.ch [82.130.89.233]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: clavadetscher@swisspug.org) by gandalf.net-wizard.org (Postfix) with ESMTPSA id 54807801A4; Mon, 1 Oct 2018 05:28:40 +0200 (CEST) Content-Type: multipart/alternative; boundary="----=_NextPart_2355173.581052527354" MIME-Version: 1.0 Date: Mon, 01 Oct 2018 05:18:15 +0200 Message-ID: Subject: Re: Postgres trigger side-effect is occurring out of order with row-level security select policy From: "Charles Clavadetscher (SwissPUG)" To: "Carl Sverre" Cc: "Adrian Klaver" , "" In-Reply-To: References: <7e9e8e8b-c368-8007-4a1d-b67308d26dd6@aklaver.com> <1155c242-cac9-4bac-b73e-6f55a2327c27@getmailbird.com> <26b52a79-cef2-903c-3696-fc3b6d6cbcb4@aklaver.com> User-Agent: Mailbird/2.5.14.0 X-Mailbird-ID: a78c224d-cc9c-4904-bdb0-c05f7c702eda@getmailbird.com List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------=_NextPart_2355173.581052527354 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Hello On 01.10.2018 05:00:02, Carl Sverre wrote: Thank you for the detailed report Charles. I think you may be missing the = =E2=80=9Creturning id=E2=80=9D clause in the insert. Can you verify it work= s when you use =E2=80=9Creturning id=E2=80=9D? Thanks! [Charles] :=C2=A0You are right: testuser@charles.localhost=3D> INSERT INTO a VALUES ('fails2') RETURNING id; NOTICE: =C2=A0inside trigger handler ERROR: =C2=A0new row violates row-level security policy for table "a" This implies that the returning_id is evaluated before the trigger executes= , i.e. Adrian's assumption is probably correct. Regards Charles On Sun, Sep 30, 2018 at 7:57 PM Charles Clavadetscher (SwissPUG) wrote: Hello On 30.09.2018 23:31:32, Adrian Klaver wrote: On 9/30/18 1:13 PM, Carl Sverre wrote: > Thanks for the initial results. Can you check that you are not using > super permissions and are enabling row security when running the test? > Super ignores row security. Yeah, big oops on my part, I was running as superuser. Running as non-superuser resulted in the failure you see. I tried to get around this with no success. My suspicion is that the new row in b is not visible to the returning(SELECT) query in a until after the transaction completes. Someone with more knowledge on this then I will have to confirm/deny my suspicion. > > Also yes, I forgot to add the policy names, sorry about that. > On Sun, Sep 30, 2018 at 1:34 AM Charles Clavadetscher (SwissPUG) > > wrote: > > Hello -- Adrian Klaver adrian.klaver@aklaver.com [mailto:adrian.klaver@aklaver.com] [Charles] :=C2=A0I also made the first test as super. However I still don't= get any errors when executing the test query as non superuser. The user is not superuser: testuser@charles.localhost=3D> SELECT CURRENT_USER; =C2=A0current_user -------------- =C2=A0testuser (1 row) testuser@charles.localhost=3D> \du testuser =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0List of roles =C2=A0Role name | Attributes | Member of -----------+------------+----------- =C2=A0testuser =C2=A0| =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0| {} The table privileges=C2=A0show that RLS is enabled and that testuser has SE= LECT and INSERT privilege on both tables. This is not related to RLS but si= mple precondition for the test: testuser@charles.localhost=3D> \d a =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Table "public.a" =C2=A0Column | Type | Collation | Nullable | Default --------+------+-----------+----------+--------- =C2=A0id =C2=A0 =C2=A0 | text | =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 | =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0| Policies (forced row security enabled): =C2=A0 =C2=A0 POLICY "a_insert" FOR INSERT =C2=A0 =C2=A0 =C2=A0 WITH CHECK (true) =C2=A0 =C2=A0 POLICY "a_select" FOR SELECT =C2=A0 =C2=A0 =C2=A0 USING ((EXISTS ( SELECT b.id [http://b.id] =C2=A0 =C2=A0FROM b =C2=A0 WHERE (a.id [http://a.id] =3D b.id [http://b.id])))) Triggers: =C2=A0 =C2=A0 reprotrigger BEFORE INSERT ON a FOR EACH ROW EXECUTE PROCEDUR= E reprohandler() testuser@charles.localhost=3D> \dp a =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Ac= cess privileges =C2=A0Schema | Name | Type =C2=A0| =C2=A0 =C2=A0Access privileges =C2=A0 = =C2=A0| Column privileges | =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Policies --------+------+-------+-------------------------+-------------------+-----= ------------------------- =C2=A0public | a =C2=A0 =C2=A0| table | charles=3DarwdDxt/charles+| =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 | a_select (r): =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 + =C2=A0 =C2=A0 =C2=A0 =C2=A0 | =C2=A0 =C2=A0 =C2=A0| =C2=A0 =C2=A0 =C2=A0 | = testuser=3Dar/charles =C2=A0 =C2=A0 | =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 | =C2=A0 (u): (EXISTS ( SELECT b.id [http://b.i= d]+ =C2=A0 =C2=A0 =C2=A0 =C2=A0 | =C2=A0 =C2=A0 =C2=A0| =C2=A0 =C2=A0 =C2=A0 | = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 | =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= | =C2=A0 =C2=A0FROM b =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 + =C2=A0 =C2=A0 =C2=A0 =C2=A0 | =C2=A0 =C2=A0 =C2=A0| =C2=A0 =C2=A0 =C2=A0 | = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 | =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= | =C2=A0 WHERE (a.id [http://a.id] =3D b.id [http://b.id]))) =C2=A0 =C2=A0= + =C2=A0 =C2=A0 =C2=A0 =C2=A0 | =C2=A0 =C2=A0 =C2=A0| =C2=A0 =C2=A0 =C2=A0 | = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 | =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= | a_insert (a): =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 + =C2=A0 =C2=A0 =C2=A0 =C2=A0 | =C2=A0 =C2=A0 =C2=A0| =C2=A0 =C2=A0 =C2=A0 | = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 | =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= | =C2=A0 (c): true testuser@charles.localhost=3D> \d b =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Table "public.b" =C2=A0Column | Type | Collation | Nullable | Default --------+------+-----------+----------+--------- =C2=A0id =C2=A0 =C2=A0 | text | =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 | =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0| testuser@charles.localhost=3D> \dp b =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Access privileges =C2=A0Schema | Name | Type =C2=A0| =C2=A0 =C2=A0Access privileges =C2=A0 = =C2=A0| Column privileges | Policies --------+------+-------+-------------------------+-------------------+-----= ----- =C2=A0public | b =C2=A0 =C2=A0| table | charles=3DarwdDxt/charles+| =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 | =C2=A0 =C2=A0 =C2=A0 =C2=A0 | =C2=A0 =C2=A0 =C2=A0| =C2=A0 =C2=A0 =C2=A0 | = testuser=3Dar/charles =C2=A0 =C2=A0 | =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 | And now the test: testuser@charles.localhost=3D> SELECT * FROM a; =C2=A0id ---- (0 rows) testuser@charles.localhost=3D> SELECT * FROM b; =C2=A0id ---- (0 rows) testuser@charles.localhost=3D> INSERT INTO a VALUES ('fails'); NOTICE: =C2=A0inside trigger handler INSERT 0 1 testuser@charles.localhost=3D> SELECT * FROM a; =C2=A0 id ------- =C2=A0fails (1 row) testuser@charles.localhost=3D> SELECT * FROM b; =C2=A0 id ------- =C2=A0fails (1 row) Version of PG: testuser@charles.localhost=3D> SELECT version(); =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 version ------------------------------------------------------------ =C2=A0PostgreSQL 10.5, compiled by Visual C++ build 1800, 64-bit (1 row) Regards Charles -- Carl Sverre ------=_NextPart_2355173.581052527354 Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable
=0A =0A = =0A = =0A =0A = =0A Hello


On 01.10.2018 05:0= 0:02, Carl Sverre <sverre.carl@gmail.com> wrote:

Thank you for the= detailed report Charles. I think you may be missing the =E2=80=9Creturning= id=E2=80=9D clause in the insert. Can you verify it works when you use =E2= =80=9Creturning id=E2=80=9D? Thanks!
[Charles] : = You are right:

testuser@charles.l= ocalhost=3D> INSERT INTO a VALUES ('fails2') RETURNING id;
<= div>NOTICE:  inside trigger handler
ERROR:  new ro= w violates row-level security policy for table "a"

This implies that the returning_id is evaluated b= efore the trigger executes, i.e. Adrian's assumption is probably correct.

=
Regards
Charles

On Sun, Sep 30, 2018 at = 7:57 PM Charles Clavadetscher (SwissPUG) <clavadetscher@swisspug.org> wrote:
=0A = =0A = =0A =0A = =0A =0A = Hello

On 30.09.2018 23:31:32, Adrian Klaver = <adrian.k= laver@aklaver.com> wrote:

On 9/30/18 1:13 PM, Carl Sverre wrote:= =0A
> Thanks for the initial results. Can you check that you are not = using =0A
> super permissions and are enabling row security when runn= ing the test? =0A
> Super ignores row security.=0A
=0A
Yeah, bi= g oops on my part, I was running as superuser. Running as =0A
non-superu= ser resulted in the failure you see. I tried to get around =0A
this with= no success. My suspicion is that the new row in b is not =0A
visible to= the returning(SELECT) query in a until after the transaction =0A
comple= tes. Someone with more knowledge on this then I will have to =0A
confirm= /deny my suspicion.=0A
=0A
=0A
> =0A
> Also yes, I forgot= to add the policy names, sorry about that.=0A
> On Sun, Sep 30, 2018= at 1:34 AM Charles Clavadetscher (SwissPUG) =0A
> <= /u>> wrote:=0A
> =0A
> Hello=0A
=0A
-- =0A
Adri= an Klaver=0A
adrian.klaver@aklaver.com=0A
=0A
[Charles] :=  I also made the first test as super. However I still don't get any er= rors when executing the test query as non superuser.
<= span style=3D"color:#3b5998">
The user is not superuser:

testuser@charles.localhost=3D> SELECT CURRENT_USER;
<= span style=3D"color:#3b5998"> current_user
--------------
 testuser
(1 row= )

testuser@charles.localhost=3D> \du testuser
        &n= bsp;  List of roles
&nb= sp;Role name | Attributes | Member of
-----------+------------+-----------
 testuser  |          = ;  | {}

The table privileges show that RLS is enabled a= nd that testuser has SELECT and INSERT privilege on both tables. This is no= t related to RLS but simple precondition for the test:

testuser@charles.localhost=3D> \d a
              &n= bsp; Table "public.a"
 = Column | Type | Collation | Nullable | Default
--------+------+-----------+----------+---------<= /div>
 id     | text | &nb= sp;         |          |
Policies (forced row security ena= bled):
    POLICY = "a_insert" FOR INSERT
 =     WITH CHECK (true)
    POLICY "a_select" FOR SELECT
      USING ((EXISTS ( SELECT b.id
   FROM b
  WHERE (a.id =3D <= a href=3D"http://b.id" target=3D"_blank">b.id))))
Triggers:
    reprotrigger BEFORE INSERT ON a FOR EACH ROW EXECUTE = PROCEDURE reprohandler()

testuser@charles.lo= calhost=3D> \dp a
                    &= nbsp;                    = Access privileges
 Sche= ma | Name | Type  |    Access privileges    | Colu= mn privileges |           Policies
--------+------+-------+-------------------= ------+-------------------+------------------------------
=  public | a    | table | charl= es=3DarwdDxt/charles+|               &nb= sp;   | a_select (r):              = +
      &nbs= p; |      |       | testuser=3Dar/charles &nb= sp;   |                  = |   (u): (EXISTS ( SELECT b= .id+
     = ;   |      |       |      = ;                   |   &= nbsp;               |    FROM = b                   +
        |  =    |       |           &= nbsp;             |       &nbs= p;           |   WHERE (a.id =3D b.id)))     +
        |      |       |=                     &nbs= p;   |                   = | a_insert (a):               +
        |  =    |       |           &= nbsp;             |       &nbs= p;           |   (c): true
<= br>
testuser@charles.localhost=3D> \d= b
              &n= bsp; Table "public.b"
 Column | Type | Collation = | Nullable | Default
--------+------+-----------+-----= -----+---------
 id     | text |  =         |          |

testuser@charles.localhost=3D> \dp b
  &= nbsp;                    =        Access privileges
 Sch= ema | Name | Type  |    Access privileges    | Col= umn privileges | Policies
--------+------+-------+-----= --------------------+-------------------+----------
&nb= sp;public | b    | table | charles=3DarwdDxt/charles+|   &nb= sp;               |
=         |      |       | = testuser=3Dar/charles     |           &n= bsp;       |

An= d now the test:

testuser@charles.localhost=3D> SELECT * FROM a;
<= div> id
----
(0 rows)=

testuser@charles.localhost=3D> SELECT * FROM b;=
 id
<= span style=3D"color:#3b5998">----
(0 rows)

testuser@charles.localhost=3D> IN= SERT INTO a VALUES ('fails');
NOTICE:  inside trigger handler
INSERT 0 1
testus= er@charles.localhost=3D> SELECT * FROM a;
  id
-------
 fails<= /div>
(1 row)

tes= tuser@charles.localhost=3D> SELECT * FROM b;
  id
-------
 fails=
(1 row)

Version of PG:
testuser@charles.localhost=3D> SELECT version();<= /div>
          &nbs= p;               version
------------------------------------------------------= ------
 PostgreSQL 10.5, compiled = by Visual C++ build 1800, 64-bit
(1 row= )

Regards
<= span style=3D"color:#3b5998">Charles

=0A = =0A
--=
Carl Sverre
=0A=0A = =0A =0A =
------=_NextPart_2355173.581052527354--