在 javax XML 验证器中禁用 DTD
Disable DTD in javax XML Validator
我正在使用 javax.xml.validation.Validator 验证我的 xml 如下 -
Validator validator = myschema.newValidator();
validator.validate(new StreamSource(new StringReader(xmlString)));
我想通过完全禁用 DTD(文档类型定义)来防止 XML 外部实体攻击,所以我希望验证器在我的 [=18] 中出现 DTD 时抛出异常=] 如果可能的话。我已经阅读了有关使用 DocumentBuilderFactory
执行此操作的信息。我如何在 Validator 中配置它?
根据 OWASP XXE prevention spreadsheet 对于 Java,以下应该有效:
SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
Schema myschema = factory.newSchema();
Validator validator = myschema.newValidator();
try {
validator.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
validator.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
validator.validate(new StreamSource(new StringReader(xmlString)));
} catch ...
详情请参阅XMLConstants
JavaDocs。
这也行-
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
Schema myschema = factory.newSchema();
Validator validator = myschema.newValidator();
validator.validate(new StreamSource(new StringReader(xmlString)));
我正在使用 javax.xml.validation.Validator 验证我的 xml 如下 -
Validator validator = myschema.newValidator();
validator.validate(new StreamSource(new StringReader(xmlString)));
我想通过完全禁用 DTD(文档类型定义)来防止 XML 外部实体攻击,所以我希望验证器在我的 [=18] 中出现 DTD 时抛出异常=] 如果可能的话。我已经阅读了有关使用 DocumentBuilderFactory
执行此操作的信息。我如何在 Validator 中配置它?
根据 OWASP XXE prevention spreadsheet 对于 Java,以下应该有效:
SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
Schema myschema = factory.newSchema();
Validator validator = myschema.newValidator();
try {
validator.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
validator.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
validator.validate(new StreamSource(new StringReader(xmlString)));
} catch ...
详情请参阅XMLConstants
JavaDocs。
这也行-
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
Schema myschema = factory.newSchema();
Validator validator = myschema.newValidator();
validator.validate(new StreamSource(new StringReader(xmlString)));