-
Notifications
You must be signed in to change notification settings - Fork 6k
RequestHeaderAuthenticationFilter creates a session even if not configured to do so #14137
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
Comments
Hi, @obourgain. I think this is similar to #13840. This is indeed affected by the change in Spring Security about who is responsible for persisting the security context. However, this has nothing to do with the DSL since you are not using it to configure the var filter = new RequestHeaderAuthenticationFilter();
filter.setSecurityContextRepository(new RequestAttributeSecurityContextRepository());
// ... |
It is indeed similar to #13840 (comment) I agree with the fact that as I created the filter, it's not Spring Security's role to change its configuration. Moreover, it may be a security issue, because with SessionCreationPolicy.NEVER, the Authentication stored in the session will be used even if in subsequent requests the header have changed, as you can see in the test in the reproducer I linked. |
You can set |
Yes, we can fix the configuration by calling setters explicitly.
The reason for using a HttpSessionSecurityContextRepository seems to be performance #6125 but I think this change in defaults is weakening the security level and probably exposing some applications that won't notice this change in behavior. |
I don't follow how it does not have a secure by default posture. The Javadoc mentions how it behaves and guides you on how to change it if needed. I agree that the following paragraph of the javadoc could be improved to be clear that the header is only required if authentication is required and link to the proper section of
|
I find the doc on both
and do not mention storing things in a session. In
but at first read, I would assume that the second paragraph
would apply and that by default the filters would just "read from the request" and not "read from the request and store that in a cookie and then for the next request use the value stored in the cookie". That's my point, now we can close the issue. |
It would be great if you could provide a PR that improves the documentation, would you be interested in doing that? |
Hi @marcusdacoregio , we also stumble upon this change lately in production. I agree with @obourgain that the current behaviour of the AbstractPreAuthenticatedProcessingFilter is not very intuitive and another big problem is that this change is not mentioned in the migration guide. Furthermore the javadoc of HttpSecurity#sessionManagement() does not give any information that this configuration is only relevant for a few filters and not all. |
Describe the bug
Spring Security, even if configured with
SessionCreationPolicy.NEVER
orSessionCreationPolicy.STATELESS
creates a session when using RequestHeaderAuthenticationFilter.It may be caused by the change here 4479cef#diff-b9376389ef77383ad282c387359020ed122ad52d641cf25de70f104deae213d8R113
that changed
AbstractPreAuthenticatedProcessingFilter
to use aHttpSessionSecurityContextRepository
by default, but fails to configure it properly withallowSessionCreation=false
when using aSessionCreationPolicy
that should not create sessions.Also, when using
NEVER
the credentials may be read from the session (as expected) but with precedence over the headers sent in the request, which should be the source of truth.This may affect all subclasses of
AbstractPreAuthenticatedProcessingFilter
and also other classes that started to useHttpSessionSecurityContextRepository
by default in the commit mentionned above, but I didn't test this.To Reproduce
RequestHeaderAuthenticationFilter
andSessionCreationPolicy.NEVER
orSessionCreationPolicy.STATELESS
in theSecurityFilterChain
.SM_USER
header.Note that using MockMvc still creates the session, as we can see with a debugger, but doesn't seem to set the Set-Cookie header.
Expected behavior
Srping Security should not create a session when configured to not create one.
Sample
I created a reproducer there:
https://github.com/obourgain/session-issue
The text was updated successfully, but these errors were encountered: