Copilot commented on code in PR #4144:
URL: https://github.com/apache/logging-log4j2/pull/4144#discussion_r3395114787


##########
log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java:
##########
@@ -368,4 +313,31 @@ public String toString() {
             return "Status [name=" + name + ", element=" + element + ", 
errorType=" + errorType + "]";
         }
     }
+
+    /**
+     * Entity resolver that resolves external entities the same way the 
configuration itself is resolved.
+     */
+    private static class ConfigurationSourceEntityResolver implements 
EntityResolver {
+
+        private static final EntityResolver INSTANCE = new 
ConfigurationSourceEntityResolver();
+
+        @Override
+        public InputSource resolveEntity(final String publicId, final String 
systemId) throws SAXException {
+            InputSource source = null;
+            try {

Review Comment:
   `resolveEntity` can receive a null `systemId` (allowed by the SAX 
`EntityResolver` contract). The current implementation would throw a 
`NullPointerException` from `new URI(systemId)`, which will surface as an 
unexpected runtime failure instead of a controlled `SAXException` / 
`ConfigurationException`. Add an explicit null check and fail with a helpful 
`SAXException` message.



##########
log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/xml/XmlConfigurationXIncludeTest.java:
##########
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.logging.log4j.core.config.xml;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.net.URL;
+import javax.xml.parsers.DocumentBuilder;
+import org.junit.jupiter.api.Test;
+import org.w3c.dom.Document;
+import org.xml.sax.InputSource;
+
+/**
+ * Unit tests for the XInclude handling of {@link 
XmlConfiguration#newDocumentBuilder(boolean)}.
+ * <p>
+ * These tests exercise the {@link DocumentBuilder} directly, so they are 
independent of the
+ * {@code log4j2.configurationEnableXInclude} property plumbing covered by the 
integration tests in
+ * {@code ConfigurationFactoryTest}.
+ * </p>
+ */
+class XmlConfigurationXIncludeTest {
+
+    private Document parse(final boolean enableXInclude) throws Exception {
+        return parse("/log4j-xinclude.xml", enableXInclude);
+    }
+
+    private Document parse(final String resource, final boolean 
enableXInclude) throws Exception {
+        final URL url = getClass().getResource(resource);
+        final DocumentBuilder builder = 
XmlConfiguration.newDocumentBuilder(enableXInclude);
+        final InputSource source = new InputSource(url.openStream());
+        // Required so that relative `href` attributes can be resolved against 
the configuration location.
+        source.setSystemId(url.toExternalForm());
+        return builder.parse(source);

Review Comment:
   The test opens a stream via `url.openStream()` but never closes it. 
`DocumentBuilder.parse(InputSource)` does not guarantee closing the provided 
stream, which can lead to file-descriptor leaks during the test suite run. Wrap 
the stream in a try-with-resources block.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to