X-Original-To: pgsql-php-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 24D6352844 for ; Fri, 27 May 2005 13:26:11 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 34925-08 for ; Fri, 27 May 2005 16:25:53 +0000 (GMT) Received: from wproxy.gmail.com (wproxy.gmail.com [64.233.184.193]) by svr1.postgresql.org (Postfix) with ESMTP id 7E7D352820 for ; Fri, 27 May 2005 13:25:52 -0300 (ADT) Received: by wproxy.gmail.com with SMTP id 68so827011wri for ; Fri, 27 May 2005 09:25:53 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=nLHGMjBHJyDY2iPoEOmT5uuP2t8yhXkYC6N2c0myntadgXnIazNPBd+69bIj/x8nH144xIwRMIgmUIVS14XpBoha3HBjRmbS/ozqoyXWQJtSiSLA8HAYzJYKg6S/V7X+dECwbSiPKyw2yqJ5yQwHUiB8Ic2v+uKGvuAdbtDdLZo= Received: by 10.54.107.4 with SMTP id f4mr1703548wrc; Fri, 27 May 2005 09:25:52 -0700 (PDT) Received: by 10.54.30.45 with HTTP; Fri, 27 May 2005 09:25:52 -0700 (PDT) Message-ID: <7104a737050527092541f9889f@mail.gmail.com> Date: Fri, 27 May 2005 19:25:52 +0300 From: Volkan YAZICI Reply-To: Volkan YAZICI To: Ed Finkler Subject: Re: Effectiveness of pg_escape_string at blocking SQL injection attacks Cc: pgsql-php@postgresql.org In-Reply-To: <4297435C.20605@cerias.purdue.edu> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline References: <4297435C.20605@cerias.purdue.edu> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=1.266 tagged_above=0 required=5 tests=AWL, DNS_FROM_RFC_ABUSE, RCVD_BY_IP, RCVD_IN_BL_SPAMCOP_NET X-Spam-Level: * X-Archive-Number: 200505/38 X-Sequence-Number: 3152 Hi, On 5/27/05, Ed Finkler wrote: > The php mysql api has a function "mysql_real_escape_string" that seems > to be able to thwart known SQL injection attacks -- at least the ones of > which I and other people I've discussed this with know. I am curious to > know if pg_escape_string is as effective. If not, what would need to be > modified to make it more effective? Both of pg_escape_string() and pg_escape_bytea() is a interface to their libpq equivalents (PQescapeString() and PQescapeBytea()). From this point of view, above question turns into "Do PQescapeString() and PQescapeBytea() functions have enough effectiveness to be able to thwart known SQL injection attacks?" form. I'm not an SQL expert, so folks will help you about above libpq functions and their effectiveness. But if I'd summarize the PHP side of it: In the PHP side, they obeyed the rules mentioned in libpq documentation [1] (like required minimum size to be allocated.) Thus, I couldn't figure out any missed point in the pg_escape_string(), pg_escape_bytea() [2] functions. [1] http://www.postgresql.org/docs/8.0/interactive/libpq-exec.html [2] http://cvs.php.net/co.php/php-src/ext/pgsql/pgsql.c?r=3D1.327 When I traced the related libpq source code for escape routines, I met with following replacements: (I'm not sure if they're enough to thwart known SQL injection attacks.) PQescapeBytea() \0 -> \\000 \' -> \' \\ -> \\\\ Chars between 0x20 - 0x7E -> Their octal equivalents \\VYZ PQescapeString() ' -> '' \ -> \\ If you think, they're not enough for SQL-Injection attacks, I'd advice you to patch libpq code, not PHP. Hope this helps. Best regards.