X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.192]) by svr1.postgresql.org (Postfix) with ESMTP id EDA23CCD62 for ; Fri, 18 Jul 2003 15:50:45 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (svr4.postgresql.org [64.117.224.192]) (amavisd-new, port 10024) with ESMTP id 60214-10 for ; Fri, 18 Jul 2003 15:51:17 +0000 (GMT) Received: from sw1.lex.lexmark.com (sw1.lexmark.com [192.146.101.70]) by svr1.postgresql.org (Postfix) with ESMTP id E2752CCCE9 for ; Fri, 18 Jul 2003 12:50:43 -0300 (ADT) Sensitivity: Subject: Re: Prepared Statements To: Csaba Nagy Cc: pgsql-jdbc@postgresql.org From: wsheldah@lexmark.com Date: Fri, 18 Jul 2003 11:10:22 -0400 Message-ID: X-MIMETrack: Serialize by Router on smtp5b/Lex/Lexmark (Release 5.0.12 |February 13, 2003) at 07/18/2003 11:16:35 AM MIME-Version: 1.0 Content-type: text/plain; charset="us-ascii" X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200307/135 X-Sequence-Number: 7519 If it only skips the escaping for numeric types, the obvious workaround would be first put the user's entry into an int variable: int userId = getUserId(); PreparedStatement s = c.prepareStatement ("select * from user where id = ?"); s.setObject(1, userId, Types.INTEGER); That way you use java's built-in type checking to avoid sending non-numeric data to the backend any time you're specifying a numeric type that will skip the escaping. Can someone confirm that it at least does do the escaping for string/varchar inputs? Wes Sheldahl Csaba Nagy @postgresql.org on 07/18/2003 10:46:33 AM Sent by: pgsql-jdbc-owner@postgresql.org To: Fernando Nasser cc: Dmitry Tkach , Barry Lind , wsheldah@lexmark.com, "pgsql-jdbc @ postgresql " ". org" Subject: Re: [JDBC] Prepared Statements I have checked, the query is indeed sent like that to the backend, I've just checked. It is a bug. Presumably for number types the parameter set is passed as it is, without any escaping. Cheers, Csaba. On Fri, 2003-07-18 at 16:38, Fernando Nasser wrote: > 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 > > > ---------------------------(end of broadcast)--------------------------- > TIP 8: explain analyze is your friend > ---------------------------(end of broadcast)--------------------------- TIP 3: if posting/reading through Usenet, please send an appropriate subscribe-nomail command to majordomo@postgresql.org so that your message can get through to the mailing list cleanly