Spring-security: Blocking in WebSessionServerCsrfTokenRepository

Created on 17 Mar 2020  路  6Comments  路  Source: spring-projects/spring-security

Summary


Detected by blockhound: WebSessionServerCsrfTokenRepository and CookieServerCsrfTokenRepository make blocking calls to UUID.randomUUID when generating the token.

It would be nice to have a non-blocking SecureRandom to solve this.
It can of course be offloaded to the boundedElastic scheduler but that looks sub optimal.

https://github.com/spring-projects/spring-security/blob/747d8817cbadc307f7407c26fc88b2ff63c37149/web/src/main/java/org/springframework/security/web/server/csrf/WebSessionServerCsrfTokenRepository.java#L112

Version


5.2.2.RELEASE

web backported bug

All 6 comments

Thanks for the report @cbornet!

I'm not aware of a non-blocking secure random source. Are you? If we don't have a non-blocking secure random source then I agree our best bet is to use the boundedElastic.

Would you be interested in submitting a PR?

I'm not aware of a non-blocking secure random source. Are you?

No, I'm not either. I guess even reading /dev/random with NIO is still offloading to a thread-pool. Would be nice to have it in Java one day though.

I'll do the PR for boundedElastic, no problem

I want to put a publishOn in generateToken but I don't find a good place
I coud do

    @Override
    public Mono<CsrfToken> generateToken(ServerWebExchange exchange) {
        return Mono.just(exchange)
            .publishOn(Schedulers.boundedElastic())
            .fromCallable(() -> createCsrfToken());
    }

but maybe there's a better way without wrapping exchange (which is not used) ?

That looks correct to me.

@cbornet Are you still interested in submitting this?

Yes. Sorry I've been quite busy. I'll do it this week.

Was this page helpful?
0 / 5 - 0 ratings