Skip to content

Commit

Permalink
Refactor XML processing
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell committed Aug 14, 2023
1 parent 1a82175 commit 7699a79
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017 Manumitting Technologies Inc and others.
* Copyright (c) 2017, 2023 Manumitting Technologies Inc and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -15,6 +15,7 @@

import javax.xml.XMLConstants;
import javax.xml.parsers.*;
import javax.xml.transform.TransformerFactory;
import org.xml.sax.*;

/**
Expand All @@ -31,6 +32,9 @@ public class SecureXMLUtil {
public static DocumentBuilderFactory newSecureDocumentBuilderFactory() throws ParserConfigurationException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
// completely disable external entities declarations:
factory.setFeature("http://xml.org/sax/features/external-general-entities", false); //$NON-NLS-1$
factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); //$NON-NLS-1$
return factory;
}

Expand All @@ -44,6 +48,10 @@ public static DocumentBuilderFactory newSecureDocumentBuilderFactory() throws Pa
public static SAXParserFactory newSecureSAXParserFactory() throws SAXNotRecognizedException, SAXNotSupportedException, ParserConfigurationException {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
// ignore DOCTYPE:
factory.setFeature("http://xml.org/sax/features/external-general-entities", false); //$NON-NLS-1$
factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); //$NON-NLS-1$
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); //$NON-NLS-1$
return factory;
}

Expand All @@ -58,4 +66,18 @@ public static XMLReader newSecureXMLReader() throws SAXException, ParserConfigur
factory.setNamespaceAware(true);
return factory.newSAXParser().getXMLReader();
}

/**
* Creates TransformerFactory which throws TransformerException when detecting
* external entities.
*
* @return javax.xml.transform.TransformerFactory
*/
public static TransformerFactory createTransformerFactoryWithErrorOnDOCTYPE() {
TransformerFactory factory = TransformerFactory.newInstance();
// prohibit the use of all protocols by external entities:
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); //$NON-NLS-1$
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, ""); //$NON-NLS-1$
return factory;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2015, 2017 Rapicorp, Inc and others.
* Copyright (c) 2015, 2023 Rapicorp, Inc and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -22,6 +22,7 @@
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.*;
import org.eclipse.equinox.internal.p2.core.helpers.SecureXMLUtil;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

Expand Down Expand Up @@ -61,18 +62,14 @@ public static InfoPListEditor loadPListEditor(File file) throws IOException {
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); //$NON-NLS-1$
builder = factory.newDocumentBuilder();
return new InfoPListEditor(builder.parse(file));
} catch (ParserConfigurationException e) {
exception = e;
} catch (SAXException e) {
exception = e;
} catch (IOException e) {
} catch (ParserConfigurationException | SAXException | IOException e) {
exception = e;
}
throw new IOException("Problem parsing " + file.getAbsolutePath(), exception); //$NON-NLS-1$
}

public void save(File file) throws TransformerException {
final TransformerFactory transformerFactory = TransformerFactory.newInstance();
final TransformerFactory transformerFactory = SecureXMLUtil.createTransformerFactoryWithErrorOnDOCTYPE();
final Transformer transformer = transformerFactory.newTransformer();
final DOMSource toSerialize = new DOMSource(document);

Expand Down

0 comments on commit 7699a79

Please sign in to comment.