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

Sast issue fix 1.0 #294

Merged
merged 17 commits into from
Aug 7, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,21 @@ public static void writeDocToXmlFile(Document doc, File inputFile) throws Except
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "yes");
doc.setXmlStandalone(true);
DOMSource source = new DOMSource(doc);
StreamResult file = new StreamResult(new OutputStreamWriter(new FileOutputStream(inputFile), "UTF-8"));
transformer.transform(source, file);
try {
StreamResult file = new StreamResult(new OutputStreamWriter(new FileOutputStream(inputFile), "UTF-8"));
transformer.transform(source, file);
} catch (Exception ex) {
LOGGER.warning("Received exception during post processing of schema file " + inputFile.getAbsolutePath() + " : " + ex.getMessage());
}
}

private static TransformerFactory getTransformerFactory() throws TransformerConfigurationException {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformerFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
// XMLConstants.ACCESS_EXTERNAL_DTD uses an empty string to deny all access to external references;
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
cherylking marked this conversation as resolved.
Show resolved Hide resolved
// XMLConstants.ACCESS_EXTERNAL_STYLESHEET uses an empty string to deny all access to external references;
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
return transformerFactory;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Set;
import java.util.logging.Logger;

import javax.xml.XMLConstants;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
Expand All @@ -50,38 +51,50 @@ public static boolean hasServerRoot(Path filePath) {
return hasServerRoot(filePath.toFile());
}

public static boolean hasServerRoot(File xmlFile) {
private static boolean hasServerRoot(File xmlFile) {
if (!xmlFile.exists() || xmlFile.length() == 0) {
return false;
}

try {
XMLInputFactory factory = getXmlInputFactory();
return hasSeverRootValues(factory,xmlFile);
} catch (Exception e) {
LOGGER.severe("Unable to access XML file "+ xmlFile.getAbsolutePath());
}

XMLEventReader reader = null;
return false;
}

try (FileInputStream fis = new FileInputStream(xmlFile)) {
reader = factory.createXMLEventReader(fis);
while (reader.hasNext()) {
XMLEvent nextEvent = reader.nextEvent();
if (nextEvent.isStartElement()) {
return isServerElement(nextEvent);
}
private static boolean hasSeverRootValues(XMLInputFactory factory, File xmlFile) {
XMLEventReader reader=null;
FileInputStream fis = null;
try {
fis = new FileInputStream(xmlFile);

reader = factory.createXMLEventReader(fis);
while (reader.hasNext()) {
XMLEvent nextEvent = reader.nextEvent();
if (nextEvent.isStartElement()) {
return isServerElement(nextEvent);
}
} catch (XMLStreamException | FileNotFoundException e) {
LOGGER.severe("Error received trying to read XML file: " + xmlFile.getAbsolutePath());
} finally {
if (reader != null) {
try {
reader.close();
} catch (Exception ignored) {
}
}
} catch (XMLStreamException | FileNotFoundException e) {
LOGGER.severe("Error received trying to read XML file: " + xmlFile.getAbsolutePath());
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception ignored) {
}
}
} catch (Exception e) {
LOGGER.severe("Unable to access XML file "+ xmlFile.getAbsolutePath());
}
if (reader != null) {
cherylking marked this conversation as resolved.
Show resolved Hide resolved
try {
reader.close();
} catch (Exception ignored) {
}
}
}

return false;
}

Expand All @@ -92,6 +105,10 @@ private static XMLInputFactory getXmlInputFactory() {
factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
factory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE);
// XMLConstants.ACCESS_EXTERNAL_DTD an empty string to deny all access to external references;
factory.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
cherylking marked this conversation as resolved.
Show resolved Hide resolved
// XMLConstants.ACCESS_EXTERNAL_SCHEMA uses an empty string to deny all access to external references;
factory.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
} catch (Exception e) {
LOGGER.warning("Could not set properties on XMLInputFactory.");
}
Expand All @@ -114,10 +131,18 @@ public static Map<String, String> getElementValues(Path file, Set<String> elemen
}
Map<String, String> returnValues = new HashMap<String, String> ();

XMLInputFactory factory = XMLInputFactory.newInstance();
XMLInputFactory factory = getXmlInputFactory();
readElementValues(file, elementNames, factory, returnValues);
return returnValues;
}

private static void readElementValues(Path file, Set<String> elementNames, XMLInputFactory factory, Map<String, String> returnValues) {
XMLEventReader reader = null;
FileInputStream fis = null;
try {
reader = factory.createXMLEventReader(new FileInputStream(file.toFile()));
fis = new FileInputStream(file.toFile());
reader = factory.createXMLEventReader(fis);

cherylking marked this conversation as resolved.
Show resolved Hide resolved
while (reader.hasNext()) {
XMLEvent event = reader.nextEvent();
if (!event.isStartElement()) {
Expand All @@ -131,21 +156,25 @@ public static Map<String, String> getElementValues(Path file, Set<String> elemen
returnValues.put(elementName, value.getData());
}
}
}
}
} catch (FileNotFoundException e) {
LOGGER.severe("Unable to access file "+ file.toFile().getName());
} catch (XMLStreamException e) {
LOGGER.severe("Error received trying to read XML file " + file.toFile().getName() + " : "+e.getMessage());
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception ignored) {
}
}
if (reader != null) {
try {
reader.close();
} catch (Exception ignored) {
} catch (Exception ignored) {
}
}
}

return returnValues;
}

protected static String getElementName(XMLEvent event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,41 +54,57 @@ public static Map<String, String> getElementValues(File file, Set<String> elemen
return returnValues;
}

readElementValues(file, elementNames, returnValues);

return returnValues;
}

private static void readElementValues(File file, Set<String> elementNames, Map<String, String> returnValues) {
XMLInputFactory factory = getXmlInputFactory();
try {
readElements(file, elementNames, returnValues, factory);
} catch (Exception e) {
LOGGER.severe("Unable to access XML file "+ file.getAbsolutePath());
}
}

private static void readElements(File file, Set<String> elementNames, Map<String, String> returnValues, XMLInputFactory factory) {
XMLEventReader reader = null;
FileInputStream fis = null;
try {
try (FileInputStream fis = new FileInputStream(file)) {
reader = factory.createXMLEventReader(fis);
while (reader.hasNext()) {
XMLEvent nextEvent = reader.nextEvent();
if (!nextEvent.isStartElement()) {
continue;
}
String elementName = getElementName(nextEvent);
if (elementNames.contains(elementName) && reader.hasNext()) {
XMLEvent elementContent = reader.nextEvent();
if (elementContent.isCharacters()) {
Characters value = elementContent.asCharacters();
returnValues.put(elementName, value.getData());
}
}
fis = new FileInputStream(file);
reader = factory.createXMLEventReader(fis);
while (reader.hasNext()) {
XMLEvent nextEvent = reader.nextEvent();
if (!nextEvent.isStartElement()) {
continue;
}
} catch (XMLStreamException | FileNotFoundException e) {
LOGGER.severe("Error received trying to read XML file: " + file.getName() +
"\n\tError" + e.getMessage());
} finally {
if (reader != null) {
try {
reader.close();
} catch (Exception ignored) {
String elementName = getElementName(nextEvent);
if (elementNames.contains(elementName) && reader.hasNext()) {
XMLEvent elementContent = reader.nextEvent();
if (elementContent.isCharacters()) {
Characters value = elementContent.asCharacters();
returnValues.put(elementName, value.getData());
}
}
}
} catch (Exception e) {
LOGGER.severe("Unable to access XML file "+ file.getAbsolutePath());
}
} catch (XMLStreamException | FileNotFoundException e) {
LOGGER.severe("Error received trying to read XML file: " + file.getName() +
"\n\tError" + e.getMessage());
} finally {
if (reader != null) {
try {
reader.close();
} catch (Exception ignored) {
}
}
if (fis != null) {
try {
fis.close();
} catch (Exception ignored) {
}
}
}

return returnValues;
}

private static XMLInputFactory getXmlInputFactory() {
Expand All @@ -98,7 +114,10 @@ private static XMLInputFactory getXmlInputFactory() {
factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
factory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE);

// XMLConstants.ACCESS_EXTERNAL_DTD an empty string to deny all access to external references;
factory.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
cherylking marked this conversation as resolved.
Show resolved Hide resolved
// XMLConstants.ACCESS_EXTERNAL_SCHEMA uses an empty string to deny all access to external references;
factory.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
} catch (Exception e) {
LOGGER.warning("Could not set properties on XMLInputFactory.");
}
Expand Down
Loading