CVE-2022-31197: Postgresql SQL Injection Vulnerability

HIGH
Advisory Identifier: GHSA-r38f-c4h4-hqq2
CVSS 7.1
CWE-89
Published
Advisory Summary

CVE-2022-31197 is a HIGH (CVSS 7.1) sql injection vulnerability (CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection')) affecting Postgresql libraries in the Maven ecosystem. The issue involves postgresql jdbc driver sql injection in resultset.refreshrow() with malicious column names. Remediation is available by upgrading affected packages to version 42.2.26, 42.4.1, 42.3.7.

CVE-2022-31197 at a Glance

CVE Identifier CVE-2022-31197
GHSA Identifier GHSA-r38f-c4h4-hqq2
Severity HIGH
CVSS v3.1 Base Score 7.1
CWE Classification CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection')
Affected Product Postgresql
Package Ecosystem Maven
Published Date
Last Updated

Security Impact

CVSS: 7.1

The security impact metrics below are parsed directly from the CVSS v3.1 vector string (CVSS:3.1/AV:N/AC:H/PR:L/UI:R/S:U/C:H/I:H/A:H).

Attack Vector Network (AV:N)
Attack Complexity High (AC:H)
Privileges Required Low (PR:L)
User Interaction Required (UI:R)
Scope Unchanged (S:U)
Confidentiality High (C:H)
Integrity High (I:H)
Availability High (A:H)

How to Fix CVE-2022-31197

To remediate this vulnerability, upgrade all affected Postgresql dependencies to a patched release. Both direct and transitive dependencies should be checked, as frameworks or security libraries may pull in vulnerable versions transitively.

org.postgresql:postgresql fixed: 42.2.26
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.2.26</version>
</dependency>
org.postgresql:postgresql fixed: 42.4.1
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.4.1</version>
</dependency>
org.postgresql:postgresql fixed: 42.3.7
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.3.7</version>
</dependency>
Verify Direct and Transitive Dependencies

Run the following command in your project directory to verify that all resolved dependencies use the patched release:

mvn dependency:tree -Dincludes=org.postgresql

How to Check if You Are Affected

To determine whether your Java application is affected by CVE-2022-31197, inspect your build dependencies using the Maven Dependency Plugin:

mvn dependency:tree -Dincludes=org.postgresql

Analyze the terminal output to locate any version of Postgresql in your dependency tree. If any resolved version matches the vulnerable ranges listed in the table below (for example, versions earlier than 42.2.26), your application is affected.

Note: The mvn dependency:tree -Dincludes=org.postgresql command inspects the resolved dependency graph declared in your project's POM files; it does not perform automated security scanning or inspect runtime dynamic classpaths. Always verify runtime container classpaths and shaded JARs.

Technical Details

Vulnerability Mechanism & Technical Analysis

This vulnerability is classified under CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection'), where the product constructs all or part of an sql command using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the intended sql command when it is sent to a downstream component. without sufficient removal or quoting of sql syntax in user-controllable inputs, the generated sql query can cause those inputs to be interpreted as sql instead of ordinary user data. In Postgresql, software state validation or input boundaries are circumvented during processing. Review the official source advisory statement below for specific affected components, classes, and reproduction details.

Source Advisory Statement

### 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 refresh_row_example ( id int PRIMARY KEY, "1 FROM refresh_row_example; SELECT pg_sleep(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 refresh_row`, would cause the additional SQL commands such as the `SELECT pg_sleep(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.

CVE-2022-31197 Affected Postgresql Versions

3 component(s)
Fixed Versions:
42.2.2642.4.142.3.7
Product / Package Ecosystem Affected Versions Fixed Version
org.postgresql:postgresql
Maven
< 42.2.26
42.2.26
org.postgresql:postgresql
Maven
≥ 42.4.0
< 42.4.1
42.4.1
org.postgresql:postgresql
Maven
≥ 42.3.0
< 42.3.7
42.3.7

References

Frequently Asked Questions

What is CVE-2022-31197?
CVE-2022-31197 is a security vulnerability in Postgresql involving PostgreSQL JDBC Driver SQL Injection in ResultSet.refreshRow() with malicious column names under CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection').
Which Postgresql versions are affected?
Affected versions include releases prior to 42.2.26, 42.4.1, 42.3.7 across affected packages: org.postgresql:postgresql.
What version fixes CVE-2022-31197?
Upgrading to 42.2.26, 42.4.1, 42.3.7 (or the latest patched release for your respective branch) resolves the vulnerability.
Is postgresql affected?
Yes, org.postgresql:postgresql is affected in releases prior to 42.2.26, 42.4.1, 42.3.7. Upgrade your dependency to 42.2.26, 42.4.1, 42.3.7 or later.
What is the CVSS score of CVE-2022-31197?
CVE-2022-31197 has a CVSS v3.1 base score of 7.1 (HIGH) with vector string CVSS:3.1/AV:N/AC:H/PR:L/UI:R/S:U/C:H/I:H/A:H.
What CWE is associated with CVE-2022-31197?
CVE-2022-31197 is classified under CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection') (The product constructs all or part of an SQL command using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the intended SQL command when it is sent to a downstream component. Without sufficient removal or quoting of SQL syntax in user-controllable inputs, the generated SQL query can cause those inputs to be interpreted as SQL instead of ordinary user data.).
How can I check Maven dependencies for CVE-2022-31197?
Run the dependency tree command: 'mvn dependency:tree -Dincludes=org.postgresql'. This command lists direct and transitive dependencies in your build configuration so you can verify if a vulnerable version is resolved.
Machine-readable OSV JSON OSV v1.5 Schema
Normalized schema output
{
  "schema_version" : "1.4.0",
  "id" : "GHSA-r38f-c4h4-hqq2",
  "modified" : "2025-11-04T16:36:10Z",
  "published" : "2022-08-06T05:51:38Z",
  "aliases" : [ "CVE-2022-31197" ],
  "summary" : "PostgreSQL JDBC Driver SQL Injection in ResultSet.refreshRow() with malicious column names",
  "details" : "### Impact\n_What kind of vulnerability is it? Who is impacted?_\n\nThe 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.\n\nUser applications that do not invoke the `ResultSet.refreshRow()` method are not impacted.\n\nUser 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.\n\nFor example:\n\n```sql\nCREATE TABLE refresh_row_example (\n  id     int PRIMARY KEY,\n  \"1 FROM refresh_row_example; SELECT pg_sleep(10); SELECT * \" int\n);\n```\n\nThis 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 refresh_row`, would cause the additional SQL commands such as the `SELECT pg_sleep(10)` invocation to be executed.\n\nAs 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.\n\nNote 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.\n\n### Patches\n_Has the problem been patched? What versions should users upgrade to?_\n\nYes, versions 42.2.26, 42.3.7, and 42.4.1 have been released with a fix.\n\n### Workarounds\n_Is there a way for users to fix or remediate the vulnerability without upgrading?_\n\nCheck that you are not using the `ResultSet.refreshRow()` method.\n\nIf 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.",
  "severity" : [ {
    "type" : "CVSS_V3",
    "score" : "CVSS:3.1/AV:N/AC:H/PR:L/UI:R/S:U/C:H/I:H/A:H"
  } ],
  "affected" : [ {
    "package" : {
      "ecosystem" : "Maven",
      "name" : "org.postgresql:postgresql"
    },
    "ranges" : [ {
      "type" : "ECOSYSTEM",
      "events" : [ {
        "introduced" : "0"
      }, {
        "fixed" : "42.2.26"
      } ]
    } ]
  }, {
    "package" : {
      "ecosystem" : "Maven",
      "name" : "org.postgresql:postgresql"
    },
    "ranges" : [ {
      "type" : "ECOSYSTEM",
      "events" : [ {
        "introduced" : "42.4.0"
      }, {
        "fixed" : "42.4.1"
      } ]
    } ]
  }, {
    "package" : {
      "ecosystem" : "Maven",
      "name" : "org.postgresql:postgresql"
    },
    "ranges" : [ {
      "type" : "ECOSYSTEM",
      "events" : [ {
        "introduced" : "42.3.0"
      }, {
        "fixed" : "42.3.7"
      } ]
    } ]
  } ],
  "references" : [ {
    "type" : "WEB",
    "url" : "https://github.com/pgjdbc/pgjdbc/security/advisories/GHSA-r38f-c4h4-hqq2"
  }, {
    "type" : "ADVISORY",
    "url" : "https://nvd.nist.gov/vuln/detail/CVE-2022-31197"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/pgjdbc/pgjdbc/commit/739e599d52ad80f8dcd6efedc6157859b1a9d637"
  }, {
    "type" : "PACKAGE",
    "url" : "https://github.com/pgjdbc/pgjdbc"
  }, {
    "type" : "WEB",
    "url" : "https://lists.debian.org/debian-lts-announce/2022/10/msg00009.html"
  }, {
    "type" : "WEB",
    "url" : "https://lists.debian.org/debian-lts-announce/2024/12/msg00017.html"
  }, {
    "type" : "WEB",
    "url" : "https://lists.fedoraproject.org/archives/list/package-announce%40lists.fedoraproject.org/message/I6WHUADTZBBQLVHO4YG4XCWDGWBT4LRP"
  }, {
    "type" : "WEB",
    "url" : "https://lists.fedoraproject.org/archives/list/package-announce%40lists.fedoraproject.org/message/UTFE6SV33P5YYU2GNTQZQKQRVR3GYE4S"
  }, {
    "type" : "WEB",
    "url" : "https://lists.fedoraproject.org/archives/list/package-announce@lists.fedoraproject.org/message/I6WHUADTZBBQLVHO4YG4XCWDGWBT4LRP"
  }, {
    "type" : "WEB",
    "url" : "https://lists.fedoraproject.org/archives/list/package-announce@lists.fedoraproject.org/message/UTFE6SV33P5YYU2GNTQZQKQRVR3GYE4S"
  } ],
  "database_specific" : {
    "cwe_ids" : [ "CWE-89" ],
    "severity" : "HIGH",
    "github_reviewed" : true,
    "github_reviewed_at" : "2022-08-06T05:51:38Z",
    "nvd_published_at" : "2022-08-03T19:15:00Z"
  }
}