Can you illustrate an example of Double Submit Cookie pattern for CSRF protection?

The Double Submit Cookie pattern for CSRF protection involves 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 identical.

Consider an HTTP POST scenario: When a user logs into a site, the server generates a unique token, stores it in the user’s session, and sends it as a cookie to the client. The client then includes this token in subsequent requests within the body or URL.

For instance, when submitting a form, the client-side JavaScript reads the CSRF token from the cookie and adds it as a hidden field to the form. On receiving the request, the server checks that the CSRF token in the cookie matches the one in the form.

This method is effective against CSRF attacks because even if an attacker can trick a victim’s browser into making a request, they cannot read the CSRF token due to same-origin policy restrictions.