Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f9aZ1-0005C9-Kd for pgsql-general@arkaria.postgresql.org; Fri, 20 Apr 2018 18:15:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f9aZ0-0002Xc-5D for pgsql-general@arkaria.postgresql.org; Fri, 20 Apr 2018 18:15:14 +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 1f9aYz-0002Wz-Px for pgsql-general@lists.postgresql.org; Fri, 20 Apr 2018 18:15:13 +0000 Received: from resqmta-po-03v.sys.comcast.net ([2001:558:fe16:19:96:114:154:162]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f9aYw-0002oY-5Q for pgsql-general@postgresql.org; Fri, 20 Apr 2018 18:15:12 +0000 Received: from resomta-po-02v.sys.comcast.net ([96.114.154.226]) by resqmta-po-03v.sys.comcast.net with ESMTP id 9V4XfvrtOgv8C9aYtfUMuf; Fri, 20 Apr 2018 18:15:07 +0000 Received: from mail.tremily.us ([67.168.0.137]) by resomta-po-02v.sys.comcast.net with ESMTPSA id 9aWvf3IBNeepi9aWwfXuay; Fri, 20 Apr 2018 18:13:07 +0000 Received: by mail.tremily.us (Postfix, from userid 1000) id 47204152979C; Fri, 20 Apr 2018 11:13:47 -0700 (PDT) Date: Fri, 20 Apr 2018 11:13:47 -0700 From: "W. Trevor King" To: "David G. Johnston" Cc: pgsql-general Subject: Re: Dynamically filtering a CTE? Message-ID: <20180420181347.GN27577@valgrind.us> References: <20180420000055.GL27577@valgrind.us> <20180420162232.GM27577@valgrind.us> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="bJ3jXuwtxrXxD2iT" Content-Disposition: inline In-Reply-To: OpenPGP: id=39A2F3FA2AB17E5D8764F388FC29BDCDF15F5BE8; url=http://tremily.us/pubkey.txt User-Agent: Mutt/1.7.2 (2016-11-26) X-CMAE-Envelope: MS4wfNr+MSkx6k/8ebNX56/2T2rSfRPbuBEACjRlLDLyFoZyXXNIg92hTL92LHSY2j2V3CWZl2R9i5H9t0jhlxuQPoB7VhNG3B7ZI7nzLmOhjWtUTPNITcRQ miSBj7mPijWIlbP2+/Ckoel9OacQrE6JiLS685FF9ByuGx+XfAhfQz9eJ7BUUAKZqPTK4/aqDVuzGv1+o7T88dl1ZxGCuxApI/O2zEw19+Gu+1hmI556vOXt List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --bJ3jXuwtxrXxD2iT Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable 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 > > =E2=80=8B[...]=E2=80=8B > > > > ', condition); > > > > SELECT * FROM ancestors('WHERE item.id =3D 62324721'); >=20 > =E2=80=8BJust 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 =E2=80=9Cmake sure this is ju= st a WHERE condition [4] without side-effects=E2=80=9D ;). 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 =E2=80=A6 ) =E2=80=A6 $$ 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#FUNCTI= ONS-STRING-FORMAT [2]: https://www.postgresql.org/docs/10/static/plpgsql-control-structures.h= tml#id-1.8.8.8.3.4 [3]: https://www.postgresql.org/docs/10/static/plpgsql-statements.html#PLPG= SQL-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%40valgrin= d.us --=20 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 --bJ3jXuwtxrXxD2iT Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEpgNNa8H/zemHkt2gprXBz9Wld7YFAlraLdUACgkQprXBz9Wl d7ZOvA/+I51dcBeRFcbfN58f947VZ7bsr6UoUJTU4b5AfG3L22qSQckuIcblkzRY DM9b7zo1O9hCIqCYHZUEoNQ+nn1LuH1+m9d06TWkFZMw2J8fgVVoAek37r2ZI4Ki 8q5dj501C9FNARwnU8fQIOgmpwW3wxke1BX3iX06Wb3nQfAFXxcxc4VsxQPkeMtX Z+yW3Zmr6279nQcUferG9rdRK3fJ3flDWLmri8IM/tu8u+sZIhOEH+szPLbBufEC qvYxZ9fynkSjcy2oE6J2tfCz/NGKBKX8WMs65WRdyQ8iv5kPOWqXTCh5mI0rQMGe 4wRfKiAMwvcpWKXLPKgxtGut5IokaOEr3FrMjWhloIw0Au9O4W70kA2SZYCOZIfZ w1W9f1UB0XWvl03cJl/S7aAr4G9PBUPrkL5DPhrCXQfINFFNHxQVJCKneFYaCamA Q9/xqIjQGeX9Qqn7w7ssLhjbwdr+I5KQ+Lu6JAOE/CWokIC26SfP9SRZLlngu4lj N3gqSkXKpU6ccHPTvnTmT7vYFWXbUr+iEHnAQQlcjQAFtalef+/Sn8ljRVd31mGm KYYUrI5UNZw4oh5QAcWIhYbaGqwjkCKOjWy3m++V4D9eOl9PTDj9WigOJyBVCmGV d7Hi1KytUZBN/fNqhgdLwFy51vbzGFG6sW4tPvFHtcJSmLiIms8= =1H6L -----END PGP SIGNATURE----- --bJ3jXuwtxrXxD2iT--