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 1g6XC3-0001eF-S9 for pgsql-general@arkaria.postgresql.org; Sun, 30 Sep 2018 08:35:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g6XC1-0006sc-VD for pgsql-general@arkaria.postgresql.org; Sun, 30 Sep 2018 08:35:09 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g6XC1-0006rs-LT for pgsql-general@lists.postgresql.org; Sun, 30 Sep 2018 08:35:09 +0000 Received: from 9v6.net-wizard.org ([2a01:4f8:172:2a5c::9] helo=gandalf.net-wizard.org) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g6XBr-0007MT-GD for pgsql-general@lists.postgresql.org; Sun, 30 Sep 2018 08:35:06 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.net-wizard.org (Postfix) with ESMTP id E13F880276; Sun, 30 Sep 2018 10:34:55 +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 KezRbS54DN21; Sun, 30 Sep 2018 10:34:52 +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 8B348801B7; Sun, 30 Sep 2018 10:34:46 +0200 (CEST) Content-Type: multipart/alternative; boundary="----=_NextPart_21908251.415478294989" MIME-Version: 1.0 Date: Sun, 30 Sep 2018 10:34:39 +0200 Message-ID: <1155c242-cac9-4bac-b73e-6f55a2327c27@getmailbird.com> Subject: Re: Postgres trigger side-effect is occurring out of order with row-level security select policy From: "Charles Clavadetscher (SwissPUG)" To: "Adrian Klaver" , "Carl Sverre" , "" In-Reply-To: <7e9e8e8b-c368-8007-4a1d-b67308d26dd6@aklaver.com> References: <7e9e8e8b-c368-8007-4a1d-b67308d26dd6@aklaver.com> User-Agent: Mailbird/2.5.14.0 X-Mailbird-ID: 1155c242-cac9-4bac-b73e-6f55a2327c27@getmailbird.com List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------=_NextPart_21908251.415478294989 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Hello On 29.09.2018 20:24:45, Adrian Klaver wrote: On 9/28/18 11:35 PM, Carl Sverre wrote: > *Context* > I am using row-level security along with triggers to implement a pure > SQL RBAC implementation. While doing so I encountered a weird behavior > between INSERT triggers and SELECT row-level security policies. > > *Question* > I have posted a very detailed question on StackOverflow here: > https://stackoverflow.com/questions/52565720/postgres-trigger-side-effect= -is-occurring-out-of-order-with-row-level-security-s > > For anyone who is just looking for a summary/repro, I am seeing the > following behavior: > > CREATE TABLE a (id TEXT); > ALTER TABLE a ENABLE ROW LEVEL SECURITY; > ALTER TABLE a FORCE ROW LEVEL SECURITY; > > CREATE TABLE b (id TEXT); > > CREATE POLICY ON a FOR SELECT > USING (EXISTS( > =C2=A0 =C2=A0 select * from b where a.id =3D b.id > )); > > CREATE POLICY ON a FOR INSERT > WITH CHECK (true); > > CREATE FUNCTION reproHandler() RETURNS TRIGGER AS $$ > BEGIN > =C2=A0 =C2=A0 RAISE NOTICE USING MESSAGE =3D 'inside trigger handler'; > =C2=A0 =C2=A0 INSERT INTO b (id) VALUES (NEW.id); > =C2=A0 =C2=A0 RETURN NEW; > END; > $$ LANGUAGE plpgsql; > > CREATE TRIGGER reproTrigger BEFORE INSERT ON a > FOR EACH ROW EXECUTE PROCEDURE reproHandler(); > > INSERT INTO a VALUES ('fails') returning id; > NOTICE:=C2=A0 inside trigger handler > ERROR:=C2=A0 new row violates row-level security policy for table "a" > > Rather than the error, I expect that something along these lines should > occur instead: > > 1. A new row ('fails') is staged for INSERT > 2. The BEFORE trigger fires with NEW set to the new row > 3. The row ('fails') is inserted into b and returned from the trigger > procedure unchanged > 4. The INSERT's WITH CHECK policy true is evaluated to true > 5. The SELECT's USING policy select * from b where a.id =3D > b.id is evaluated.=C2=A0 *This should return true due to step 3* > 6. Having passed all policies, the row ('fails') is inserted in table > 7. The id (fails) of the inserted row is returned > > If anyone can point me in the right direction I would be extremely thankf= ul. When I tried to reproduce the above I got: test=3D# CREATE POLICY ON a FOR SELECT test-# USING (EXISTS( test(# select * from b where a.id =3D b.id test(# )); ERROR: syntax error at or near "ON" LINE 1: CREATE POLICY ON a FOR SELECT ^ test=3D# test=3D# CREATE POLICY ON a FOR INSERT test-# WITH CHECK (true); ERROR: syntax error at or near "ON" LINE 1: CREATE POLICY ON a FOR INSERT Changing your code to: CREATE TABLE a (id TEXT); ALTER TABLE a ENABLE ROW LEVEL SECURITY; ALTER TABLE a FORCE ROW LEVEL SECURITY; CREATE TABLE b (id TEXT); CREATE POLICY a_select ON a FOR SELECT USING (EXISTS( select * from b where a.id =3D b.id )); CREATE POLICY a_insert ON a FOR INSERT WITH CHECK (true); CREATE FUNCTION reproHandler() RETURNS TRIGGER AS $$ BEGIN RAISE NOTICE USING MESSAGE =3D 'inside trigger handler'; INSERT INTO b (id) VALUES (NEW.id); RETURN NEW; END; $$ LANGUAGE plpgsql; CREATE TRIGGER reproTrigger BEFORE INSERT ON a FOR EACH ROW EXECUTE PROCEDURE reproHandler(); Resulted in: test=3D# INSERT INTO a VALUES ('fails') returning id; NOTICE: inside trigger handler id ------- fails (1 row) INSERT 0 1 test=3D# select * from a; id ------- fails (1 row) > > Carl Sverre > > http://www.carlsverre.com -- Adrian Klaver adrian.klaver@aklaver.com [Charles] :=C2=A0I did the same test with PG version 10 on Windows and PG 9= .6.2 on Linux (RedHat) with exactly the same result. db=3D# INSERT INTO a VALUES ('fails') returning id; NOTICE: =C2=A0inside trigger handler =C2=A0 id ------- =C2=A0fails (1 row) INSERT 0 1 db=3D# select * from a; =C2=A0 id ------- =C2=A0fails (1 row) db=3D# select * from b; =C2=A0 id ------- =C2=A0fails (1 row) Regards Charles ------=_NextPart_21908251.415478294989 Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable
=0A =0A = =0A = =0A =0A = =0A Hello


On 29.09.2018 20:24= :45, Adrian Klaver <adrian.klaver@aklaver.com> wrote:

On 9/28/18 1= 1:35 PM, Carl Sverre wrote:=0A
> *Context*=0A
> I am using row-= level security along with triggers to implement a pure =0A
> SQL RBAC= implementation. While doing so I encountered a weird behavior =0A
> = between INSERT triggers and SELECT row-level security policies.=0A
> = =0A
> *Question*=0A
> I have posted a very detailed question on= StackOverflow here:=0A
> https://stackoverflow.com/questions/5256572= 0/postgres-trigger-side-effect-is-occurring-out-of-order-with-row-level-sec= urity-s=0A
> =0A
> For anyone who is just looking for a summary= /repro, I am seeing the =0A
> following behavior:=0A
> =0A
&= gt; CREATE TABLE a (id TEXT);=0A
> ALTER TABLE a ENABLE ROW LEVEL SEC= URITY;=0A
> ALTER TABLE a FORCE ROW LEVEL SECURITY;=0A
> =0A> CREATE TABLE b (id TEXT);=0A
> =0A
> CREATE POLICY ON a F= OR SELECT=0A
> USING (EXISTS(=0A
>     select * from= b where a.id =3D b.id =0A
> ));= =0A
> =0A
> CREATE POLICY ON a FOR INSERT=0A
> WITH CHECK= (true);=0A
> =0A
> CREATE FUNCTION reproHandler() RETURNS TRIG= GER AS $$=0A
> BEGIN=0A
>     RAISE NOTICE USING MES= SAGE =3D 'inside trigger handler';=0A
>     INSERT INTO b = (id) VALUES (NEW.id);=0A
>     RETURN NEW;=0A
> END;= =0A
> $$ LANGUAGE plpgsql;=0A
> =0A
> CREATE TRIGGER repr= oTrigger BEFORE INSERT ON a=0A
> FOR EACH ROW EXECUTE PROCEDURE repro= Handler();=0A
> =0A
> INSERT INTO a VALUES ('fails') returning = id;=0A
> NOTICE:  inside trigger handler=0A
> ERROR: = new row violates row-level security policy for table "a"=0A
> =0A> Rather than the error, I expect that something along these lines shou= ld =0A
> occur instead:=0A
> =0A
> 1. A new row ('fails')= is staged for INSERT=0A
> 2. The BEFORE trigger fires with NEW set t= o the new row=0A
> 3. The row ('fails') is inserted into b and return= ed from the trigger =0A
> procedure unchanged=0A
> 4. The INSER= T's WITH CHECK policy true is evaluated to true=0A
> 5. The SELECT's = USING policy select * from b where a.id =3D =0A
> b= .id is evaluated.  *This should return true due to s= tep 3*=0A
> 6. Having passed all policies, the row ('fails') is inser= ted in table=0A
> 7. The id (fails) of the inserted row is returned= =0A
> =0A
> If anyone can point me in the right direction I wou= ld be extremely thankful.=0A
=0A
When I tried to reproduce the above = I got:=0A
=0A
test=3D# CREATE POLICY ON a FOR SELECT=0A
test-# USI= NG (EXISTS(=0A
test(# select * from b where a.id =3D b.id=0A
test= (# ));=0A
ERROR: syntax error at or near "ON"=0A
LINE 1: CREATE POLI= CY ON a FOR SELECT=0A
^=0A
test=3D#=0A
test= =3D# CREATE POLICY ON a FOR INSERT=0A
test-# WITH CHECK (true);=0A
ER= ROR: syntax error at or near "ON"=0A
LINE 1: CREATE POLICY ON a FOR INS= ERT=0A
=0A
Changing your code to:=0A
=0A
CREATE TABLE a (id TEX= T);=0A
ALTER TABLE a ENABLE ROW LEVEL SECURITY;=0A
ALTER TABLE a FORC= E ROW LEVEL SECURITY;=0A
=0A
CREATE TABLE b (id TEXT);=0A
=0A
C= REATE POLICY a_select ON a FOR SELECT=0A
USING (EXISTS(=0A
selec= t * from b where a.id =3D b.id=0A
));=0A
=0A
CREATE POLICY a_inser= t ON a FOR INSERT=0A
WITH CHECK (true);=0A
=0A
CREATE FUNCTION rep= roHandler() RETURNS TRIGGER AS $$=0A
BEGIN=0A
RAISE NOTICE USING= MESSAGE =3D 'inside trigger handler';=0A
INSERT INTO b (id) VALUES= (NEW.id);=0A
RETURN NEW;=0A
END;=0A
$$ LANGUAGE plpgsql;=0A<= br>=0A
CREATE TRIGGER reproTrigger BEFORE INSERT ON a=0A
FOR EACH ROW= EXECUTE PROCEDURE reproHandler();=0A
=0A
Resulted in:=0A
=0A
t= est=3D# INSERT INTO a VALUES ('fails') returning id;=0A
NOTICE: inside = trigger handler=0A
id=0A
-------=0A
fails=0A
(1 row)=0A=0A
INSERT 0 1=0A
test=3D# select * from a;=0A
id=0A
------= -=0A
fails=0A
(1 row)=0A
=0A
=0A
> =0A
> Carl Sve= rre=0A
> =0A
> http://www.carlsverre.com=0A
=0A
=0A
--= =0A
Adrian Klaver=0A
adrian.klaver@aklaver.com=0A
=0A
[Charl= es] : I did the same test wi= th PG version 10 on Windows and PG 9.6.2 on Linux (RedHat) with exactly the= same result.

db=3D# INSERT INTO = a VALUES ('fails') returning id;
NOTICE:  inside trigger handler
  id
-= ------
 fails
(1 row)

I= NSERT 0 1
db=3D# select * f= rom a;
  id
-------
 fails
(1 row)

db=3D# select * from b;
=
  id
-------
 fails
(1 row)=

Regards
Charles


=0A <= /http:>
=0A = =0A
------=_NextPart_21908251.415478294989--