25
25
import javax .xml .parsers .ParserConfigurationException ;
26
26
import javax .xml .transform .OutputKeys ;
27
27
import javax .xml .transform .Transformer ;
28
- import javax .xml .transform .TransformerConfigurationException ;
29
28
import javax .xml .transform .TransformerFactory ;
30
29
import javax .xml .transform .dom .DOMSource ;
31
30
import javax .xml .transform .stream .StreamResult ;
@@ -42,7 +41,7 @@ private XmlAsserts() {
42
41
}
43
42
44
43
private static DocumentBuilder getDocumentBuilder () {
45
- DocumentBuilderFactory dbf = DocumentBuilderFactory . newInstance ();
44
+ DocumentBuilderFactory dbf = newSecureDocumentBuilderFactory ();
46
45
try {
47
46
return dbf .newDocumentBuilder ();
48
47
} catch (ParserConfigurationException e ) {
@@ -74,7 +73,7 @@ private static String formatXml(String xmlDocumentString) throws Exception {
74
73
}
75
74
76
75
private static String formatXml (Document xmlDocument ) throws Exception {
77
- Transformer transformer = transformerFactory ().newTransformer ();
76
+ Transformer transformer = newSecureTransformerFactory ().newTransformer ();
78
77
transformer .setOutputProperty (OutputKeys .INDENT , "yes" );
79
78
StreamResult result = new StreamResult (new StringWriter ());
80
79
DOMSource source = new DOMSource (xmlDocument );
@@ -84,12 +83,47 @@ private static String formatXml(Document xmlDocument) throws Exception {
84
83
}
85
84
}
86
85
87
- private static TransformerFactory transformerFactory () throws TransformerConfigurationException {
88
- TransformerFactory factory = TransformerFactory .newInstance ();
89
- factory .setFeature (XMLConstants .FEATURE_SECURE_PROCESSING , true );
90
- if (factory .getFeature (XMLConstants .ACCESS_EXTERNAL_DTD )) {
91
- factory .setFeature (XMLConstants .ACCESS_EXTERNAL_DTD , false );
86
+ private static DocumentBuilderFactory newSecureDocumentBuilderFactory () {
87
+ DocumentBuilderFactory docFactory = DocumentBuilderFactory .newInstance ();
88
+ docFactory .setXIncludeAware (false );
89
+ docFactory .setExpandEntityReferences (false );
90
+ trySetFeature (docFactory , XMLConstants .FEATURE_SECURE_PROCESSING , true );
91
+ trySetFeature (docFactory , "http://apache.org/xml/features/disallow-doctype-decl" , true );
92
+ trySetFeature (docFactory , "http://xml.org/sax/features/external-general-entities" , false );
93
+ trySetFeature (docFactory , "http://xml.org/sax/features/external-parameter-entities" , false );
94
+ trySetAttribute (docFactory , "http://javax.xml.XMLConstants/property/accessExternalDTD" , "" );
95
+ trySetAttribute (docFactory , "http://javax.xml.XMLConstants/property/accessExternalSchema" , "" );
96
+ return docFactory ;
97
+ }
98
+
99
+ private static TransformerFactory newSecureTransformerFactory () {
100
+ TransformerFactory transformerFactory = TransformerFactory .newInstance ();
101
+ trySetAttribute (transformerFactory , XMLConstants .ACCESS_EXTERNAL_DTD , "" );
102
+ trySetAttribute (transformerFactory , XMLConstants .ACCESS_EXTERNAL_STYLESHEET , "" );
103
+ return transformerFactory ;
104
+ }
105
+
106
+ private static void trySetFeature (DocumentBuilderFactory factory , String feature , boolean value ) {
107
+ try {
108
+ factory .setFeature (feature , value );
109
+ } catch (Exception e ) {
110
+ throw new RuntimeException (e );
111
+ }
112
+ }
113
+
114
+ private static void trySetAttribute (DocumentBuilderFactory factory , String feature , String value ) {
115
+ try {
116
+ factory .setAttribute (feature , value );
117
+ } catch (Exception e ) {
118
+ throw new RuntimeException (e );
119
+ }
120
+ }
121
+
122
+ private static void trySetAttribute (TransformerFactory factory , String feature , Object value ) {
123
+ try {
124
+ factory .setAttribute (feature , value );
125
+ } catch (Exception e ) {
126
+ throw new RuntimeException (e );
92
127
}
93
- return factory ;
94
128
}
95
129
}
0 commit comments