csrf ?not_allowed_handler ?key ?input_name ?secret ()
returns a middleware that enables CSRF protection for unsafe HTTP requests.
not_allowed_handler
is used if an unsafe request does not pass the CSRF protection check. By default, not_allowed_handler
returns an empty response with status 403.
key
is the key in the session cookie under which a CSRF token will be stored.
Internally, the CSRF protection is implemented as a Double Submit Cookie approach. session_key
is the name of the session cookie the CSRF token should be stored in. By default, the value is _session
. If you want the CSRF cookie to use a __Host
prefix, you have to adjust the session cookie key.
input_name
is the name of the input element that is used to send the CSRF token. By default, the value is _csrf
. It is recommended to use a <hidden>
field in a <form>
.
secret
is the secret used to encrypt the CSRF cookie value with. By default, SIHL_SECRET
is used.
For security purposes, AES is used for encryption.