Skip to content

Commit 3456fc8

Browse files
authored
Merge pull request #5343 from eclipse-ee4j/mojarra_pr_5339_improved_impl
Improved impl of #5339
2 parents e2907ab + 886f1bc commit 3456fc8

5 files changed

Lines changed: 50 additions & 9 deletions

File tree

impl/src/main/java/com/sun/faces/config/manager/Documents.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static com.sun.faces.RIConstants.DOCUMENT_NAMESPACE;
2020
import static com.sun.faces.RIConstants.DOCUMENT_VERSION;
21+
import static com.sun.faces.util.Util.createLocalDocumentBuilderFactory;
2122
import static com.sun.faces.util.Util.isEmpty;
2223
import static java.util.Arrays.asList;
2324
import static java.util.logging.Level.INFO;
@@ -231,7 +232,7 @@ public static DocumentInfo[] sortDocuments(DocumentInfo[] facesDocuments, FacesC
231232
}
232233

233234
private static DOMImplementation createDOMImplementation() throws ParserConfigurationException {
234-
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
235+
DocumentBuilderFactory documentBuilderFactory = createLocalDocumentBuilderFactory();
235236
documentBuilderFactory.setNamespaceAware(true);
236237

237238
return documentBuilderFactory.newDocumentBuilder().getDOMImplementation();

impl/src/main/java/com/sun/faces/config/processor/FacesFlowDefinitionConfigProcessor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.sun.faces.config.processor;
1818

19+
import static com.sun.faces.util.Util.createLocalDocumentBuilderFactory;
1920
import static com.sun.faces.util.Util.notNull;
2021

2122
import java.net.MalformedURLException;
@@ -127,7 +128,7 @@ public static Document synthesizeEmptyFlowDefinition(URI uri) throws ParserConfi
127128
}
128129
String flowName = segments[segments.length - 2];
129130

130-
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
131+
DocumentBuilderFactory dbf = createLocalDocumentBuilderFactory();
131132
dbf.setNamespaceAware(true);
132133
DocumentBuilder builder = dbf.newDocumentBuilder();
133134
DOMImplementation domImpl = builder.getDOMImplementation();

impl/src/main/java/com/sun/faces/util/Util.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import static com.sun.faces.RIConstants.FACES_SERVLET_MAPPINGS;
2323
import static com.sun.faces.RIConstants.FACES_SERVLET_REGISTRATION;
24+
import static com.sun.faces.RIConstants.NO_VALUE;
2425
import static com.sun.faces.util.MessageUtils.ILLEGAL_ATTEMPT_SETTING_APPLICATION_ARTIFACT_ID;
2526
import static com.sun.faces.util.MessageUtils.NAMED_OBJECT_NOT_FOUND_ERROR_MESSAGE_ID;
2627
import static com.sun.faces.util.MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID;
@@ -62,6 +63,7 @@
6263

6364
import javax.naming.InitialContext;
6465
import javax.naming.NamingException;
66+
import javax.xml.XMLConstants;
6567
import javax.xml.namespace.NamespaceContext;
6668
import javax.xml.parsers.DocumentBuilderFactory;
6769
import 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;

impl/src/test/java/com/sun/faces/config/processor/FacesConfigNamespaceContextTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.sun.faces.config.processor;
1818

19+
import static com.sun.faces.util.Util.createLocalDocumentBuilderFactory;
1920
import static org.junit.Assert.assertEquals;
2021
import static org.junit.Assert.assertNotNull;
2122

@@ -79,7 +80,7 @@ public void testJakartaEENSWithParameter() throws ParserConfigurationException,
7980

8081
private Document createFacesConfig(String flowName, String namespace, String version)
8182
throws ParserConfigurationException {
82-
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
83+
DocumentBuilderFactory documentBuilderFactory = createLocalDocumentBuilderFactory();
8384
documentBuilderFactory.setNamespaceAware(true);
8485
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
8586
Document docFlowConfig = documentBuilder.newDocument();

impl/src/test/java/jakarta/faces/FacesConfigOrderingTestCase.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package jakarta.faces;
1818

19+
import static com.sun.faces.util.Util.createLocalDocumentBuilderFactory;
20+
1921
import java.lang.reflect.Method;
2022
import java.util.ArrayList;
2123
import java.util.Arrays;
@@ -25,17 +27,16 @@
2527
import javax.xml.parsers.DocumentBuilderFactory;
2628
import javax.xml.parsers.ParserConfigurationException;
2729

28-
import junit.framework.Test;
29-
import junit.framework.TestCase;
30-
import junit.framework.TestSuite;
31-
3230
import org.w3c.dom.Document;
3331
import org.w3c.dom.Element;
3432

3533
import com.sun.faces.config.manager.documents.DocumentInfo;
3634
import com.sun.faces.config.manager.documents.DocumentOrderingWrapper;
3735

3836
import jakarta.faces.context.FacesContext;
37+
import junit.framework.Test;
38+
import junit.framework.TestCase;
39+
import junit.framework.TestSuite;
3940

4041
public class FacesConfigOrderingTestCase extends TestCase {
4142

@@ -391,7 +392,7 @@ private void populateIds(String elementName, List<String> ids, String ns,
391392

392393
private Document newDocument() throws ParserConfigurationException {
393394

394-
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
395+
DocumentBuilderFactory factory = createLocalDocumentBuilderFactory();
395396
factory.setValidating(false);
396397
factory.setNamespaceAware(true);
397398
return factory.newDocumentBuilder().newDocument();

0 commit comments

Comments
 (0)