[java-opensaml] branch main updated: OSJ-307: Support Santuario's XMLParser interface

Brent Putman putmanb at georgetown.edu
Sat Aug 5 02:53:53 UTC 2023


This is an automated email from the git hooks/post-receive script.

putmanb pushed a commit to branch main
in repository java-opensaml.

View the commit online:
http://git.shibboleth.net/view/?p=java-opensaml.git;a=commit;h=55b0057f090ee981a55241918a379c4d14463601

The following commit(s) were added to refs/heads/main by this push:
     new 55b0057f0 OSJ-307: Support Santuario's XMLParser interface
55b0057f0 is described below

commit 55b0057f090ee981a55241918a379c4d14463601
Author: Brent Putman <putmanb at georgetown.edu>
AuthorDate: Fri Aug 4 22:16:07 2023 -0400

    OSJ-307: Support Santuario's XMLParser interface
---
 .../opensaml/xmlsec/impl/SantuarioXMLParser.java   | 86 ++++++++++++++++++++++
 .../xmlsec/impl/SantuarioXMLParserTest.java        | 58 +++++++++++++++
 .../resources/org/opensaml/xmlsec/impl/NotXML.txt  |  1 +
 3 files changed, 145 insertions(+)

diff --git a/opensaml-xmlsec-impl/src/main/java/org/opensaml/xmlsec/impl/SantuarioXMLParser.java b/opensaml-xmlsec-impl/src/main/java/org/opensaml/xmlsec/impl/SantuarioXMLParser.java
new file mode 100644
index 000000000..fbb79b0a7
--- /dev/null
+++ b/opensaml-xmlsec-impl/src/main/java/org/opensaml/xmlsec/impl/SantuarioXMLParser.java
@@ -0,0 +1,86 @@
+/*
+ * 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.opensaml.xmlsec.impl;
+
+import java.io.InputStream;
+
+import javax.annotation.Nonnull;
+
+import org.apache.xml.security.parser.XMLParser;
+import org.apache.xml.security.parser.XMLParserException;
+import org.apache.xml.security.utils.XMLUtils;
+import org.opensaml.core.config.InitializationException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.w3c.dom.Document;
+
+import net.shibboleth.shared.component.ComponentInitializationException;
+import net.shibboleth.shared.xml.impl.BasicParserPool;
+
+/**
+ * Implementation of Santuario's {@link XMLParser} which simply wraps an instance of {@link BasicParserPool}.
+ * 
+ * <p>
+ * Note:
+ * </p>
+ * <ul>
+ * <li>This class is required to have a no-arg constructor.</li>
+ * <li>It will fail on any calls where <code>disallowDocTypeDeclarations=false</code>.</li>
+ * <li>It is configured into Santuario by setting the class name using system
+ *     property <code>org.apache.xml.security.XMLParser</code>. For details see: {@link XMLUtils}.</li>
+ * <li>The internal parser pool's max pool size may be configured via system property
+ *     <code>org.opensaml.xmlsec.impl.SantuarioXMLParser.maxPoolSize</code>.</li>
+ * </ul>
+ */
+public class SantuarioXMLParser implements XMLParser {
+    
+    /** Logger. */
+    @Nonnull final private Logger log = LoggerFactory.getLogger(SantuarioXMLParser.class);
+    
+    /** Wrapped instance of {@link BasicParserPool}. */
+    @Nonnull final private BasicParserPool parserPool;
+
+    /**
+     * Constructor. 
+     * 
+     * @throws InitializationException if the internal {@link BasicParserPool} can not be initialized successfully
+     * 
+     * */
+    public SantuarioXMLParser() throws InitializationException {
+        try {
+            parserPool = new BasicParserPool();
+            parserPool.setMaxPoolSize(Integer.getInteger("org.opensaml.xmlsec.impl.SantuarioXMLParser.maxPoolSize", 50));
+            parserPool.initialize();
+        } catch (final ComponentInitializationException e) {
+            throw new InitializationException("Error initializing parser pool", e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public Document parse(InputStream inputStream, boolean disallowDocTypeDeclarations) throws XMLParserException {
+        if (!disallowDocTypeDeclarations) {
+           throw new XMLParserException("This implementation does not support disallowDocTypeDeclarations=false");
+        }
+        
+        try {
+            return parserPool.parse(inputStream);
+        } catch (final Exception e) {
+            log.warn("Fatal error parsing XML InputStream", e);
+            throw new XMLParserException(e, "Fatal error parsing XML InputStream");
+        }
+    }
+
+}
diff --git a/opensaml-xmlsec-impl/src/test/java/org/opensaml/xmlsec/impl/SantuarioXMLParserTest.java b/opensaml-xmlsec-impl/src/test/java/org/opensaml/xmlsec/impl/SantuarioXMLParserTest.java
new file mode 100644
index 000000000..d73abced1
--- /dev/null
+++ b/opensaml-xmlsec-impl/src/test/java/org/opensaml/xmlsec/impl/SantuarioXMLParserTest.java
@@ -0,0 +1,58 @@
+/*
+ * 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.opensaml.xmlsec.impl;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.xml.security.parser.XMLParserException;
+import org.opensaml.core.config.InitializationException;
+import org.testng.Assert;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+import org.w3c.dom.Document;
+
+public class SantuarioXMLParserTest {
+    
+    private SantuarioXMLParser parser;
+    
+    @BeforeClass
+    public void setUpClass() throws InitializationException {
+        parser = new SantuarioXMLParser();
+    }
+    
+    @Test
+    public void success() throws XMLParserException, IOException {
+       try (final InputStream is = getClass().getResourceAsStream("/org/opensaml/xmlsec/signature/support/envelopedSignature.xml")) {
+           final Document document = parser.parse(is, true);
+           Assert.assertNotNull(document);
+       }
+    }
+
+    @Test(expectedExceptions = XMLParserException.class)
+    public void failOnInvalidDisallowDocTypeDeclarations() throws XMLParserException, IOException {
+       try (final InputStream is = getClass().getResourceAsStream("/org/opensaml/xmlsec/signature/support/envelopedSignature.xml")) {
+           parser.parse(is, false);
+       }
+    }
+
+    @Test(expectedExceptions = XMLParserException.class)
+    public void failOnInvalidXML() throws XMLParserException, IOException {
+        try (final InputStream is = getClass().getResourceAsStream("/org/opensaml/xmlsec/impl/NotXML.txt")) {
+           parser.parse(is, true);
+       }
+    }
+
+}
diff --git a/opensaml-xmlsec-impl/src/test/resources/org/opensaml/xmlsec/impl/NotXML.txt b/opensaml-xmlsec-impl/src/test/resources/org/opensaml/xmlsec/impl/NotXML.txt
new file mode 100644
index 000000000..8274cafb6
--- /dev/null
+++ b/opensaml-xmlsec-impl/src/test/resources/org/opensaml/xmlsec/impl/NotXML.txt
@@ -0,0 +1 @@
+this is not XML
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list