I am working on a rest api end point. I am noticing that some end points work without X-csrf-token header. Digging into the core revealed the following
// This check only applies if
// 1. the user was successfully authenticated and
// 2. the request comes with a session cookie.
if ($account->isAuthenticated()
&& $this->sessionConfiguration->hasSession($request)
) {
if (!$request->headers->has('X-CSRF-Token')) {
return AccessResult::forbidden()->setReason('X-CSRF-Token request header is missing')->setCacheMaxAge(0);
}
This means that the X-csrf-token header is only checked for authenticated users. Are anonymous users not required to have this header? This really seems counter-intuitive to me as I feel that authenticated users already have a session cookie and its the anonymous users that need some kind of validation.
How do we add token validation for anonmyous users?