From e6f43f7786bdd01c93d6bdcb429d3e724022bf1c Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Sun, 21 May 2006 20:20:48 +0000 Subject: Modify libpq's string-escaping routines to be aware of encoding considerations and standard_conforming_strings. The encoding changes are needed for proper escaping in multibyte encodings, as per the SQL-injection vulnerabilities noted in CVE-2006-2313 and CVE-2006-2314. Concurrent fixes are being applied to the server to ensure that it rejects queries that may have been corrupted by attempted SQL injection, but this merely guarantees that unpatched clients will fail rather than allow injection. An actual fix requires changing the client-side code. While at it we have also fixed these routines to understand about standard_conforming_strings, so that the upcoming changeover to SQL-spec string syntax can be somewhat transparent to client code. Since the existing API of PQescapeString and PQescapeBytea provides no way to inform them which settings are in use, these functions are now deprecated in favor of new functions PQescapeStringConn and PQescapeByteaConn. The new functions take the PGconn to which the string will be sent as an additional parameter, and look inside the connection structure to determine what to do. So as to provide some functionality for clients using the old functions, libpq stores the latest encoding and standard_conforming_strings values received from the backend in static variables, and the old functions consult these variables. This will work reliably in clients using only one Postgres connection at a time, or even multiple connections if they all use the same encoding and string syntax settings; which should cover many practical scenarios. Clients that use homebrew escaping methods, such as PHP's addslashes() function or even hardwired regexp substitution, will require extra effort to fix :-(. It is strongly recommended that such code be replaced by use of PQescapeStringConn/PQescapeByteaConn if at all feasible. --- doc/src/sgml/libpq.sgml | 267 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 193 insertions(+), 74 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index f0cb3cdb674..f9f67ed818d 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -1,5 +1,5 @@ @@ -880,115 +880,234 @@ as with a PGresult returned by libpq - Escaping strings for inclusion in SQL queries + Escaping Strings for Inclusion in SQL Commands + PQescapeStringConn + PQescapeString escaping strings -PQescapeString - Escapes a string for use within an SQL query. +PQescapeStringConn escapes a string for use within an SQL +command. This is useful when inserting data values as literal constants +in SQL commands. Certain characters (such as quotes and backslashes) must +be escaped to prevent them from being interpreted specially by the SQL parser. +PQescapeStringConn performs this operation. + + + + +It is especially important to do proper escaping when handling strings that +were received from an untrustworthy source. Otherwise there is a security +risk: you are vulnerable to SQL injection attacks wherein unwanted +SQL commands are fed to your database. + + + + -size_t PQescapeString (char *to, const char *from, size_t length); +size_t PQescapeStringConn (PGconn *conn, + char *to, const char *from, size_t length, + int *error); -If you want to include strings that have been received -from a source that is not trustworthy (for example, because a random user -entered them), you cannot directly include them in SQL -queries for security reasons. Instead, you have to quote special -characters that are otherwise interpreted by the SQL parser. + -PQescapeString performs this operation. The -from points to the first character of the string that -is to be escaped, and the length parameter counts the -number of characters in this string (a terminating zero byte is -neither necessary nor counted). to shall point to a -buffer that is able to hold at least one more character than twice -the value of length, otherwise the behavior is -undefined. A call to PQescapeString writes an escaped +PQescapeStringConn writes an escaped version of the from string to the to -buffer, replacing special characters so that they cannot cause any +buffer, escaping special characters so that they cannot cause any harm, and adding a terminating zero byte. The single quotes that -must surround PostgreSQL string literals are not part of the result -string. +must surround PostgreSQL string literals are not +included in the result string; they should be provided in the SQL +command that the result is inserted into. +The parameter from points to the first character of the string +that is to be escaped, and the length parameter gives the +number of bytes in this string. A terminating zero byte is not +required, and should not be counted in length. (If +a terminating zero byte is found before length bytes are +processed, PQescapeStringConn stops at the zero; the behavior +is thus rather like strncpy.) +to shall point to a +buffer that is able to hold at least one more byte than twice +the value of length, otherwise the behavior is +undefined. +Behavior is likewise undefined if the to and from +strings overlap. + + +If the error parameter is not NULL, then *error +is set to zero on success, nonzero on error. Presently the only possible +error conditions involve invalid multibyte encoding in the source string. +The output string is still generated on error, but it can be expected that +the server will reject it as malformed. On error, a suitable message is +stored in the conn object, whether or not error +is NULL. -PQescapeString returns the number of characters written +PQescapeStringConn returns the number of bytes written to to, not including the terminating zero byte. -Behavior is undefined when the to and from -strings overlap. + + + + +size_t PQescapeString (char *to, const char *from, size_t length); + + + + +PQescapeString is an older, deprecated version of +PQescapeStringConn; the difference is that it does not +take conn or error parameters. Because of this, +it cannot adjust its behavior depending on the connection properties (such as +character encoding) and therefore it may give the wrong results. +Also, it has no way to report error conditions. + + +PQescapeString can be used safely in single-threaded client +programs that work with only one PostgreSQL connection at +a time (in this case it can find out what it needs to know behind the +scenes). In other contexts it is a security hazard and should be avoided +in favor of PQescapeStringConn. - Escaping binary strings for inclusion in SQL queries + Escaping Binary Strings for Inclusion in SQL Commands + - escaping binary strings + bytea + in libpq + + + + PQescapeByteaConnPQescapeByteaConn + - PQescapeBytea - Escapes a binary string (bytea type) for use within an SQL query. - - unsigned char *PQescapeBytea(unsigned char *from, - size_t from_length, - size_t *to_length); - - - Certain ASCII characters must - be escaped (but all characters may be escaped) - when used as part of a bytea - string literal in an SQL statement. In general, to - escape a character, it is converted into the three digit octal number - equal to the decimal ASCII value, and preceded by - two backslashes. The single quote (') and backslash (\) characters have - special alternate escape sequences. See the User's Guide - for more information. PQescapeBytea - performs this operation, escaping only the minimally - required characters. + Escapes binary data for use within an SQL command with the type + bytea. + +unsigned char *PQescapeByteaConn(PGconn *conn, + const unsigned char *from, + size_t from_length, + size_t *to_length); + + + + + Certain byte values must be escaped (but all + byte values can be escaped) when used as part + of a bytea literal in an SQL + statement. In general, to escape a byte, it is converted into the + three digit octal number equal to the octet value, and preceded by + one or two backslashes. The single quote (') and backslash + (\) characters have special alternative escape + sequences. PQescapeByteaConn performs this + operation, escaping only the minimally required bytes. The from parameter points to the first - character of the string that is to be escaped, and the - from_length parameter reflects the number of - characters in this binary string (a terminating zero byte is - neither necessary nor counted). The to_length - parameter shall point to a buffer suitable to hold the resultant - escaped string length. The result string length includes the terminating + byte of the string that is to be escaped, and the + from_length parameter gives the number of + bytes in this binary string. (A terminating zero byte is + neither necessary nor counted.) The to_length + parameter points to a variable that will hold the resultant + escaped string length. This result string length includes the terminating zero byte of the result. - PQescapeBytea returns an escaped version of the - from parameter binary string, to a caller-provided - buffer. The return string has all special characters replaced - so that they can be properly processed by the PostgreSQL string literal - parser, and the bytea input function. A terminating zero - byte is also added. The single quotes that must surround - PostgreSQL string literals are not part of the result string. + PQescapeByteaConn returns an escaped version of the + from parameter binary string in memory + allocated with malloc(). This memory must be freed using + free() when the result is no longer needed. The + return string has all special characters replaced so that they can + be properly processed by the PostgreSQL + string literal parser, and the bytea input function. A + terminating zero byte is also added. The single quotes that must + surround PostgreSQL string literals are + not part of the result string. + + + + On error, a NULL pointer is returned, and a suitable error message + is stored in the conn object. Currently, the only + possible error is insufficient memory for the result string. + + + + + + PQescapeByteaPQescapeBytea + + + PQescapeBytea is an older, deprecated version of + PQescapeByteaConn. + +unsigned char *PQescapeBytea(unsigned char *from, + size_t from_length, + size_t *to_length); + + + + + The only difference from PQescapeByteaConn is that + PQescapeBytea does not + take a PGconn parameter. Because of this, it cannot adjust + its behavior depending on the connection properties + and therefore it may give the wrong results. Also, it + has no way to return an error message on failure. - PQunescapeBytea - Converts an escaped string representation of binary data into binary - data - the reverse of PQescapeBytea. - - unsigned char *PQunescapeBytea(unsigned char *from, size_t *to_length); - - - The from parameter points to an escaped string - such as might be returned by PQgetvalue of a - BYTEA column. PQunescapeBytea converts - this string representation into its binary representation, filling the supplied buffer. - It returns a pointer to the buffer which is NULL on error, and the size - of the buffer in to_length. The pointer may - subsequently be used as an argument to the function - free(3). + PQescapeBytea can be used safely in single-threaded client + programs that work with only one PostgreSQL connection at + a time (in this case it can find out what it needs to know behind the + scenes). In other contexts it is a security hazard and should be + avoided in favor of PQescapeByteaConn. + + + + + PQunescapeByteaPQunescapeBytea + + + Converts a string representation of binary data into binary + data --- the reverse of PQescapeBytea. + This is needed when retrieving bytea data in text format, + but not when retrieving it in binary format. + + +unsigned char *PQunescapeBytea(unsigned char *from, size_t *to_length); + + + + + The from parameter points to a string + such as might be returned by PQgetvalue when applied + to a bytea column. PQunescapeBytea + converts this string representation into its binary representation. + It returns a pointer to a buffer allocated with + malloc(), or null on error, and puts the size of + the buffer in to_length. The result must be + freed using free() when it is no longer needed. + + + + This conversion is not exactly the inverse of + PQescapeBytea, because the string is not expected + to be escaped when received from PQgetvalue. + In particular this means there is no need for string quoting considerations, + and so no need for a PGconn parameter. + + + + - Retrieving SELECT Result Information -- cgit v1.2.3