I read a lot of articles on CSRF protection ( this is a good option ) and various questions here on SO, but none of them seem informative enough to answer my question.
I am developing my own CMS and I want to protect my login and comment forms. I will allow anonymous users to comment on my site.
All forms on my site are protected with tokens. I already know about this approach, but the problem is that it needs an active session (that is, after the user logs in). The problem with login and comment formats is that they are accessible to everyone and do not require login - what would be the best protection against CSRF in this case?
In the above link, I read that when trying to log into the system and then switching to the usual anti-CSRF methods (for example, assigning a token to a user session), “pre-season” time can be created, but I don’t understand how to do it.
The referrer header is a weak solution, so I think I should not worry. The Origin header, as far as I tested, is only supported on Google Chrome. What about custom headers? XMLHTTPRequest seems like an opportunity, but I spent literally more than three hours searching Google for some information on how to implement this kind of security measure on my website. But even if I could use a custom header, doesn't it make it useless, as HTTP headers can be completely fake?
So the question is: how do I protect my login and comment forms from CSRF?
Edit: here is more information from the link above:
We recommend strict Referer validation to protect against CSRF login because login forms are usually submitted via HTTPS, where the Referer header is reliably present for legitimate requests. If the login request is missing the Referer header, the site should reject the request for protection against malicious suppression.
and
Secret verification tokens can protect against CSRF logging in, but developers often forget to implement security because there is no session before logging in to bind the CSRF token. To use secret tokens verification to protect against entering the CSRF system, the site must first create a "presession", implement CSRF protection based on tokens, and then switch to a real session after successful authentication.
I just can't put an end to this argument after reading the quotes above. One of them mentions the use of the referrer header, but I'm not quite sure if this really adds much to webapp security.
Edit 2: How to use CAPTCHA?