Skip site navigation (1) Skip section navigation (2)

Re: Protection from SQL injection

From: "Thomas Mueller" <thomas(dot)tom(dot)mueller(at)gmail(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: Protection from SQL injection
Date: 2008-04-27 18:43:32
Message-ID: 5f211bd50804271143y6929541eh98e43b754c03d792@mail.gmail.com (view raw or flat)
Thread:
Lists: pgsql-sql
Hi,

>  The problem may be legacy code.

Of course. There is a lot of legacy code that needs to be made secure
('hardened'). The best solution is to use parameterized queries. To
find out what statements are still not doing that, first disable text
literals only. If some places are incorrect, the relevant statements
will be in the log file. After text literals are fixed, number
literals can be disabled.

>  Since it is going to make dev nervous because it adds code bloat
>  that's going to cause more bugs than the SQL injections it may
>  prevent.

I don't think it will really bloat the code. In fact, the code will be
cleaner afterwards.

>  "select a.id, b.name from a join b on b.id=a.id where
>  a.status='pending' and b.id>7 and b.status='logged'

I even saw code like this in a VIEW: ... and b.type = 10430001 ... The
10430001 had a special meaning. It was documented somewhere, but not
in the code of course ;-) 'pending' and 'logged' are better, but in
regular programming languages usually constants are defined. This also
solves the 'disable literals' pain you have described. The code would
look like this:

select a.id, b.name from a join b on b.id=a.id where
a.status=STATUS_PENDING and b.id>? and b.status=STATUS_LOGGED.

So parameters only need to be used for the actual user input.

>  Prepared statements force you to match input with position and it is
>  definitively error prone.

The correctness of the application in the normal case is usually
tested. But I didn't so far see any SQL injection tests in regular
application.

>  Everything is still in the hands of the dev.

Not always, specially not in banking applications.

> you want to make your dev life a hell

Using parameterized queries is not 'hell', it's best practice.

Regards,
Thomas

On Sun, Apr 27, 2008 at 12:38 PM, Ivan Sergio Borgonovo
<mail(at)webthatworks(dot)it> wrote:
> On Sat, 26 Apr 2008 21:50:10 -0600
>  "Scott Marlowe" <scott(dot)marlowe(at)gmail(dot)com> wrote:
>
>  > Agreed.  My point was that to do what the OP wants, wouldn't it make
>  > more sense to just lobotomize libpq so it doesn't understand
>  > anything but prepared queries.  Doesn't obviate the need for a
>  > client side language based solution.  Just seems to make WAY more
>  > sense than trying to make the change at the server level in pgsql.
>
>  The problem may be legacy code.
>
>  You'd like to statically point out places where multiple statements
>  could get injected.
>  All calls to your "query" function get routed to a wrapper that
>  actually call prepare/execute logic.
>  You do a BIG search&replace and see where your code fail cos you
>  actually needed more than one statement in a query.
>  Now you just have to grep your code for direct call to "plain"
>  queries during commit of your rcs.
>
>  My proposal was to add a switch that force routing to prepared
>  statement logic in libpq.
>
>  I'm thinking about situation in which you're using a library that
>  already wrap your query call.
>  You don't want to change the wrapper, so you don't want to take the
>  responsibility, sync troubles etc... of the library maintainer but
>  still you'd like to add a safety net to your code.
>
>  People dealing with your code would still see the familiar library
>  wrapper (you're not wrapping the wrapper) but you'd be able to switch
>  to "single statement mode".
>
>
>  Still ALLOW_LITERALS is a nice feature even if I think it won't fix
>  the notorious SQL injection problem forever.
>  Since it is going to make dev nervous because it adds code bloat
>  that's going to cause more bugs than the SQL injections it may
>  prevent.
>  Once you've developers that are so patient to write stuff like:
>
>  "select a.id, b.name from a join b on b.id=a.id where
>  a.status='pending' and b.id>7 and b.status='logged'"
>
>  into
>
>  "select a.id, b.name from a join b on b.id=a.id where
>  a.status=? and b.id>? and b.status=?", 'pending', 7, 'logged'
>
>  there are high chances they will prefer to spend some of their time
>  actually thinking about what they are writing.
>
>  I do know that thinking can't be taken for granted and that habits
>  and automatic methods may be generally preferable to "thinking", but
>  automatic methods works when they don't look painful.
>  Prepared statements force you to match input with position and it is
>  definitively error prone.
>
>  It is a tool... you may have some section of your code where that
>  parameter can't be changed, but most of the time you'll find it
>  useful if its default is set to NONE and dev *can* change it.
>
>  Now... let's think at the poor programmer...
>
>  He is writing a SQL statement that is static. He has to disable
>  ALLOW_LITERALS.
>  He is writing dynamic SQL that DON'T take user input.
>  ALLOW_LITERALS may still have some sense as a debugging tool but
>  there are high chances he will disable it to avoid other errors and
>  make coding simpler.
>  He is writing dynamic SQL that does take user input. He should be
>  forced to use ALLOW_LITTERALS NONE. But how can he be forced in the
>  middle of a program?
>  He is writing a "mixed" statement where some input is actually static
>  but not all... he may think it is easier to allow literals.
>
>  Everything is still in the hands of the dev.
>  Such setting may help you in static code evaluation since you may
>  spot easier the places where there could be breach of policy... but
>  still unless you want to make your dev life a hell... it is not going
>  to solve the SQL injection problem.
>
>  "mixed" statements that use external input and static input are quite
>  common and writing them avoiding literals may be a pain that your dev
>  won't be willing to suffer.
>
>  Queued statements in one query are far less common.
>
>  Still I do think that ALLOW_LITERAL is a valuable tool.
>  Same problems for legacy code apply.
>
>
>  --
>  Ivan Sergio Borgonovo
>  http://www.webthatworks.it
>
>
>  --
>
>
> Sent via pgsql-sql mailing list (pgsql-sql(at)postgresql(dot)org)
>  To make changes to your subscription:
>  http://www.postgresql.org/mailpref/pgsql-sql
>

In response to

pgsql-sql by date

Next:From: chester c youngDate: 2008-04-27 21:33:33
Subject: psql: no schema info
Previous:From: JoeDate: 2008-04-27 18:25:06
Subject: Re: Protection from SQL injection

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group