Skip to content

Commit 930a1c8

Browse files
committed
Prevent XXE vulnerability
DEVSIX-1273
1 parent 03c809e commit 930a1c8

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

forms/src/main/java/com/itextpdf/forms/xfa/XfaForm.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ This file is part of the iText (R) project.
5757
import org.w3c.dom.Element;
5858
import org.w3c.dom.Node;
5959
import org.w3c.dom.NodeList;
60+
import org.xml.sax.EntityResolver;
6061
import org.xml.sax.InputSource;
6162
import org.xml.sax.SAXException;
6263

@@ -69,6 +70,7 @@ This file is part of the iText (R) project.
6970
import java.io.FileInputStream;
7071
import java.io.IOException;
7172
import java.io.InputStream;
73+
import java.io.StringReader;
7274
import java.nio.charset.StandardCharsets;
7375
import java.util.HashMap;
7476
import java.util.Map;
@@ -486,6 +488,7 @@ public void fillXfaForm(InputSource is, boolean readOnly) throws IOException {
486488
DocumentBuilder db;
487489
try {
488490
db = dbf.newDocumentBuilder();
491+
db.setEntityResolver(new SafeEmptyEntityResolver());
489492
Document newdoc = db.parse(is);
490493
fillXfaForm(newdoc.getDocumentElement(), readOnly);
491494
} catch (ParserConfigurationException e) {
@@ -619,6 +622,7 @@ private void initXfaForm(InputStream inputStream) throws ParserConfigurationExce
619622
DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
620623
fact.setNamespaceAware(true);
621624
DocumentBuilder db = fact.newDocumentBuilder();
625+
db.setEntityResolver(new SafeEmptyEntityResolver());
622626
setDomDocument(db.parse(inputStream));
623627
xfaPresent = true;
624628
}
@@ -680,4 +684,11 @@ private Node findDataNode(Node datasetsNode) {
680684
return null;
681685
}
682686

687+
// Prevents XXE attacks
688+
private static class SafeEmptyEntityResolver implements EntityResolver {
689+
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
690+
return new InputSource(new StringReader(""));
691+
}
692+
}
693+
683694
}

kernel/src/main/java/com/itextpdf/kernel/utils/XmlUtils.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,12 @@ This file is part of the iText (R) project.
4242
*/
4343
package com.itextpdf.kernel.utils;
4444

45-
import java.io.IOException;
46-
import java.io.InputStream;
47-
import java.io.OutputStream;
45+
import org.w3c.dom.Document;
46+
import org.xml.sax.EntityResolver;
47+
import org.xml.sax.InputSource;
48+
import org.xml.sax.SAXException;
49+
50+
import javax.xml.XMLConstants;
4851
import javax.xml.parsers.DocumentBuilder;
4952
import javax.xml.parsers.DocumentBuilderFactory;
5053
import javax.xml.parsers.ParserConfigurationException;
@@ -54,12 +57,18 @@ This file is part of the iText (R) project.
5457
import javax.xml.transform.TransformerFactory;
5558
import javax.xml.transform.dom.DOMSource;
5659
import javax.xml.transform.stream.StreamResult;
57-
import org.w3c.dom.Document;
58-
import org.xml.sax.SAXException;
60+
import java.io.IOException;
61+
import java.io.InputStream;
62+
import java.io.OutputStream;
63+
import java.io.StringReader;
5964

6065
class XmlUtils {
6166
public static void writeXmlDocToStream(Document xmlReport, OutputStream stream) throws TransformerException {
6267
TransformerFactory tFactory = TransformerFactory.newInstance();
68+
try {
69+
tFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
70+
tFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
71+
} catch (Exception exc) {}
6372
Transformer transformer = tFactory.newTransformer();
6473
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
6574
DOMSource source = new DOMSource(xmlReport);
@@ -74,6 +83,7 @@ public static boolean compareXmls(InputStream xml1, InputStream xml2) throws Par
7483
dbf.setIgnoringElementContentWhitespace(true);
7584
dbf.setIgnoringComments(true);
7685
DocumentBuilder db = dbf.newDocumentBuilder();
86+
db.setEntityResolver(new SafeEmptyEntityResolver());
7787

7888
Document doc1 = db.parse(xml1);
7989
doc1.normalizeDocument();
@@ -87,4 +97,12 @@ public static boolean compareXmls(InputStream xml1, InputStream xml2) throws Par
8797
public static Document initNewXmlDocument() throws ParserConfigurationException {
8898
return DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
8999
}
100+
101+
// Prevents XXE attacks
102+
private static class SafeEmptyEntityResolver implements EntityResolver {
103+
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
104+
return new InputSource(new StringReader(""));
105+
}
106+
}
107+
90108
}

0 commit comments

Comments
 (0)