CVE-2024-1597: pgjdbc SQL Injection via line comment generation

Published Feb 19, 2024
·
Updated

Impact SQL injection is possible when using the non-default connection property preferQueryMode=simple in combination with application code that has a vulnerable SQL that negates a parameter value.

There is no vulnerability in the driver when using the default query mode. Users that do not override the query mode are not impacted.

Exploitation

To exploit this behavior the following conditions must be met:

1. A placeholder for a numeric value must be immediately preceded by a minus (i.e. -) 1. There must be a second placeholder for a string value after the first placeholder on the same line. 1. Both parameters must be user controlled.

The prior behavior of the driver when operating in simple query mode would inline the negative value of the first parameter and cause the resulting line to be treated as a -- SQL comment. That would extend to the beginning of the next parameter and cause the quoting of that parameter to be consumed by the comment line. If that string parameter includes a newline, the resulting text would appear unescaped in the resulting SQL.

When operating in the default extended query mode this would not be an issue as the parameter values are sent separately to the server. Only in simple query mode the parameter values are inlined into the executed SQL causing this issue.

Example

java PreparedStatement stmt = conn.prepareStatement("SELECT -?, ?"); stmt.setInt(1, -1); stmt.setString(2, "\nWHERE false --"); ResultSet rs = stmt.executeQuery();

The resulting SQL when operating in simple query mode would be:

sql SELECT --1,' WHERE false --'

The contents of the second parameter get injected into the command. Note how both the number of result columns and the WHERE clause of the command have changed. A more elaborate example could execute arbitrary other SQL commands.

Patch Problem will be patched upgrade to 42.7.2, 42.6.1, 42.5.5, 42.4.4, 42.3.9, 42.2.28, 42.2.28.jre7

The patch fixes the inlining of parameters by forcing them all to be serialized as wrapped literals. The SQL in the prior example would be transformed into:

sql SELECT -('-1'::int4), (' WHERE false --')

Workarounds Do not use the connection propertypreferQueryMode=simple. (NOTE: If you do not explicitly specify a query mode then you are using the default of extended and are not impacted by this issue.)

Other sources

Duplicate Advisory This advisory has been withdrawn because it is a duplicate of GHSA-24rp-q3w6-vc56. This link is maintained to preserve external references.

Original Description pgjdbc, the PostgreSQL JDBC Driver, allows attacker to inject SQL if using PreferQueryMode=SIMPLE. Note this is not the default. In the default mode there is no vulnerability. A placeholder for a numeric value must be immediately preceded by a minus. There must be a second placeholder for a string value after the first placeholder; both must be on the same line. By constructing a matching string payload, the attacker can inject SQL to alter the query,bypassing the protections that parameterized queries bring against SQL Injection attacks. Versions before 42.7.2, 42.6.1, 42.5.5, 42.4.4, 42.3.9, and 42.2.8 are affected.

GitHub

pgjdbc, the PostgreSQL JDBC Driver, allows attacker to inject SQL if using PreferQueryMode=SIMPLE. Note this is not the default. In the default mode there is no vulnerability. A placeholder for a numeric value must be immediately preceded by a minus. There must be a second placeholder for a string value after the first placeholder; both must be on the same line. By constructing a matching string payload, the attacker can inject SQL to alter the query,bypassing the protections that parameterized queries bring against SQL Injection attacks. Versions before 42.7.2, 42.6.1, 42.5.5, 42.4.4, 42.3.9, and 42.2.28 are affected.

NVD

pgjdbc, the PostgreSQL JDBC Driver, allows attacker to inject SQL if using PreferQueryMode=SIMPLE. Note this is not the default. In the default mode there is no vulnerability. A placeholder for a numeric value must be immediately preceded by a minus. There must be a second placeholder for a string value after the first placeholder; both must be on the same line. By constructing a matching string payload, the attacker can inject SQL to alter the query,bypassing the protections that parameterized queries bring against SQL Injection attacks. Versions before 42.7.2, 42.6.1, 42.5.5, 42.4.4, 42.3.9, and 42.2.8 are affected.

https://github.com/pgjdbc/pgjdbc/security/advisories/GHSA-24rp-q3w6-vc56 https://www.enterprisedb.com/docs/jdbcconnector/latest/01jdbcrelnotes/ https://www.enterprisedb.com/docs/security/assessments/cve-2024-1597/

Red Hat

Affected Software

22 affected componentsFixes available
maven/org.postgresql:postgresql>=42.7.0<42.7.2
42.7.2
maven/org.postgresql:postgresql>=42.6.0<42.6.1
42.6.1
maven/org.postgresql:postgresql>=42.5.0<42.5.5
42.5.5
maven/org.postgresql:postgresql>=42.4.0<42.4.4
42.4.4
maven/org.postgresql:postgresql>=42.3.0<42.3.9
42.3.9
maven/org.postgresql:postgresql<42.2.28
42.2.28
maven/org.postgresql:postgresql<42.2.8
42.2.8
PostgreSQL PostgreSQL JDBC driver<42.2.28
PostgreSQL PostgreSQL JDBC driver>=42.3.0<42.3.9
PostgreSQL PostgreSQL JDBC driver>=42.4.0<42.4.4
PostgreSQL PostgreSQL JDBC driver>=42.5.0<42.5.5
PostgreSQL PostgreSQL JDBC driver>=42.6.0<42.6.1
PostgreSQL PostgreSQL JDBC driver>=42.7.0<42.7.2
Fedoraproject Fedora=40
redhat/PostgreSQL JDBC Driver<42.7.2
42.7.2
redhat/PostgreSQL JDBC Driver<42.6.1
42.6.1
redhat/PostgreSQL JDBC Driver<42.5.5
42.5.5
redhat/PostgreSQL JDBC Driver<42.4.4
42.4.4
redhat/PostgreSQL JDBC Driver<42.3.9
42.3.9
redhat/PostgreSQL JDBC Driver<42.2.28
42.2.28
redhat/PostgreSQL JDBC Driver<42.2.28.
42.2.28.
IBM Cognos Analytics Certified Containers<=12.1.0

Event History

Feb 19, 2024
CVE Published
via MITRE·12:58 PM
Data Sourced
via MITRE·12:58 PM
DescriptionSeverityWeakness
Data Sourced
via NVD·01:15 PM
DescriptionSeverityWeaknessAffected Software
Advisory Published
via GitHub·03:30 PM
Feb 21, 2024
Withdrawn
via GitHub·11:18 PM
Feb 28, 2024
Data Sourced
via Red Hat·04:38 AM
DescriptionSeverityAffected Software
Mar 25, 2024
News Published
via The Register·01:15 AM
News Published
via The Register·01:18 AM
Jun 20, 2024
News Published
via BleepingComputer·12:43 AM
News Published
via BleepingComputer·12:43 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 the severity of CVE-2024-1597?

The severity of CVE-2024-1597 is classified as high due to the potential for SQL injection.

2

How do I fix CVE-2024-1597?

To fix CVE-2024-1597, upgrade to PostgreSQL JDBC Driver version 42.7.2 or later.

3

What applications are affected by CVE-2024-1597?

Applications using PostgreSQL JDBC Driver versions prior to 42.7.2 with preferQueryMode=simple are affected by CVE-2024-1597.

4

Can CVE-2024-1597 be exploited remotely?

Yes, CVE-2024-1597 can potentially be exploited remotely if vulnerable SQL code is exposed to user input.

5

What types of attacks can CVE-2024-1597 enable?

CVE-2024-1597 enables SQL injection attacks, which can lead to unauthorized access to database information.

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