|
17 | 17 | import java.util.Map; |
18 | 18 | import java.util.Optional; |
19 | 19 |
|
| 20 | +import javax.xml.parsers.ParserConfigurationException; |
20 | 21 | import javax.xml.parsers.SAXParserFactory; |
21 | 22 | import javax.xml.transform.Source; |
22 | 23 | import javax.xml.transform.sax.SAXSource; |
@@ -271,7 +272,25 @@ public static ValidationReport validateDescriptiveBinary(ContentPayload descript |
271 | 272 | try (InputStreamReader inputStreamReader = new InputStreamReader( |
272 | 273 | new BOMInputStream(descriptiveMetadataPayload.createInputStream()))) { |
273 | 274 |
|
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 | + |
275 | 294 | xmlReader.setEntityResolver(new RodaEntityResolver()); |
276 | 295 | InputSource inputSource = new InputSource(inputStreamReader); |
277 | 296 | Source source = new SAXSource(xmlReader, inputSource); |
|
0 commit comments