@@ -29,26 +29,69 @@ public XMLValidator newXMLValidator(EntityResolver er) throws XMLException {
2929 public static SAXParser makeSAXParser (boolean val , boolean nsa ) throws XMLException {
3030 return (newInstance (val , nsa ).newSAXParser ());
3131 }
32+
33+ public static SAXParser makeSAXParserSecure (boolean val , boolean nsa ) throws XMLException {
34+ return (newInstanceSecure (val , nsa ).newSAXParser ());
35+ }
3236
3337 public SAXParser newSAXParser () throws XMLException {
3438 return SafeFunction .getEx ( () -> this .factory .newSAXParser (), XMLException .CONVERT_FUN );
3539 }
3640
3741 public static XMLFactorySAX newInstance () throws XMLException {
3842 return newInstance (false , false );
39- }
43+ }
44+
45+ public static XMLFactorySAX newInstanceSecure () throws XMLException {
46+ return newInstanceSecure (false );
47+ }
4048
4149 public static XMLFactorySAX newInstance (boolean validating ) throws XMLException {
4250 return newInstance (validating , false );
4351 }
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+
4561 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+ } );
5295 }
5396
5497 public void setValidating (boolean val ) {
0 commit comments