From 3b1790f9875e0221d6ae305a596f506647fa2554 Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Tue, 27 Jun 2006 00:03:42 +0000 Subject: Add PQescapeIdentifier() to libpq Christopher Kings-Lynne --- doc/src/sgml/libpq.sgml | 64 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) (limited to 'doc/src') diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index 229e9d5a28f..7dec1214c83 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -1,4 +1,4 @@ - + <application>libpq</application> - C Library @@ -2279,6 +2279,68 @@ in favor of PQescapeStringConn. + + Escaping Identifier for Inclusion in SQL Commands + + PQescapeIdentifier + escaping strings + + +PQescapeIdentifier escapes a string for use +as an identifier name within an SQL command. For example; table names, +column names, view names and user names are all identifiers. +Double quotes (") must be escaped to prevent them from being interpreted +specially by the SQL parser. PQescapeIdentifier 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. + + + + +Note that it is still necessary to do escaping of identifiers when +using functions that support parameterized queries such as PQexecParams or +its sibling routines. Only literal values are automatically escaped +using these functions, not identifiers. + + +size_t PQescapeIdentifier (char *to, const char *from, size_t length); + + + + +The parameter from points to the first character of the +string that is to be escaped, and the length parameter +gives the number of characters 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, PQescapeIdentifier 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 character +than twice the value of length, otherwise the behavior is +undefined. A call to PQescapeIdentifier writes an escaped +version of the from string to the to buffer, +replacing special characters so that they cannot cause any harm, and +adding a terminating zero byte. The double quotes that may surround +PostgreSQL identifiers are not included in the result +string; they should be provided in the SQL command that the result is +inserted into. + + +PQescapeIdentifier returns the number of characters written +to to, not including the terminating zero byte. + + +Behavior is undefined if the to and from +strings overlap. + + Escaping Binary Strings for Inclusion in SQL Commands -- cgit v1.2.3