pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Volkan YAZICI <volkan.yazici@gmail.com>
To: Ed Finkler <coj@cerias.purdue.edu>
Cc: pgsql-php@postgresql.org
Subject: Re: Effectiveness of pg_escape_string at blocking SQL injection attacks
Date: Fri, 27 May 2005 19:25:52 +0300
Message-ID: <7104a737050527092541f9889f@mail.gmail.com> (raw)
In-Reply-To: <4297435C.20605@cerias.purdue.edu>
References: <4297435C.20605@cerias.purdue.edu>

Hi,

On 5/27/05, Ed Finkler <coj@cerias.purdue.edu> 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=1.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.



view thread (9+ messages)  latest in thread

Message-ID: <7104a737050527092541f9889f@mail.gmail.com>
Permalink:  ../7104a737050527092541f9889f@mail.gmail.com/
Also on:    postgresql.org/message-id/7104a737050527092541f9889f@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-php@postgresql.org
  Cc: volkan.yazici@gmail.com, coj@cerias.purdue.edu
  Subject: Re: Effectiveness of pg_escape_string at blocking SQL injection attacks
  In-Reply-To: <7104a737050527092541f9889f@mail.gmail.com>
  References: <4297435C.20605@cerias.purdue.edu> <7104a737050527092541f9889f@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox