Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Java Security Ultimate Security Repo Scanner 2023 #5339

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions impl/src/main/java/com/sun/faces/config/manager/Documents.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.concurrent.FutureTask;
import java.util.logging.Logger;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

Expand Down Expand Up @@ -232,6 +233,26 @@ public static DocumentInfo[] sortDocuments(DocumentInfo[] facesDocuments, FacesC

private static DOMImplementation createDOMImplementation() throws ParserConfigurationException {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
String FEATURE = null;
try {
FEATURE = "http://xml.org/sax/features/external-parameter-entities";
documentBuilderFactory.setFeature(FEATURE, false);

FEATURE = "http://apache.org/xml/features/nonvalidating/load-external-dtd";
documentBuilderFactory.setFeature(FEATURE, false);

FEATURE = "http://xml.org/sax/features/external-general-entities";
documentBuilderFactory.setFeature(FEATURE, false);

documentBuilderFactory.setXIncludeAware(false);
documentBuilderFactory.setExpandEntityReferences(false);

documentBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);

} catch (ParserConfigurationException e) {
throw new IllegalStateException("The feature '"
+ FEATURE + "' is not supported by your XML processor.", e);
}
documentBuilderFactory.setNamespaceAware(true);

return documentBuilderFactory.newDocumentBuilder().getDOMImplementation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
Expand Down Expand Up @@ -128,6 +129,26 @@ public static Document synthesizeEmptyFlowDefinition(URI uri) throws ParserConfi
String flowName = segments[segments.length - 2];

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
String FEATURE = null;
try {
FEATURE = "http://xml.org/sax/features/external-parameter-entities";
dbf.setFeature(FEATURE, false);

FEATURE = "http://apache.org/xml/features/nonvalidating/load-external-dtd";
dbf.setFeature(FEATURE, false);

FEATURE = "http://xml.org/sax/features/external-general-entities";
dbf.setFeature(FEATURE, false);

dbf.setXIncludeAware(false);
dbf.setExpandEntityReferences(false);

dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);

} catch (ParserConfigurationException e) {
throw new IllegalStateException("The feature '"
+ FEATURE + "' is not supported by your XML processor.", e);
}
dbf.setNamespaceAware(true);
DocumentBuilder builder = dbf.newDocumentBuilder();
DOMImplementation domImpl = builder.getDOMImplementation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -257,7 +258,7 @@ public UIComponent _createComponent(FacesContext context, String taglibURI, Stri
try {

// create a temporary file in that directory
tempFile = File.createTempFile("mojarra", ".tmp", tmpDir);
tempFile = Files.createTempFile(tmpDir.toPath(), "mojarra", ".tmp").toFile();
osw = new OutputStreamWriter(new FileOutputStream(tempFile), RIConstants.CHAR_ENCODING);
osw.append("<?xml version='1.0' encoding='");
osw.append(RIConstants.CHAR_ENCODING);
Expand Down
4 changes: 4 additions & 0 deletions impl/src/main/java/com/sun/faces/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
Expand Down Expand Up @@ -276,6 +277,9 @@ public static TransformerFactory createTransformerFactory() {
try {
Thread.currentThread().setContextClassLoader(Util.class.getClassLoader());
factory = TransformerFactory.newInstance();
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
} finally {
Thread.currentThread().setContextClassLoader(cl);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
Expand Down Expand Up @@ -80,6 +81,26 @@ public void testJakartaEENSWithParameter() throws ParserConfigurationException,
private Document createFacesConfig(String flowName, String namespace, String version)
throws ParserConfigurationException {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
String FEATURE = null;
try {
FEATURE = "http://xml.org/sax/features/external-parameter-entities";
documentBuilderFactory.setFeature(FEATURE, false);

FEATURE = "http://apache.org/xml/features/nonvalidating/load-external-dtd";
documentBuilderFactory.setFeature(FEATURE, false);

FEATURE = "http://xml.org/sax/features/external-general-entities";
documentBuilderFactory.setFeature(FEATURE, false);

documentBuilderFactory.setXIncludeAware(false);
documentBuilderFactory.setExpandEntityReferences(false);

documentBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);

} catch (ParserConfigurationException e) {
throw new IllegalStateException("The feature '"
+ FEATURE + "' is not supported by your XML processor.", e);
}
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document docFlowConfig = documentBuilder.newDocument();
Expand Down
21 changes: 21 additions & 0 deletions impl/src/test/java/jakarta/faces/FacesConfigOrderingTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Collections;
import java.util.List;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

Expand Down Expand Up @@ -392,6 +393,26 @@ private void populateIds(String elementName, List<String> ids, String ns,
private Document newDocument() throws ParserConfigurationException {

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
String FEATURE = null;
try {
FEATURE = "http://xml.org/sax/features/external-parameter-entities";
factory.setFeature(FEATURE, false);

FEATURE = "http://apache.org/xml/features/nonvalidating/load-external-dtd";
factory.setFeature(FEATURE, false);

FEATURE = "http://xml.org/sax/features/external-general-entities";
factory.setFeature(FEATURE, false);

factory.setXIncludeAware(false);
factory.setExpandEntityReferences(false);

factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);

} catch (ParserConfigurationException e) {
throw new IllegalStateException("The feature '"
+ FEATURE + "' is not supported by your XML processor.", e);
}
factory.setValidating(false);
factory.setNamespaceAware(true);
return factory.newDocumentBuilder().newDocument();
Expand Down
Loading