Skip to content

Commit 6006491

Browse files
authored
Content Security Policy (#1964)
Default to a very narrow policy since there are no CDNs or third-party resources to contend with. Configurable via: - config.x.content_security_policy.* for fizzy-saas gem overrides - DISABLE_CSP to skip entirely - CSP_REPORT_ONLY to enable report-only mode - CSP_REPORT_URI for violation reporting
1 parent 4c09410 commit 6006491

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

Gemfile.saas.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ GIT
2121

2222
GIT
2323
remote: https://github.com/basecamp/fizzy-saas
24-
revision: 97d126e0a6084905aceaadf4facaa2a48b44e124
24+
revision: 3b30f3d56d8318bb9256469cb91c06a5a5c760a6
2525
specs:
2626
fizzy-saas (0.1.0)
2727
audits1984
Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
11
# Be sure to restart your server when you modify this file.
22

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+
# Define an application-wide Content Security Policy.
4+
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy
65

7-
# Rails.application.configure do
8-
# config.content_security_policy do |policy|
9-
# policy.default_src :self, :https
10-
# policy.font_src :self, :https, :data
11-
# policy.img_src :self, :https, :data
12-
# policy.object_src :none
13-
# policy.script_src :self, :https
14-
# policy.style_src :self, :https
15-
# # Specify URI for violation reports
16-
# # policy.report_uri "/csp-violation-report-endpoint"
17-
# end
18-
#
19-
# # Generate session nonces for permitted importmap, inline scripts, and inline styles.
20-
# config.content_security_policy_nonce_generator = ->(request) { request.session.id.to_s }
21-
# config.content_security_policy_nonce_directives = %w(script-src style-src)
22-
#
23-
# # Report violations without enforcing the policy.
24-
# # config.content_security_policy_report_only = true
25-
# end
6+
Rails.application.configure do
7+
# Configure from environment variables (fizzy-saas can override by setting config.x values first)
8+
config.x.content_security_policy.report_uri ||= ENV["CSP_REPORT_URI"]
9+
config.x.content_security_policy.report_only ||= ENV["CSP_REPORT_ONLY"] == "true"
10+
11+
# Generate nonces for importmap and inline scripts
12+
config.content_security_policy_nonce_generator = ->(request) { SecureRandom.base64(16) }
13+
config.content_security_policy_nonce_directives = %w[ script-src ]
14+
15+
config.content_security_policy do |policy|
16+
policy.script_src :self
17+
policy.style_src :self, :unsafe_inline
18+
policy.img_src :self, "blob:", "data:", "https:"
19+
policy.font_src :self
20+
policy.object_src :none
21+
22+
policy.base_uri :none
23+
policy.form_action :self
24+
policy.frame_ancestors :self
25+
26+
# Specify URI for violation reports (e.g., Sentry CSP endpoint)
27+
if report_uri = config.x.content_security_policy.report_uri
28+
policy.report_uri report_uri
29+
end
30+
end
31+
32+
# Report violations without enforcing the policy.
33+
config.content_security_policy_report_only = config.x.content_security_policy.report_only
34+
end unless ENV["DISABLE_CSP"]

0 commit comments

Comments
 (0)