[英]Spring MVC application filtering HTML in URL - Is this a security issue?
我现有的 Spring Web MVC 应用程序在 Controller 中有以下处理程序映射。
@RequestMapping(method = RequestMethod.GET, value = "/welcome")
我触发了以下请求http://www.example.com/welcome
并且效果很好。
问题是
http://www.example.com/welcome.check.blah
也有效!!!
此外,带有脚本标记的应用程序的 HTTP GET 请求 URL 正在重新显示,尽管它未通过授权。
示例http://www.example.com/welcome<script>alert("hi")</script>
在浏览器窗口中重新显示,并且由于我的授权逻辑“未授权”消息显示。
我想知道这是否是一个安全问题,我是否需要在代码中进行任何编码/过滤?
此行为是由于选项useSuffixPatternMatch
,它在RequestMappingHandlerMapping
默认为true(我假设您使用的是Spring MVC 3.1)。
useSuffixPatternMatch:在将模式与请求匹配时是否使用后缀模式匹配(“。*”)。 如果启用,映射到“/ users”的方法也匹配“/users.*”。 默认值是true”。
要将useSuffixPatternMatch
设置为false ,最简单的方法是使用@Configuration
:
@Configuration
@EnableWebMvc
public class Api extends WebMvcConfigurationSupport {
@Override
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
RequestMappingHandlerMapping mapping = super.requestMappingHandlerMapping();
mapping.setUseSuffixPatternMatch(false);
return mapping;
}
}
在当前的Spring Java配置中,有一种稍微简单的方法来配置相同的东西:
@Configuration
public class DispatcherConfig extends WebMvcConfigurationSupport {
@Override
protected void configurePathMatch(PathMatchConfigurer configurer) {
configurer.setUseSuffixPatternMatch(false);
}
}
您可以使用useDefaultSuffixPattern属性。
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="useDefaultSuffixPattern" value="false" />
</bean>
当您使用Spring请求该类型的映射(即“/ anything”)时,Spring实际上将您的控制器映射到多个URL:
/欢迎
/欢迎。*
/欢迎/
要防止这种情况 - 要么在RequestMapping(即/welcome.htm)时更具体,要么手动将URL映射到Xml配置中的控制器:
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/welcome">YourControllerBean</prop>
</props>
</property>
</bean>
干杯,皮特
您还可以通过提及url模式在web.xml中对此进行限制。 您可以在web.xml中提及“/ .htm”而不是“/ ” 。
就像是
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/application/*.htm</url-pattern>
</servlet-mapping>
从 Spring 框架 5.3 开始, useDefaultSuffixPattern 已被弃用并默认关闭。 Spring 升级笔记, “Spring MVC 中不推荐使用路径扩展的使用”部分
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.