Skip to content

Commit f81df0a

Browse files
committed
TIKA-4471 -- add unit tests to confirm defense against xxe in sax, dom and stax -- add flexibility for DOM
(cherry picked from commit 2c7caa7)
1 parent e2bcbbe commit f81df0a

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

tika-core/src/test/java/org/apache/tika/utils/XMLReaderUtilsTest.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,7 @@ public void testSAXBillionLaughs() throws Exception {
136136
XMLReaderUtils.parseSAX(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)),
137137
new ToTextContentHandler(), new ParseContext());
138138
} catch (SAXException e) {
139-
if (e.getMessage() != null && e
140-
.getMessage()
141-
.contains("entity expansions")) {
139+
if (e.getMessage() != null && e.getMessage().contains("entity expansions")) {
142140
//do nothing
143141
} else {
144142
throw e;
@@ -150,8 +148,22 @@ public void testSAXBillionLaughs() throws Exception {
150148
@Test
151149
public void testDOMBillionLaughs() throws Exception {
152150
//confirm that ExpandEntityReferences has been set to false.
151+
152+
//some implementations ignore the expandEntityReferences=false, and we are still
153+
//protected by the "The parser has encountered more than "20" entity expansions" SAXException.
154+
//We need to check for either: empty content and no exception, or this SAXException
153155
for (String xml : BILLION_LAUGHS) {
154-
Document doc = XMLReaderUtils.buildDOM(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)), new ParseContext());
156+
Document doc = null;
157+
try {
158+
doc = XMLReaderUtils.buildDOM(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)), new ParseContext());
159+
} catch (SAXException e) {
160+
if (e.getMessage() != null && e.getMessage().contains("entity expansions")) {
161+
//do nothing
162+
continue;
163+
} else {
164+
throw e;
165+
}
166+
}
155167
NodeList nodeList = doc.getChildNodes();
156168
StringBuilder sb = new StringBuilder();
157169
dumpChildren(nodeList, sb);

0 commit comments

Comments
 (0)