Skip to content

Commit 8f6a7f6

Browse files
committed
#28 Disable downloading external resources with DocumentHelper.parseText() helper.
1 parent 983701f commit 8f6a7f6

File tree

4 files changed

+35
-19
lines changed

4 files changed

+35
-19
lines changed

.idea/modules/dom4j_main.iml

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules/dom4j_test.iml

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.gradle

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,16 @@ repositories {
1919

2020
dependencies {
2121

22-
compileOnly(
22+
implementation(
2323
'jaxen:jaxen:1.1.6',
2424
'javax.xml.stream:stax-api:1.0-2',
2525
'net.java.dev.msv:xsdlib:2013.6.1',
26-
'xpp3:xpp3:1.1.4c',
27-
'pull-parser:pull-parser:2',
2826
'javax.xml.bind:jaxb-api:2.2.12',
27+
'pull-parser:pull-parser:2',
28+
'xpp3:xpp3:1.1.4c',
2929
)
3030

31-
32-
testCompile(
31+
testImplementation(
3332
'org.testng:testng:6.8.21',
3433

3534
'xerces:xercesImpl:2.11.0',
@@ -89,6 +88,12 @@ publishing {
8988
developerConnection = 'scm:git:[email protected]:dom4j/dom4j.git'
9089
url = '[email protected]:dom4j/dom4j.git'
9190
}
91+
92+
withXml {
93+
asNode().dependencies.dependency.findAll { xmlDep ->
94+
xmlDep.appendNode('optional').value = 'true'
95+
}
96+
}
9297
}
9398
}
9499
}

src/main/java/org/dom4j/DocumentHelper.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.jaxen.VariableContext;
1919

2020
import org.xml.sax.InputSource;
21+
import org.xml.sax.SAXException;
2122

2223
/**
2324
* <code>DocumentHelper</code> is a collection of helper methods for using
@@ -256,6 +257,8 @@ public static void sort(List<Node> list, String expression, boolean distinct) {
256257
* <code>parseText</code> parses the given text as an XML document and
257258
* returns the newly created Document.
258259
* </p>
260+
*
261+
* Loading external DTD and entities is disabled (if it is possible) for security reasons.
259262
*
260263
* @param text
261264
* the XML text to be parsed
@@ -267,6 +270,14 @@ public static void sort(List<Node> list, String expression, boolean distinct) {
267270
*/
268271
public static Document parseText(String text) throws DocumentException {
269272
SAXReader reader = new SAXReader();
273+
try {
274+
reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
275+
reader.setFeature("http://xml.org/sax/features/external-general-entities", false);
276+
reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
277+
} catch (SAXException e) {
278+
//Parse with external resources downloading allowed.
279+
}
280+
270281
String encoding = getEncoding(text);
271282

272283
InputSource source = new InputSource(new StringReader(text));

0 commit comments

Comments
 (0)