Skip to content

Commit

Permalink
feat: Added nonce support
Browse files Browse the repository at this point in the history
  • Loading branch information
Varpuspaavi committed Jul 9, 2024
1 parent 677428b commit 477eda2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
38 changes: 19 additions & 19 deletions examples/demo/config/initializers/content_security_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
# See the Securing Rails Applications Guide for more information:
# https://guides.rubyonrails.org/security.html#content-security-policy-header

# Rails.application.configure do
# config.content_security_policy do |policy|
# policy.default_src :self, :https
# policy.font_src :self, :https, :data
# policy.img_src :self, :https, :data
# policy.object_src :none
# policy.script_src :self, :https
# policy.style_src :self, :https
# # Specify URI for violation reports
# # policy.report_uri "/csp-violation-report-endpoint"
# end
#
# # Generate session nonces for permitted importmap and inline scripts
# config.content_security_policy_nonce_generator = ->(request) { request.session.id.to_s }
# config.content_security_policy_nonce_directives = %w(script-src)
#
# # Report violations without enforcing the policy.
# # config.content_security_policy_report_only = true
# end
Rails.application.configure do
config.content_security_policy do |policy|
policy.default_src :self, :https
policy.font_src :self, :https, :data
policy.img_src :self, :https, :data
policy.object_src :none
policy.script_src :self, :https, :strict_dynamic
policy.style_src :self, :https, :unsafe_inline
# Specify URI for violation reports
# policy.report_uri "/csp-violation-report-endpoint"
end

# Generate session nonces for permitted importmap and inline scripts
config.content_security_policy_nonce_generator = ->(request) { request.session.id.to_s }
config.content_security_policy_nonce_directives = %w[script-src]

# Report violations without enforcing the policy.
# config.content_security_policy_report_only = true
end
2 changes: 1 addition & 1 deletion lib/phraseapp-in-context-editor-ruby/view_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def load_in_context_editor(opts = {})
}.merge(opts)

snippet = <<-EOS
<script>
<script nonce='#{content_security_policy_nonce}'>
window.PHRASEAPP_CONFIG = #{configuration.to_json};
(function() {
let phraseapp = document.createElement('script');
Expand Down

0 comments on commit 477eda2

Please sign in to comment.