SecureCookies
SecureCookies is an extract of the cookie functionality from secure_headers. Rails has good header support but the cookie support is still lacking. Maybe one day this functionality will be added to rails core.
Configuration
These can be defined in the form of a boolean, or as a Hash for more refined configuration.
Note: Regardless of the configuration specified, Secure cookies are only enabled for HTTPS requests.
Defaults
By default, all cookies will get both Secure
, HttpOnly
, and SameSite=Lax
.
config.cookies = {
secure: true,
httponly: true,
samesite: {
lax: true
}
}
Boolean-based configuration
Boolean-based configuration is intended to globally enable or disable a specific cookie attribute. Note: As of 4.0, you must use OPT_OUT rather than false to opt out of the defaults.
config.cookies = {
secure: true,
httponly: OPT_OUT,
}
Hash-based configuration
Hash-based configuration allows for fine-grained control.
config.cookies = {
secure: { except: ['_guest'] },
httponly: { only: ['_rails_session'] },
}
SameSite cookie configuration
SameSite cookies permit either Strict
or Lax
enforcement mode options.
config.cookies = {
samesite: {
strict: true
}
}
Strict
and Lax
enforcement modes can also be specified using a Hash.
config.cookies = {
samesite: {
strict: { only: ['_rails_session'] },
lax: { only: ['_guest'] }
}
}