Class: Otto::Security::Configurator
- Inherits:
-
Object
- Object
- Otto::Security::Configurator
- Defined in:
- lib/otto/security/configurator.rb
Overview
Consolidates all security configuration methods into a single configurator class. This provides a unified interface for configuring CSRF protection, input validation, rate limiting, trusted proxies, and authentication strategies.
Instance Attribute Summary collapse
-
#auth_config ⇒ Object
Returns the value of attribute auth_config.
-
#middleware_stack ⇒ Object
readonly
Returns the value of attribute middleware_stack.
-
#security_config ⇒ Object
readonly
Returns the value of attribute security_config.
Instance Method Summary collapse
-
#add_auth_strategy(name, strategy) ⇒ Object
Add a single authentication strategy.
-
#add_rate_limit_rule(name, options) ⇒ Object
Add a custom rate limiting rule.
-
#add_trusted_proxy(proxy) ⇒ Object
Add a trusted proxy server for accurate client IP detection.
-
#configure(csrf_protection: false, request_validation: false, rate_limiting: false, trusted_proxies: [], security_headers: {}, hsts: false, csp: false, frame_protection: false, authentication: false) ⇒ Object
Unified security configuration method with sensible defaults.
-
#configure_auth_strategies(strategies, default_strategy: 'noauth') ⇒ Object
Configure authentication strategies for route-level access control.
-
#configure_rate_limiting(config) ⇒ Object
Configure rate limiting settings.
-
#enable_csp!(policy = "default-src 'self'") ⇒ Object
Enable Content Security Policy (CSP) header to prevent XSS attacks.
-
#enable_csp_with_nonce!(debug: false) ⇒ Object
Enable Content Security Policy (CSP) with nonce support for dynamic header generation.
-
#enable_csrf_protection! ⇒ Object
Enable CSRF protection for POST, PUT, DELETE, and PATCH requests.
-
#enable_frame_protection!(option = 'SAMEORIGIN') ⇒ Object
Enable X-Frame-Options header to prevent clickjacking attacks.
-
#enable_hsts!(max_age: 31_536_000, include_subdomains: true) ⇒ Object
Enable HTTP Strict Transport Security (HSTS) header.
-
#enable_rate_limiting!(options = {}) ⇒ Object
Enable rate limiting to protect against abuse and DDoS attacks.
-
#enable_request_validation! ⇒ Object
Enable request validation including input sanitization, size limits, and protection against XSS and SQL injection attacks.
-
#initialize(security_config, middleware_stack, auth_config = nil) ⇒ Configurator
constructor
A new instance of Configurator.
-
#security_headers=(headers) ⇒ Object
Set custom security headers that will be added to all responses.
Constructor Details
#initialize(security_config, middleware_stack, auth_config = nil) ⇒ Configurator
Returns a new instance of Configurator.
19 20 21 22 23 24 |
# File 'lib/otto/security/configurator.rb', line 19 def initialize(security_config, middleware_stack, auth_config = nil) @security_config = security_config @middleware_stack = middleware_stack # Use provided auth_config or initialize a new one @auth_config = auth_config || { auth_strategies: {}, default_auth_strategy: 'noauth' } end |
Instance Attribute Details
#auth_config ⇒ Object
Returns the value of attribute auth_config.
17 18 19 |
# File 'lib/otto/security/configurator.rb', line 17 def auth_config @auth_config end |
#middleware_stack ⇒ Object (readonly)
Returns the value of attribute middleware_stack.
16 17 18 |
# File 'lib/otto/security/configurator.rb', line 16 def middleware_stack @middleware_stack end |
#security_config ⇒ Object (readonly)
Returns the value of attribute security_config.
16 17 18 |
# File 'lib/otto/security/configurator.rb', line 16 def security_config @security_config end |
Instance Method Details
#add_auth_strategy(name, strategy) ⇒ Object
Add a single authentication strategy
Part of the Security::Configurator facade for consolidated configuration. This delegates to the same storage as Otto#add_auth_strategy, allowing authentication to be configured alongside other security features.
Prefer using Otto#add_auth_strategy directly for simpler cases, or use this when configuring multiple security features together via the security facade.
185 186 187 188 189 190 191 192 |
# File 'lib/otto/security/configurator.rb', line 185 def add_auth_strategy(name, strategy) # Strict mode: Detect strategy name collisions if @auth_config[:auth_strategies].key?(name) raise ArgumentError, "Authentication strategy '#{name}' is already registered" end @auth_config[:auth_strategies][name] = strategy end |
#add_rate_limit_rule(name, options) ⇒ Object
Add a custom rate limiting rule.
118 119 120 |
# File 'lib/otto/security/configurator.rb', line 118 def add_rate_limit_rule(name, ) @security_config.rate_limiting_config[:custom_rules][name.to_s] = end |
#add_trusted_proxy(proxy) ⇒ Object
Add a trusted proxy server for accurate client IP detection. Only requests from trusted proxies will have their forwarded headers honored.
126 127 128 |
# File 'lib/otto/security/configurator.rb', line 126 def add_trusted_proxy(proxy) @security_config.add_trusted_proxy(proxy) end |
#configure(csrf_protection: false, request_validation: false, rate_limiting: false, trusted_proxies: [], security_headers: {}, hsts: false, csp: false, frame_protection: false, authentication: false) ⇒ Object
Unified security configuration method with sensible defaults
Provides a comprehensive, one-stop configuration method for Otto’s security features. This method allows configuring multiple security aspects in a single call, with flexible options.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/otto/security/configurator.rb', line 56 def configure( csrf_protection: false, request_validation: false, rate_limiting: false, trusted_proxies: [], security_headers: {}, hsts: false, csp: false, frame_protection: false, authentication: false ) enable_csrf_protection! if csrf_protection enable_request_validation! if request_validation enable_rate_limiting!(rate_limiting.is_a?(Hash) ? rate_limiting : {}) if rate_limiting Array(trusted_proxies).each { |proxy| add_trusted_proxy(proxy) } self.security_headers = security_headers unless security_headers.empty? enable_hsts! if hsts enable_csp! if csp enable_frame_protection! if frame_protection end |
#configure_auth_strategies(strategies, default_strategy: 'noauth') ⇒ Object
Configure authentication strategies for route-level access control.
198 199 200 201 202 |
# File 'lib/otto/security/configurator.rb', line 198 def configure_auth_strategies(strategies, default_strategy: 'noauth') # Merge new strategies with existing ones, preserving shared state @auth_config[:auth_strategies].merge!(strategies) @auth_config[:default_auth_strategy] = default_strategy end |
#configure_rate_limiting(config) ⇒ Object
Configure rate limiting settings.
210 211 212 |
# File 'lib/otto/security/configurator.rb', line 210 def configure_rate_limiting(config) @security_config.rate_limiting_config.merge!(config) end |
#enable_csp!(policy = "default-src 'self'") ⇒ Object
Enable Content Security Policy (CSP) header to prevent XSS attacks. The default policy only allows resources from the same origin.
152 153 154 |
# File 'lib/otto/security/configurator.rb', line 152 def enable_csp!(policy = "default-src 'self'") @security_config.enable_csp!(policy) end |
#enable_csp_with_nonce!(debug: false) ⇒ Object
Enable Content Security Policy (CSP) with nonce support for dynamic header generation. This enables the res.send_csp_headers response helper method.
167 168 169 |
# File 'lib/otto/security/configurator.rb', line 167 def enable_csp_with_nonce!(debug: false) @security_config.enable_csp_with_nonce!(debug: debug) end |
#enable_csrf_protection! ⇒ Object
Enable CSRF protection for POST, PUT, DELETE, and PATCH requests. This will automatically add CSRF tokens to HTML forms and validate them on unsafe HTTP methods.
82 83 84 85 86 87 |
# File 'lib/otto/security/configurator.rb', line 82 def enable_csrf_protection! return if middleware_enabled?(Otto::Security::Middleware::CSRFMiddleware) @security_config.enable_csrf_protection! @middleware_stack.add(Otto::Security::Middleware::CSRFMiddleware) end |
#enable_frame_protection!(option = 'SAMEORIGIN') ⇒ Object
Enable X-Frame-Options header to prevent clickjacking attacks.
159 160 161 |
# File 'lib/otto/security/configurator.rb', line 159 def enable_frame_protection!(option = 'SAMEORIGIN') @security_config.enable_frame_protection!(option) end |
#enable_hsts!(max_age: 31_536_000, include_subdomains: true) ⇒ Object
Enable HTTP Strict Transport Security (HSTS) header. WARNING: This can make your domain inaccessible if HTTPS is not properly configured. Only enable this when you’re certain HTTPS is working correctly.
144 145 146 |
# File 'lib/otto/security/configurator.rb', line 144 def enable_hsts!(max_age: 31_536_000, include_subdomains: true) @security_config.enable_hsts!(max_age: max_age, include_subdomains: include_subdomains) end |
#enable_rate_limiting!(options = {}) ⇒ Object
Enable rate limiting to protect against abuse and DDoS attacks. This will automatically add rate limiting rules based on client IP.
104 105 106 107 108 109 |
# File 'lib/otto/security/configurator.rb', line 104 def enable_rate_limiting!( = {}) return if middleware_enabled?(Otto::Security::Middleware::RateLimitMiddleware) configure_rate_limiting() @middleware_stack.add(Otto::Security::Middleware::RateLimitMiddleware) end |
#enable_request_validation! ⇒ Object
Enable request validation including input sanitization, size limits, and protection against XSS and SQL injection attacks.
91 92 93 94 95 96 |
# File 'lib/otto/security/configurator.rb', line 91 def enable_request_validation! return if middleware_enabled?(Otto::Security::Middleware::ValidationMiddleware) @security_config.input_validation = true @middleware_stack.add(Otto::Security::Middleware::ValidationMiddleware) end |
#security_headers=(headers) ⇒ Object
Set custom security headers that will be added to all responses. These merge with the default security headers.
134 135 136 |
# File 'lib/otto/security/configurator.rb', line 134 def security_headers=(headers) @security_config.security_headers.merge!(headers) end |