Skip to content

Commit

Permalink
Fixes from upstream (#20)
Browse files Browse the repository at this point in the history
* Cherry-picked 739e599 to fix CVE-2022-31197
* Fix CVE-2022-41946 from commits 9008dc9 and 9752441
  • Loading branch information
ashetkar authored Nov 16, 2023
1 parent e3728b8 commit 8ede139
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 4 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ kotlin.parallel.tasks.in.project=true
# This is version for PgJdbc itself
# Note: it should not include "-SNAPSHOT" as it is automatically added by build.gradle.kts
# Release version can be generated by using -Prelease or -Prc=<int> arguments
pgjdbc.version=42.3.5-yb-3
pgjdbc.version=42.3.5-yb-4

# The options below configures the use of local clone (e.g. testing development versions)
# You can pass un-comment it, or pass option -PlocalReleasePlugins, or -PlocalReleasePlugins=<path>
Expand Down
5 changes: 3 additions & 2 deletions pgjdbc/src/main/java/org/postgresql/jdbc/PgResultSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -1361,7 +1361,7 @@ public void refreshRow() throws SQLException {
if (i > 1) {
selectSQL.append(", ");
}
selectSQL.append(pgmd.getBaseColumnName(i));
Utils.escapeIdentifier(selectSQL, pgmd.getBaseColumnName(i));
}
selectSQL.append(" from ").append(onlyTable).append(tableName).append(" where ");

Expand All @@ -1371,7 +1371,8 @@ public void refreshRow() throws SQLException {
for (int i = 0; i < numKeys; i++) {

PrimaryKey primaryKey = primaryKeys.get(i);
selectSQL.append(primaryKey.name).append(" = ?");
Utils.escapeIdentifier(selectSQL, primaryKey.name);
selectSQL.append(" = ?");

if (i < numKeys - 1) {
selectSQL.append(" and ");
Expand Down
3 changes: 2 additions & 1 deletion pgjdbc/src/main/java/org/postgresql/util/StreamWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;

/**
* Wrapper around a length-limited InputStream.
Expand Down Expand Up @@ -51,7 +52,7 @@ public StreamWrapper(InputStream stream) throws PSQLException {

if (memoryLength == -1) {
final int diskLength;
final File tempFile = File.createTempFile(TEMP_FILE_PREFIX, null);
final File tempFile = Files.createTempFile(TEMP_FILE_PREFIX, ".tmp").toFile();
FileOutputStream diskOutputStream = new FileOutputStream(tempFile);
diskOutputStream.write(rawData);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
ReplaceProcessingTest.class,
ResultSetMetaDataTest.class,
ResultSetTest.class,
ResultSetRefreshTest.class,
ReturningParserTest.class,
SearchPathLookupTest.class,
ServerCursorTest.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2022, PostgreSQL Global Development Group
* See the LICENSE file in the project root for more information.
*/

package org.postgresql.test.jdbc2;

import static org.junit.Assert.assertTrue;

import org.postgresql.test.TestUtil;

import org.junit.Test;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ResultSetRefreshTest extends BaseTest4 {
@Test
public void testWithDataColumnThatRequiresEscaping() throws Exception {
TestUtil.dropTable(con, "refresh_row_bad_ident");
TestUtil.execute("CREATE TABLE refresh_row_bad_ident (id int PRIMARY KEY, \"1 FROM refresh_row_bad_ident; SELECT 2; SELECT *\" int)", con);
TestUtil.execute("INSERT INTO refresh_row_bad_ident (id) VALUES (1), (2), (3)", con);

Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("SELECT * FROM refresh_row_bad_ident");
assertTrue(rs.next());
try {
rs.refreshRow();
} catch (SQLException ex) {
throw new RuntimeException("ResultSet.refreshRow() did not handle escaping data column identifiers", ex);
}
rs.close();
stmt.close();
}

@Test
public void testWithKeyColumnThatRequiresEscaping() throws Exception {
TestUtil.dropTable(con, "refresh_row_bad_ident");
TestUtil.execute("CREATE TABLE refresh_row_bad_ident (\"my key\" int PRIMARY KEY)", con);
TestUtil.execute("INSERT INTO refresh_row_bad_ident VALUES (1), (2), (3)", con);

Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("SELECT * FROM refresh_row_bad_ident");
assertTrue(rs.next());
try {
rs.refreshRow();
} catch (SQLException ex) {
throw new RuntimeException("ResultSet.refreshRow() did not handle escaping key column identifiers", ex);
}
rs.close();
stmt.close();
}
}

0 comments on commit 8ede139

Please sign in to comment.