Adapt plain values in reactive method security#19435
Open
kamilkrzywanski wants to merge 1 commit into
Open
Conversation
Kotlin suspend methods with @Cacheable can return a plain cached value on a cache hit. Reactive method security assumed proceed() always returned a Mono or Flux, which caused a ClassCastException. Normalize proceed() results so plain values are adapted to Mono or Flux. Closes spring-projectsgh-19400 Signed-off-by: Kamil Krzywannski <kamilkrzywanski01@gmail.com>
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.
Closes
Closes gh-19400
Context
When a Kotlin
suspendmethod is annotated with both@PreAuthorizeand@Cacheableunder reactive method security, the first request succeeds(cache miss) but a subsequent request fails with:
Root cause
Mono.@PreAuthorize(order ~200) is the outer advisor;@Cacheable(
LOWEST_PRECEDENCE) is the inner advisor.CacheAspectSupportreturns theplain cached value for suspending functions (return type is
Object,so no
ReactiveAdapteris applied infindInCaches).mi.proceed()always returned aMono/Flux, soMono.defer(() -> proceed(mi))checkcasts the plainvalue and throws
ClassCastException.Solution
Normalize the result of
proceed()in reactive method security interceptors:Mono/Flux/Publisher, use it as-isMono.justOrEmpty(...)/Flux.just(...)This matches how Spring AOP already handles mixed return shapes at the
proxy boundary (
awaitSingleOrNull).Changes
ReactiveMethodInvocationUtils: addproceedAsMono/proceedAsFluxAuthorizationManagerBeforeReactiveMethodInterceptorAuthorizationManagerAfterReactiveMethodInterceptorPrePostAdviceReactiveMethodInterceptor(deprecated path, same fix)Tests
proceed()) forsuspending methods on both before and after interceptors
@PreAuthorize,@Cacheable, and a Kotlinsuspendmethod (cache miss + cache hit)Notes
Spring Framework's
CacheAspectSupportstill does not treat suspendingfunctions as reactive on the cache-hit path. This change hardens Spring
Security against that (and any other advisor that returns a plain value
for a suspending method). A complementary Framework fix may still be
worthwhile for consistency.