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 1huzoq-0003Cp-JJ for pgsql-general@arkaria.postgresql.org; Tue, 06 Aug 2019 13:48:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1huzop-0007FO-Bz for pgsql-general@arkaria.postgresql.org; Tue, 06 Aug 2019 13:48:03 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1huzop-0007FH-2d for pgsql-general@lists.postgresql.org; Tue, 06 Aug 2019 13:48:03 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1huzol-00043w-J7 for pgsql-general@postgresql.org; Tue, 06 Aug 2019 13:48:02 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x76DlsnZ019557; Tue, 6 Aug 2019 09:47:54 -0400 From: Tom Lane To: VO Ipfix cc: pgsql-general@postgresql.org Subject: Re: Preventing in-session 'set role' commands In-reply-to: References: Comments: In-reply-to VO Ipfix message dated "Tue, 06 Aug 2019 01:25:43 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <19555.1565099274.1@sss.pgh.pa.us> Date: Tue, 06 Aug 2019 09:47:54 -0400 Message-ID: <19556.1565099274@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk VO Ipfix writes: > Hello everyone! I am working on a multi-tenant (sigh) DB design using > schemas. I anticipate a bunch of junior developers coming in before we > fully mature our testing process, so SQLi is a concern. Basically, I want > to have a role for each tenant, and have a user/role that will est. a DB > session from a connection pool then perform a set role followed by a set > schema to the schema that the tenant role has grants to. So, my main > requirement is this: after these two (or more) commands are invoked, the > current role should not be able to do a set role to any other role (tenant) > other than itself. This is to prevent an attacker-controlled SQL query that > has set role as part of its payload.Is this something that can be > accomplished with PostgreSQL? There's nothing built-in for that, but probably an event trigger could be written to implement such a restriction. As noted by another respondent, SET SESSION AUTHORIZATION might be a better fit to your goals than SET ROLE. (I don't recall the exact distinction between them -- ENOCAFFEINE -- but I think the former gives up more privilege than the latter.) You'd still need a trigger. regards, tom lane