Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StrictHttpFirewall in spring security 4.2 vs spring MVC @MatrixVariable

Tags:

Having upgraded to spring security 4.2.4 I discovered that StrictHttpFirewall is now the default. Unfortunately it doesn't play well with spring MVC @MatrixVariable since ";" are not allowed anymore. How to get around that?

Example:

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

This could be called like this:

mockMvc.perform(get("/someparam;key=value")) 

And the matrix map would be populated. Now spring security blocks it.

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) 

I could use a custom HttpFirewall that would allow semicolons. Is there a way to use @MatrixVariable without using forbidden characters?

BTW: the javadoc is incorrect https://docs.spring.io/autorepo/docs/spring-security/4.2.x/apidocs/index.html?org/springframework/security/web/firewall/StrictHttpFirewall.html

Since:

5.0.1

I guess it was backported?

like image 719
Крис Avatar asked Feb 02 '18 10:02

Крис


People also ask

Can we use Spring Security in Spring MVC?

To enable Spring Security integration with Spring MVC add the @EnableWebSecurity annotation to your configuration. Spring Security provides the configuration using Spring MVC's WebMvcConfigurer.

What is StrictHttpFirewall?

The StrictHttpFirewall provides an allowed list of valid HTTP methods that are allowed to protect against Cross Site Tracing (XST) and HTTP Verb Tampering.

Which authorization levels are supported by Spring Security?

1. Overview. Simply put, Spring Security supports authorization semantics at the method level. Typically, we could secure our service layer by, for example, restricting which roles are able to execute a particular method — and test it using dedicated method-level security test support.

How do I disable StrictHttpFirewall?

There is no way to disable this as it is considered extremely risky to disable this constraint. A few options to allow this behavior is to normalize the request prior to the firewall or using DefaultHttpFirewall instead.


1 Answers

You can dilute the default spring security firewall using your custom defined instance of StrictHttpFirewall (at your own risk)

@Bean public HttpFirewall allowUrlEncodedSlashHttpFirewall() {     StrictHttpFirewall firewall = new StrictHttpFirewall();     firewall.setAllowUrlEncodedSlash(true);     firewall.setAllowSemicolon(true);     return firewall; } 

And then use this custom firewall bean in WebSecurity (Spring boot does not need this change)

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

That shall work with Spring Security 4.2.4+, but of-course that brings some risks!

like image 53
Munish Chandel Avatar answered Sep 23 '22 17:09

Munish Chandel