CSRF + Session
CSRF-with-session Example
Example GoFiber web app using Cross Site Request Forgery (CSRF) middleware with session.
This example impliments multiple best-practices for CSRF protection:
- CSRF Tokens are linked to the user's session.
- Pre-sessions are used, so that CSRF tokens are always available, even for anonymous users (eg for login forms).
- Cookies are set with a defense-in-depth approach:
- Secure: true
- HttpOnly: true
- SameSite: Lax
- Expiration: 30 minutes (of inactivity)
- Cookie names are prefixed with "__Host-" (see MDN-Set-Cookie for more information))
What CSRF protection stops
In a CSRF attack, a page on another site makes a logged-in user's browser send a state-changing request to your app, for example by auto-submitting a hidden form that transfers funds. The browser attaches the session cookie to that request automatically, so the cookie alone cannot show that the user meant to send it.
The CSRF middleware closes that gap. Every state-changing request must carry a token that is tied to the user's session and that a page on another origin cannot read. A forged request has no valid token, so it is rejected with 403 Forbidden.
You can see the rejection without a browser. With the server running (see below), send a POST without a token:
curl -k -i -X POST https://127.0.0.1:8443/protected -d 'message=hello'The response is 403 Forbidden.
Requirements
Install Go Modules
Like any golang project, you will need to download and install the required modules for the project to run. Change into the "csrf-with-session" directory:
cd csrf-with-sessionAnd then:
go mod downloadThis command downloads the golang dependencies needed to run the project.
Once the modules have finished downloading, you can run the project like this:
go run main.goYou should see Fiber's startup banner with the address of the server if everything is OK:
INFO Server started on: https://127.0.0.1:8443On the first start the app also generates a self-signed certificate (cert.pem and key.pem in the current directory) and prints a message about it.
Try the demo
Start the server by running:
go run main.goOpen your browser and navigate to 127.0.0.1:8443. The server only speaks HTTPS, so a link that starts with http:// does not work.
Accept the self-signed certificate warning and visit the site.
In Chrome:
- Click on "Advanced"
- Click on "Proceed to 127.0.0.1:8443 (unsafe)"
In Firefox:
- Click on "Advanced"
- Click on "Accept the Risk and Continue"
In Safari:
- Click on "Show Details"
- Click on "visit this website"
Try to access the /protected page
Login using one of the test accounts:
- Username:
user1 - Password:
password1OR - Username:
user2 - Password:
password2
Once logged in, you will be able to see the /protected page.
Submit the form on the /protected page
Once logged in, you will be able to see the /protected page. The /protected page contains a form that submits to the /protected page. If you try to submit the form without a valid CSRF token, you will get a 403 Forbidden error.
The form has a "Send CSRF token in form data" checkbox. Submit a message with the box ticked and the page shows your message. Untick the box so that the token is left out of the request, submit again, and you get the 403 Forbidden page.
CSRF Protection
All methods except GET, HEAD, OPTIONS, and TRACE are checked for the CSRF token. If the token is not present or does not match the token in the session, the request is aborted with a 403 Forbidden error.
Token Lifecycle
The CSRF token is generated when the user visits any page on the site. The token is stored in the session and is valid for until it expires, or the authorization scope changes (e.g. the user logs in, or logs out).
It is important that CSRF tokens do not persist beyond the scope of the user's session, that a new session is created when the user logs in, and that the session is destroyed when the user logs out.
The CSRF middleware has a SingleUseToken configuration option that can be used to generate a new token for each request. This is useful for some applications, but is not used in this example. Single use tokens have usability implications in scenarios where the user has multiple tabs open, or when the user uses the back button in their browser.
Session Storage
Sessions are stored in memory for this example, but you can use any session store you like. See the Fiber session documentation for more information.
Note on pre-sessions
GoFiber's CSRF middleware will automatically create a session if one does not exist. That means that we always have pre-sessions when using the CSRF middleware. In this example we set a session variable loggedIn
to true when the user logs in, in order to distinguish between logged in and logged out users.
Going further
Here are some useful links where you can learn more about this topic: