DefaultWebInvocationPrivilegeEvaluator gets wrong AbstractSecurityInterceptor, causing privilege evaluation to fail
currently I'm on a course named "Learning Spring Boot" by Greg L. Turnquist @gregturn , and met an issue that's identical with this one which was closed because of this SO answer, but I firmly believe that's not the real answer and not related to this issue, so I create a test repo here, which will reproduce the same issue if @EnableWebSecurity is commented out on SecurityConfiguration like below:
@Configuration
@EnableWebSecurity//if you forgot to add this annotation, thymeleaf th:authorize-url fail to work
//@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
}
when @EnableWebSecurity is commented out, th:authorize-url in index.html won't work correctly:
<form sec:authorize-url="POST /imgs" method="post" th:action="@{/imgs}" enctype="multipart/form-data">
<p><input name="file" type="file" /></p>
<p><input name="Upload" type="submit" value="Upload"/></p>
</form>
and I also found an article which've analyzed this issue this year, although I know that put @EnableWebSecurity on SecurityConfiguration like above will workaround this issue, but what I don't understand is that I thought we only need to provide our own WebSecurityConfigurer to override the default WebSecurityConfiguration, in that case why do we still have to explicitly add @EnableWebSecurity to address this issue, is this a boot-auto-config related or thymeleaf-related bug ?
@wilkinsona @dsyer @gregturn @rwinch @joshlong @philwebb
in Learning Spring Boot by Greg L. Turnquist, Lesson Chapter 13 : Securing Operations with Authorization Rules - Adding URL-based Security controls, he solved this issue by adding @EnableWebSecurity but didn't mention why.
Update
I've also tried adding @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) on my SecurityConfiguration without adding @EnableWebSecurity, 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. because I find that all WebSecurityConfigurer are ordered according to its @Order value in ascending manner within org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration as below, so as long as my custom WebSecurityConfigurer ordered to be the last one, everything works fine.
package org.springframework.security.config.annotation.web.configuration;
@Configuration
public class WebSecurityConfiguration implements ImportAware, BeanClassLoaderAware {
//......
@Autowired(required = false)
public void setFilterChainProxySecurityConfigurer(
ObjectPostProcessor<Object> objectPostProcessor,
@Value("#{@autowiredWebSecurityConfigurersIgnoreParents.getWebSecurityConfigurers()}") List<SecurityConfigurer<Filter, WebSecurity>> webSecurityConfigurers)
throws Exception {
webSecurity = objectPostProcessor
.postProcess(new WebSecurity(objectPostProcessor));
if (debugEnabled != null) {
webSecurity.debug(debugEnabled);
}
//order by asc, that's why I have to give my custom WebSecurityConfigurer a larger order value for it to be ordered as the last one
Collections.sort(webSecurityConfigurers, AnnotationAwareOrderComparator.INSTANCE);
Integer previousOrder = null;
Object previousConfig = null;
for (SecurityConfigurer<Filter, WebSecurity> config : webSecurityConfigurers) {
Integer order = AnnotationAwareOrderComparator.lookupOrder(config);
if (previousOrder != null && previousOrder.equals(order)) {
throw new IllegalStateException(
"@Order on WebSecurityConfigurers must be unique. Order of "
+ order + " was already used on " + previousConfig + ", so it cannot be used on "
+ config + " too.");
}
previousOrder = order;
previousConfig = config;
}
for (SecurityConfigurer<Filter, WebSecurity> webSecurityConfigurer : webSecurityConfigurers) {
webSecurity.apply(webSecurityConfigurer);
}
this.webSecurityConfigurers = webSecurityConfigurers;
}
//......
}
This looks to either be a bug in Spring Security or in Thymeleaf's Spring Security integration, but I'm not sure which. I'd like to get some help from @rwinch and @jgrandja.
Rob, Joe, you can reproduce the problem by running the application with the dev profile active. If you open http://localhost:8080 in your browser and login using jack as the username and password, you'll see that the upload and choose file buttons appear in the UI when they shouldn't do so. jack is not an admin and that URL requires the admin role. Indeed, if you attempt to upload a file it will fail with a 403. In short, the upload URL is secure, but Thymeleaf's failing to reflect that fact when it's generating the HTML.
As described in the blog post, the security check is performed by Thymeleaf's AuthorizeUrlAttrProcessor. It winds up calling the context's single WebInvocationPrivilegeEvaluator (which is a DefaultWebInvocationPrivilegeEvaluator) and getting what appears to be the wrong answer when it calls isAllowed.
I believe it will turn out to be a bug in your code. Maybe you could try and create a more minimal sample, e.g. based on one of the samples in Spring Boot?
as @wilkinsona have tested, you can reproduce the the same issue. @dsyer
Here's a much simpler application that reproduces the problem.
Without @EnableWebSecurity the Secure link will appear when logged in as either tom (tom:tom) or jack (jack:jack) (but it will only work when logged in as tom). With @EnableWebSecurity, the Secure link only appears when logged in as tom.
A slightly different workaround is to set @Order(SecurityProperties.BASIC_AUTH_ORDER + 1) (to order your custom filter after the Boot one), but set security.basic.path=/none (a non-existent path), so that the default rules are not applied but the Thymeleaf expressions evaluate using the right interceptor. That ensures that you get the ignored paths support from Spring Boot, otherwise you'd have to add it back in yourself.
It looks like this needs to be addressed in Spring Security so I'm going to close this. We'll automatically pick up any fix that is made in Security in due course.