首页 > 解决方案 > 如何在 Ruby on Rails 中设置特定的 Content-Security-Policy

问题描述

嘿,我有一个 Ror 应用程序,我已经在我的应用程序全局中设置了 CSP,这是我的 config/initilaizers/content_security_policy 中的 CSP 文件

Rails.application.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
end

但我对 ckeditor 和 google 字体有疑问

第一个谷歌字体不会加载字体

第二个ckeditor在这样的样式上有错误

ckeditor.js:94 拒绝应用内联样式,因为它违反了以下内容安全策略指令:“style-src 'self' https:”。启用内联执行需要“unsafe-inline”关键字、哈希(“sha256-ZVjd2zfSTfAVh1y7eCcNk0SPGUQOP/H8vzrFJIVgg90=”)或随机数(“nonce-...”)。

问题是,我可以只为 ckeditor CSP添加特定的unsafe_inline吗?这是因为我只在 rails_admin 中使用这个 CKEditor

为什么在我生成完整性哈希后不会加载谷歌字体?

谢谢

标签: ruby-on-railsckeditorcontent-security-policygoogle-font-api

解决方案


推荐阅读