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

SEC-1667: Consider adding a method to SecurityContextHolderStrategy that returns the current state of the SecurityContext without creating a new one #1890

Closed
spring-projects-issues opened this issue Feb 1, 2011 · 5 comments
Assignees
Labels
in: core An issue in spring-security-core status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement type: jira An issue that was migrated from JIRA

Comments

@spring-projects-issues
Copy link

Kyrill Alyoshin (Migrated from SEC-1667) said:

We do have scenarios when we have to call SecurityContextHolder#getContext outside of web requests. (Obviously, the context will not be populated in such cases.) What will happen though is a new empty SecurityContext will be created and put on a ThreadLocal without being cleared by the servlet filter (as is the case during web requests). This will, of course, lead to class loader based memory leaks on hot redeploys.

It sure would be nice to add, say, getExistingContext() method to SecurityContextHolderStrategy, which would not create a context if it is not available, and just return an existing one or null otherwise. Then we can call SecurityContextHolder.getContextHolderStrategy().getExistingContext() and
we're safe.

What do you think?

@spring-projects-issues spring-projects-issues added in: core An issue in spring-security-core Open type: enhancement A general enhancement type: jira An issue that was migrated from JIRA labels Feb 5, 2016
@rwinch rwinch removed the Open label May 3, 2019
@rwinch
Copy link
Member

rwinch commented May 24, 2021

This does not make sense to me. If you are accessing the SecurityContext you need to ensure it is cleared out. If you are outside of the context of a request then you should not be accessing the SecurityContext because it won't be populated. If you want to ensure it is populated, then whatever populates the context should clear it out. A likely option is to use Spring's concurrency support. In particular, you could use DelegatingSecurityContextRunnable or one of the higher level support classes.

@rwinch rwinch closed this as completed May 24, 2021
@rwinch rwinch self-assigned this May 24, 2021
@rwinch rwinch added the status: invalid An issue that we don't feel is valid label May 24, 2021
@jzheaux
Copy link
Contributor

jzheaux commented Jun 14, 2021

Given #9877, I think this is something that should be reconsidered. In summary, Spring Security supplies a reactive hook that inspects the security context, and this hook is invoked during shutdown, causing a memory leak like the one described in this ticket.

More generally, it seems reasonable when operating at the framework level that there will be circumstances when performing a peek is valuable, somewhat similar to how Spring Security will often ask for an instance of the HttpSession without wanting to create it.

Instead of getExistingContext(), though, I believe peekContext() will be more descriptive.

To maintain backward compatibility, it would need to be a default method on SecurityContextHolderStrategy that returns getContext(). This is so that peekContext() == null means that there is no context. That said, peekContext() should be overridden by all implementations to return the context.

@jzheaux jzheaux reopened this Jun 14, 2021
@jzheaux jzheaux removed the status: invalid An issue that we don't feel is valid label Jun 14, 2021
@jzheaux jzheaux assigned jzheaux and unassigned rwinch Jun 14, 2021
@jzheaux
Copy link
Contributor

jzheaux commented Jun 2, 2022

Note that a684115 addresses the use case I referenced in #1890 (comment) by deferring the SecurityContext lookup. Given that, I'd like to see if #10913 is a better fit to clean up that use case before implementing this ticket.

@jzheaux jzheaux added the status: blocked An issue that's blocked on an external project change label Jun 2, 2022
@jzheaux jzheaux added this to the 6.0.x milestone Jun 2, 2022
@jzheaux
Copy link
Contributor

jzheaux commented Sep 20, 2022

After some additional review of #10913, it appears that the most consistent approach is for Spring Security's servlet-based ExchangeFilterFunctions to anticipate a Supplier<Authentication> instead of an Authentication in the set of reactor context attributes. Spring Security code should only call for Authentication at the time of use.

As such, I've created #11885 to change the servlet-based ExchangeFilterFunctions to read a Supplier<Authentication> from the reactor context instead of an Authentication.

Given that, I don't think that the use case in #1890 (comment) applies for adding peekContext.

@jzheaux jzheaux removed this from the 6.0.x milestone Sep 20, 2022
@jzheaux jzheaux added status: declined A suggestion or change that we don't feel we should currently apply and removed status: blocked An issue that's blocked on an external project change labels Sep 20, 2022
@jzheaux jzheaux closed this as completed Sep 20, 2022
@xenoterracide
Copy link

hah, I've asked for this before because not every application is a web application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core An issue in spring-security-core status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement type: jira An issue that was migrated from JIRA
Projects
Status: Done
Development

No branches or pull requests

4 participants