Description
Whether to match to URLs irrespective of the presence of a trailing slash. If enabled a method mapped to
"/users"
also matches to"/users/"
.
The default value istrue
.
Even though this behavior has been long present in Spring, it introduces ambiguity that can (combined with some other choices) easily have consequences in shape of security vulnerabilities. Consider this example:
@SpringBootApplication
@RestController
public class SampleApplication {
public static void main(String[] args) {
SpringApplication.run(SampleApplication.class, args);
}
@GetMapping("/resources")
String resources() {
return "Hello from /resources";
}
@GetMapping("/resources/{id}")
String resourceById(@PathVariable Long id) {
return "Hello from /resources/" + id;
}
@Bean
SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
return httpSecurity
.authorizeHttpRequests(requests -> {
requests.antMatchers("/resources").hasRole("admin");
requests.antMatchers("/resources/**").hasRole("user");
requests.anyRequest().denyAll();
})
.httpBasic(Customizer.withDefaults())
.build();
}
}
spring.security.user.password=password
spring.security.user.roles=user
Default user (with role user
) will get 403
attempting to GET /resources
but can avoid protection by issuing GET /resources/
, which wouldn't be possible with trailing slash matching disabled.
Let me note that I'm aware that using mvcMatchers
instead of antMatchers
would have prevented this issue but that doesn't change the fact that there are many configurations out there relying on antMatchers
and that sometimes antMatchers
are simply more suitable for other reasons.
Also, I personally see little real benefit of having trailing slash matching enabled because:
- if application renders server-side generated views navigation is either way established using hyperlinks
- if application exposes APIs then even more so it is expected that requests are aligned with the API docs
For places where it's really needed for application to respond to both requests, I'd argue that it's either way better solution to configure redirects instead of application wide ambiguous request mappings.
Please consider making this change for 6.0
.