A short guide to cross-site request forgery attacks



Photo from Negativespace.

This is a follow-on post to my post on clickjacking attacks.

I found this article from Auth0 (https://auth0.com/blog/cross-site-request-forgery-csrf/) while reading the one on clickjacking.  As before, it's worth reading.  Here's a summary.

What CSRF attacks are

CSRF, aka "c-surf", is an acronym fro Cross-Site Request Forgery.  The attack is where a malicious site uses another site's active session cookie to do something in the place of the user.  E.g. if you're logged in to your bank and go to a site in another browser tab, the other site could use the bank site's active session cookie to transfer money.

How to prevent them

User

Log out of websites, especially sensitive ones.

Back end

The article and OWASP do a good job of explaining the details, so I'll link to them here and give an overview.

  1. Check the origin header of the request.  This is fraught with peril.  Use as a last resort.
  2. Create an anti-CSRF token and store it in the session data on the server.  Send a copy to the client.  When the client responds, the responses will contain the anti-CSRF secret.  You can compare the client’s anti-CSRF secret to the secret stored with the session information.
  3. ✨ Use the double submit cookie strategy. This is the way to go because it doesn't require you to keep track of sessions on the server.  Basically, the idea is:
    • Put a secret value in a hidden input field in the form on the page.  This will be sent to the server along with the other form submission data.
    • Put the same secret value in an HTTP-only cookie that you send to the client.
    • When the form submits, compare the secret in the cookie to the secret in the form submission data. If they match, then the request came from a reliable source: an attacker wouldn't have the value from the HTML form.
For implementation, see the Auth0 (TLDR: use the csurf NPM package with the 'cookie' option).


Comments

Popular posts from this blog

Optional object property access

How is an application like a bride's outfit? - 1 minute read