pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Albert REINER <areiner@tph.tuwien.ac.at>
To: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] security: escaping user-supplied data
Date: Tue, 12 Oct 1999 14:22:39 +0200
Message-ID: <19991012142239.A16678@frithjof> (raw)
In-Reply-To: <l03130300b428b43b893a@[147.233.159.109]>
References: <38028148.8066E930@uhlenkott.net>
	<l03130300b428b43b893a@[147.233.159.109]>

On Tue, Oct 12, 1999 at 11:53:44AM +0200, Herouth Maoz wrote:
> At 02:31 +0200 on 12/10/1999, Jason Uhlenkott wrote:
> 
> 
> > The statements I generate are usually of the form:
> > INSERT INTO foo (bar, bas) VALUES ('abc', 'def');
> > but the 'abc' and 'def' come from an untrusted source, so if they supply
> > a string like "def'); delete from foo; '" they can make me do this:
> > INSERT INTO foo (bar, bas) VALUES ('abc', 'def'); delete from foo; '');
> >
> > What do I need to do to prevent this?  My current plan is to prepend a
> > backslash to every single-quote, backslash, and semicolon in the
> > untrusted string.  Are there any other special characters I should watch
> > out for?  Is it possible to do something evil despite your special
> > characters being prepended with a backslash?
> 
> I don't see why you would want to escape a semicolon. If you escape single
> quotes and backslashes, the above situation won't happen - the string won't
> be finished until the first unescaped quote - yours - is encountered.
> Semicolons are not special in strings.
> 
> Herouth

I once posted a similar question to the pgsql-novice mailing
list. There, David Rugge (1 Aug 1999) told me to escape ', ", and %,
even though I am not quite sure why you have to escape " and %. But
now that I think of it: you also need to escape \, of course, or
backslashes will either get lost or, even worse, may escape the
closing quote (think of $def="\"). Thus, using Perl and Pg, you would
do:

use Pg;
$conn = ...;

$abc="abc";
$def="def";
$conn->exec("INSERT INTO foo (bar, bas) VALUES ('" .
    &stdstr($abc) . "', '" . &stdstr($def) . "')";

sub stdstr {
    local $or = $_[0];
    $or =~ s /\'/\\\'/g;
    $or =~ s /\"/\\\"/g;
    $or =~ s /%/\\%/g;
    $or =~ s /\\/\\\\/g;
    return $or;
}

Hope that helps,

Albert.

-- 

---------------------------------------------------------------------------
  Post an / Mail to / Skribu al: Albert Reiner <areiner@tph.tuwien.ac.at>
---------------------------------------------------------------------------



view thread (8+ messages)  latest in thread

Message-ID: <19991012142239.A16678@frithjof>
Permalink:  ../19991012142239.A16678@frithjof/
Also on:    postgresql.org/message-id/19991012142239.A16678@frithjof

 · 

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-sql@postgresql.org
  Cc: areiner@tph.tuwien.ac.at
  Subject: Re: [SQL] security: escaping user-supplied data
  In-Reply-To: <19991012142239.A16678@frithjof>
  References: <38028148.8066E930@uhlenkott.net> <l03130300b428b43b893a@[147.233.159.109]> <19991012142239.A16678@frithjof>

* 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