Azu::Handler::CSRF
Inherits HTTP::Handler < Reference < Object
The CSRF Handler implements Cross-Site Request Forgery protection following OWASP recommendations for token-based mitigation
Constants
COOKIE_KEY = "csrf_token"
COOKIE_MAX_AGE = 86400
Cookie configuration
COOKIE_SAME_SITE = HTTP::Cookie::SameSite::Strict
HEADER_KEY = "X-CSRF-TOKEN"
Headers and parameters for CSRF tokens
HMAC_SECRET_LENGTH = 64
PARAM_KEY = "_csrf"
TOKEN_LENGTH = 32
Token configuration
HTTP methods that require CSRF protection
Constructors
default
Get default instance (backward compatibility) Uses double-checked locking to avoid mutex contention
new(skip_routes : Array(String) = [] of String, strategy : Strategy = Strategy::SignedDoubleSubmit, secret_key : String | Nil = nil, cookie_name : String = COOKIE_KEY, header_name : String = HEADER_KEY, param_name : String = PARAM_KEY, cookie_max_age : Int32 = COOKIE_MAX_AGE, cookie_same_site : HTTP::Cookie::SameSite = COOKIE_SAME_SITE, secure_cookies : Bool = true)
SourceClass methods
cookie_max_age
Sourcecookie_name
Sourceheader_name
Sourceparam_name
Sourcesecret_key
Sourcesecure_cookies
Sourcestrategy=(value : Strategy)
SourceClass-level methods for backward compatibility These delegate to the default instance
use_double_submit!
Sourceuse_synchronizer_token!
SourceInstance methods
call(context : HTTP::Server::Context)
Sourcecookie_max_age
Sourcecookie_name
Sourcecookie_same_site
Sourcecookie_same_site=(cookie_same_site : HTTP::Cookie::SameSite)
Sourceheader_name
SourceGenerate meta tag with CSRF token for AJAX requests
param_name
Sourcesecret_key
Sourcesecure_cookies?
SourceValidate CSRF token based on configured strategy
Origin validation (additional security layer)