Repository navigation
Expand file tree
/
Copy pathXmlDocumentBuilder.java
More file actions
93 lines (80 loc) · 4 KB
/
Copy pathXmlDocumentBuilder.java
File metadata and controls
93 lines (80 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package utils;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* Class is used to configure and create a Document Builder for processing XML <br>
* <br>
* This file is part of the Security Shepherd Project.
*
* <p>The Security Shepherd project is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.<br>
*
* <p>The Security Shepherd project is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.<br>
*
* <p>You should have received a copy of the GNU General Public License along with the Security
* Shepherd project. If not, see <http://www.gnu.org/licenses/>.
*
* @author ismisepaul
*/
public class XmlDocumentBuilder {
private static final Logger log = LogManager.getLogger(XmlDocumentBuilder.class);
public static DocumentBuilder xmlDocBuilder(
Boolean disallow_doctype_decl,
Boolean external_general_entities,
Boolean external_parameter_entities,
Boolean load_external_dtd,
Boolean xIncludeAware,
Boolean expandEntityReferences) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
String FEATURE = null;
DocumentBuilder db = null;
try {
// This is the PRIMARY defense. If DTDs (doctypes) are disallowed, almost all
// XML entity attacks are prevented
// Xerces 2 only - http://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl
// safe=true
FEATURE = "http://apache.org/xml/features/disallow-doctype-decl";
dbf.setFeature(FEATURE, disallow_doctype_decl);
// If you can't completely disable DTDs, then at least do the following:
// Xerces 1 - http://xerces.apache.org/xerces-j/features.html#external-general-entities
// Xerces 2 - http://xerces.apache.org/xerces2-j/features.html#external-general-entities
// JDK7+ - http://xml.org/sax/features/external-general-entities
// safe=false
FEATURE = "http://xml.org/sax/features/external-general-entities";
dbf.setFeature(FEATURE, external_general_entities);
// Xerces 1 - http://xerces.apache.org/xerces-j/features.html#external-parameter-entities
// Xerces 2 - http://xerces.apache.org/xerces2-j/features.html#external-parameter-entities
// JDK7+ - http://xml.org/sax/features/external-parameter-entities
// safe=false
FEATURE = "http://xml.org/sax/features/external-parameter-entities";
dbf.setFeature(FEATURE, external_parameter_entities);
// Disable external DTDs as well
// safe=false
FEATURE = "http://apache.org/xml/features/nonvalidating/load-external-dtd";
dbf.setFeature(FEATURE, load_external_dtd);
// and these as well, per Timothy Morgan's 2014 paper: "XML Schema, DTD, and Entity Attacks"
// safe=false (for both)
dbf.setXIncludeAware(xIncludeAware);
dbf.setExpandEntityReferences(expandEntityReferences);
// And, per Timothy Morgan: "If for some reason support for inline DOCTYPEs are a requirement,
// then
// ensure the entity settings are disabled (as shown above) and beware that SSRF attacks
// (http://cwe.mitre.org/data/definitions/918.html) and denial
// of service attacks (such as billion laughs or decompression bombs via "jar:") are a risk."
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
// This should catch a failed setFeature feature
log.warn(
"ParserConfigurationException was thrown. The feature '"
+ FEATURE
+ "' is probably not supported by your XML processor.");
}
return db;
}
}