漏洞公告:https://spring.io/security/cve-2024-38821

该漏洞成因主要是Spring WebFlux静态资源解析在特定情况下与Spring Security的pathMatchers的匹配存在解析差异,从而导致了权限绕过。
Spring Security 提供的注解 @EnableWebFluxSecurity ,是用于Spring WebFlux应用程序中启用 Spring Security 功能。这个注解会激活 Spring Security 的 WebFlux 支持,允许你定义安全配置,比如认证、授权、CSRF 保护等。
而pathMatchers() 方法用于指定哪些路径需要被特定的安全配置所应用,可以通过该方法来设置哪些路径应该被认证、哪些路径可以公开访问等。

pathMatchers的匹配主要跟PathPatternParserServerWebExchangeMatcher有关。
org.springframework.security.config.web.server.AbstractServerWebExchangeMatcherRegistry#pathMatchers(java.lang.String…)
org.springframework.security.web.server.util.matcher.ServerWebExchangeMatchers#pathMatchers(org.springframework.http.HttpMethod, java.lang.String…)
而PathPatternParserServerWebExchangeMatcher的matches方法主要用来用来判断请求是否匹配。实际上使用的PathPattern模式进行解析。在PathPattern解析模式,解析 ../ 会当成一个目录进行处理,不会进行额外的目录回溯。比如/static/admin/../secret.txt将无法被pathMatchers(“/static/*.txt”).hasRole(“USER”)给匹配到。
org.springframework.security.web.server.util.matcher.PathPatternParserServerWebExchangeMatcher#matches
而在之前的Spring Framework路径遍历漏洞(CVE-2024-38816)漏洞中,我们知道会调用StringUtils.cleanPath对路径进行处理,比如/static/admin/../secret.txt会处理成/static/secret.txt。正是这种差异导致了认证的绕过。

