-
-
Notifications
You must be signed in to change notification settings - Fork 304
/
XML.java
173 lines (164 loc) · 6.63 KB
/
XML.java
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package aQute.lib.xml;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.stream.XMLInputFactory;
import javax.xml.transform.TransformerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
// https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html#java
public final class XML {
private static final Logger logger = LoggerFactory.getLogger(XML.class);
private XML() {}
/**
* Create and return a DocumentBuilderFactory instance.
* <p>
* The returned DocumentBuilderFactory is configured to avoid XML External
* Entity (XXE) attacks.
*
* @return A properly configured DocumentBuilderFactory instance.
*/
public static DocumentBuilderFactory newDocumentBuilderFactory() {
DocumentBuilderFactory instance = DocumentBuilderFactory.newInstance();
instance.setXIncludeAware(false);
instance.setExpandEntityReferences(false);
try {
instance.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
} catch (ParserConfigurationException e) {
try { // Xerces 2 only fallback
instance.setFeature("http://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl", true);
} catch (ParserConfigurationException e2) {
logger.info("Unable to set feature to disallow DTD (doctypes): XML External Entity (XXE) attack risk",
e);
}
}
try {
instance.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
} catch (ParserConfigurationException e) {
logger.info("Unable to set feature to disable load-external-dtd: XML External Entity (XXE) attack risk", e);
}
try {
instance.setFeature("http://xml.org/sax/features/external-general-entities", false);
} catch (ParserConfigurationException e) {
try { // Xerces 2 only fallback
instance.setFeature("http://xerces.apache.org/xerces2-j/features.html#external-general-entities",
false);
} catch (ParserConfigurationException e2) {
logger.info(
"Unable to set feature to disallow external-general-entities: XML External Entity (XXE) attack risk",
e);
}
}
try {
instance.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
} catch (ParserConfigurationException e) {
try { // Xerces 2 only fallback
instance.setFeature("http://xerces.apache.org/xerces2-j/features.html#external-parameter-entities",
false);
} catch (ParserConfigurationException e2) {
logger.info(
"Unable to set feature to disallow external-parameter-entities: XML External Entity (XXE) attack risk",
e);
}
}
return instance;
}
/**
* Create and return a SAXParserFactory instance.
* <p>
* The returned SAXParserFactory is configured to avoid XML External Entity
* (XXE) attacks.
*
* @return A properly configured SAXParserFactory instance.
*/
public static SAXParserFactory newSAXParserFactory() {
SAXParserFactory instance = SAXParserFactory.newInstance();
instance.setXIncludeAware(false);
try {
instance.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
} catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) {
try { // Xerces 2 only fallback
instance.setFeature("http://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl", true);
} catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e2) {
logger.info("Unable to set feature to disallow DTD (doctypes): XML External Entity (XXE) attack risk",
e);
}
}
try {
instance.setFeature("http://xml.org/sax/features/external-general-entities", false);
} catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) {
try { // Xerces 2 only fallback
instance.setFeature("http://xerces.apache.org/xerces2-j/features.html#external-general-entities",
false);
} catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e2) {
logger.info(
"Unable to set feature to disallow external-general-entities: XML External Entity (XXE) attack risk",
e);
}
}
try {
instance.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
} catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) {
try { // Xerces 2 only fallback
instance.setFeature("http://xerces.apache.org/xerces2-j/features.html#external-parameter-entities",
false);
} catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e2) {
logger.info(
"Unable to set feature to disallow external-parameter-entities: XML External Entity (XXE) attack risk",
e);
}
}
return instance;
}
/**
* Create and return a XMLInputFactory instance.
* <p>
* The returned XMLInputFactory is configured to avoid XML External Entity
* (XXE) attacks.
*
* @return A properly configured XMLInputFactory instance.
*/
public static XMLInputFactory newXMLInputFactory() {
XMLInputFactory instance = XMLInputFactory.newInstance();
try {
instance.setProperty(XMLInputFactory.SUPPORT_DTD, false);
} catch (IllegalArgumentException e) {
logger.info("Unable to set property {} to false: XML External Entity (XXE) attack risk",
XMLInputFactory.SUPPORT_DTD, e);
}
try {
instance.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
} catch (IllegalArgumentException e) {
logger.info("Unable to set property {} to false: XML External Entity (XXE) attack risk",
XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, e);
}
return instance;
}
/**
* Create and return a TransformerFactory instance.
* <p>
* The returned TransformerFactory is configured to avoid XML External
* Entity (XXE) attacks.
*
* @return A properly configured TransformerFactory instance.
*/
public static TransformerFactory newTransformerFactory() {
TransformerFactory instance = TransformerFactory.newInstance();
try {
instance.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
} catch (IllegalArgumentException e) {
logger.info("Unable to set attribute {} to \"\": XML External Entity (XXE) attack risk",
XMLConstants.ACCESS_EXTERNAL_DTD, e);
}
try {
instance.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
} catch (IllegalArgumentException e) {
logger.info("Unable to set attribute {} to \"\": XML External Entity (XXE) attack risk",
XMLConstants.ACCESS_EXTERNAL_STYLESHEET, e);
}
return instance;
}
}