Securing HTML forms from CSRF
One of the most common security attacks is CSRF, where a malicious third party tricks a user into sending a web form with different values than intended. One way to mitigate this attack is by sending a one-time token along with the form content. The web server then checks the token validity to ensure the request comes from the correct web browser.
We can create such a token in a Rocket application by creating a fairing that will generate a token and check the form value sent back. Let's look at the steps:
- First, we need to add the dependencies for this. We are going to need a
base64crate to encode and decode binary values into a string. We also need thesecretsfeature from Rocket to store and retrieve private cookies. Private cookies are just like regular cookies, but they are encrypted by the key we configured in theRocket.tomlfile withsecret_key.
For dependencies, we also need to add time as a dependency. Add the following...