我正在尝试实现一个 XML 验证,它应该可以防止 XXE 注入。OWASP 页面上显示的代码与本机 JDK8 完美配合。
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(fSchema);
Validator validator = schema.newValidator();
validator.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
validator.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
validator.validate(new StreamSource(fXml));
问题是我在内部使用 Xerces2 (xercesImpl-2.11.0.SP4) 并且无法识别所需的 XMLConstants 的 Wildfly10 上使用此代码。
Exception in thread "main" org.xml.sax.SAXNotRecognizedException: Property 'http://javax.xml.XMLConstants/property/accessExternalDTD' is not recognized.
在具有给定 Maven 依赖项的单元测试中,问题很容易重现
<!-- https://mvnrepository.com/artifact/xerces/xercesImpl -->
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.11.0.SP4</version>
</dependency>
虽然可以使用参数停用 Wildfly10 上的 Xerces2
-jaxpmodule“javax.xml.jaxp-provider”
这不是我想做的。
有人知道如何正确配置 Xerxces2 以防止 XXE 注入...