Skip to content

Commit b26d72b

Browse files
Potential fix for code scanning alert no. 166: Resolving XML external entity in user-controlled data (#3582)
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent 7eddcae commit b26d72b

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

roda-core/roda-core/src/main/java/org/roda/core/common/validation/ValidationUtils.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.Map;
1818
import java.util.Optional;
1919

20+
import javax.xml.parsers.ParserConfigurationException;
2021
import javax.xml.parsers.SAXParserFactory;
2122
import javax.xml.transform.Source;
2223
import javax.xml.transform.sax.SAXSource;
@@ -271,7 +272,25 @@ public static ValidationReport validateDescriptiveBinary(ContentPayload descript
271272
try (InputStreamReader inputStreamReader = new InputStreamReader(
272273
new BOMInputStream(descriptiveMetadataPayload.createInputStream()))) {
273274

274-
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
275+
XMLReader xmlReader;
276+
try {
277+
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
278+
saxParserFactory.setNamespaceAware(true);
279+
// Enable secure processing
280+
saxParserFactory.setFeature("http://javax.xml.XMLConstants/feature/secure-processing", true);
281+
// Disallow DOCTYPE declarations
282+
saxParserFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
283+
// Disable external entities
284+
saxParserFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
285+
saxParserFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
286+
// Do not load external DTDs
287+
saxParserFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
288+
289+
xmlReader = saxParserFactory.newSAXParser().getXMLReader();
290+
} catch (ParserConfigurationException | SAXException e) {
291+
throw new IOException("Failed to securely configure XML parser", e);
292+
}
293+
275294
xmlReader.setEntityResolver(new RodaEntityResolver());
276295
InputSource inputSource = new InputSource(inputStreamReader);
277296
Source source = new SAXSource(xmlReader, inputSource);

0 commit comments

Comments
 (0)