StrictHttpFirewall在spring security 4.2 vs spring MVC @MatrixVariable

Кри*_*рис 15 spring-mvc spring-security

升级到spring security 4.2.4后,我发现StrictHttpFirewall现在是默认设置.不幸的是,它与弹簧MVC @MatrixVariable的效果不佳,因为";" 是不允许的.怎么解决这个问题?

例:

@GetMapping(path = "/{param}")
public void example(@PathVariable String param,
                    @MatrixVariable Map<String, String> matrix) {
    //...
}
Run Code Online (Sandbox Code Playgroud)

这可以像这样调用:

mockMvc.perform(get("/someparam;key=value"))
Run Code Online (Sandbox Code Playgroud)

并填充矩阵图.现在Spring安全阻止了它.

org.springframework.security.web.firewall.RequestRejectedException: The request was rejected because the URL contained a potentially malicious String ";"

at org.springframework.security.web.firewall.StrictHttpFirewall.rejectedBlacklistedUrls(StrictHttpFirewall.java:140)
Run Code Online (Sandbox Code Playgroud)

我可以使用一个允许分号的自定义HttpFirewall.有没有办法在不使用禁用字符的情况下使用@MatrixVariable?

BTW:javadoc不正确https://docs.spring.io/autorepo/docs/spring-security/4.2.x/apidocs/index.html?org/springframework/security/web/firewall/StrictHttpFirewall.html

以来:

5.0.1

我想这是向后移植的?

Mun*_*del 29

您可以使用自定义的StrictHttpFirewall实例稀释默认的spring安全防火墙(风险自负)

@Bean
public HttpFirewall allowUrlEncodedSlashHttpFirewall() {
    StrictHttpFirewall firewall = new StrictHttpFirewall();
    firewall.setAllowUrlEncodedSlash(true);
    firewall.setAllowSemicolon(true);
    return firewall;
}
Run Code Online (Sandbox Code Playgroud)

然后在WebSecurity中使用此自定义防火墙bean(Spring启动不需要此更改)

@Override
public void configure(WebSecurity web) throws Exception {
  super.configure(web);
  // @formatter:off
  web.httpFirewall(allowUrlEncodedSlashHttpFirewall());
...
}
Run Code Online (Sandbox Code Playgroud)

这适用于Spring Security 4.2.4+,但当然会带来一些风险!

  • 在`configure(WebSecurity web)`方法中连接bean不应该是必需的,因为`WebSecurity`实例试图通过使用`ApplicationContextAware`注释在上下文中找到类型为`HttpFirewall`的bean.换句话说,只是实例化一个类型为"HttpFirewall"的bean就足够了. (6认同)
  • 由于某种原因,它不适用于 Spring Security 5.0.3.RELEASE。 (3认同)
  • StrictHttpFirewall阻止请求,因为允许它们很危险。由于Servlet规范中的含糊之处以及Servlet容器之间的行为不一致(缺少拒绝URL编码的分号),因此我们无法找到一种很好的方法来防止此类攻击。 (2认同)

mor*_*n.c 7

正如???? 在评论中,如果您更喜欢使用 XML 方法,您可以将以下部分添加到您的 securityContext.xml(或任何与 spring-security 相关的 xml-config 调用):

<bean id="allowSemicolonHttpFirewall" 
      class="org.springframework.security.web.firewall.StrictHttpFirewall"> 
        <property name="allowSemicolon" value="true"/> 
</bean> 
<security:http-firewall ref="allowSemicolonHttpFirewall"/>
Run Code Online (Sandbox Code Playgroud)

<bean>部分定义了一个StrictHttpFirewall带有 id的新bean,allowSemicolonHttpFirewall然后<security>通过引用id 将其设置为标签中的默认 http-firewall 。