-
Notifications
You must be signed in to change notification settings - Fork 30
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
Sast security fix #455
Sast security fix #455
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ | |
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
import javax.xml.XMLConstants; | ||
import javax.xml.parsers.DocumentBuilder; | ||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import javax.xml.parsers.ParserConfigurationException; | ||
|
@@ -71,6 +72,7 @@ public class ServerConfigDocument { | |
private static final XPathExpression XPATH_SERVER_ENTERPRISE_APPLICATION; | ||
private static final XPathExpression XPATH_SERVER_INCLUDE; | ||
private static final XPathExpression XPATH_SERVER_VARIABLE; | ||
private static final XPathExpression XPATH_ALL_SERVER_APPLICATIONS; | ||
|
||
static { | ||
try { | ||
|
@@ -80,6 +82,7 @@ public class ServerConfigDocument { | |
XPATH_SERVER_ENTERPRISE_APPLICATION = xPath.compile("/server/enterpriseApplication"); | ||
XPATH_SERVER_INCLUDE = xPath.compile("/server/include"); | ||
XPATH_SERVER_VARIABLE = xPath.compile("/server/variable"); | ||
XPATH_ALL_SERVER_APPLICATIONS = xPath.compile("/server/application | /server/webApplication | /server/enterpriseApplication"); | ||
} catch (XPathExpressionException ex) { | ||
// These XPath expressions should all compile statically. | ||
// Compilation failures mean the expressions are not syntactically | ||
|
@@ -141,7 +144,14 @@ private DocumentBuilder getDocumentBuilder() { | |
docBuilderFactory.setValidating(false); | ||
try { | ||
docBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false); | ||
docBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); | ||
docBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); | ||
docBuilderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); | ||
docBuilderFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); | ||
docBuilderFactory.setFeature("http://xml.org/sax/features/external-general-entities", false); | ||
docBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); | ||
docBuilderFactory.setXIncludeAware(false); | ||
docBuilderFactory.setNamespaceAware(true); | ||
docBuilderFactory.setExpandEntityReferences(false); | ||
docBuilder = docBuilderFactory.newDocumentBuilder(); | ||
} catch (ParserConfigurationException e) { | ||
// fail catastrophically if we can't create a document builder | ||
|
@@ -229,20 +239,20 @@ private void initializeAppsLocation(CommonLoggerI log, File serverXML, File conf | |
parseApplication(doc, XPATH_SERVER_APPLICATION); | ||
parseApplication(doc, XPATH_SERVER_WEB_APPLICATION); | ||
parseApplication(doc, XPATH_SERVER_ENTERPRISE_APPLICATION); | ||
parseNames(doc, "/server/application | /server/webApplication | /server/enterpriseApplication"); | ||
parseNames(doc, XPATH_ALL_SERVER_APPLICATIONS); | ||
parseInclude(doc); | ||
parseConfigDropinsDir(); | ||
|
||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
log.error("Exception while initializing app location " + e.getMessage()); | ||
} | ||
} | ||
|
||
//Checks for application names in the document. Will add locations without names to a Set | ||
private void parseNames(Document doc, String expression) throws XPathExpressionException, IOException, SAXException { | ||
private void parseNames(Document doc, XPathExpression expression) throws XPathExpressionException, IOException, SAXException { | ||
// parse input document | ||
XPath xPath = XPathFactory.newInstance().newXPath(); | ||
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(doc, XPathConstants.NODESET); | ||
NodeList nodeList = (NodeList) expression | ||
.evaluate(doc, XPathConstants.NODESET); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. spacing is weird here |
||
|
||
for (int i = 0; i < nodeList.getLength(); i++) { | ||
if (nodeList.item(i).getAttributes().getNamedItem("name") != null) { | ||
|
@@ -511,8 +521,14 @@ private Document parseDocument(URL url) throws IOException, SAXException { | |
} | ||
|
||
private Document parseDocument(InputStream in) throws SAXException, IOException { | ||
try (InputStream ins = in) { // ins will be auto-closed | ||
InputStream ins = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we getting rid of try with resources which is the new better way to ensure streams are closed? |
||
try { // ins will be auto-closed | ||
ins = in; | ||
return getDocumentBuilder().parse(ins); | ||
} finally { | ||
if (ins != null) { | ||
ins.close(); | ||
} | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,8 +52,8 @@ public abstract class BinaryScannerUtil { | |
"in the application’s API usage: %s. Review and update your application to ensure it is not using conflicting APIs " + | ||
"from different levels of MicroProfile, Java EE, or Jakarta EE."; | ||
public static final String BINARY_SCANNER_CONFLICT_MESSAGE4 = "[None available]"; // format should match JVM Set.toString() | ||
public static final String BINARY_SCANNER_CONFLICT_MESSAGE5 = "A working set of features could not be generated due to conflicts " + | ||
"in the required features: %s and required levels of MicroProfile: %s, Java EE or Jakarta EE: %s. Review and update your application to ensure it " + | ||
public static final String BINARY_SCANNER_CONFLICT_MESSAGE5 = "A working set of features could not be generated due to conflicts " + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are there any actual changes to this file? or all formatting? If the latter, please revert the changes to this file. |
||
"in the required features: %s and required levels of MicroProfile: %s, Java EE or Jakarta EE: %s. Review and update your application to ensure it " + | ||
"is using the correct levels of MicroProfile, Java EE, or Jakarta EE, or consider removing the following set of features: %s."; | ||
public static final String BINARY_SCANNER_INVALID_MP_MESSAGE = "The MicroProfile version number %s specified in the build file " + | ||
"is not supported for feature generation."; | ||
|
@@ -94,7 +94,7 @@ public BinaryScannerUtil(File binaryScanner) { | |
* optimize parameter. The currentFeatureSet parameter indicates the starting list of features and all the | ||
* generated features will be compatible. The generated features will also be compatible with the indicated | ||
* versions of Java EE or Jakarta EE and MicroProfile. | ||
* | ||
* | ||
* @param currentFeatureSet - the features already specified in the server configuration | ||
* @param classFiles - a set of class files for the scanner to handle. Should be a subset of allClassesDirectories | ||
* @param allClassesDirectories - the directories containing all the class files of the application | ||
|
@@ -109,7 +109,7 @@ public BinaryScannerUtil(File binaryScanner) { | |
* @throws NoRecommendationException - indicates a problem and there are no recommended features | ||
* @throws RecommendationSetException - indicates a problem but the scanner was able to generate a set of | ||
* features that should work to run the application | ||
* @throws FeatureModifiedException - indicates a problem but the scanner was able to generate a set of features | ||
* @throws FeatureModifiedException - indicates a problem but the scanner was able to generate a set of features | ||
* that should work if certain features are modified | ||
* @throws FeatureUnavailableException - indicates a problem between required features and required MP/EE levels but | ||
* the scanner was able to generate a set of features that should be removed | ||
|
@@ -118,7 +118,7 @@ public BinaryScannerUtil(File binaryScanner) { | |
* scanner when used in combination with each other. E.g. EE 7 and MP 2.1 | ||
*/ | ||
public Set<String> runBinaryScanner(Set<String> currentFeatureSet, List<String> classFiles, Set<String> allClassesDirectories, | ||
String logLocation, String targetJavaEE, String targetMicroProfile, boolean optimize) | ||
String logLocation, String targetJavaEE, String targetMicroProfile, boolean optimize) | ||
throws PluginExecutionException, NoRecommendationException, RecommendationSetException, FeatureModifiedException, | ||
FeatureUnavailableException, IllegalTargetException, IllegalTargetComboException { | ||
Set<String> featureList = null; | ||
|
@@ -189,7 +189,7 @@ public Set<String> runBinaryScanner(Set<String> currentFeatureSet, List<String> | |
Set<String> modifications = getFeatures(scannerException); | ||
// rerun binary scanner with all class files and without the current feature set | ||
Set<String> sampleFeatureList = reRunIfFailed ? reRunBinaryScanner(allClassesDirectories, logLocation, targetJavaEE, targetMicroProfile) : null; | ||
throw new FeatureModifiedException(modifications, | ||
throw new FeatureModifiedException(modifications, | ||
(sampleFeatureList == null) ? getNoSampleFeatureList() : sampleFeatureList, scannerException.getLocalizedMessage()); | ||
} else if (scannerException.getClass().getName().equals(FEATURE_NOT_AVAILABLE_EXCEPTION)) { | ||
// The list of features required by app or passed to binary scanner do not exist | ||
|
@@ -241,10 +241,10 @@ public Set<String> runBinaryScanner(Set<String> currentFeatureSet, List<String> | |
/** | ||
* The method is intended to call the binary scanner to generate a list of the optimal features for an | ||
* application. This optimal list can be reported to the user as a suggested list of features. | ||
* | ||
* | ||
* In order to generate the optimal list we must scan all classes in the application and we do not consider | ||
* the features already specified in the server configuration (server.xml). | ||
* | ||
* | ||
* @param allClassesDirectories - the scanner will find all the class files in this set of directories | ||
* @param logLocation - directory name relative to project or absolute path passed to binary scanner | ||
* @param targetJavaEE - generate features valid for the indicated version of EE | ||
|
@@ -267,13 +267,13 @@ public Set<String> reRunBinaryScanner(Set<String> allClassesDirectories, String | |
logLocation = null; | ||
} | ||
debug("Recalling binary scanner with the following inputs...\n" + | ||
" binaryInputs: " + binaryInputs + "\n" + | ||
" targetJavaEE: " + targetJavaEE + "\n" + | ||
" targetMicroP: " + targetMicroProfile + "\n" + | ||
" currentFeatures: " + currentFeaturesSet + "\n" + | ||
" logLocation: " + logLocation + "\n" + | ||
" logLevel: " + logLevel + "\n" + | ||
" locale: " + java.util.Locale.getDefault()); | ||
" binaryInputs: " + binaryInputs + "\n" + | ||
" targetJavaEE: " + targetJavaEE + "\n" + | ||
" targetMicroP: " + targetMicroProfile + "\n" + | ||
" currentFeatures: " + currentFeaturesSet + "\n" + | ||
" logLocation: " + logLocation + "\n" + | ||
" logLevel: " + logLevel + "\n" + | ||
" locale: " + java.util.Locale.getDefault()); | ||
featureList = (Set<String>) generateFeatureSetMethod.invoke(null, binaryInputs, targetJavaEE, targetMicroProfile, | ||
currentFeaturesSet, logLocation, logLevel, java.util.Locale.getDefault()); | ||
for (String s : featureList) {debug(s);}; | ||
|
@@ -446,12 +446,12 @@ public static String composeMPVersion(String ver) { | |
|
||
/** | ||
* Convenience method to build the string reported to the user when the exception is detected. | ||
* | ||
* | ||
* This is used after the caller has analyzed the Java or Jakarta EE version number and the MicroProfile | ||
* version number and generated argument values to pass to the binary scanner. If the binary scanner | ||
* detects a problem and throws an exception it reports the invalid arguments. We must map the invalid | ||
* arguments back to the user specified version number in order to fix the problem. | ||
* | ||
* | ||
* @param invalidEEArg - the argument passed to the binary scanner which may be returned as invalid. | ||
* @param invalidMPArg - the argument passed to the binary scanner which may be returned as invalid. | ||
* @param eeVersion - the user specified version string from the build file used to generate the arg. | ||
|
@@ -593,4 +593,4 @@ public class IllegalTargetComboException extends AbstractIllegalTargetException | |
super(eeLevel, mpLevel); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we not allowed to have a singleton instance of the factory? We were only doing the initialization once before. This can get called many times from maven/gradle plugins. This new code is creating a new one every single time.