Re: Prepared Statements - Mailing list pgsql-jdbc

From Fernando Nasser
Subject Re: Prepared Statements
Date
Msg-id 3F18065A.1060406@redhat.com
Whole thread Raw
In response to Re: Prepared Statements  (wsheldah@lexmark.com)
Responses Re: Prepared Statements  (Csaba Nagy <nagy@ecircle-ag.com>)
Re: Prepared Statements  (Dmitry Tkach <dmitry@openratings.com>)
List pgsql-jdbc
Dmitry Tkach wrote:
> Barry Lind wrote:
>
>> If using a PreparedStatement the driver correctly escapes all values
>> to avoid SQL injection attacks.
>
>
> No, it doesn't :-)
> For example:
>
> PreparedStatement s = c.prepareStatement ("select * from user where id =
> ?");
> s.setObject (1, "null;drop database mydatabase", Types.INTEGER);
> System.out.println (s.toString ());
>
> select * from user where id=null;drop database mydb
>
> :-)
>

I don't believe this is actually being sent to the backend, maybe it is
just a toString() bug.

The backend should get:

select * from user where id='null;drop database mydb'

(If it does not it is a bug.)


P.S.: The example case would only succeed if the DBA is an idiot.
You program should not be accessing the database (for this queries at
least) as an user who can drop databases unless it is a privileged
program for privileged users (who could do the damage using plain psql
anyway).  Perhaps the injection of a 'DELETE FROM mytable' would be a
more realistic example.


--
Fernando Nasser
Red Hat Canada Ltd.                     E-Mail:  fnasser@redhat.com
2323 Yonge Street, Suite #300
Toronto, Ontario   M4P 2C9


pgsql-jdbc by date:

Previous
From: Csaba Nagy
Date:
Subject: Re: Prepared Statements
Next
From: Csaba Nagy
Date:
Subject: Re: Prepared Statements