CVE-2022-31197: SQL Injection in ResultSet.refreshRow() with malicious column names in pgjdbc

Published Aug 3, 2022
·
Updated

Impact What kind of vulnerability is it? Who is impacted?

The PGJDBC implementation of the java.sql.ResultRow.refreshRow() method is not performing escaping of column names so a malicious column name that contains a statement terminator, e.g. ;, could lead to SQL injection. This could lead to executing additional SQL commands as the application's JDBC user.

User applications that do not invoke the ResultSet.refreshRow() method are not impacted.

User application that do invoke that method are impacted if the underlying database that they are querying via their JDBC application may be under the control of an attacker. The attack requires the attacker to trick the user into executing SQL against a table name who's column names would contain the malicious SQL and subsequently invoke the refreshRow() method on the ResultSet.

For example:

sql CREATE TABLE refreshrowexample ( id int PRIMARY KEY, "1 FROM refreshrowexample; SELECT pgsleep(10); SELECT " int );

This example has a table with two columns. The name of the second column is crafted to contain a statement terminator followed by additional SQL. Invoking the ResultSet.refreshRow() on a ResultSet that queried this table, e.g. SELECT FROM refreshrow, would cause the additional SQL commands such as the SELECT pgsleep(10) invocation to be executed.

As the multi statement command would contain multiple results, it would not be possible for the attacker to get data directly out of this approach as the ResultSet.refreshRow() method would throw an exception. However, the attacker could execute any arbitrary SQL including inserting the data into another table that could then be read or any other DML / DDL statement.

Note that the application's JDBC user and the schema owner need not be the same. A JDBC application that executes as a privileged user querying database schemas owned by potentially malicious less-privileged users would be vulnerable. In that situation it may be possible for the malicious user to craft a schema that causes the application to execute commands as the privileged user.

Patches Has the problem been patched? What versions should users upgrade to?

Yes, versions 42.2.26, 42.3.7, and 42.4.1 have been released with a fix.

Workarounds Is there a way for users to fix or remediate the vulnerability without upgrading?

Check that you are not using the ResultSet.refreshRow() method.

If you are, ensure that the code that executes that method does not connect to a database that is controlled by an unauthenticated or malicious user. If your application only connects to its own database with a fixed schema with no DDL permissions, then you will not be affected by this vulnerability as it requires a maliciously crafted schema.

Other sources

A flaw was found in PostgresQL. This flaw allows an attacker to benefit from a miss escaping character and leads to a SQL injection attack due to Java.sql.ResultRow.refreshRow() implementation from PGSQL.

PostgreSQL JDBC Driver (PgJDBC for short) allows Java programs to connect to a PostgreSQL database using standard, database independent Java code. The PGJDBC implementation of the java.sql.ResultRow.refreshRow() method is not performing escaping of column names so a malicious column name that contains a statement terminator, e.g. ;, could lead to SQL injection. This could lead to executing additional SQL commands as the application's JDBC user. User applications that do not invoke the ResultSet.refreshRow() method are not impacted. User application that do invoke that method are impacted if the underlying database that they are querying via their JDBC application may be under the control of an attacker. The attack requires the attacker to trick the user into executing SQL against a table name who's column names would contain the malicious SQL and subsequently invoke the refreshRow() method on the ResultSet. Note that the application's JDBC user and the schema owner need not be the same. A JDBC application that executes as a privileged user querying database schemas owned by potentially malicious less-privileged users would be vulnerable. In that situation it may be possible for the malicious user to craft a schema that causes the application to execute commands as the privileged user. Patched versions will be released as 42.2.26 and 42.4.1. Users are advised to upgrade. There are no known workarounds for this issue.

Affected Software

16 affected componentsFixes available
redhat/postgresql-jdbc<0:42.2.18-6.el9_1
0:42.2.18-6.el9_1
maven/org.postgresql:postgresql>=42.3.0<42.3.7
42.3.7
maven/org.postgresql:postgresql>=42.4.0<42.4.1
42.4.1
maven/org.postgresql:postgresql<42.2.26
42.2.26
redhat/postgresql-jdbc<42.2.26
42.2.26
redhat/postgresql-jdbc<42.3.7
42.3.7
redhat/postgresql-jdbc<42.4.1
42.4.1
PostgreSQL PostgreSQL JDBC driver<42.2.26
PostgreSQL PostgreSQL JDBC driver>=42.3.0<42.3.7
PostgreSQL PostgreSQL JDBC driver=42.4.0
PostgreSQL PostgreSQL JDBC driver=42.4.0-rc1
PostgreSQL PostgreSQL JDBC driver=42.4.1-rc1
Debian Debian Linux=10.0
Fedoraproject Fedora=35
Fedoraproject Fedora=36
IBM Cognos Analytics Certified Containers<=12.1.0

Event History

Aug 3, 2022
CVE Published
via MITRE·12:00 AM
Data Sourced
via MITRE·12:00 AM
DescriptionSeverityWeakness
Data Sourced
via NVD·07:15 PM
RemedyDescriptionSeverityWeaknessAffected Software
Aug 6, 2022
Advisory Published
05:51 AM
Nov 7, 2025
Data Sourced
via IBM·12:00 AM
DescriptionAffected Software

Parent advisories

This vulnerability appears in the following advisories.

Free Weekly Intel

Don't miss critical vulnerabilities

Join thousands of security professionals who receive our weekly digest of trending CVEs, zero-days, and exploited vulnerabilities.

No spam. Unsubscribe anytime.

Frequently Asked Questions

1

What is CVE-2022-31197?

CVE-2022-31197 is a vulnerability in the PostgreSQL JDBC Driver (PgJDBC) that allows an attacker to exploit a miss escaping character issue in the `java.sql.ResultRow.refreshRow()` method.

2

What is the severity of CVE-2022-31197?

CVE-2022-31197 has a severity level of high.

3

How does CVE-2022-31197 affect PostgreSQL JDBC Driver?

CVE-2022-31197 affects the PgJDBC implementation's `java.sql.ResultRow.refreshRow()` method and its handling of column names.

4

Which versions of PostgreSQL JDBC Driver are affected by CVE-2022-31197?

Versions 0:42.2.18-6.el9_1, 42.2.26, 42.3.7, and 42.4.1 of PostgreSQL JDBC Driver are affected by CVE-2022-31197.

5

How can I mitigate CVE-2022-31197?

To mitigate CVE-2022-31197, update to version 42.4.1 (or later) of PostgreSQL JDBC Driver.

Contact

SecAlerts Pty Ltd.
132 Wickham Terrace
Fortitude Valley,
QLD 4006, Australia
info@secalerts.co
By using SecAlerts services, you agree to our services end-user license agreement. This website is safeguarded by reCAPTCHA and governed by the Google Privacy Policy and Terms of Service. All names, logos, and brands of products are owned by their respective owners, and any usage of these names, logos, and brands for identification purposes only does not imply endorsement. If you possess any content that requires removal, please get in touch with us.
© 2026 SecAlerts Pty Ltd.
ABN: 70 645 966 203, ACN: 645 966 203