|
1 | | -# Be sure to restart your server when you modify this file. |
| 1 | +# frozen_string_literal: true |
2 | 2 |
|
3 | | -# Define an application-wide content security policy. |
4 | | -# See the Securing Rails Applications Guide for more information: |
5 | | -# https://guides.rubyonrails.org/security.html#content-security-policy-header |
| 3 | +# Configure Content Security Policy headers |
| 4 | +# See: https://guides.rubyonrails.org/security.html#content-security-policy-header |
6 | 5 |
|
7 | | -Rails.application.config.to_prepare do |
8 | | - Rails.application.config.content_security_policy do |policy| |
9 | | - policy.default_src '*', :data, :mediastream, :blob, :filesystem, :ws, :wss, :unsafe_eval, :unsafe_inline |
| 6 | +require_dependency 'three_scale/content_security_policy' |
| 7 | + |
| 8 | +if ThreeScale::ContentSecurityPolicy.enabled? |
| 9 | + # Apply configurable CSP from YAML |
| 10 | + Rails.application.configure do |
| 11 | + # Configure nonce generation if enabled |
| 12 | + if ThreeScale::ContentSecurityPolicy.nonce_enabled? |
| 13 | + config.content_security_policy_nonce_generator = ->(request) { |
| 14 | + SecureRandom.base64(16) |
| 15 | + } |
| 16 | + |
| 17 | + nonce_directives = ThreeScale::ContentSecurityPolicy.nonce_directives |
| 18 | + config.content_security_policy_nonce_directives = nonce_directives unless nonce_directives.empty? |
| 19 | + end |
| 20 | + |
| 21 | + # Set report-only mode if configured |
| 22 | + if ThreeScale::ContentSecurityPolicy.report_only? |
| 23 | + config.content_security_policy_report_only = true |
| 24 | + end |
10 | 25 | end |
11 | | -end |
12 | 26 |
|
13 | | -# Rails.application.configure do |
14 | | -# config.content_security_policy do |policy| |
15 | | -# policy.default_src :self, :https |
16 | | -# policy.font_src :self, :https, :data |
17 | | -# policy.img_src :self, :https, :data |
18 | | -# policy.object_src :none |
19 | | -# policy.script_src :self, :https |
20 | | -# policy.style_src :self, :https |
21 | | -# # Specify URI for violation reports |
22 | | -# # policy.report_uri "/csp-violation-report-endpoint" |
23 | | -# end |
24 | | -# |
25 | | -# # Generate session nonces for permitted importmap, inline scripts, and inline styles. |
26 | | -# config.content_security_policy_nonce_generator = ->(request) { request.session.id.to_s } |
27 | | -# config.content_security_policy_nonce_directives = %w(script-src style-src) |
28 | | -# |
29 | | -# # Report violations without enforcing the policy. |
30 | | -# # config.content_security_policy_report_only = true |
31 | | -# end |
| 27 | + # Apply global CSP policy from configuration |
| 28 | + Rails.application.config.to_prepare do |
| 29 | + policy_config = ThreeScale::ContentSecurityPolicy.policy_config |
| 30 | + |
| 31 | + if policy_config.present? |
| 32 | + Rails.application.config.content_security_policy do |policy| |
| 33 | + # Apply each directive from YAML config |
| 34 | + policy_config.each do |directive, sources| |
| 35 | + next unless sources.is_a?(Array) |
| 36 | + |
| 37 | + method_name = directive.to_s |
| 38 | + if policy.respond_to?(method_name) |
| 39 | + policy.public_send(method_name, *sources) |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + # Add report-uri if configured |
| 44 | + if (uri = ThreeScale::ContentSecurityPolicy.report_uri) |
| 45 | + policy.report_uri uri |
| 46 | + end |
| 47 | + end |
| 48 | + end |
| 49 | + end |
| 50 | +else |
| 51 | + # Fallback to permissive policy when config is disabled |
| 52 | + Rails.application.config.to_prepare do |
| 53 | + Rails.application.config.content_security_policy do |policy| |
| 54 | + policy.default_src '*', :data, :mediastream, :blob, :filesystem, :ws, :wss, :unsafe_eval, :unsafe_inline |
| 55 | + end |
| 56 | + end |
| 57 | +end |
0 commit comments