Prevent the injection of invalidly encoded strings by PL/Python into PostgreSQL
authorPeter Eisentraut
Thu, 18 Mar 2010 19:43:03 +0000 (19:43 +0000)
committerPeter Eisentraut
Thu, 18 Mar 2010 19:43:03 +0000 (19:43 +0000)
with a few strategically placed pg_verifymbstr calls.

doc/src/sgml/plpython.sgml
src/pl/plpython/plpython.c

index e8e55a39a5fff0eb52cfe6506e072c7e09d90cba..a82c0f39b63b0ea9c081057705f5eae2c6b864d2 100644 (file)
@@ -1,4 +1,4 @@
-
+
 
 
  PL/Python - Python Procedural Language
@@ -340,6 +340,17 @@ $$ LANGUAGE plpythonu;
        builtin str, and the result is passed to the
        input function of the PostgreSQL data type.
       
+
+      
+       Strings in Python 2 are required to be in the PostgreSQL server
+       encoding when they are passed to PostgreSQL.  Strings that are
+       not valid in the current server encoding will raise an error,
+       but not all encoding mismatches can be detected, so garbage
+       data can still result when this is not done correctly.  Unicode
+       strings are converted to the correct encoding automatically, so
+       it can be safer and more convenient to use those.  In Python 3,
+       all strings are Unicode strings.
+      
      
 
      
index 6b5a56e0c76d5f843ea24c0ff2dcb441e5d35092..2329d4eb28c582461c003af9aea0d9f2f2fa4f37 100644 (file)
@@ -1,7 +1,7 @@
 /**********************************************************************
  * plpython.c - python as a procedural language for PostgreSQL
  *
- * $PostgreSQL: pgsql/src/pl/plpython/plpython.c,v 1.140 2010/03/18 13:23:56 petere Exp $
+ * $PostgreSQL: pgsql/src/pl/plpython/plpython.c,v 1.141 2010/03/18 19:43:03 petere Exp $
  *
  *********************************************************************
  */
@@ -2174,6 +2174,7 @@ PLyObject_ToDatum(PLyTypeInfo *info,
                     errmsg("could not convert Python object into cstring: Python string representation appears to contain null bytes")));
        else if (slen > plen)
            elog(ERROR, "could not convert Python object into cstring: Python string longer than reported length");
+       pg_verifymbstr(plrv_sc, slen, false);
        rv = InputFunctionCall(&arg->typfunc, plrv_sc, arg->typioparam, -1);
    }
    PG_CATCH();
@@ -2871,6 +2872,7 @@ PLy_spi_prepare(PyObject *self, PyObject *args)
            }
        }
 
+       pg_verifymbstr(query, strlen(query), false);
        plan->plan = SPI_prepare(query, plan->nargs, plan->types);
        if (plan->plan == NULL)
            elog(ERROR, "SPI_prepare failed: %s",
@@ -3078,6 +3080,7 @@ PLy_spi_execute_query(char *query, long limit)
    oldcontext = CurrentMemoryContext;
    PG_TRY();
    {
+       pg_verifymbstr(query, strlen(query), false);
        rv = SPI_execute(query, PLy_curr_procedure->fn_readonly, limit);
    }
    PG_CATCH();
@@ -3353,6 +3356,7 @@ PLy_output(volatile int level, PyObject *self, PyObject *args)
    oldcontext = CurrentMemoryContext;
    PG_TRY();
    {
+       pg_verifymbstr(sv, strlen(sv), false);
        elog(level, "%s", sv);
    }
    PG_CATCH();