Received: from localhost (maia-5.hub.org [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id 91B5D9FB63A for ; Thu, 25 Jan 2007 19:44:30 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-new, port 10024) with ESMTP id 97451-06 for ; Thu, 25 Jan 2007 19:44:16 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from net2.micro-automation.com (net2.micro-automation.com [64.7.141.29]) by postgresql.org (Postfix) with SMTP id 1EFFD9FB4C8 for ; Thu, 25 Jan 2007 19:44:26 -0400 (AST) Received: (qmail 7977 invoked from network); 25 Jan 2007 23:44:24 -0000 Received: from dcdsl.ebox.com (HELO ?192.168.1.14?) (davec@64.7.143.116) by net2.micro-automation.com with SMTP; 25 Jan 2007 23:44:24 -0000 In-Reply-To: References: Mime-Version: 1.0 (Apple Message framework v752.3) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: Cc: pgsql-jdbc@postgresql.org Content-Transfer-Encoding: 7bit From: Dave Cramer Subject: Re: escape string for pgsql (using jdbc/java)? Date: Thu, 25 Jan 2007 18:44:30 -0500 To: Tobias Thierer X-Mailer: Apple Mail (2.752.3) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200701/162 X-Sequence-Number: 16527 Tobias, On 25-Jan-07, at 6:34 PM, Tobias Thierer wrote: > Hi, > > I'm writing a servlet that gets a few strings and puts them into a > pgsql database. In assembling an insert statement such as > > INSERT INTO table column1='value1' column2='value2' > > etc., of course I have to make sure an attacker can't put things > into value1 that will breaky my system (such as something that > contains a ' which will then be interpreted as terminating the > string). In other words, I have to escape value* so that it's safe > to use in an sql statement (more specifically inside a string). > > I was previously using MySQL and escaped strings following the > document at: > > http://dev.mysql.com/doc/refman/5.0/en/string-syntax.html > > But I couldn't find a corresponding specification for pgsql. The > only way of doing this through JDBC that I'm aware of is to prepare > a statement first, which just seems wrong because my insert > statement is generated dynamically and executed exactly once (the > subset of the columns for which a value is actually set change > every time the code is run). > > So, > > 1.) Is there a built-in method somewhere in the jdbc driver that > escapes > strings and makes them safe to use in an SQL statement (inside a > string)? > Yup, preparedstatement.setString Dave > 2.) Which characters do I need to escape for pgsql? Is ' the only > one, > and I need to escape it as '' ? Do I need to escape \ ? Will > I need to > escape all the characters that I escaped for MySQL? Where can > I find > out more? > > Cheers, > > Tobias > > ---------------------------(end of > broadcast)--------------------------- > TIP 1: 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 >