7 CSRF
(require koyo/csrf) | package: koyo-lib |
This module provides wrapper procedures for protecting your application against CSRF attacks.
parameter
(current-csrf-error-handler handler) → void? handler : (-> request? response?)
This parameter is used by both wrap-corf and wrap-cors.
7.1 Header-based Approach
The cross-origin request forgery wrapper protects a handler from CSRF attacks by inspecting the Sec-Fetch-Site and Origin headers. This method is preferred over the older Token-based Approach since it involves fewer moving parts and does not require the developer to pass a token with every form submission.
Browsers that don’t pass either a Sec-Fetch-Site header or an Origin header are treated the same as non-browser clients. That is, validation passes.
7.2 Token-based Approach
parameter
(current-csrf-token-generator generator) → void? generator : (-> non-empty-string?)
= generate-random-string
parameter
(current-csrf-token) → (or/c #f non-empty-string?)
(current-csrf-token token) → void? token : (or/c #f non-empty-string?)
parameter
→ (-> request? (or/c #f non-empty-string?)) (current-csrf-token-reader reader) → void? reader : (-> request? (or/c #f non-empty-string?))
procedure
((wrap-csrf sessions) handler)
→ (-> request? any/c ... response?) sessions : session-manager? handler : procedure?
CSRF tokens are automatically generated and stored in each users’ sessions. If a user’s session already contains a CSRF token, then it is reused until the session expires.
This wrapper must be applied after wrap-session.