@@ -29,26 +29,69 @@ public XMLValidator newXMLValidator(EntityResolver er) throws XMLException {
29
29
public static SAXParser makeSAXParser (boolean val , boolean nsa ) throws XMLException {
30
30
return (newInstance (val , nsa ).newSAXParser ());
31
31
}
32
+
33
+ public static SAXParser makeSAXParserSecure (boolean val , boolean nsa ) throws XMLException {
34
+ return (newInstanceSecure (val , nsa ).newSAXParser ());
35
+ }
32
36
33
37
public SAXParser newSAXParser () throws XMLException {
34
38
return SafeFunction .getEx ( () -> this .factory .newSAXParser (), XMLException .CONVERT_FUN );
35
39
}
36
40
37
41
public static XMLFactorySAX newInstance () throws XMLException {
38
42
return newInstance (false , false );
39
- }
43
+ }
44
+
45
+ public static XMLFactorySAX newInstanceSecure () throws XMLException {
46
+ return newInstanceSecure (false );
47
+ }
40
48
41
49
public static XMLFactorySAX newInstance (boolean validating ) throws XMLException {
42
50
return newInstance (validating , false );
43
51
}
44
-
52
+
53
+ public static XMLFactorySAX newInstanceSecure (boolean validating ) throws XMLException {
54
+ return newInstanceSecure (validating , false );
55
+ }
56
+
57
+ public static XMLFactorySAX newInstanceSecure (boolean validating , boolean namespaceAware ) throws XMLException {
58
+ return newInstance ( validating , namespaceAware , Boolean .TRUE );
59
+ }
60
+
45
61
public static XMLFactorySAX newInstance (boolean validating , boolean namespaceAware ) throws XMLException {
46
- return XMLException .get ( () -> {
47
- SAXParserFactory saxFac = SAXParserFactory .newInstance ();
48
- saxFac .setValidating (validating );
49
- saxFac .setNamespaceAware (namespaceAware );
50
- return new XMLFactorySAX (saxFac );
51
- } );
62
+ return newInstance ( validating , namespaceAware , Boolean .FALSE );
63
+ }
64
+
65
+ /**
66
+ * Creates a new XMLFactorySAX wrapping a javax.xml.parsers.SAXParserFactory
67
+ *
68
+ * if the secure flag is set, the external entities will be disabled :
69
+ *
70
+ factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
71
+ factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
72
+ factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
73
+ factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
74
+ *
75
+ * @param validating to set the XMLFactorySAX as validating
76
+ * @param namespaceAware to set the XMLFactorySAX as namespaceAware
77
+ * @param secure to set the XMLFactorySAX as secure (external entities disabled)
78
+ * @return the new configured XMLFactorySAX
79
+ * @throws XMLException in case any issue arise
80
+ */
81
+ public static XMLFactorySAX newInstance (boolean validating , boolean namespaceAware , boolean secure ) throws XMLException {
82
+ return XMLException .get ( () -> {
83
+ SAXParserFactory factory = SAXParserFactory .newInstance ();
84
+ factory .setValidating (validating );
85
+ factory .setNamespaceAware (namespaceAware );
86
+ if ( secure ) {
87
+ factory .setFeature ("http://apache.org/xml/features/disallow-doctype-decl" , true );
88
+ factory .setFeature ("http://xml.org/sax/features/external-general-entities" , false );
89
+ factory .setFeature ("http://xml.org/sax/features/external-parameter-entities" , false );
90
+ factory .setFeature ("http://apache.org/xml/features/nonvalidating/load-external-dtd" , false );
91
+ factory .setXIncludeAware (false );
92
+ }
93
+ return new XMLFactorySAX ( factory );
94
+ } );
52
95
}
53
96
54
97
public void setValidating (boolean val ) {
0 commit comments