Skip to content

[JDBC-V2] Make DatabaseMetaDataImpl use PreparedStatement. #2739

Open

Description

@chernser

Description

There are many places in DatabaseMetaDataImpl that construct a dynamic query. It is wrong because leads to a security issue.

  • All queries should use PreparedStatement
  • Where is it no possible - should be reviewed and needed escaping should be added

Activity

  1. added this to the 0.9.8 milestone on Feb 9, 2026
  2. added theissue type on Feb 9, 2026
  3. added
    jdbc
    jdbc-metadata
    on Feb 9, 2026
  4. modified the milestones: 0.9.8, 0.9.9 on Mar 16, 2026
  5. added
    queries
    on Nov 26, 2026
  6. polyglotAI-bot commented on Nov 28, 2026

    @polyglotAI-bot
    Collaborator

    Recommendation: Close — already fixed
    #3171 binds the patterns of getSchemas, getPrimaryKeys and getFunctions as PreparedStatement parameters (first release v0.11.0). On main, the other metadata methods also bind or escape the values of the caller.

    Analysis: On main, the values of the caller go into the SQL as parameters (DatabaseMetaDataImpl.java:1069-1070, :1312-1313, :1871, :1927), or escaped with SQLUtils.enquoteLiteral (getColumns with jdbc_metadata_use_show_statements=false, DatabaseMetaDataImpl.java:1177-1179). The SHOW path binds the LIKE pattern and quotes the names (ShowStatementsMetaData.java:216, :339). The engine list at DatabaseMetaDataImpl.java:1023 comes from the driver, not from the caller.

    Reproduction: complete program and output
    import java.sql.Connection;
    import java.sql.DatabaseMetaData;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.util.Properties;
    
    public class Repro {
        static final String URL = System.getenv().getOrDefault("CLICKHOUSE_URL", "http://localhost:8123");
        static final String PASSWORD = System.getenv().getOrDefault("CLICKHOUSE_PASSWORD", "");
        // Matches nothing while it stays inside its literal, and everything when it leaves the literal.
        static final String P = "nomatch' OR '1'='1";
    
        interface Call { ResultSet run(DatabaseMetaData md) throws Exception; }
    
        static String count(DatabaseMetaData md, String name, Call c) {
            try (ResultSet rs = c.run(md)) {
                int n = 0;
                while (rs.next()) n++;
                return " " + name + "=" + n;
            } catch (Exception e) {
                return " " + name + "=" + e.getClass().getSimpleName() + ": " + e.getMessage();
            }
        }
    
        public static void main(String[] args) throws Exception {
            Properties props = new Properties();
            props.setProperty("user", "default");
            props.setProperty("password", PASSWORD);
            String base = "jdbc:clickhouse:" + URL.replaceFirst("^https?:", "") + "/default";
            try (Connection conn = DriverManager.getConnection(base, props); Statement s = conn.createStatement()) {
                s.execute("CREATE DATABASE IF NOT EXISTS triage2739");
                s.execute("CREATE TABLE IF NOT EXISTS triage2739.t (id UInt32) ENGINE = MergeTree ORDER BY id");
            }
            for (String show : new String[] {"true", "false"}) {
                try (Connection conn = DriverManager.getConnection(base + "?jdbc_metadata_use_show_statements=" + show, props)) {
                    DatabaseMetaData md = conn.getMetaData();
                    System.out.println("show_statements=" + show + ", rows:"
                            + count(md, "getSchemas", m -> m.getSchemas(null, P))
                            + count(md, "getTables(schema)", m -> m.getTables(null, P, "%", null))
                            + count(md, "getTables(table)", m -> m.getTables(null, "triage2739", P, null))
                            + count(md, "getColumns", m -> m.getColumns(null, "triage2739", "t", P))
                            + count(md, "getPrimaryKeys", m -> m.getPrimaryKeys(null, P, "t"))
                            + count(md, "getFunctions", m -> m.getFunctions(null, null, P))
                            + count(md, "contrast getColumns(%)", m -> m.getColumns(null, "triage2739", "t", "%")));
                }
            }
            try (Connection conn = DriverManager.getConnection(base, props); Statement s = conn.createStatement()) {
                s.execute("DROP DATABASE IF EXISTS triage2739");
            }
        }
    }

    Output on main (8480ca001), ClickHouse 26.11.2.4:

    show_statements=true, rows: getSchemas=0 getTables(schema)=0 getTables(table)=0 getColumns=0 getPrimaryKeys=0 getFunctions=0 contrast getColumns(%)=1
    show_statements=false, rows: getSchemas=0 getTables(schema)=0 getTables(table)=0 getColumns=0 getPrimaryKeys=0 getFunctions=0 contrast getColumns(%)=1
    

    Output on v0.10.0 (Maven Central), ClickHouse 26.11.2.4. v0.10.0 does not have jdbc_metadata_use_show_statements, so the loop runs once without it, with compress=0 (#3107):

    show_statements=(none), rows: getSchemas=6 getTables(schema)=0 getTables(table)=0 getColumns=0 getPrimaryKeys=1 getFunctions=2033 contrast getColumns(%)=1
    

    Automated triage by Polyglot AI. A maintainer makes the final decision.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugjdbcjdbc-metadataqueries

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions