X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id F362B9DCA7C for ; Fri, 3 Feb 2006 12:54:07 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 56974-08 for ; Fri, 3 Feb 2006 12:54:06 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from uproxy.gmail.com (uproxy.gmail.com [66.249.92.199]) by postgresql.org (Postfix) with ESMTP id 0A0B19DCA51 for ; Fri, 3 Feb 2006 12:54:02 -0400 (AST) Received: by uproxy.gmail.com with SMTP id s2so413924uge for ; Fri, 03 Feb 2006 08:53:59 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:subject:from:to:content-type:date:message-id:mime-version:x-mailer; b=jxjPxgLZepMUSJYq9+1F+rpIfALq+fDb7KwnWIQR3vA4+P52xrEu5cLRDdUwlFi8lwf1FA3KVP5VsrN9YZWjeeLF4tc5Z7aFwB9Tt/KZheRG1otRpkyRfwpK1r4s7VcFq4EGXFzpUZgAZaejI4ML0wklAG9yfiLPAT4jaUT7qWA= Received: by 10.49.17.1 with SMTP id u1mr555269nfi; Fri, 03 Feb 2006 08:53:58 -0800 (PST) Received: from ?10.0.0.5? ( [151.44.139.37]) by mx.gmail.com with ESMTP id h1sm1194598nfe.2006.02.03.08.53.57; Fri, 03 Feb 2006 08:53:58 -0800 (PST) Subject: escapeQuotes causes faults in DatabaseMataData From: Paolo Predonzani To: pgsql-jdbc@postgresql.org Content-Type: multipart/alternative; boundary="=-c1LBxGOK545eDi2AF+AK" Date: Fri, 03 Feb 2006 17:54:41 +0100 Message-Id: <1138985682.7276.24.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Evolution 2.4.1 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.667 required=5 tests=[AWL=-0.666, HTML_MESSAGE=0.001, RCVD_IN_BL_SPAMCOP_NET=1.332] X-Spam-Score: 0.667 X-Spam-Level: X-Archive-Number: 200602/19 X-Sequence-Number: 14616 --=-c1LBxGOK545eDi2AF+AK Content-Type: text/plain Content-Transfer-Encoding: 7bit Hi everybody! There is a particular type of input that causes trouble when calling DatabaseMetaData's methods such as getTables(), ecc. The input I'm talking about are strings containing backslash characters in situations like the following: dbmd.getTables(null, null, "my\\table", types); The result is generally a wrong answer from the getTables method or, in the worst situation where the backslash is the last character, a PSQLException. Some sample code: public void testPostgresQuote() throws Exception { System.out.println("testPostgresQuote:"); Connection conn = null; try { String[] types = {"TABLE"}; conn = ConnectionFactory.getConnection(); DatabaseMetaData dbmd = conn.getMetaData(); dbmd.getTables(null, null, "\\", types); } finally { if (conn != null) { try {conn.close();} catch (Exception e2) {} } } } this causes a nasty exception (and opens a possibility for SQL injection?): ERROR: unterminated quoted string at or near "'\' AND (false ) ORDER BY TABLE_TYPE,TABLE_SCHEM,TABLE_NAME " org.postgresql.util.PSQLException: ERROR: unterminated quoted string at or near "'\' AND (false ) ORDER BY TABLE_TYPE,TABLE_SCHEM,TABLE_NAME " at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:1512) at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1297) at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:188) at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:430) at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:332) at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:231) at org.postgresql.jdbc2.AbstractJdbc2DatabaseMetaData.getTables(AbstractJdbc2DatabaseMetaData.java:2190) I've looked at the source code for postgresql-jdbc-8.1-404 and the trouble seems to come from the escapeQuotes function in AbstractJdbc2DatabaseMetaData.java protected static String escapeQuotes(String s) { StringBuffer sb = new StringBuffer(); int length = s.length(); char prevChar = ' '; char prevPrevChar = ' '; for (int i = 0; i < length; i++) { char c = s.charAt(i); sb.append(c); if (c == '\'' && (prevChar != '\\' || (prevChar == '\\' && prevPrevChar == '\\'))) { sb.append("'"); } prevPrevChar = prevChar; prevChar = c; } return sb.toString(); } I believe escapeQuotes only checks for single quotes, and does nothing to backslashes. My temporary solution has been to patch escapeQuotes as follows: protected static String escapeQuotes(String s) { StringBuffer sb = new StringBuffer(); int length = s.length(); char prevChar = ' '; char prevPrevChar = ' '; for (int i = 0; i < length; i++) { char c = s.charAt(i); if ( c == '\\' || c == '\'' ) sb.append('\\'); sb.append(c); } return sb.toString(); } ... which seems to work. Regards Paolo --=-c1LBxGOK545eDi2AF+AK Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 7bit Hi everybody!
There is a particular type of input that causes trouble when calling DatabaseMetaData's methods such as getTables(), ecc.

The input I'm talking about are strings containing backslash characters in situations like the following:
dbmd.getTables(null, null, "my\\table", types);

The result is generally a wrong answer from the getTables method or, in the worst situation where the backslash is the last character, a PSQLException.

Some sample code:

    public void testPostgresQuote() throws Exception {
        System.out.println("testPostgresQuote:");
        Connection conn = null;
        try {
            String[] types = {"TABLE"};
            conn = ConnectionFactory.getConnection();
            DatabaseMetaData dbmd = conn.getMetaData();
            dbmd.getTables(null, null, "\\", types);
        } finally {
            if (conn != null) {
                try {conn.close();} catch (Exception e2) {}
            }
        }
    }

this causes a nasty exception (and opens a possibility for SQL injection?):

ERROR: unterminated quoted string at or near "'\'  AND (false )  ORDER BY TABLE_TYPE,TABLE_SCHEM,TABLE_NAME "
org.postgresql.util.PSQLException: ERROR: unterminated quoted string at or near "'\'  AND (false )  ORDER BY TABLE_TYPE,TABLE_SCHEM,TABLE_NAME "
        at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:1512)
        at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1297)
        at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:188)
        at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:430)
        at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:332)
        at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:231)
        at org.postgresql.jdbc2.AbstractJdbc2DatabaseMetaData.getTables(AbstractJdbc2DatabaseMetaData.java:2190)


I've looked at the source code for  postgresql-jdbc-8.1-404 and the trouble seems to come from the escapeQuotes function in AbstractJdbc2DatabaseMetaData.java

    protected static String escapeQuotes(String s) {
        StringBuffer sb = new StringBuffer();
        int length = s.length();
        char prevChar = ' ';
        char prevPrevChar = ' ';
        for (int i = 0; i < length; i++)
        {
            char c = s.charAt(i);
            sb.append(c);
            if (c == '\'' && (prevChar != '\\' || (prevChar == '\\' && prevPrevChar == '\\')))
            {
                sb.append("'");
            }
            prevPrevChar = prevChar;
            prevChar = c;
        }
        return sb.toString();
    }


I believe escapeQuotes only checks for single quotes, and does nothing to backslashes.
My temporary solution has been to patch escapeQuotes as follows:

    protected static String escapeQuotes(String s) {
        StringBuffer sb = new StringBuffer();
        int length = s.length();
        char prevChar = ' ';
        char prevPrevChar = ' ';
        for (int i = 0; i < length; i++)
        {
            char c = s.charAt(i);
            if ( c == '\\' || c == '\'' )
                sb.append('\\');

            sb.append(c);
        }
        return sb.toString();
    }

... which seems to work.

Regards

Paolo

--=-c1LBxGOK545eDi2AF+AK--