Skip to content

Commit 0e88b7a

Browse files
authored
DRILL-8461: Prevent XXE Attacks in XML Format Plugin (#2845)
1 parent 097da74 commit 0e88b7a

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

contrib/format-xml/src/main/java/org/apache/drill/exec/store/xml/XMLReader.java

+3
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ private enum xmlState {
100100
public XMLReader(InputStream fsStream, int dataLevel, boolean allTextMode) throws XMLStreamException {
101101
this.fsStream = fsStream;
102102
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
103+
104+
// This property prevents XXE attacks by disallowing DTD.
105+
inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
103106
reader = inputFactory.createXMLEventReader(fsStream);
104107
fieldNameStack = new Stack<>();
105108
rowWriterStack = new Stack<>();

contrib/format-xml/src/test/java/org/apache/drill/exec/store/xml/TestXMLReader.java

+14
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.drill.exec.store.xml;
2020

2121
import org.apache.drill.categories.RowSetTest;
22+
import org.apache.drill.common.exceptions.UserException;
2223
import org.apache.drill.common.types.TypeProtos.DataMode;
2324
import org.apache.drill.common.types.TypeProtos.MinorType;
2425
import org.apache.drill.exec.physical.rowSet.RowSet;
@@ -41,6 +42,8 @@
4142
import static org.apache.drill.test.rowSet.RowSetUtilities.objArray;
4243
import static org.apache.drill.test.rowSet.RowSetUtilities.strArray;
4344
import static org.junit.Assert.assertEquals;
45+
import static org.junit.Assert.assertTrue;
46+
import static org.junit.Assert.fail;
4447

4548
@Category(RowSetTest.class)
4649
public class TestXMLReader extends ClusterTest {
@@ -86,6 +89,17 @@ public void testWildcard() throws Exception {
8689
new RowSetComparison(expected).verifyAndClearAll(results);
8790
}
8891

92+
@Test
93+
public void testXXE() throws Exception {
94+
String sql = "SELECT * FROM cp.`xml/bad.xml`";
95+
try {
96+
client.queryBuilder().sql(sql).rowSet();
97+
fail();
98+
} catch (UserException e) {
99+
assertTrue(e.getMessage().contains("DATA_READ ERROR: Error parsing XML file"));
100+
}
101+
}
102+
89103
@Test
90104
public void testAllTextMode() throws Exception {
91105
String sql = "SELECT attributes, int_field, bigint_field, float_field, double_field, " +
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!--
2+
3+
Licensed to the Apache Software Foundation (ASF) under one
4+
or more contributor license agreements. See the NOTICE file
5+
distributed with this work for additional information
6+
regarding copyright ownership. The ASF licenses this file
7+
to you under the Apache License, Version 2.0 (the
8+
"License"); you may not use this file except in compliance
9+
with the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
<?xml version="1.0" encoding="ISO-8859-1"?>
22+
<!DOCTYPE foo [
23+
<!ELEMENT foo ANY >
24+
<!ENTITY xxe SYSTEM "/etc/passwd" >]
25+
>
26+
<creds>
27+
<user>&xxe;</user>
28+
<pass>mypass</pass>
29+
</creds>

0 commit comments

Comments
 (0)