class Grip::Middleware::CrossSiteRequestForgery

Included Modules

Defined in:

grip/middleware/cross_site_request_forgery.cr

Constant Summary

CHECK_METHODS = ["PUT", "POST", "PATCH", "DELETE"] of ::String
COOKIE_KEY = "csrf_token"
HEADER_KEY = "X-CSRF-Token"
PARAM_KEY = "_csrf"
TOKEN_LENGTH = 32

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(secret : String, secure : Bool = true) #

[View source]

Class Method Detail

def self.existing_token(context) : Bytes | Nil #

Returns the raw token from a valid signed cookie, or nil


[View source]
def self.mask(raw : Bytes) : String #

XORs the raw token with a random one-time pad (pad + ciphertext), mitigating BREACH


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

Meta tag so JavaScript can read the token and send it as a header


[View source]
def self.raw_token(context) : Bytes #

Returns the raw token, issuing a new cookie if the client has none


[View source]
def self.secret : String #

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

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

[View source]
def self.secure? : Bool #

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

Hidden input for HTML forms


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

Masked token, different on every call; safe to embed in HTML


[View source]
def self.unmask(masked : Bytes) : Bytes #

Reverses mask by XORing the ciphertext with the pad that prefixes it


[View source]
def self.valid?(raw : Bytes, sent : String) : Bool #

Unmasks the submitted token and compares it to the raw token in constant time


[View source]

Instance Method Detail

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

Rejects unsafe requests whose submitted token doesn't match the cookie token


[View source]