pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: W. Trevor King <wking@tremily.us>
To: David G. Johnston <david.g.johnston@gmail.com>
Cc: pgsql-general <pgsql-general@postgresql.org>
Subject: Re: Dynamically filtering a CTE?
Date: Fri, 20 Apr 2018 11:13:47 -0700
Message-ID: <20180420181347.GN27577@valgrind.us> (raw)
In-Reply-To: <CAKFQuwaOQr9Hm3msA2X+tmh2AhoyN1m+fKyRarAr3EmL20OaXg@mail.gmail.com>
References: <20180420000055.GL27577@valgrind.us>
	<CAKFQuwY36yat8Ycf_Ha6z25sZnNYMH9rHmTsG3QfkDZt1zX2XA@mail.gmail.com>
	<20180420162232.GM27577@valgrind.us>
	<CAKFQuwaOQr9Hm3msA2X+tmh2AhoyN1m+fKyRarAr3EmL20OaXg@mail.gmail.com>

On Fri, Apr 20, 2018 at 09:33:22AM -0700, David G. Johnston wrote:
> On Fri, Apr 20, 2018 at 9:22 AM, W. Trevor King wrote:
> > format('
> >     WITH RECURSIVE _ancestors(id, ancestors) AS (
> >         SELECT
> >           item.id AS id,
> >           ARRAY[item.ancestor_id] AS ancestors
> >         FROM items AS item
> >         %s
> > ​[...]​
> >
> >     ', condition);
> >
> >   SELECT * FROM ancestors('WHERE item.id = 62324721');
> 
> ​Just keep in mind that this opens up a huge SQL-injection hole in
> your database.  Depending on how its called you might want to
> validation the input text for both whitelist and blacklist items
> before executing it.

I'm not calling it on user-supplied conditions, but yeah, if I were it
would certainly need some guards.  Unfortunately, neither format [1]
nor USING [2,3] seem to have auto-quoting for “make sure this is just
a WHERE condition [4] without side-effects” ;).  I think we'd need a
WHERE-condition data type to support that, just like we'd need a
WHERE-condition data type (or a function data type) to support my
initial idea [5]:

  CREATE OR REPLACE FUNCTION ancestors(condition WHERE-condition-type)
      RETURNS TABLE(id integer, ancestors integer[]) AS
    $$
      WITH RECURSIVE _ancestors(id, ancestors) AS (
          SELECT
            item.id AS id,
            ARRAY[item.ancestor_id] AS ancestors
          FROM items AS item
          WHERE condition -- or, with a function type, condition(item)
        UNION ALL
          …
      )
      …
    $$ LANGUAGE SQL;

And even if you had a WHERE-condition data type, enforcing the
no-side-effects constraint would be tricky.

Things like blacklisting condition text with semicolons, etc. might
help against unintentional typos, although they seem too easily
avoided to be relied on against potentially malicious user input.
Parsing the condition text as WHERE-clause SQL to look for dangerous
constructs might be strong enough, but seems like a lot of work and
something I'm likely to get wrong if I tried ;).  For now, I'm just
making sure I'm not allowing untrusted users to provide condition
text.

Thanks,
Trevor

[1]: https://www.postgresql.org/docs/10/static/functions-string.html#FUNCTIONS-STRING-FORMAT
[2]: https://www.postgresql.org/docs/10/static/plpgsql-control-structures.html#id-1.8.8.8.3.4
[3]: https://www.postgresql.org/docs/10/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN
[4]: https://www.postgresql.org/docs/10/static/sql-select.html#SQL-WHERE
[5]: https://www.postgresql.org/message-id/20180420000055.GL27577%40valgrind.us

-- 
This email may be signed or encrypted with GnuPG (http://www.gnupg.org).
For more information, see http://en.wikipedia.org/wiki/Pretty_Good_Privacy

Attachments:

  signature.asc (application/pgp-signature, 832B) download

view thread (5+ messages)

Message-ID: <20180420181347.GN27577@valgrind.us>
Permalink:  ../20180420181347.GN27577@valgrind.us/
Also on:    postgresql.org/message-id/20180420181347.GN27577@valgrind.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: wking@tremily.us, david.g.johnston@gmail.com
  Subject: Re: Dynamically filtering a CTE?
  In-Reply-To: <20180420181347.GN27577@valgrind.us>
  References: <20180420000055.GL27577@valgrind.us> <CAKFQuwY36yat8Ycf_Ha6z25sZnNYMH9rHmTsG3QfkDZt1zX2XA@mail.gmail.com> <20180420162232.GM27577@valgrind.us> <CAKFQuwaOQr9Hm3msA2X+tmh2AhoyN1m+fKyRarAr3EmL20OaXg@mail.gmail.com> <20180420181347.GN27577@valgrind.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox