Skip to content
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

Treat a SerializationException retrieving a session as though there w… #2099

Conversation

dbyron-sf
Copy link

@dbyron-sf dbyron-sf commented May 23, 2022

…ere no session.

This makes zero-downtime / seamless upgrades possible in more cases. Currently, upgrading spring-security across versions that change SpringSecurityCoreVersion.SERIAL_VERSION_ID requires manual deletion of cached sessions.

See also spring-projects/spring-security#9204 and spring-projects/spring-security#3737

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 23, 2022
@dbyron-sf dbyron-sf force-pushed the improve-imcompatible-sessions branch from baaaa8f to 4cabe8a Compare May 23, 2022 22:57
@dbyron-sf dbyron-sf changed the title Treat a SerializationException retrieving a session at though there w… Treat a SerializationException retrieving a session as though there w… May 24, 2022
…ere no session.

This makes zero-downtime / seamless upgrades possible in more cases.  Currently, upgrading spring-security across versions that change SpringSecurityCoreVersion.SERIAL_VERSION_ID requires manual deletion of cached sessions.
@dbyron-sf dbyron-sf force-pushed the improve-imcompatible-sessions branch from 4cabe8a to d4d8072 Compare May 24, 2022 02:42
@quaff
Copy link
Contributor

quaff commented Jun 8, 2022

Customize bean springSessionDefaultRedisSerializer as workaround if you are using redis as session store

@Configuration(proxyBeanMethods = false)
@EnableSpringHttpSession
public class SessionConfiguration {

	@Bean
	public RedisSerializer<?> springSessionDefaultRedisSerializer() {
		return new JdkSerializationRedisSerializer(getClass().getClassLoader()) {
			@Override
			public Object deserialize(byte[] bytes) throws SerializationException {
				try {
					return super.deserialize(bytes);
				}
				catch (SerializationException ex) {
					if (ex.getCause() instanceof SerializationFailedException) {
						return null; // ignore SerializationFailedException
					}
					throw ex;
				}
			}
		};
	}

}

@dbyron-sf
Copy link
Author

@marcusdacoregio Hope it's OK that I'm tagging you here as things have been quiet on this PR for awhile. I see some activity elsewhere and would love a review.

Thanks much!

@marcusdacoregio
Copy link
Contributor

Hi @dbyron-sf, thanks for the ping.

I'll tag @rwinch so we can talk about this issue and get back to you. It may take a couple of weeks for us to take a look at this so I will ask you for a little bit more patience.

@dbyron-sf
Copy link
Author

Thanks so much @marcusdacoregio for taking a look!

@rwinch rwinch added this to the 3.0.x milestone Aug 10, 2022
@dbyron-sf
Copy link
Author

Hey @marcusdacoregio (or maybe @rwinch). Sorry to nag, but it's been awhile and I'm still hoping to get some traction here. Thanks for your help.

@vpavic
Copy link
Contributor

vpavic commented Aug 17, 2022

I don't think this should be addressed as proposed here. The PR targets only a single SessionRepository implementation (and the one that isn't a default anymore in 3.0) while in reality this is a concern that affects all session repositories that support Java based session serialization (and that's most if not all). Duplicating the try-catch block proposed here across different SessionRepository implementations isn't an attractive option either, for obvious reasons.

Note that this concern (safe session deserialization) in general is tracked under #529.

My opinion is that this should be addressed using a centralized strategy that would deal with deserialization failures and would then be reused across all session repositories that support Java serialization.

However, taking a step back, my suggestion to anyone affected by this is to strongly consider opting into a different (non-default) session serialization mechanism. JSON serialization is the most attractive option there, and is already supported by all Redis based SessionRepository implementations. This way, you shouldn't be affected by Spring Security's changes of serial version id while also standing a much better chance of doing major upgrades without affecting persisted sessions.

Note that there's also #1913 tracking the effort to move away from Java serialization.

@dbyron-sf
Copy link
Author

Thanks for the comments @vpavic. What you're saying makes sense...and especially if it wouldn't be backported to some probably consider ancient/closed versions like 2.5/2.6/2.7 it won't help those upgrades. And if this isn't the default in 3.0 it won't help that upgrade either.

Any chance you can give some pointers on how to select JSON serialization?

PS I'm following both those issues, but they've been quiet for quite some time.

@vpavic
Copy link
Contributor

vpavic commented Aug 17, 2022

There's a sample application within the Spring Session codebase that shows how to set up JSON based serialization. It also uses Spring Security's Jackson mixins.

@dbyron-sf
Copy link
Author

Just saw #2124 fly by which looks related.

@rwinch rwinch removed this from the 3.0.x milestone Nov 15, 2022
@marcusdacoregio
Copy link
Contributor

Based on the comment from @vpavic, I'll close this as duplicate of #529

@marcusdacoregio marcusdacoregio self-assigned this Sep 1, 2023
@marcusdacoregio marcusdacoregio added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged labels Sep 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants