SecurityContextHolderThreadLocalAccessor should not share SecurityContext instance across threads#18210
Open
ttddyy wants to merge 1 commit into
Open
Conversation
`SecurityContextHolderThreadLocalAccessor` currently propagates the same `SecurityContext` to other threads when Micrometer Context Propagation is used. This leads to unintended sharing of mutable state and can cause authentication to leak between threads. This change updates the accessor to create a new `SecurityContext` for the target thread while reusing only the `Authentication` value. Each thread now receives its own `SecurityContext` instance, preventing cross-thread interference and aligning with recommended `SecurityContext` usage. Signed-off-by: Tadaya Tsuyukubo <tadaya@ttddyy.net>
| public void setValue(SecurityContext securityContext) { | ||
| Assert.notNull(securityContext, "securityContext cannot be null"); | ||
| SecurityContextHolder.setContext(securityContext); | ||
| SecurityContext newContext = SecurityContextHolder.createEmptyContext(); |
Contributor
There was a problem hiding this comment.
question : Can we apply singleton concept here?
Contributor
Author
There was a problem hiding this comment.
The whole point of this PR is not to share the SecurityContext, so making it a singleton defeats the purpose.
| Assert.notNull(securityContext, "securityContext cannot be null"); | ||
| SecurityContextHolder.setContext(securityContext); | ||
| SecurityContext newContext = SecurityContextHolder.createEmptyContext(); | ||
| newContext.setAuthentication(securityContext.getAuthentication()); |
Contributor
There was a problem hiding this comment.
newContext.setAuthentication(Objects.requireNonNull(securityContext.getAuthentication()))
Contributor
Author
There was a problem hiding this comment.
Spring projects use Assert, which throws IllegalArgumentException instead of NullPointerException.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
We encountered an issue where authentication was being mixed across threads.
During our analysis, we discovered that
SecurityContextHolderThreadLocalAccessorpropagates the sameSecurityContextto other threads when using Micrometer Context Propagation.Below is a simplified example that demonstrates the problem:
When Micrometer context propagation is in use, the current implementation effectively shares the
SecurityContextinstance between threads. This is generally not recommended and can cause subtle and hard-to-diagnose behavior, such as authentication leakage across threads.We understand that swapping the
Authenticationdirectly is rarely a good practice. However, if a newSecurityContexthad been used for the different thread, it would have been isolated and harmless. The underlying issue happens only because the sameSecurityContextinstance is shared across threads.Proposed Change
This PR updates
SecurityContextHolderThreadLocalAccessorto create a newSecurityContextwhenever a thread switch happens, and propagte only theAuthentication.I have targetted the PR to
6.5.xbranch as I consider it is a bug and should be fixed in there and up.