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 B053ECCA08 for ; Thu, 17 Jul 2003 14:46:50 +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 20881-07 for ; Thu, 17 Jul 2003 14:47:14 +0000 (GMT) Received: from exchange.hq.openratings.com (unknown [141.149.165.226]) by svr1.postgresql.org (Postfix) with ESMTP id D4D62CCA19 for ; Thu, 17 Jul 2003 11:46:48 -0300 (ADT) Received: from openratings.com (cancun.hq.openratings.com [10.0.0.225]) by exchange.hq.openratings.com with SMTP (Microsoft Exchange Internet Mail Service Version 5.5.2653.13) id 38QM44F0; Thu, 17 Jul 2003 10:47:18 -0400 Message-ID: <3F16B715.4010006@openratings.com> Date: Thu, 17 Jul 2003 10:47:49 -0400 From: Dmitry Tkach User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20021130 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Paul Thomas Cc: "pgsql-jdbc @ postgresql . org" Subject: Re: Prepared Statements References: <200307161624.39340.julien.legoff@laposte.net> <20030717002329.A26390@bacon> In-Reply-To: <20030717002329.A26390@bacon> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200307/79 X-Sequence-Number: 7463 > > Forget performance for a moment and consider database security. Lets > imagine that you have an address book table called address_book > > CREATE TABLE address_booK > ( > name varchar(30), > address text > ); > > and you want to select a row by name. You might write > > String query = "SELECT * from address_book WHERE name = "+strName > > where strName was typed in by the user. What would happen if the user > typed: > > joe;delete from address_book Either the exact same thing as what you describe below with the PreparedStatement, or a syntax eror if you forget to put the user's input into quotes when constracting your sql :-) > > This is a security hole known as SQL injection. No, it isn't :-) The "hole" you are referring to is letting the users type in entire queries, not just input parameters. As long as you have control over how your sql is constructed, you not any less (nor any more) safe with plain Statements than you would be with PreparedStatements. The do the same exact thing. Dima > If you are using a normal Statement then your users can probably > delete whole tables from the database but with a PreparedStatement you > would write > > String query = "SELECT * from address_book WHERE name = ?" > > and the command actually passed over to the database would be > > SELECT * from address_book WHERE name = 'joe;delete from address_book' > > I'm sure you can see the difference. Maybe PreparedStatements will > have a performance gain in some future release but at the moment they > have a vital role to play in database security. >