DefaultWebInvocationPrivilegeEvaluator gets wrong AbstractSecurityInterceptor, causing privilege evaluation to fail
Hello,
I am experiencing this bug: https://stackoverflow.com/questions/23686022/spring-boot-security-thymeleaf-secauthorize-url-not-working on my Spring boot setup.
I've tracked it down to the DefaultWebInvocationPrivilegeEvaluator having a different AbstractSecurityInteceptor then the one with the settings I configure in my Spring Security configuration.
The SecurityInterceptor used by ``DefaultWebInvocationPrivilegeEvaluatoronly contains a single record -/** - hasAnyRole("USER", "ADMIN"), and as such any use of sec:authorize-url` fails, however actually visiting the URL does as expected.
I believe this could be due to multiple WebSecurityConfigurerAdapter, since the post build action being ran, setting the securityInterceptor which in turn is used by getPrivilegeEvaluator used by Thymeleaf in its sec:authorize-url processor, ends up up setting the securityInterceptor to the last instance of the WebSecurityConfigurerAdapter being loaded?
public void init(final WebSecurity web) throws Exception {
final HttpSecurity http = getHttp();
web
.addSecurityFilterChainBuilder(http)
.postBuildAction(new Runnable() {
public void run() {
FilterSecurityInterceptor securityInterceptor = http.getSharedObject(FilterSecurityInterceptor.class);
web.securityInterceptor(securityInterceptor);
}
});
}
I am using Spring boot 1.1.4.
@dsyer any ideas?
I have no idea how the Thymeleaf tags work. It would help a lot if we could isolate this to something that didn't involve Thymeleaf, or else get some more detail about how Thymeleaf works.
Thymeleaf uses processors for every attribute, which is allowed to modify the structure of the DOM to hide/add objects, edit values and so on so forth.
This is the processor for sec:authorize-url: https://github.com/thymeleaf/thymeleaf-extras-springsecurity3/blob/2.1-master/src/main/java/org/thymeleaf/extras/springsecurity3/dialect/processor/AuthorizeUrlAttrProcessor.java
The authorization happens in org.thymeleaf.extras.springsecurity3.auth.AuthUtils#authorizeUsingUrlCheck: [source]
The code obtains the first (and only) DefaultWebInvocationPrivilegeEvaluator instance, and then calls DefaultWebInvocationPrivilegeEvaluator#isAllowed(String contextPath, String uri, String method, Authentication authentication)
phlebas updated the Stack overflow issue and he seems to believe the issue is somehow related to the auto configured basic authentication.
I can't quite reproduce the problem, but there's something bad going on in there. Here's a sample app: https://github.com/spring-projects/spring-boot-issues/tree/master/gh-1319 (you get a NPE instead of the wrong rule evaluations). Some notes:
From WebSecurityConfigurerAdapter.init(WebSecurity):
public void init(final WebSecurity web) throws Exception {
final HttpSecurity http = getHttp();
web
.addSecurityFilterChainBuilder(http)
.postBuildAction(new Runnable() {
public void run() {
FilterSecurityInterceptor securityInterceptor = http.getSharedObject(FilterSecurityInterceptor.class);
web.securityInterceptor(securityInterceptor);
}
});
}
I actually don't understand how this is supposed to work since there
is only one WebSecurity (and only one post build hook) and there can
be many WebSecurityConfigurerAdapters (and many HttpSecurity
instances).
The problem I am seeing is that I get a null
FilterSecurityInterceptor from one of my
WebSecurityConfigurerAdapters and it happens to be the last one
applied, so it leads to a null WebInvocationPrivilegeEvaluator (and
errors in JSP/Thymeleaf rendering). I don't understand how the
renderer support supposed to work either because the it needs to know
the runtime behaviour of the whole filter chain, but it's only ever
going to get one HttpSecurity configuration this way, even if it
isn't null.
To see the null error, just take the "spring-boot-sample-web-secure" app and add Thymeleaf Spring Security, then put this in the home page:
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
...
<div sec:authorize-url="/" th:text="${message}">Fake content</div>
...
Run the app, log in and get an NPE.
As @dsyer noticed, the configured WebSecurity has no FilterSecurityInterceptor set and therefore returns null for the PrivilegeEvaluator. The http.getSharedObject(FilterSecurityInterceptor.class); returns null for some reason.
However, you can workaround this, by simply adding the following to your custom ApplicationSecurity:
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
protected static class ApplicationSecurity extends WebSecurityConfigurerAdapter {
@Override
public void configure(final WebSecurity web) throws Exception {
final HttpSecurity http = getHttp();
web.postBuildAction(new Runnable() {
@Override
public void run() {
web.securityInterceptor(http.getSharedObject(FilterSecurityInterceptor.class));
}
});
}
Now, at this point the shared object will not be null, so something odd must thave happened between the WebSecurity.init and WebSecurity.configure, therefore a DefaultWebInvocationPrivilegeEvaluator will be instantiated and the authorize-url tag will work, at least for URL mappings, it will not respect any @Secured annotations though, which is technically obvious looking at thymeleaf security dialect, but not the expected behaviour, at least from my point of view.
Having basic autoconfig enabled will return a PrivilegeEvaluator, but the wrong one, since it comes from a different WebSecurity that does not know about our custom URL mappings.
Using the method outlined above however, we can have BASIC enabled again, use the recommended ACCESS_OVERRIDE_ORDER and still have the authorize-url tag working.
I will update my question at SO asap. Maybe someone has an explanation.
I can see the SO issue has an accepted answer and that's the version that boot brings now. Let me know if I am missing anything.
I saw this issue was opened in the year 2014, but now, it's 2017, the issue still exists. I'm using spring boot 1.4.7.RELEASE, and faced with the same issue above. I've seen the SO answer, but it's not relevant. this article had analyzed the whole process and found a workaround: http://vkuzel.blogspot.com/2017/02/how-not-to-get-confused-by-spring-boot.html but I think it's related with org.springframework.boot.autoconfigure.security.SpringBootWebSecurityConfiguration.ApplicationWebSecurityConfigurerAdapter and not related with the SO answer.
@allbutone As explained above and in the question on Stack Overflow , we believe this issue is addressed by using Thymeleaf's Spring Security 4 integration.
If you believe that's not the case, please open a new issue with a minimal sample that reproduces the problem.
@wilkinsona I've updated my comment, and put link below, someone else had analysed this issue: http://vkuzel.blogspot.com/2017/02/how-not-to-get-confused-by-spring-boot.html
the real workaround is to put @EnableWebSecurity on custom WebSecurityConfigurer, if not, the issue will reproduce, what I dont understand is that I thought we only need to custom default WebSecurityConfiguration by providing our own WebSecurityConfigurer, why do we have to explicitly add @EnableWebSecurity to address this issue.
currently I'm on a course named "Learning Spring Boot" by Greg L. Turnquist @gregturn , and in lesson Chapter 13 : Securing Operations with Authorization Rules - Adding URL-based Security controls, he solved this issue by adding @EnableWebSecurity but didn't mention too much.
Unfortunately, I think both Greg's video and that blog post are likely to have given you bad advice as @EnableWebSecurity is rarely the right answer in a Spring Boot application. It should only be used if you want to switch off all of Spring Boot's auto-configuration of web security and anything that talks about using it should make it very clear that is what it does.
A better alternative is to provide your own WebSecurityConfigurerAdapter with an order that means it takes precedence over the auto-configured ApplicationWebSecurityConfigurerAdapter. The auto-configured adapter uses SecurityProperties.BASIC_AUTH_ORDER and you can use @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) on your own adapter to override it.
If you have further questions, please follow up on Stack Overflow or Gitter. If you still believe that there's a bug in Spring Boot, please raise a new issue as described above.
thank you @wilkinsona for your quick answer, I've opened a detailed issue here, and I also don't understand why I have to switch off the whole WebSecurityConfiguration to solve this issue, I've also tried your suggestion by adding @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER), it still won't work, I have to adjust the order value higher than SecurityProperties.BASIC_AUTH_ORDER which is the default order of org.springframework.boot.autoconfigure.security.SpringBootWebSecurityConfiguration.ApplicationWebSecurityConfigurerAdapter to make it work. as long as my custom WebSecurityConfigurer ordered to be the last one, everything works fine.