Prevent XXE (External Entity Processing) Attack with JAXB + Spring RESTful Web Services

本小妞迷上赌 提交于 2019-12-07 21:59:50

问题


I know that we can prevent the XXE attack by setting the property IS_SUPPORTING_EXTERNAL_ENTITIES in the abstract class XMLInputFactory to false in JAXB.

I have also seen this stackoverflow answer.

My question here is,

How do I create a instance of XMLInputFactory and set this IS_SUPPORTING_EXTERNAL_ENTITIES property to false when the spring application loads up. And that particular XMLInputFactory instance should only be used for all the JAXB conversion for all the classes that uses javax.xml.bind.annotation package.


回答1:


Spring uses RequestMappingHandlerAdapter which is an AbstractHandlerMethodAdapter that supports HandlerMethods with the signature -- method argument and return types, defined in @RequestMapping.

There are 7 seven HttpMessageConverters and one of them is Jaxb2RootElementHttpMessageConverter

Jaxb2RootElementHttpMessageConverter is from the spring-web package.

From 3.2.8 version of spring-web onwards Jaxb2RootElementHttpMessageConverter sets the processExternalEntities to false which in turn sets the XMLInputFactory property IS_SUPPORTING_EXTERNAL_ENTITIES to false.

Refer :

http://grepcode.com/file/repo1.maven.org/maven2/org.springframework/spring-web/3.2.8.RELEASE/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverter.java?av=f

Answer use
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.2.8.RELEASE</version> </dependency>



来源:https://stackoverflow.com/questions/28310617/prevent-xxe-external-entity-processing-attack-with-jaxb-spring-restful-web-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!