Apply CSRF Protection to a plumber2 API
api_csrf.RdInstalls CSRF middleware on a plumber2 API using the double-submit cookie pattern.
Details
When installed:
Any
formelement inside template automatically include a CSRF token.If working in JavaScript contexts, the
csrf_token()helper is also accessible inside templates.
Middleware behaviour:
On safe methods (
GET,HEAD,OPTIONS), if the CSRF cookie is missing, a new token is generated and set as a cookie.On unsafe methods (
POST,PUT,DELETE,PATCH), the request is rejected with 403 unless a token provided via theX-CSRF-Tokenheader or acsrf_tokenfield in the parsed request body matches the CSRF cookie.