Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hdyCa-0006St-7g for pgsql-general@arkaria.postgresql.org; Thu, 20 Jun 2019 14:38: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 1hdyCZ-0004Fi-06 for pgsql-general@arkaria.postgresql.org; Thu, 20 Jun 2019 14:38:11 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hdyCY-0004DC-Gg for pgsql-general@lists.postgresql.org; Thu, 20 Jun 2019 14:38:10 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hdyCV-00053B-Lm for pgsql-general@lists.postgresql.org; Thu, 20 Jun 2019 14:38:09 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 6802A2211C; Thu, 20 Jun 2019 10:38:06 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Thu, 20 Jun 2019 10:38:06 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm3; bh=m tq1dJJ8zi7phgjZlaS0P7tVDBB5QTtt759ihlIdBYw=; b=raasUZ1azDPDFz9N/ GYcH1f9cT1WA/uZlKy1AieHOyfWPap4KWlUMRcm2lET94MG72i/kY1gfuGW5Fyqv 44WnwEAQZlLLj/WYiTd9EUW6f4HmCAAN4OkeUgzsir4l4d+rYGskA7fSLCrzjkwo QWR1O4AuwQQEP5p9KTMHL3+R84ITjewMd84gsyH6lG7Lm6Nr21F+n1scEk3IDgpL cgW9jIw5kN/Tj6Uf+q9WR4IITj2D9G84JrtNdbby4RLZGNma9Ai5gbGtOhcprS9X yUIqTS/MLPJ+gX2/6qfiTiyFxWqj9FEM8+jqCQVqVYT4c8iQpbomSO+mBaS3HALU w72Jg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=mtq1dJJ8zi7phgjZlaS0P7tVDBB5QTtt759ihlIdB Yw=; b=JwGgghSTw34ZvqEBP2H7eEjXPwEUnCECP6fYGpLjhg3LMwDDhjgD0HYhU ug5ngiH1wTJNEayEmhhftPQf3qjtYFjW04KbqOdKlRI1nJyHp8QPpX02Llr1CDxy bvDONcN9pM9mcrKhD2HpAo0tchD7VrFHkVxbkZRMZ2xQkkEYaNgsG3He7Cchsncr mEoLBAV8OqQVvTBzZ1l0e/2T/q2dvA8+MF9j9fy7frS0SCq7ebavtSSA1Ba1hzdr MkhA9NDaBfI5jbU7K/FFDs+wD123RxngRUEoUocCiNm4+vK7JHzVLs3Quby2cY7R Vr+ugxXQNCXuIqGVTpN1k2GDfMp/A== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduvddrtdeggdekudcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecukfhppeejuddrvdduvddrudehtddrvddtvdenucfrrghrrghmpehmrghilhhfrhho mheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomhenucevlhhushhtvg hrufhiiigvpedt X-ME-Proxy: Received: from [192.168.1.4] (unknown [71.212.150.202]) by mail.messagingengine.com (Postfix) with ESMTPA id 0E2858006B; Thu, 20 Jun 2019 10:38:04 -0400 (EDT) Subject: Re: Inserts restricted to a trigger To: Miles Elam , pgsql-general References: <6fe78cce-7e74-926b-7d3d-2e09da698cc8@aklaver.com> <20190618004728.ph5aeg4wlucll7y5@raf.org> From: Adrian Klaver Message-ID: Date: Thu, 20 Jun 2019 07:38:04 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 6/19/19 3:07 PM, Miles Elam wrote: > Hi Adrian, thanks for responding. > > How would I restrict access to the SECURITY DEFINER function? If it can > be called by the trigger, it can be called by the user as well I would > think. Same issue as access to the table itself only now with a > superuser intermediary, right? We may need to back this up: 1) What is the audit table recording? 2) How much access do your users have to the database itself, outside the Web app? 3) What access do users have in the Web app? > > > On Tue, Jun 18, 2019 at 6:20 PM Adrian Klaver > wrote: > > On 6/18/19 10:14 AM, Miles Elam wrote: > > Thanks for the suggestion. Unfortunately we only have a single login > > role (it's a web app) and then we SET ROLE according to the > contents of > > a JSON Web Token. So we end up with SESSION_USER as the logged in > user > > and the active role as CURRENT_USER. > > Have not tried it but nested function?: > > 1) Outer function runs as normal user and grabs the CURRENT_USER. This > is passed into 2) > > 2) Audit function that runs with SECURITY DEFINER. > > Other option is to record the CURRENT_USER in the table the trigger is > on and just pass that to the audit function. > > > > > It may be that we're just stuck with a gap and need to just try > and keep > > track of our mutation points, such as limit what is accessible > through > > REST or GraphQL, and there is no way to fundamentally lock this > down in > > Postgres. I was checking the mailing list to see if I'd missed > anything. > > > > > > On Tue, Jun 18, 2019 at 9:47 AM Torsten Förtsch > > > >> > wrote: > > > >     Have you tried session_user? > > > >     create function xx() returns table (cur text, sess text) > >     security definer language sql as $$ > >          select current_user::text, session_user::text; > >     $$; > > > >     Then log in as different user and: > > > >     => select (xx()).*; > >         cur    | sess > >     ----------+------- > >       postgres | write > > > > > >     On Tue, Jun 18, 2019 at 6:30 PM Miles Elam > >      > >> wrote: > > > >         That seems straightforward. Unfortunately I also want to know > >         the user/role that performed the operation. If I use SECURITY > >         DEFINER, I get the superuser account back from > CURRENT_USER, not > >         the actual user. > > > >         Sorry, should have included that in the original email. > How do I > >         restrict access while still retaining info about the current > >         user/role? > > > > > >         On Mon, Jun 17, 2019 at 5:47 PM > >         >> wrote: > > > >             Adrian Klaver wrote: > > > >              > On 6/17/19 4:54 PM, Miles Elam wrote: > >              > > Is there are way to restrict direct access to a > table > >             for inserts but > >              > > allow a trigger on another table to perform an > insert > >             for that user? > >              > > > >              > > I'm trying to implement an audit table without > allowing > >             user tampering > >              > > with the audit information. > >              > > >              > Would the below not work?: > >              > CREATE the table as superuser or other privileged user > >              > Have trigger function run as above user(use > SECURITY DEFINER) > > > >             and make sure not to give any other users > insert/update/delete > >             permissions on the audit table. > > > >              > > Thanks in advance, > >              > > > >              > > Miles Elam > >              > > >              > -- > >              > Adrian Klaver > >              > adrian.klaver@aklaver.com > > > > > > > > > -- > Adrian Klaver > adrian.klaver@aklaver.com > -- Adrian Klaver adrian.klaver@aklaver.com