Skip to content

Spring websession's CSRF with Redis fails with JSON serialization #1478

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
apuravchauhan opened this issue Jul 22, 2019 · 3 comments
Closed
Assignees
Labels
for: external-project For an external project and not something we can fix

Comments

@apuravchauhan
Copy link

Spring websession with Redis fails with the attached exception. Below is the project config:

@EnableWebFluxSecurity
@EnableReactiveMethodSecurity
@EnableRedisWebSession 
public class WebfluxConfig implements BeanClassLoaderAware {

	private ClassLoader loader;

	@Bean
	public RedisSerializer<Object> springSessionDefaultRedisSerializer() {
		return new GenericJackson2JsonRedisSerializer(objectMapper());
	}

	private ObjectMapper objectMapper() {
		ObjectMapper mapper = new ObjectMapper();
		mapper.registerModules(SecurityJackson2Modules.getModules(this.loader));
		return mapper;
	}

	@Override
	public void setBeanClassLoader(ClassLoader classLoader) {
		this.loader = classLoader;
	}

	@Bean
	public MapReactiveUserDetailsService userDetailsService() {
		User.UserBuilder userBuilder = User.withDefaultPasswordEncoder();
        UserDetails rob = userBuilder.username("rob")
            .password("rob")
            .roles("USER")
            .build();
        UserDetails admin = userBuilder.username("admin")
            .password("admin")
            .roles("USER","ADMIN")
            .build();
        return new MapReactiveUserDetailsService(rob, admin);
	}
	@Bean
    public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
        http
            .authorizeExchange()
                .anyExchange().authenticated()
                .and()
            .httpBasic().and()
            .formLogin();
       	// http.csrf().disable();
        return http.build();
    }
	@Bean
	public LettuceConnectionFactory redisConnectionFactory() {
		return new LettuceConnectionFactory(); 
	}
}

Exception is related to redis serializer failing to serialize csrf data.

If you disable csrf then things work fine.

Maven:

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.1.6.RELEASE</version>
	<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>apuravsecurity</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>apuravsecurity</name>
<description>Demo project for Spring Boot</description>

<properties>
	<java.version>1.8</java.version>
</properties>

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-security</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-webflux</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.session</groupId>
		<artifactId>spring-session-data-redis</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-devtools</artifactId>
		<scope>runtime</scope>
		<optional>true</optional>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>
</dependencies>

exception-spring-redis-websession.log

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 22, 2019
@apuravchauhan
Copy link
Author

@rwinch Any help or pointers will be appreciated. : )

@vpavic vpavic self-assigned this Jul 23, 2019
@apuravchauhan
Copy link
Author

@vpavic Thanks for looking into this. Let me know if I can help with any more information

@vpavic
Copy link
Contributor

vpavic commented Aug 2, 2019

Thanks for the report @apuravchauhan, I've reproduced the problem.

This is due to Spring Security 5.1 not having WebFlux support for Jackson modules - this was added in Spring Security 5.2.0.M1 via spring-projects/spring-security#6305.

I've verified this in a sample project by upgrading Spring Security version to the latest 5.2 milestone (which is 5.2.0.M3 ATM).

@vpavic vpavic closed this as completed Aug 2, 2019
@vpavic vpavic added for: external-project For an external project and not something we can fix and removed status: waiting-for-triage An issue we've not yet triaged labels Aug 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project For an external project and not something we can fix
Projects
None yet
Development

No branches or pull requests

3 participants