MissingCsrfTokenException message is misleading when not storing the CSRF tokens in the session #10436
Labels
in: web
An issue in web modules (web, webmvc)
status: backported
An issue that has been backported to maintenance branches
type: bug
A general bug
Milestone
When using the
org.springframework.security.web.csrf.CookieCsrfTokenRepository
for CSRF protection, no information is stored in the HTTP session. When aorg.springframework.security.web.csrf.MissingCsrfTokenException
is thrown, because there was no CSRF cookie, the message is misleading and confusing at first:I was scratching my head, "which session!?".
The message is hardcoded in the
MissingCsrfTokenException
, the exception is thrown inorg.springframework.security.web.csrf.CsrfFilter#doFilterInternal
whentokenRepository.loadToken
returnsnull
.A very simple fix would be to change the message to something akin to
"Could not verify the provided CSRF token because no token was found in the CSRF token repository."
A slightly more involved fix would create a different message for the different token repository implementations, like "because your session was not found" and "because no cookie was found" respectively, but that would require changing the
CsrfTokenRepository
interface to add a message getter or some other means by which theCsrfFilter
can generate the appropriate message.If you think either solution is worthwhile, I can provide a pull request.
The text was updated successfully, but these errors were encountered: