Preventing CSRF Using Double Submit Cookie Pattern

In previous blog post, what CSRF is and how to preventing CSRF using Synchronizer Token Pattern are discussed. This blog post will discuss how to use Double Submit Cookie Pattern to prevent from CSRF attacks. 

How Double Submit Cookie Pattern works?

When a user authenticates to a site, the site should generate a (cryptographically strong) pseudo-random value and set it as a cookie on the user’s machine separate from the session ID. The server does not have to save this value in any way, that's why this pattern is also called Stateless CSRF Defense.

The site then requires that every transaction request include this random value as a hidden form value (or other request parameter). A cross origin attacker cannot read any data sent from the server or modify cookie values, per the same-origin policy.  



Let's modify the previously built NodeJS application to demonstrate this

1. Handle /login POST Request.

Once the user is authenticated, a cookie containing csrf token will be create in the user's browser and it is set to httpOnly to make it readable by javascript.

 

2. Create submission form page.

A client-side script will retrieve csrf token value and inject it to a hidden field on the form loading to be submitted along with the form.  


3. Handle /message POST request.

Upon receiving the request, the cookie's CSRF token and the request's CSRF token are compared to validate the request and the message submission will be completed if the provided values are correct.


GitHub URL: https://github.com/TharinduMPerera/DoubleSubmitCookiesPattern


______________________________________________________________

Tip: Understand the concept well. Then you will be able to develop better applications.:octocat::zap:


Comments

Popular posts from this blog

Preventing CSRF Using Synchronizer Token Pattern

Code Coverage Using Clover