---
updatedAt: 2026-05-28T23:48:34.000Z
agentTools:
  projectIndex: https://docs.wristband.dev/llms.txt
---

# Test CSRF Middleware

Test that your CSRF middleware is protecting your application's authenticated endpoints.

<Callout icon="🤔" theme="default">
  ### Tests not working?

  If you get stuck, [contact us](https://docs.wristband.dev/docs/support) and our development team will work directly with you to get unblocked.
</Callout>

After implementing the Signed Double Submit Cookie pattern, let's verify that it's working correctly. The following tests will ensure that the CSRF token is created during login, that your CSRF middleware verifies the CSRF token for authenticated endpoints, and that the CSRF cookie is cleaned up after logout.

<br />

## 1. Verify CSRF Token is Created During Login

After a user successfully authenticates, Wristband will redirect to your application's Callback Endpoint.  The Callback Endpoint should then create a `CSRF-TOKEN` cookie containing the CSRF token.  Also, when your application loads and calls the Session Endpoint, you should see the CSRF token getting passed in the `X-Csrf-Token` request header.

<br />

### Test Steps

1. Open a browser and set up the browser's dev tools to actively monitor network requests. Also, ensure that network requests are configured to be preserved so you can examine them afterward.
2. Navigate to your application's Tenant Login Page.
3. Complete the authentication flow.

<br />

### Verification Checks

1. In your browser's developer tools, find the network request to your application's Callback Endpoint. Verify in the network request that you see the `Set-Cookie` response header containing the `CSRF-TOKEN`.

   <Image alt="Verify XSRF-TOKEN cookie is created" align="center" src="https://files.readme.io/99f994bf4b730e117c6e1419b7b8e5d21164bdbb15c66e5c5d863782591c6bf8-Screenshot_2025-04-14_at_11.09.42_AM.png" />

<br />

2. Verify the `CSRF-TOKEN` Cookie appears in your browser's storage.

   <Image alt="Verify XSRF-TOKEN cookie is stored in the browser" align="center" src="https://files.readme.io/3dc53c58f4a97817df77d273bfdbbe64d1d9721c2ca8c9c0baff7e019528ecb0-Screenshot_2025-04-14_at_11.04.52_AM.png" />

<br />

3. In your browser's developer tools, find the network request to your application's Session Endpoint. Verify in the network request that you see the `X-Csrf-Token` request header being set.

   <Image alt="Verify that the X-Xsrf-Token request header is sent" align="center" src="https://files.readme.io/c9b3fa89ca926c15b7468e77361615831ddada9a998e589a9982da2d0fc077c4-Screenshot_2025-04-14_at_11.21.47_AM.png" />

<br />

4. Verify that the Session Endpoint returns a 200 response.

   <Image alt="Verify that the Session Endpoint returns a 200" align="center" src="https://files.readme.io/ee613c3b1063319548a324e1741dea506ae69eea7fc19b06fbc299e0dfdc110a-Screenshot_2025-04-14_at_12.21.25_PM.png" />

   <br />

***

<br />

## 2. Verify Requests With Invalid CSRF Tokens Fail

Now that we've tested that the happy path works, let's try deleting the `CSRF-TOKEN` cookie so that the `X-Csrf-Token` request headers will no longer contain a valid CSRF token.  Requests to the Session Endpoint should now fail with a `403 Forbidden` response.

<br />

### Test Steps

1. While still logged in to the application, manually delete the CSRF cookie from the browser.

   <Image alt="Delete XSRF-TOKEN cookie" align="center" src="https://files.readme.io/61f216df176ba4bf90bd979992562e8ca69944271757bba32d36d31ed4ed016c-Screenshot_2025-04-14_at_12.38.39_PM.png" />

   <br />
2. Refresh your application to trigger a call to the Session Endpoint.

<br />

### Verification Checks

1. In your browser's developer tools, find the network request to your application's Session Endpoint. Verify that the response is now a 403.

   <Image alt="Verify Session Endpoint returns a 403" align="center" src="https://files.readme.io/e6ba9c44397fa8b8ece158f1e4964df929a599fea97ab0eff19d074af3c31120-Screenshot_2025-04-14_at_12.46.22_PM.png" />

   <br />
2. Verify that after receiving the 403 response, you are redirected to your application's Login Endpoint.

<br />

***

<br />

## 3. Verify CSRF Cookie Removal on Logout

Finally, let's make sure the CSRF cookie gets removed from the browser when a user logs out.

<br />

### Test Steps

1. Ensure that you are logged into your application and can see that the `CSRF-TOKEN` cookie is stored in your browser.  In the previous test, we deleted this cookie, so you'll probably need to log in again to recreate it.
2. Update the browser's address bar to navigate to your application's Logout Endpoint (e.g. http\://localhost:3000/auth/logout).  This will cause the logout flow to execute.

<br />

### Verification Checks

1. In your browser's developer tools, find the network request to your application's Logout Endpoint. Verify in the network request that you can see the Set-Cookie response header containing a cookie named `CSRF-TOKEN`. The cookie's value should be blank, and its `Max-Age` set to 0 in order to delete the cookie from the browser.

   <Image alt="alt text" align="center" src="https://files.readme.io/1ff82c676b6b1e93a35d5e967ac50770762fa6855b6ce93b35cf2483616368eb-Screenshot_2025-04-14_at_1.09.14_PM.png" />

   <br />

* Check your browser's cookie storage to ensure that there is no longer a `CSRF-TOKEN` cookie associated with your application's domain.

<br />

If all tests succeed, then CSRF protection has been successfully implemented. :tada:

<Image alt="Ultimate Defense" align="center" width="360px" src="https://files.readme.io/e678c37151a98dd60d1fc6e9ae512e8c6320b34e90a3ff5213f525b684211124-ironman_gif.gif">
  Well done. You've got the ultimate defense now.
</Image>

<br />

<HTMLBlock>{`
<style>
.UpdatedAt {
  display: none;
}
</style>
`}</HTMLBlock>