2121
2222import static com .sun .faces .RIConstants .FACES_SERVLET_MAPPINGS ;
2323import static com .sun .faces .RIConstants .FACES_SERVLET_REGISTRATION ;
24+ import static com .sun .faces .RIConstants .NO_VALUE ;
2425import static com .sun .faces .util .MessageUtils .ILLEGAL_ATTEMPT_SETTING_APPLICATION_ARTIFACT_ID ;
2526import static com .sun .faces .util .MessageUtils .NAMED_OBJECT_NOT_FOUND_ERROR_MESSAGE_ID ;
2627import static com .sun .faces .util .MessageUtils .NULL_PARAMETERS_ERROR_MESSAGE_ID ;
6263
6364import javax .naming .InitialContext ;
6465import javax .naming .NamingException ;
66+ import javax .xml .XMLConstants ;
6567import javax .xml .namespace .NamespaceContext ;
6668import javax .xml .parsers .DocumentBuilderFactory ;
6769import javax .xml .parsers .ParserConfigurationException ;
@@ -269,12 +271,35 @@ public static boolean isUnitTestModeEnabled() {
269271 return unitTestModeEnabled ;
270272 }
271273
274+ public static interface ThrowingBiConsumer <T , U > {
275+ void accept (T t , U u ) throws Exception ;
276+ }
277+
278+ private static <F > void setFeature (ThrowingBiConsumer <F , Boolean > setter , F feature , Boolean flag ) {
279+ try {
280+ setter .accept (feature , flag );
281+ } catch (Exception e ) {
282+ throw new IllegalArgumentException ("The feature '" + feature + "' is not supported by your XML processor." , e );
283+ }
284+ }
285+
286+ private static <F > void setPossiblyUnsupportedFeature (ThrowingBiConsumer <F , Boolean > setter , F feature , Boolean flag ) {
287+ try {
288+ setFeature (setter , feature , flag );
289+ } catch (IllegalArgumentException e ) {
290+ LOGGER .log (Level .FINE , e .getMessage (), e );
291+ }
292+ }
293+
272294 public static TransformerFactory createTransformerFactory () {
273295 ClassLoader cl = Thread .currentThread ().getContextClassLoader ();
274296 TransformerFactory factory ;
275297 try {
276298 Thread .currentThread ().setContextClassLoader (Util .class .getClassLoader ());
277299 factory = TransformerFactory .newInstance ();
300+ factory .setAttribute (XMLConstants .ACCESS_EXTERNAL_DTD , NO_VALUE );
301+ factory .setAttribute (XMLConstants .ACCESS_EXTERNAL_STYLESHEET , NO_VALUE );
302+ setFeature (factory ::setFeature , XMLConstants .FEATURE_SECURE_PROCESSING , true );
278303 } finally {
279304 Thread .currentThread ().setContextClassLoader (cl );
280305 }
@@ -298,13 +323,25 @@ public static DocumentBuilderFactory createDocumentBuilderFactory() {
298323 DocumentBuilderFactory factory ;
299324 try {
300325 Thread .currentThread ().setContextClassLoader (Util .class .getClassLoader ());
301- factory = DocumentBuilderFactory . newInstance ();
326+ factory = createLocalDocumentBuilderFactory ();
302327 } finally {
303328 Thread .currentThread ().setContextClassLoader (cl );
304329 }
305330 return factory ;
306331 }
307332
333+ public static DocumentBuilderFactory createLocalDocumentBuilderFactory () {
334+ DocumentBuilderFactory factory ;
335+ factory = DocumentBuilderFactory .newInstance ();
336+ factory .setXIncludeAware (false );
337+ factory .setExpandEntityReferences (false );
338+ setFeature (factory ::setFeature , XMLConstants .FEATURE_SECURE_PROCESSING , true );
339+ setPossiblyUnsupportedFeature (factory ::setFeature , "http://xml.org/sax/features/external-general-entities" , false );
340+ setPossiblyUnsupportedFeature (factory ::setFeature , "http://xml.org/sax/features/external-parameter-entities" , false );
341+ setPossiblyUnsupportedFeature (factory ::setFeature , "http://apache.org/xml/features/nonvalidating/load-external-dtd" , false );
342+ return factory ;
343+ }
344+
308345 public static SchemaFactory createSchemaFactory (String uri ) {
309346 ClassLoader cl = Thread .currentThread ().getContextClassLoader ();
310347 SchemaFactory factory ;
0 commit comments