什么时候(什么时候该不该)运行`sanitize`方法?

时间:2019-04-06 14:03:08

标签: ruby-on-rails ruby security ruby-on-rails-5 sanitization

我想明确地知道在我的Rails 5应用程序中何时运行sanitize方法(何时不运行,例如因为它不会自动运行)。

例如,在嵌入<%= ... %>内的视图文件中,我应该使用这些文件吗?

sanitize(record.value)

link_to(sanitize(record.value), ...)

tag.span(sanitize(record.value))

t("translation_string_html", :argument_value => sanitize(record.value))

还是这些?

record.value

link_to(record.value, ...)

tag.span(record.value)

t("translation_string_html", :argument_value => record.value)

1 个答案:

答案 0 :(得分:0)

通常使用Rails助手时无需进行消毒。

例如,如果您遵循link_to方法的结尾,则这是相关行

https://www.rubydoc.info/github/rails/rails/ActionView%2FHelpers%2FTagHelper%2FTagBuilder:content_tag_string

def content_tag_string(name, content, options, escape = true)
  tag_options = tag_options(options, escape) if options
  content     = ERB::Util.unwrapped_html_escape(content) if escape
  "<#{name}#{tag_options}>#{PRE_CONTENT_STRINGS[name]}#{content}</#{name}>".html_safe
end

如您所见,它既是用Erb的一些utils进行html转义的,又是html_safe的转义,然后最终返回。