class Session::Configuration

Defined in:

configuration.cr

Constant Summary

DEFAULT_SECRET = "1sxc1aNxGHTTZKlK5cpCgufJAqGM4G13"

The secret older releases signed with when none was configured. It is public, so it is never used by default any more and is refused where #require_secure_secret holds.

MIN_SECRET_BYTES = 32

AES-256 takes a 32-byte key, and the secret is that key unless #use_kdf derives one. The HMAC signature always uses the secret itself, so the minimum holds with #use_kdf too.

PRODUCTION_ENVIRONMENTS = ["production", "staging"] of ::String

Environments that must not run on the published default secret.

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.from_preset(preset : Symbol) : Configuration #

Load configuration from a preset (returns a new Configuration instance) Available presets: :development, :production, :high_security, :testing, :clustered


[View source]

Class Method Detail

def self.process_secret : String #

The random fallback, one per process so every configuration agrees on it; warns once.


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

[View source]

Instance Method Detail

def allow_insecure_cookies : Bool #

Where a secure secret is required (production, staging), insecure cookies are refused too, unless this opts out explicitly.


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

Where a secure secret is required (production, staging), insecure cookies are refused too, unless this opts out explicitly.


[View source]
def apply_preset(preset : Symbol) : self #

Apply a preset's settings to this configuration instance Available presets: :development, :production, :high_security, :testing, :clustered


[View source]
def bind_to_ip : Bool #

Session binding: bind sessions to client fingerprint for security


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

Session binding: bind sessions to client fingerprint for security


[View source]
def bind_to_user_agent : Bool #

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

[View source]
def circuit_breaker_config : CircuitBreakerConfig #

[View source]
def circuit_breaker_config=(circuit_breaker_config : CircuitBreakerConfig) #

[View source]
def circuit_breaker_enabled : Bool #

Circuit breaker configuration for Redis connections


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

Circuit breaker configuration for Redis connections


[View source]
def cluster : ClusterConfig #

Cluster configuration for multi-node deployments with Redis Pub/Sub


[View source]
def cluster=(cluster : ClusterConfig) #

Cluster configuration for multi-node deployments with Redis Pub/Sub


[View source]
def compress_data : Bool #

Data compression: when enabled, session data is compressed before storage


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

Data compression: when enabled, session data is compressed before storage


[View source]
def compression_threshold : Int32 #

Minimum size in bytes before compression is applied (smaller data may grow with compression)


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

Minimum size in bytes before compression is applied (smaller data may grow with compression)


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

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

[View source]
def cookie_secure : Bool #

Cookie attributes. Cookies are always HttpOnly. Secure keeps them off plain http (browsers still accept a Secure cookie over http on localhost); turn it off only for an http-only setup.


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

Cookie attributes. Cookies are always HttpOnly. Secure keeps them off plain http (browsers still accept a Secure cookie over http on localhost); turn it off only for an http-only setup.


[View source]
def digest_algorithm : Symbol #

Digest algorithm for HMAC signatures (:sha256 recommended, :sha1 for legacy)


[View source]
def digest_algorithm=(digest_algorithm : Symbol) #

Digest algorithm for HMAC signatures (:sha256 recommended, :sha1 for legacy)


[View source]
def digest_fallback : Bool #

Enable auto-fallback to SHA1 for migrating old sessions (logs deprecation warning)


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

Enable auto-fallback to SHA1 for migrating old sessions (logs deprecation warning)


[View source]
def enable_retry : Bool #

Error handling configuration


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

Error handling configuration


[View source]
def encrypt_redis_data : Bool #

Redis encryption: when enabled, session data stored in Redis is encrypted


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

Redis encryption: when enabled, session data stored in Redis is encrypted


[View source]
def encryptor : Message::Encryptor #

The encryptor for the current settings. It is reused while they stay the same, so PBKDF2 (#use_kdf) runs once per secret, salt and iteration count, not on every encrypt and decrypt.


[View source]
def fail_fast_on_corruption : Bool #

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

[View source]
def kdf_iterations : Int32 #

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

[View source]
def kdf_salt : String #

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

[View source]
def log_errors : Bool #

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

[View source]
def metrics_backend : Metrics::Backend #

Metrics backend for observability (default: NullBackend discards all metrics)


[View source]
def metrics_backend=(metrics_backend : Metrics::Backend) #

Metrics backend for observability (default: NullBackend discards all metrics)


[View source]
def on_client : Proc(String, Session::Base, Nil) #

[View source]
def on_client=(on_client : Proc(String, Session::Base, Nil)) #

[View source]
def on_deleted : Proc(String, Session::Base, Nil) #

[View source]
def on_deleted=(on_deleted : Proc(String, Session::Base, Nil)) #

[View source]
def on_loaded : Proc(String, Session::Base, Nil) #

[View source]
def on_loaded=(on_loaded : Proc(String, Session::Base, Nil)) #

[View source]
def on_regenerated : Proc(String, String, Session::Base, Nil) #

[View source]
def on_regenerated=(on_regenerated : Proc(String, String, Session::Base, Nil)) #

[View source]
def on_started : Proc(String, Session::Base, Nil) #

[View source]
def on_started=(on_started : Proc(String, Session::Base, Nil)) #

[View source]
def require_secure_secret : Bool #

[View source]
def require_secure_secret=(require_secure_secret : Bool | Nil) #

Whether a missing or published secret is an error rather than a warning. Unless set, it holds in production and staging, read from CRYSTAL_ENV (case-insensitive) each time it is checked.


[View source]
def retry_config : RetryConfig #

Retry configuration for resilient operations


[View source]
def retry_config=(retry_config : RetryConfig) #

Retry configuration for resilient operations


[View source]
def secret : String #

The key sessions are encrypted and signed with: the one configured, else SESSION_SECRET. Without either, InsecureSecretException where #require_secure_secret holds, and otherwise a random key for this process only (sessions do not survive a restart or reach another instance). Not memoised: the random fallback is refused again as soon as #require_secure_secret holds.


[View source]
def secret=(secret : String | Nil) #

[View source]
def session #

[View source]
def session_key : String #

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

[View source]
def sliding_expiration : Bool #

Sliding expiration: when enabled, session timeout resets on each access


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

Sliding expiration: when enabled, session timeout resets on each access


[View source]
def timeout : Time::Span #

[View source]
def timeout=(timeout : Time::Span) #

[View source]
def trust_proxy_headers : Bool #

Whether X-Forwarded-For and X-Real-IP name the client for IP binding. Any client can send them, so they are believed only when a trusted proxy in front of the app sets them.


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

Whether X-Forwarded-For and X-Real-IP name the client for IP binding. Any client can send them, so they are believed only when a trusted proxy in front of the app sets them.


[View source]
def use_kdf : Bool #

Key Derivation Function (KDF) settings When enabled, derives cipher key from secret using PBKDF2-SHA256


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

Key Derivation Function (KDF) settings When enabled, derives cipher key from secret using PBKDF2-SHA256


[View source]
def using_default_secret? : Bool #

Check if the current secret is the insecure default


[View source]
def validate_cookie_options! : Nil #

Raises InvalidCookieOptions for cookie options browsers reject or that defeat the secret requirement.


[View source]
def validate_secret! : Nil #

Validate secret security - warns or raises based on configuration


[View source]
def validate_secret_for!(store : Store) : Nil #

Raises InsecureSecretException when store encrypts with the secret (Store#encrypts_with_secret?: a CookieStore, or a Redis store with #encrypt_redis_data) and the secret is missing, published or too short.


[View source]