axum-csrf
Simple, automatic CSRF handling for Axum
Concept
All requests that has no CSRF cookie will get set-cookie header automatically.
All state changing requests (PUT,DELETE,POST,PATCH) will request x-csrf-header with a valid header.
The x-csrf-header is compared with the request cookie of CSRF header. If not matched, request will be rejected with 401.
No need to do any other change
Key feature
- Fully automated. PUT, DELETE, POST, PATCH requests always has CSRF protection.
- Resilient. Requests that does not have CSRF cookie will get a new token automatically
- Easy to use. No need to touch your html. You can use
GET /api/get_csrf_tokento retrieve the token programmatically, and use it forever. It won't change
How to use
dependency
Add the dependency:
server side
/// import as alias (optional)
use axum_csrf_simple as csrf;
/// Set your preferred token signing key. All token is <uuid>-<hmac_signature> format. Invalid tokens won't be trusted.
set_csrf_token_sign_key.await; // if not set, the default signing key is 32 char auto generated signed key
let app = new
.route
.route // expose the API for your client to retrieve CSRF token. One time only, and it can be cached.
.route_layer;
/// Test accessing /admin/endpoint1 with post, it will require CSRF validation.
client side
Client has to submit request with x-csrf-token header value to be able to submit successfully.
Retrieving CSRF token:
Submitting the form
async function submitMyForm(event) {
event.preventDefault();
console.log("Submit called");
const form = event.target;
const formData = new FormData(form); // Collect form data
try {
const response = await fetch(form.action, {
method: "POST",
headers: {
"x-csrf-token": CSRF_TOKEN
},
body: formData // Send form data
});
const result = await response.json(); // Assuming server returns JSON
} catch (error) {
console.error("Error:", error);
}
}
Your code to verify CSRF code?
Not required. All requests that reaches your code is CSRF validated.