This is an automated email from the ASF dual-hosted git repository.
pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi-xmlbeans.git
The following commit(s) were added to refs/heads/trunk by this push:
new 34b9240a ignore external entities in LoadSaveUtils.xmlText2GenericDom
(#165)
34b9240a is described below
commit 34b9240ab3e5f2c9cb96ede9d5d7c680070237c5
Author: aizu-m <[email protected]>
AuthorDate: Sat Nov 14 23:48:24 2026 +0530
ignore external entities in LoadSaveUtils.xmlText2GenericDom (#165)
xmlText2GenericDom parsed with SAXParser.parse(is, handler), which makes
the Sax2Dom DefaultHandler the entity resolver. DefaultHandler resolves
nothing, so external entities were fetched by the parser and no XmlBeans
entity expansion limit was set. With Apache Xerces on the classpath a
SYSTEM entity pointing at a local file was inlined into the DOM.
Build the reader with SAXHelper.newXMLReader like the other load paths,
so it gets IGNORING_ENTITY_RESOLVER and the configured expansion limit.
---
.../apache/xmlbeans/impl/common/LoadSaveUtils.java | 12 +++--
.../xmlbeans/impl/common/LoadSaveUtilsTest.java | 52 ++++++++++++++++++++++
2 files changed, 60 insertions(+), 4 deletions(-)
diff --git a/src/main/java/org/apache/xmlbeans/impl/common/LoadSaveUtils.java
b/src/main/java/org/apache/xmlbeans/impl/common/LoadSaveUtils.java
index 853aa7db..8ee15956 100644
--- a/src/main/java/org/apache/xmlbeans/impl/common/LoadSaveUtils.java
+++ b/src/main/java/org/apache/xmlbeans/impl/common/LoadSaveUtils.java
@@ -19,10 +19,12 @@
*/
package org.apache.xmlbeans.impl.common;
+import org.apache.xmlbeans.XmlOptions;
import org.w3c.dom.Document;
+import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
-import javax.xml.parsers.SAXParser;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.stream.XMLOutputFactory;
@@ -38,12 +40,14 @@ public class LoadSaveUtils
public static Document xmlText2GenericDom(InputStream is, Document
emptyDoc)
throws SAXException, ParserConfigurationException, IOException
{
- SAXParser parser = SAXHelper.saxFactory().newSAXParser();
+ XMLReader reader = SAXHelper.newXMLReader(new XmlOptions());
Sax2Dom handler = new Sax2Dom(emptyDoc);
- parser.setProperty("http://xml.org/sax/properties/lexical-handler",
handler);
- parser.parse(is, handler);
+ reader.setContentHandler(handler);
+ reader.setErrorHandler(handler);
+ reader.setProperty("http://xml.org/sax/properties/lexical-handler",
handler);
+ reader.parse(new InputSource(is));
return (Document) handler.getDOM();
}
diff --git
a/src/test/java/org/apache/xmlbeans/impl/common/LoadSaveUtilsTest.java
b/src/test/java/org/apache/xmlbeans/impl/common/LoadSaveUtilsTest.java
new file mode 100644
index 00000000..5412f4ea
--- /dev/null
+++ b/src/test/java/org/apache/xmlbeans/impl/common/LoadSaveUtilsTest.java
@@ -0,0 +1,52 @@
+/* Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed 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.xmlbeans.impl.common;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+import org.w3c.dom.Document;
+
+import java.io.ByteArrayInputStream;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class LoadSaveUtilsTest {
+
+ @Test
+ void xmlText2GenericDom() throws Exception {
+ String xml = "<r a='1'><c>text</c><!--x--></r>";
+ Document doc = LoadSaveUtils.xmlText2GenericDom(
+ new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)),
DocumentHelper.createDocument());
+ assertEquals("r", doc.getDocumentElement().getNodeName());
+ assertEquals("1", doc.getDocumentElement().getAttribute("a"));
+ assertEquals("text", doc.getDocumentElement().getTextContent());
+ }
+
+ // xmlText2GenericDom must not resolve external entities, like every other
+ // XmlBeans parse path (SAXHelper.newXMLReader / DocumentHelper).
+ @Test
+ void externalEntityIsNotResolved(@TempDir Path dir) throws Exception {
+ Path secret = dir.resolve("secret.txt");
+ Files.write(secret, "SECRET".getBytes(StandardCharsets.UTF_8));
+ String xml = "<!DOCTYPE r [<!ENTITY x SYSTEM '" + secret.toUri() +
"'>]><r>&x;</r>";
+ Document doc = LoadSaveUtils.xmlText2GenericDom(
+ new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)),
DocumentHelper.createDocument());
+ assertEquals("", doc.getDocumentElement().getTextContent());
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]