class Azu::Handler::CSRF

Overview

The CSRF Handler implements Cross-Site Request Forgery protection following OWASP recommendations for token-based mitigation

Included Modules

Defined in:

azu/handler/csrf.cr

Constant Summary

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

UNSAFE_METHODS = ["POST", "PUT", "PATCH", "DELETE"] of ::String

HTTP methods that require CSRF protection

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.default : CSRF #

Get default instance (backward compatibility) Uses double-checked locking to avoid mutex contention


[View source]
def self.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) #

[View source]

Class Method Detail

def self.configure(&) #

Configuration block


[View source]
def self.cookie_max_age #

[View source]
def self.cookie_max_age=(value : Int32) #

[View source]
def self.cookie_name #

[View source]
def self.cookie_name=(value : String) #

[View source]
def self.header_name #

[View source]
def self.header_name=(value : String) #

[View source]
def self.metatag(context : HTTP::Server::Context) : String #

[View source]
def self.param_name #

[View source]
def self.param_name=(value : String) #

[View source]
def self.reset_default! #

Reset default instance (useful for testing)


[View source]
def self.secret_key #

[View source]
def self.secret_key=(value : String) #

[View source]
def self.secure_cookies #

[View source]
def self.secure_cookies=(value : Bool) #

[View source]
def self.strategy #

Class-level property accessors that delegate to default instance


[View source]
def self.strategy=(value : Strategy) #

[View source]
def self.tag(context : HTTP::Server::Context) : String #

[View source]
def self.token(context : HTTP::Server::Context) : String #

Class-level methods for backward compatibility These delegate to the default instance


[View source]
def self.use_double_submit! #

[View source]
def self.use_signed_double_submit! #

Configuration helper methods


[View source]
def self.use_synchronizer_token! #

[View source]
def self.validate_origin(context : HTTP::Server::Context) : Bool #

[View source]

Instance Method Detail

def call(context : HTTP::Server::Context) #

[View source]
def cookie_max_age : Int32 #

[View source]
def cookie_max_age=(cookie_max_age : Int32) #

[View source]
def cookie_name : String #

[View source]
def cookie_name=(cookie_name : String) #

[View source]
def cookie_same_site : HTTP::Cookie::SameSite #

[View source]
def cookie_same_site=(cookie_same_site : HTTP::Cookie::SameSite) #

[View source]
def header_name : String #

[View source]
def header_name=(header_name : String) #

[View source]
def metatag(context : HTTP::Server::Context) : String #

Generate meta tag with CSRF token for AJAX requests


[View source]
def param_name : String #

[View source]
def param_name=(param_name : String) #

[View source]
def secret_key : String #

[View source]
def secret_key=(secret_key : String) #

[View source]
def secure_cookies=(secure_cookies : Bool) #

[View source]
def secure_cookies? : Bool #

[View source]
def strategy : Strategy #

Instance-level configuration properties


[View source]
def strategy=(strategy : Strategy) #

Instance-level configuration properties


[View source]
def tag(context : HTTP::Server::Context) : String #

Generate HTML hidden input with CSRF token


[View source]
def token(context : HTTP::Server::Context) : String #

Generate CSRF token for forms/AJAX requests


[View source]
def valid_token?(context : HTTP::Server::Context) : Bool #

Validate CSRF token based on configured strategy


[View source]
def validate_origin(context : HTTP::Server::Context) : Bool #

Origin validation (additional security layer)


[View source]