It depends on the acceptable risk level of your site.
How does this csrf token help protecting an anonymous user?
The risk is that the anonymous comment will be credited as coming from that particular anonymous user. That is, all the metadata will point to them (IP address, browser, OS, cookies, etc), rather than to the malicious user and site. The only evidence that it was from a CSRF attack would be headers such as Origin
and referer
.
csrf token should be associated with a user session id. What's the
equivalent used for an anonymous user? The ip address?
You can protect your pages in much the same way as you can protect against login CSRF. This is effectively a Double Submit Cookies approach.
So the process is:
- HTTP response for the HTML form contains a
Set-Cookie:
header for a cryptographically secure random token, and the form itself contains a hidden field with the same token.
- When the form is submitted, the token in the cookie sent by the browser is compared to the token in the hidden field.
This will prevent a malicious site from submitting the cross domain form and "crediting" it to the browser user because although the malicious site can cause the browser to send the cookie in the token, they can't discover the token value to include in the payload itself (i.e. the hidden field when submitted legitimately).