Rails 5清理帮助程序的白名单css属性
我需要允许内联Rails 5清理帮助程序的白名单css属性,css,ruby-on-rails,ruby-on-rails-5,sanitize,Css,Ruby On Rails,Ruby On Rails 5,Sanitize,我需要允许内联“style=position:absolute;”“通过清理(post.content)输出。我发现他说 config.action_view.sanitized_allowed_css_properties = ['position'] 在application.rb中,会将属性添加到白名单中,但我找不到文档说明Rails 5是否仍然如此,并且在多次重新启动服务器后,它似乎不起作用。有没有一种方法可以轻松添加列入白名单的css属性?这意味着一个猴子补丁,但我不知道在哪里或如何
“style=position:absolute;”“
通过清理(post.content)
输出。我发现他说
config.action_view.sanitized_allowed_css_properties = ['position']
在application.rb中,会将属性添加到白名单中,但我找不到文档说明Rails 5是否仍然如此,并且在多次重新启动服务器后,它似乎不起作用。有没有一种方法可以轻松添加列入白名单的css属性?这意味着一个猴子补丁,但我不知道在哪里或如何这样做
更新:安装允许上面的配置行工作,所以看起来不推荐使用经过消毒的\u allowed\u css\u属性。在Rails 5中肯定有这样做的方法吗?我不能退回到4,我需要将内联样式位置列为白名单,以便让第三方插件(CKEditor+Iframely)正常工作。我最后添加了默认允许属性列表
default_tags = Loofah::HTML5::WhiteList::ALLOWED_CSS_PROPERTIES.add('position')
到application.rb,默认情况下允许位置通过sanitize。不知道这有多安全。我完全不知道@Jim Hogan试图用他的答案做什么。我试过了,但没用。因此,我花了一点时间分析了一切,并找到了自己的答案: 我们从
ActionController::Base.helpers
获得一个名为sanitize\u css
的助手
那么,为什么不通过提取原始样式来使用它呢?Nokogiri包含在Rails>4中
def patched_sanitize(html_tag_string)
sanitize html_tag_string, tags: %w(a b strong), attributes: manual_attributes
end
def manual_attributes
attributes = %w(href target align)
attributes << 'style' unless style_unsafe?
attributes
end
def style_unsafe?
ActionController::Base.helpers.sanitize_css(style_attributes_of(string)).empty?
end
def style_attributes_of(string)
Nokogiri::HTML(self.body).xpath('//body').children.map{|e| e.attr('style')}.join(' ')
end
def修补的\u清理(html\u标记\u字符串)
清理html\u标记\u字符串,标记:%w(a b strong),属性:手动\u属性
结束
def手动属性
属性=%w(href目标对齐)
属性您可以为Rails 5 Sanitarizer在丝瓜白名单中添加多个CSS属性
Loofah::HTML5::WhiteList::ALLOWED_CSS_PROPERTIES.merge %w(position background-image left list-style min-width top z-index)
在application.rb中添加上述行
(同样不确定这有多安全)您可以用“`”来正确标记代码