How does the double submit Cookie pattern work?

How does the double submit Cookie pattern work?

Double submitting cookies is defined as sending a random value in both a cookie and as a request parameter, with the server verifying if the cookie value and request value are equal. How does it work? When a user logs into the site, a session is created, and the session ID is set as a cookie in the browser.

How is a cookie set for a CSRF token?

When a user logs into the site, a session is created and the session ID is set as a cookie in the browser. At the same time, another cookie is set for the CSRF token. Next, when the user submits a secure form, this token is extracted from the cookie and is set as a hidden input field in the HTML.

Is there an alternative to an encrypted Cookie?

A simpler alternative to an encrypted cookie is to HMAC the token with a secret key known only by the server and place this value in a cookie. This is similar to an encrypted cookie (both require knowledge only the server holds), but is less computationally intensive than encrypting and decrypting the cookie.

With a significant amount of users consuming the system at a given time, the memory used will also grow exponentially. This is when Double Submit Cookie pattern comes to play. In Double Submit Cookie Pattern, the server will not store the tokens, hence called Stateless CSRF Defense. How does it work?

Can a CSRF token be transmitted using cookies?

CSRF tokens should not be transmitted using cookies. The CSRF token can be added through hidden fields, headers, and can be used with forms, and AJAX calls. Make sure that the token is not leaked in the server logs, or in the URL.

How is SameSite used to mitigate CSRF attacks?

SameSite is a cookie attribute (similar to HTTPOnly, Secure etc.) which aims to mitigate CSRF attacks. It is defined in RFC6265bis. This attribute helps the browser decide whether to send cookies along with cross-site requests. Possible values for this attribute are Lax, Strict, or None.