Skip to content

Commit

Permalink
fix potential XXE vulnerability
Browse files Browse the repository at this point in the history
- fixes: SIRI-1037
  • Loading branch information
ymo-sci committed Dec 9, 2024
1 parent 42ebd9b commit 12cc6fa
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/main/java/sirius/kernel/xml/XMLGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
Expand Down Expand Up @@ -146,6 +147,7 @@ public static Document createDocument(@Nullable String namespaceURI,
String qualifiedName,
@Nullable DocumentType docType) throws ParserConfigurationException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
DocumentBuilder builder = factory.newDocumentBuilder();
DOMImplementation impl = builder.getDOMImplementation();
return impl.createDocument(namespaceURI, qualifiedName, docType);
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/sirius/kernel/xml/XMLReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import sirius.kernel.commons.Strings;
import sirius.kernel.health.Exceptions;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
Expand Down Expand Up @@ -63,7 +64,9 @@ public class XMLReader extends DefaultHandler {
*/
public XMLReader() {
try {
documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
documentBuilder = documentBuilderFactory.newDocumentBuilder();
taskContext = TaskContext.get();
} catch (ParserConfigurationException exception) {
throw Exceptions.handle(exception);
Expand Down Expand Up @@ -178,6 +181,7 @@ static class UserInterruptException extends RuntimeException {
public void parse(InputStream stream, Function<String, InputStream> resourceLocator) throws IOException {
try (stream) {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
SAXParser saxParser = factory.newSAXParser();
org.xml.sax.XMLReader reader = saxParser.getXMLReader();
reader.setEntityResolver(new EntityResolver() {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/sirius/kernel/xml/XMLStructuredInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
Expand All @@ -38,6 +39,7 @@ public class XMLStructuredInput implements StructuredInput {
public XMLStructuredInput(InputStream inputStream, @Nullable NamespaceContext namespaceContext) throws IOException {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
if (namespaceContext != null) {
factory.setNamespaceAware(true);
}
Expand Down

0 comments on commit 12cc6fa

Please sign in to comment.