Re: Escaping strings for inclusion into SQL queries - Mailing list pgsql-hackers

From Florian Weimer
Subject Re: Escaping strings for inclusion into SQL queries
Date
Msg-id tg3d5tajsw.fsf@mercury.rus.uni-stuttgart.de
Whole thread Raw
In response to Re: Escaping strings for inclusion into SQL queries  ("Joe Conway" <joseph.conway@home.com>)
Responses Re: Escaping strings for inclusion into SQL queries  (Bruce Momjian <pgman@candle.pha.pa.us>)
Re: Escaping strings for inclusion into SQL queriesh  (Bruce Momjian <pgman@candle.pha.pa.us>)
List pgsql-hackers
"Joe Conway" <joseph.conway@home.com> writes:

> I found a problem with PQescapeString (I think). Since it escapes
> null bytes to be literally '\0', the following can happen:
> 1. User inputs string value as "<null byte>##" where ## are digits in the
> range of 0 to 7.
> 2. PQescapeString converts this to "\0##"
> 3. Escaped string is used in a context that causes "\0##" to be evaluated as
> an octal escape sequence.

I agree that this is a problem, though it is not possible to do
anything harmful with it.  In addition, it only occurs if there are
any NUL characters in its input, which is very unlikely if you are
using C strings.

The patch below addresses the issue by removing escaping of \0
characters entirely.

> If the goal is to "safely" encode null bytes, and preserve the rest of the
> string as it was entered, I think the null bytes should be escaped as \\000
> (note that if you simply use \000 the same string truncation problem
> occurs).

We can't do that, this would require 4n + 1 bytes of storage for the
result, breaking the interface.

--
Florian Weimer                       Florian.Weimer@RUS.Uni-Stuttgart.DE
University of Stuttgart           http://cert.uni-stuttgart.de/
RUS-CERT                          +49-711-685-5973/fax +49-711-685-5898


Attachment

pgsql-hackers by date:

Previous
From: Peter Eisentraut
Date:
Subject: Re: enable-unicode-conversion option?
Next
From: Peter Eisentraut
Date:
Subject: Re: pg_dump -C option