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 4f0d982925b089bd256c3b2c57c1cbc035c42c62 Author: opencode <[email protected]> AuthorDate: Tue Dec 1 21:25:30 2026 +0100 Reject non-PoolProperties configurations before validation ConnectionPool and DataSourceProxy accepted any PoolConfiguration, but snapshot creation only supports PoolProperties. A custom implementation was therefore accepted, inspected and potentially corrected by checkPoolConfiguration(), then failed later with an internal error. Reject non-PoolProperties configurations at construction or set time with a clear message, before validation, and document the requirement. Add regression coverage for the constructor, setter, and ConnectionPool entry points, including a configuration that validation would otherwise modify. mvn test passes: 1182 tests, 0 failures. --- .../apache/tomcat/jdbc/pool/ConnectionPool.java | 8 +++-- .../apache/tomcat/jdbc/pool/DataSourceProxy.java | 17 +++++++++-- .../pool/DataSourceProxyPoolPropertiesBase.java | 35 ++++++++++++++++++++++ 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/ConnectionPool.java b/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/ConnectionPool.java index c9461be87c..7a95ab3ff1 100644 --- a/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/ConnectionPool.java +++ b/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/ConnectionPool.java @@ -283,14 +283,18 @@ public class ConnectionPool { * @param prop PoolProperties - all the properties for this connection pool * * @throws SQLException Pool initialization error - * @throws IllegalArgumentException if {@code prop} is a display-only bean/JMX configuration view instead of the - * underlying {@link PoolProperties} + * @throws IllegalArgumentException if {@code prop} is a display-only bean/JMX configuration view, or is not a + * {@link PoolProperties} implementation */ public ConnectionPool(PoolConfiguration prop) throws SQLException { if (prop instanceof DataSourceProxy || prop instanceof org.apache.tomcat.jdbc.pool.jmx.ConnectionPool) { throw new IllegalArgumentException( MessageFactory.get("poolProperties.displayConfigurationNotAllowed")); } + if (!(prop instanceof PoolProperties)) { + throw new IllegalArgumentException(MessageFactory.get( + "connectionPool.unsupportedConfiguration", prop.getClass().getName())); + } init(prop); } diff --git a/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/DataSourceProxy.java b/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/DataSourceProxy.java index bbc4621c2f..ba4140e5c4 100644 --- a/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/DataSourceProxy.java +++ b/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/DataSourceProxy.java @@ -97,7 +97,11 @@ public class DataSourceProxy implements PoolConfiguration { /** * Construct a DataSourceProxy with the given pool configuration. * - * @param poolProperties The pool configuration properties + * @param poolProperties The pool configuration properties, which must be a {@link PoolProperties} implementation + * + * @throws NullPointerException if the configuration is null + * @throws IllegalArgumentException if the configuration is a display-only bean/JMX view, or is not a + * {@link PoolProperties} implementation */ public DataSourceProxy(PoolConfiguration poolProperties) { if (poolProperties == null) { @@ -108,6 +112,10 @@ public class DataSourceProxy implements PoolConfiguration { throw new IllegalArgumentException( MessageFactory.get("poolProperties.displayConfigurationNotAllowed")); } + if (!(poolProperties instanceof PoolProperties)) { + throw new IllegalArgumentException(MessageFactory.get( + "connectionPool.unsupportedConfiguration", poolProperties.getClass().getName())); + } this.poolProperties = poolProperties; } @@ -644,7 +652,8 @@ public class DataSourceProxy implements PoolConfiguration { * @param poolProperties The new pool configuration * * @throws NullPointerException if the configuration is null - * @throws IllegalArgumentException if the configuration is a display-only bean/JMX view + * @throws IllegalArgumentException if the configuration is a display-only bean/JMX view, or is not a + * {@link PoolProperties} implementation * @throws UnsupportedOperationException if the pool is being created, has already been created or this data source * has been closed */ @@ -657,6 +666,10 @@ public class DataSourceProxy implements PoolConfiguration { throw new IllegalArgumentException( MessageFactory.get("poolProperties.displayConfigurationNotAllowed")); } + if (!(poolProperties instanceof PoolProperties)) { + throw new IllegalArgumentException(MessageFactory.get( + "connectionPool.unsupportedConfiguration", poolProperties.getClass().getName())); + } checkPoolNotStarted("poolProperties"); this.poolProperties = poolProperties; } diff --git a/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/pool/DataSourceProxyPoolPropertiesBase.java b/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/pool/DataSourceProxyPoolPropertiesBase.java index 65b710db54..dfeb4edcb2 100644 --- a/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/pool/DataSourceProxyPoolPropertiesBase.java +++ b/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/pool/DataSourceProxyPoolPropertiesBase.java @@ -16,6 +16,9 @@ */ package org.apache.tomcat.jdbc.pool; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Proxy; + import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -88,6 +91,38 @@ abstract class DataSourceProxyPoolPropertiesBase extends JdbcPoolBaseTest { Assertions.assertTrue(poolFailure.getMessage().contains("display-only")); } + @Test + void replacingTheConfigurationWithACustomImplementationIsRejectedBeforeValidation() { + PoolProperties base = replacementProperties(); + base.setMaxActive(2); + base.setInitialSize(1000); + PoolConfiguration custom = (PoolConfiguration) Proxy.newProxyInstance( + PoolConfiguration.class.getClassLoader(), new Class<?>[] { PoolConfiguration.class }, + (proxy, method, args) -> { + try { + return method.invoke(base, args); + } catch (InvocationTargetException x) { + throw x.getCause(); + } + }); + + IllegalArgumentException constructorFailure = Assertions.assertThrows(IllegalArgumentException.class, + () -> new DataSourceProxy(custom)); + IllegalArgumentException setterFailure = Assertions.assertThrows(IllegalArgumentException.class, + () -> new DataSourceProxy().setPoolProperties(custom)); + IllegalArgumentException poolFailure = Assertions.assertThrows(IllegalArgumentException.class, + () -> new ConnectionPool(custom)); + + Assertions.assertTrue(constructorFailure.getMessage().contains("cannot be snapshotted"), + "the constructor must reject a configuration that cannot be snapshotted"); + Assertions.assertTrue(setterFailure.getMessage().contains("cannot be snapshotted"), + "the setter must reject a configuration that cannot be snapshotted"); + Assertions.assertTrue(poolFailure.getMessage().contains("cannot be snapshotted"), + "the pool constructor must reject a configuration that cannot be snapshotted"); + Assertions.assertEquals(1000, base.getInitialSize(), + "the rejected configuration must not have been modified by validation"); + } + @Test void replacingTheConfigurationWithAJmxWrapperIsRejected() throws Exception { DataSource ds = newDataSource(); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
