This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/commons-secure-xml.git
The following commit(s) were added to refs/heads/main by this push:
new e38e39b Fix Java 28 test compatibility and run SpotBugs after
verification (#125)
e38e39b is described below
commit e38e39b17dea4b76b24f952e72800ecd1e25cae5
Author: Gary Gregory <[email protected]>
AuthorDate: Sat Nov 14 14:22:56 2026 -0500
Fix Java 28 test compatibility and run SpotBugs after verification (#125)
Enable jdk.reflect.allowNonSerializableConstruction for Surefire and
Failsafe test JVMs only on Java 28 and later.
Move spotbugs:check and pmd:check after verify so it analyzes compiled
classes.
Fix SecureXMLReader constructor safety and narrow SpotBugs exclusions
Move delegate validation and resolver-floor installation into a static
helper evaluated during constructor chaining, before Object construction
completes. Keep the private constructor limited to assigning final
fields,
preserving subclass support and preventing the CT_CONSTRUCTOR_THROW
finding without introducing finalization APIs.
Add tests for null delegate rejection, propagation of
resolver-installation
failures, and successful resolver-floor installation and delegation.
Exclude EI_EXPOSE_REP only for these exact method signatures:
- SecureSAXParser.getParser()
- SecureSAXParser.getXMLReader()
- SecureTransformerHandler.getTransformer()
---
pom.xml | 27 +++++++++++++++-
src/conf/spotbugs-exclude-filter.xml | 15 +++++++++
.../apache/commons/xml/secure/SecureXMLReader.java | 17 ++++++++--
.../commons/xml/secure/SecureXMLReaderTest.java | 36 ++++++++++++++++++++++
4 files changed, 91 insertions(+), 4 deletions(-)
diff --git a/pom.xml b/pom.xml
index 630d73d..e5edb39 100644
--- a/pom.xml
+++ b/pom.xml
@@ -171,7 +171,7 @@ limitations under the License.
</dependency>
</dependencies>
<build>
- <defaultGoal>clean checkstyle:check spotbugs:check pmd:check
javadoc:javadoc verify</defaultGoal>
+ <defaultGoal>clean checkstyle:check javadoc:javadoc verify pmd:check
spotbugs:check</defaultGoal>
<pluginManagement>
<plugins>
<plugin>
@@ -682,6 +682,31 @@ limitations under the License.
</plugins>
</reporting>
<profiles>
+ <profile>
+ <id>java28-tests</id>
+ <activation>
+ <jdk>[28,)</jdk>
+ </activation>
+ <build>
+ <plugins>
+ <!-- Temporarily allow Mockito/Objenesis to construct test doubles
on JDK 28 and later. -->
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <argLine>@{argLine}
-Djdk.reflect.allowNonSerializableConstruction=true</argLine>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-failsafe-plugin</artifactId>
+ <configuration>
+ <argLine>@{argLine}
-Djdk.reflect.allowNonSerializableConstruction=true</argLine>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ </profile>
<!--
Fold the Android instrumented run into the coverage data.
-->
diff --git a/src/conf/spotbugs-exclude-filter.xml
b/src/conf/spotbugs-exclude-filter.xml
index 5ee3d92..7af77ad 100644
--- a/src/conf/spotbugs-exclude-filter.xml
+++ b/src/conf/spotbugs-exclude-filter.xml
@@ -19,4 +19,19 @@
xmlns="https://github.com/spotbugs/filter/3.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://github.com/spotbugs/filter/3.0.0
https://raw.githubusercontent.com/spotbugs/spotbugs/3.1.0/spotbugs/etc/findbugsfilter.xsd">
+ <Match>
+ <Class name="org.apache.commons.xml.secure.SecureSAXParser"/>
+ <Method name="getParser" params="" returns="org.xml.sax.Parser"/>
+ <Bug pattern="EI_EXPOSE_REP"/>
+ </Match>
+ <Match>
+ <Class name="org.apache.commons.xml.secure.SecureSAXParser"/>
+ <Method name="getXMLReader" params="" returns="org.xml.sax.XMLReader"/>
+ <Bug pattern="EI_EXPOSE_REP"/>
+ </Match>
+ <Match>
+ <Class name="org.apache.commons.xml.secure.SecureTransformerHandler"/>
+ <Method name="getTransformer" params=""
returns="javax.xml.transform.Transformer"/>
+ <Bug pattern="EI_EXPOSE_REP"/>
+ </Match>
</FindBugsFilter>
diff --git a/src/main/java/org/apache/commons/xml/secure/SecureXMLReader.java
b/src/main/java/org/apache/commons/xml/secure/SecureXMLReader.java
index 4abdc85..837ecea 100644
--- a/src/main/java/org/apache/commons/xml/secure/SecureXMLReader.java
+++ b/src/main/java/org/apache/commons/xml/secure/SecureXMLReader.java
@@ -47,6 +47,13 @@
*/
class SecureXMLReader implements XMLReader {
+ private static FallbackIgnoreEntityResolver2 installFloor(final XMLReader
delegate) {
+ Objects.requireNonNull(delegate, "delegate");
+ final FallbackIgnoreEntityResolver2 floor = new
FallbackIgnoreEntityResolver2(null);
+ delegate.setEntityResolver(floor);
+ return floor;
+ }
+
private final XMLReader delegate;
private final FallbackIgnoreEntityResolver2 floor;
@@ -58,9 +65,13 @@ class SecureXMLReader implements XMLReader {
* @throws NullPointerException Thrown if {@code delegate} is {@code null}.
*/
SecureXMLReader(final XMLReader delegate) {
- this.delegate = Objects.requireNonNull(delegate, "delegate");
- this.floor = new FallbackIgnoreEntityResolver2(null);
- delegate.setEntityResolver(floor);
+ // Complete potentially throwing initialization before Object
construction to prevent finalizer attacks.
+ this(delegate, installFloor(delegate));
+ }
+
+ private SecureXMLReader(final XMLReader delegate, final
FallbackIgnoreEntityResolver2 floor) {
+ this.delegate = delegate;
+ this.floor = floor;
}
@Override
diff --git
a/src/test/java/org/apache/commons/xml/secure/SecureXMLReaderTest.java
b/src/test/java/org/apache/commons/xml/secure/SecureXMLReaderTest.java
index 6ba1162..9300357 100644
--- a/src/test/java/org/apache/commons/xml/secure/SecureXMLReaderTest.java
+++ b/src/test/java/org/apache/commons/xml/secure/SecureXMLReaderTest.java
@@ -17,6 +17,10 @@
package org.apache.commons.xml.secure;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -27,6 +31,7 @@
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
+import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLFilterImpl;
@@ -51,6 +56,37 @@ public void parse(final String systemId) {
}
}
+ @Test
+ void constructorInstallsFloorOnDelegate() {
+ final XMLFilterImpl delegate = new XMLFilterImpl();
+ final SecureXMLReader reader = new SecureXMLReader(delegate);
+ final FallbackIgnoreEntityResolver2 floor =
assertInstanceOf(FallbackIgnoreEntityResolver2.class,
delegate.getEntityResolver());
+ assertSame(delegate, reader.getDelegate());
+ assertNull(reader.getEntityResolver());
+ final DefaultHandler handler = new DefaultHandler();
+ reader.setEntityResolver(handler);
+ assertSame(floor, delegate.getEntityResolver());
+ assertSame(handler, floor.getDelegate());
+ assertSame(handler, reader.getEntityResolver());
+ }
+
+ @Test
+ void constructorPropagatesResolverInstallationFailure() {
+ final IllegalStateException failure = new
IllegalStateException("test");
+ final XMLFilterImpl delegate = new XMLFilterImpl() {
+ @Override
+ public void setEntityResolver(final EntityResolver resolver) {
+ throw failure;
+ }
+ };
+ assertSame(failure, assertThrows(IllegalStateException.class, () ->
new SecureXMLReader(delegate)));
+ }
+
+ @Test
+ void constructorRejectsNullDelegate() {
+ assertEquals("delegate", assertThrows(NullPointerException.class, ()
-> new SecureXMLReader(null)).getMessage());
+ }
+
@Test
void forwardsBothParseOverloads() throws Exception {
final RecordingReader delegate = new RecordingReader();