简体   繁体   English

使用Jaxb2Marshaller防止解析包含DTD的XML文件

[英]Prevent parsing XML files containing DTD using Jaxb2Marshaller

I saw many solutions using XMLInputFactory, SAXParser and DocumentBuilderFactory. 我看到了很多使用XMLInputFactory,SAXParser和DocumentBuilderFactory的解决方案。 Our project is spring web service and the only thing we do is: 我们的项目是春季网络服务,我们唯一做的是:

@Bean
public Jaxb2Marshaller unmarshaller() {
   Jaxb2Marshaller unmarshaller = new Jaxb2Marshaller();
   unmarshaller.setContextPath("foo");
   unmarshaller.setProcessExternalEntities(false);
   return unmarshaller;
}    

And then we pass this marshaller and unmarshaller to MarshallingPayloadMethodProcessor. 然后我们将这个marshaller和unmarshaller传递给MarshallingPayloadMethodProcessor。 So my question is if there is some property for Jaxb2Marshaller that will prevent DTD. 所以我的问题是,如果Jaxb2Marshaller有一些属性可以阻止DTD。 Something like: unmarshaller.setProperty(foo.SUPPORT_DTD, false); 类似的东西: unmarshaller.setProperty(foo.SUPPORT_DTD, false);

We have .xsd schema but in case of xml bomb the entity needs to be exanded for purpose of validation, so it seems like this is not the solution. 我们有.xsd架构但是在xml炸弹的情况下,实体需要被扩展用于验证,所以看起来这不是解决方案。

As far as I can see from the code, this must be the default behaviour. 从代码中我可以看出,这必须是默认行为。

In the JAXB RI there is a context property com.sun.xml.bind.disableXmlSecurity which is reasonably set to false by default. 在JAXB RI中,有一个上下文属性com.sun.xml.bind.disableXmlSecurity ,默认情况下合理地设置为false JAXB RI the uses this property when it creates the parser . JAXB RI在创建解析器时使用此属性。 So, at the end it configures the FEATURE_SECURE_PROCESSING feature of the parser: 因此,最后它配置解析器的FEATURE_SECURE_PROCESSING功能:

        SAXParserFactory factory = SAXParserFactory.newInstance();
        if (LOGGER.isLoggable(Level.FINE)) {
            LOGGER.log(Level.FINE, "SAXParserFactory instance: {0}", factory);
        }
        factory.setNamespaceAware(true);
        factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, !isXMLSecurityDisabled(disableSecureProcessing));
        return factory;

You can also use the system property javax.xml.accessExternalDTD . 您还可以使用系统属性javax.xml.accessExternalDTD

See also this answer: 另见这个答案:

How to disable DTD fetching using JAXB2.0 如何使用JAXB2.0禁用DTD获取

If you want to make it ever more secure, you may write and configure your own entity resolver . 如果您想让它更安全,您可以编写和配置自己的实体解析器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM