|
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::AdminPortal.enabled? |
| 9 | + # Apply configurable CSP from YAML |
| 10 | + Rails.application.configure do |
| 11 | + # Set report-only mode if configured |
| 12 | + config.content_security_policy_report_only = true if ThreeScale::ContentSecurityPolicy::AdminPortal.report_only? |
10 | 13 | end |
11 | | -end |
12 | 14 |
|
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 |
| 15 | + # Apply global CSP policy from configuration |
| 16 | + Rails.application.config.to_prepare do |
| 17 | + policy_config = ThreeScale::ContentSecurityPolicy::AdminPortal.policy_config |
| 18 | + |
| 19 | + if policy_config.present? |
| 20 | + Rails.application.config.content_security_policy do |policy| |
| 21 | + ThreeScale::ContentSecurityPolicy::AdminPortal.add_policy_config(policy, policy_config) |
| 22 | + end |
| 23 | + end |
| 24 | + end |
| 25 | +else |
| 26 | + # Fallback to permissive policy when config is disabled |
| 27 | + Rails.application.config.to_prepare do |
| 28 | + Rails.application.config.content_security_policy do |policy| |
| 29 | + policy.default_src '*', :data, :mediastream, :blob, :filesystem, :ws, :wss, :unsafe_eval, :unsafe_inline |
| 30 | + end |
| 31 | + end |
| 32 | +end |
0 commit comments