Hcs.Plug.Csrf
hcs · API reference
CSRF protection plug.
Implements Double Submit Cookie pattern for CSRF protection.
val generate_token : unit -> stringval get_cookie_token : cookie_name:string -> Server.request -> string optionExtract token from cookie header
val get_header_token : header_name:string -> Server.request -> string optionExtract token from header
val get_form_token : field_name:string -> Server.request -> string optionExtract token from form body (application/x-www-form-urlencoded)
type config = {
cookie_name : string; (* Name of CSRF cookie *)
header_name : string; (* Name of CSRF header *)
field_name : string; (* Name of CSRF form field *)
secure : bool; (* Secure cookie flag *)
same_site : [ `Strict | `Lax | `None ]; (* SameSite cookie attribute *)
}val default_config : configval is_safe_method : [> `GET | `HEAD | `OPTIONS | `TRACE ] -> boolCheck if method is safe (doesn't need CSRF protection)
val make_cookie : config:config -> string -> stringCreate Set-Cookie header value
val create :
?config:config ->
unit ->
(Server.request -> Server.response) ->
Server.request ->
Server.responseCreate CSRF protection plug.
For safe methods (GET, HEAD, OPTIONS, TRACE): sets CSRF cookie if not present. For unsafe methods: validates token from header or form matches cookie.
parameter config CSRF configuration