Repository navigation
Adopt Apache Commons Secure XML for XML parsing #201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
app/src/main/java/org/apache/roller/weblogger/util/SecureXmlParsers.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| /* | ||
| * 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. For additional information regarding | ||
| * copyright in this work, please see the NOTICE file in the top level | ||
| * directory of this distribution. | ||
| */ | ||
|
|
||
| package org.apache.roller.weblogger.util; | ||
|
|
||
| import javax.xml.parsers.ParserConfigurationException; | ||
| import javax.xml.parsers.SAXParserFactory; | ||
|
|
||
| import org.apache.commons.xml.secure.SecureSAXParserFactory; | ||
| import org.apache.xmlrpc.util.SAXParsers; | ||
| import org.jdom2.JDOMException; | ||
| import org.jdom2.input.sax.XMLReaderJDOMFactory; | ||
| import org.xml.sax.SAXException; | ||
| import org.xml.sax.XMLReader; | ||
|
|
||
| /** | ||
| * The XML parser configuration Roller uses for every document it parses itself. | ||
| * | ||
| * <p>Parsers come from Apache Commons Secure XML, which configures the JAXP | ||
| * implementation so that nothing a document names is resolved, and throws | ||
| * rather than continuing if a setting cannot be applied. On top of that, | ||
| * Roller refuses any document type declaration: none of the documents Roller | ||
| * reads needs one. | ||
| */ | ||
| public final class SecureXmlParsers { | ||
|
|
||
| /** Xerces feature name, honoured by the JDK's own parser. */ | ||
| static final String DISALLOW_DOCTYPE = | ||
| "http://apache.org/xml/features/disallow-doctype-decl"; | ||
|
|
||
| /** Supplies JDOM with readers from {@link #newSAXParserFactory()}. */ | ||
| public static final XMLReaderJDOMFactory JDOM_READERS = new XMLReaderJDOMFactory() { | ||
| @Override | ||
| public XMLReader createXMLReader() throws JDOMException { | ||
| try { | ||
| return newSAXParserFactory().newSAXParser().getXMLReader(); | ||
| } catch (ParserConfigurationException | SAXException e) { | ||
| throw new JDOMException("Could not create an XML reader", e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isValidating() { | ||
| return false; | ||
| } | ||
| }; | ||
|
|
||
| private SecureXmlParsers() { | ||
| } | ||
|
|
||
| /** | ||
| * Returns a namespace-aware, non-validating SAX parser factory that | ||
| * resolves nothing a document names and refuses document type | ||
| * declarations. | ||
| * | ||
| * @throws IllegalStateException if the configuration cannot be applied | ||
| */ | ||
| public static SAXParserFactory newSAXParserFactory() { | ||
| SAXParserFactory factory = SecureSAXParserFactory.newNSInstance(); | ||
| factory.setValidating(false); | ||
| try { | ||
| factory.setFeature(DISALLOW_DOCTYPE, true); | ||
| } catch (ParserConfigurationException | SAXException e) { | ||
| throw new IllegalStateException( | ||
| "The XML parser does not support refusing document type declarations", e); | ||
| } | ||
|
Comment on lines
+77
to
+82
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If #198 (comment) is implemented, I don't think there is a compelling reason to disable |
||
| return factory; | ||
| } | ||
|
|
||
| /** | ||
| * Makes the XML-RPC library parse requests with {@link #newSAXParserFactory()}. | ||
| * | ||
| * @throws IllegalStateException if the configuration cannot be applied | ||
| */ | ||
| public static void installForXmlRpc() { | ||
| SAXParsers.setSAXParserFactory(newSAXParserFactory()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
app/src/test/java/org/apache/roller/weblogger/util/SecureXmlParsersTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| /* | ||
| * 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. For additional information regarding | ||
| * copyright in this work, please see the NOTICE file in the top level | ||
| * directory of this distribution. | ||
| */ | ||
|
|
||
| package org.apache.roller.weblogger.util; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import javax.xml.parsers.SAXParserFactory; | ||
|
|
||
| import org.apache.commons.xml.secure.SecureSAXParserFactory; | ||
| import org.apache.xmlrpc.util.SAXParsers; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.io.TempDir; | ||
| import org.xml.sax.Attributes; | ||
| import org.xml.sax.helpers.DefaultHandler; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| class SecureXmlParsersTest { | ||
|
|
||
| private static final String NAMESPACED = | ||
| "<?xml version=\"1.0\"?><feed xmlns=\"http://www.w3.org/2005/Atom\"><title>t</title></feed>"; | ||
|
|
||
| private static final String WITH_DOCTYPE = | ||
| "<?xml version=\"1.0\"?><!DOCTYPE feed [<!ELEMENT feed ANY>]><feed/>"; | ||
|
|
||
| @Test | ||
| void factoryIsNamespaceAwareAndNonValidating() { | ||
| SAXParserFactory factory = SecureXmlParsers.newSAXParserFactory(); | ||
| assertTrue(factory.isNamespaceAware()); | ||
| assertFalse(factory.isValidating()); | ||
| } | ||
|
|
||
| @Test | ||
| void ordinaryNamespacedDocumentsParse() throws Exception { | ||
| RootRecorder root = new RootRecorder(); | ||
| SecureXmlParsers.newSAXParserFactory().newSAXParser().parse(bytes(NAMESPACED), root); | ||
| assertEquals("http://www.w3.org/2005/Atom", root.uri); | ||
| assertEquals("feed", root.localName); | ||
| } | ||
|
|
||
| @Test | ||
| void documentTypeDeclarationsAreRefused() { | ||
| assertThrows(Exception.class, () -> SecureXmlParsers.newSAXParserFactory() | ||
| .newSAXParser().parse(bytes(WITH_DOCTYPE), new DefaultHandler())); | ||
| } | ||
|
|
||
| /** The Commons layer on its own does not read a declared external entity. */ | ||
| @Test | ||
| void commonsLayerDoesNotReadExternalEntities(@TempDir Path dir) throws Exception { | ||
| Path marker = dir.resolve("marker.txt"); | ||
| Files.write(marker, "MARKER-CONTENT".getBytes(StandardCharsets.UTF_8)); | ||
| String withEntity = "<?xml version=\"1.0\"?>" | ||
| + "<!DOCTYPE feed [<!ENTITY m SYSTEM \"" + marker.toUri() + "\">]>" | ||
| + "<feed>&m;</feed>"; | ||
| StringBuilder text = new StringBuilder(); | ||
| try { | ||
| SecureSAXParserFactory.newNSInstance().newSAXParser().parse(bytes(withEntity), | ||
| new DefaultHandler() { | ||
| @Override | ||
| public void characters(char[] ch, int start, int length) { | ||
| text.append(ch, start, length); | ||
| } | ||
| }); | ||
| } catch (Exception refused) { | ||
| // Refusing the document outright is also acceptable. | ||
| } | ||
| assertFalse(text.toString().contains("MARKER-CONTENT")); | ||
| } | ||
|
|
||
| @Test | ||
| void xmlRpcUsesTheSameConfiguration() throws Exception { | ||
| SAXParserFactory previous = SAXParsers.getSAXParserFactory(); | ||
| try { | ||
| SecureXmlParsers.installForXmlRpc(); | ||
| SAXParserFactory installed = SAXParsers.getSAXParserFactory(); | ||
| assertTrue(installed.isNamespaceAware()); | ||
| assertThrows(Exception.class, () -> installed.newSAXParser() | ||
| .parse(bytes(WITH_DOCTYPE), new DefaultHandler())); | ||
| } finally { | ||
| SAXParsers.setSAXParserFactory(previous); | ||
| } | ||
| } | ||
|
|
||
| private static ByteArrayInputStream bytes(String xml) { | ||
| return new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)); | ||
| } | ||
|
|
||
| private static final class RootRecorder extends DefaultHandler { | ||
| String uri; | ||
| String localName; | ||
|
|
||
| @Override | ||
| public void startElement(String uri, String localName, String qName, Attributes attributes) { | ||
| if (this.localName == null) { | ||
| this.uri = uri; | ||
| this.localName = localName; | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new
1.1.0version of Commons Secure XML, which is already under vote and will be released today, introducesSecureSAXParserFactory.newNSXMLReader().