This is an automated email from the ASF dual-hosted git repository. rmaucher pushed a commit to branch 10.1.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit f4211c4e7d7f1f85eaeb073a206b0e3ea28b62dc Author: opencode <[email protected]> AuthorDate: Tue Dec 1 21:31:06 2026 +0100 Clear credentials when releasing a failed connection A failed connect marks the PooledConnection discarded and leaves its session fields null. The terminal release's disconnect(true) then returned early before removing the cached credentials, leaving the plaintext user name and password in a released object. Move the terminal credential removal before the early return so a released connection never keeps them. Add a regression test covering a failed connect followed by release. mvn test passes: 1183 tests, 0 failures. --- .../apache/tomcat/jdbc/pool/PooledConnection.java | 16 ++++++------ .../PooledConnectionConfigurationReadsBase.java | 30 ++++++++++++++++++++++ 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/PooledConnection.java b/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/PooledConnection.java index f11a101b31..8890020fe9 100644 --- a/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/PooledConnection.java +++ b/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/PooledConnection.java @@ -1113,6 +1113,14 @@ public class PooledConnection implements PooledConnectionMBean { java.sql.Connection session; javax.sql.XAConnection xa; javax.sql.PooledConnection cpds; + if (finalize) { + // The terminal notification clears the cached credentials even + // when there is no session left to close below. A failed connect + // marks the connection discarded with all session fields null, so + // the early return in the monitor would otherwise skip this block. + attributes.remove(PROP_USER); + attributes.remove(PROP_PASSWORD); + } // Take the terminal session out of the fields under the monitor shared // with the release claim and the connect completion check. Only this // call captures it, so a concurrent release/connect/post-connect @@ -1151,14 +1159,6 @@ public class PooledConnection implements PooledConnectionMBean { // session even when obtaining its logical connection failed. closeSession(session, xa, cpds); } - if (finalize) { - // The terminal release ends the connection's ability to renew - // itself, so the cached credentials have no further use and must - // not stay in the attribute map of an object other code may still - // hold. - attributes.remove(PROP_USER); - attributes.remove(PROP_PASSWORD); - } // The chain's teardown reset is delivered once, by the release that // clears the handler after it has freed the pool slot - see // ConnectionPool.release(). The disconnect event above is a separate diff --git a/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/pool/PooledConnectionConfigurationReadsBase.java b/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/pool/PooledConnectionConfigurationReadsBase.java index 080edd6071..9787e53898 100644 --- a/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/pool/PooledConnectionConfigurationReadsBase.java +++ b/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/pool/PooledConnectionConfigurationReadsBase.java @@ -18,6 +18,7 @@ package org.apache.tomcat.jdbc.pool; import java.lang.reflect.Field; import java.sql.Connection; +import java.sql.SQLException; import java.util.concurrent.atomic.AtomicInteger; import org.junit.jupiter.api.Assertions; @@ -82,6 +83,35 @@ abstract class PooledConnectionConfigurationReadsBase extends JdbcPoolBaseTest { "a terminal release must not leave the cached password behind"); } + @Test + void aFailedConnectReleaseRemovesTheCachedCredentials() throws Exception { + DataSource ds = newDataSource(); + ds.createPool(); + PoolProperties failing = new PoolProperties(); + failing.setUrl("jdbc:h2:mem:missing-" + System.nanoTime() + ";IFEXISTS=TRUE"); + failing.setDriverClassName("org.h2.Driver"); + failing.setUsername("secretUser"); + failing.setPassword("secretPass"); + PooledConnection pooled = new PooledConnection(failing, ds.getPool()); + try { + Assertions.assertThrows(SQLException.class, pooled::connect, + "the missing database must fail the connect"); + Assertions.assertEquals("secretUser", pooled.getAttributes().get(PooledConnection.PROP_USER), + "precondition: the failed attempt staged the user name"); + Assertions.assertEquals("secretPass", pooled.getAttributes().get(PooledConnection.PROP_PASSWORD), + "precondition: the failed attempt staged the password"); + + pooled.release(); + + Assertions.assertFalse(pooled.getAttributes().containsKey(PooledConnection.PROP_USER), + "a terminal release must clear the cached user name of a failed connect"); + Assertions.assertFalse(pooled.getAttributes().containsKey(PooledConnection.PROP_PASSWORD), + "a terminal release must clear the cached password of a failed connect"); + } finally { + pooled.release(); + } + } + @Test void theAbandonTimeoutUsesOneConfigurationRead() throws Exception { DataSource ds = newDataSource(); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
