title: Protecting login and comment forms against CSRF
body: <p>I have read many articles about CSRF protection (<a href="http://seclab.stanford.edu/websec/csrf/csrf.pdf" rel="nofollow">this is a good one</a>) and various questions here on SO, but none of them seem to be informative enough to answer my question.</p>
  
  <p>I am developing my own CMS and I want to secure my login and comment forms. I am going to allow anonymous users to comment on my website.</p>
  
  <p>All of the forms on my website are secured using tokens. I already know about that approach, but the problem is that it needs an active session (that is, after the user logs in). The problem with the login and comment forms is that they are accessible to just about anyone and do not require you to log in - what would be the best protection against CSRF in this case?</p>
  
  <p>On the link above, I read that it could be possible to create a "pre-session" when the user tries to log in and then proceed to the usual anti-CSRF methods (like assigning a token to the user's session), but I have no insight on how to achieve this.</p>
  
  <p>The referrer header is a weak solution, so I guess I shouldn't bother. The Origin header, is, as far as I have tested, only supported in Google Chrome. What about custom headers? XMLHTTPRequest seems like a possibility, however, I have spent literally more than three hours on Google looking up some information about how should one implement such a security measure on their website. But even if I could use a custom header, doesn't it make it useless since the HTTP headers can be faked completely?</p>
  
  <p>So, the question: how should I protect my login and comment forms against CSRF?</p>
  
  <p><strong>Edit:</strong> here's some additional information from the link that I provided above:</p>
  
  <blockquote>
    <p>We recommend strict Referer validation to protect against login CSRF
    because login forms typically submit over HTTPS, where the Referer
    header is reliably present for legitimate requests. If a login request
    lacks a Referer header, the site should reject the request to defend
    against malicious suppression.</p>
  </blockquote>
  
  <p>and</p>
  
  <blockquote>
    <p>Secret validation tokens can defend against login CSRF, but developers
    often forget to implement the defense because, before login, there is
    no session to which to bind the CSRF token. To use secret validation
    tokens to protect against login CSRF, the site must ﬁrst create a
    “presession,” implement token-based CSRF protection, and then
    transition to a real session after successful authentication.</p>
  </blockquote>
  
  <p>I just cannot put an end to this argument after reading the above quotes. One of them mentions using the referrer header, but I'm not quite sure whether it really adds much to the security of the webapp.</p>
  
  <p><strong>Edit 2:</strong> What about using CAPTCHAs?</p>