何时(何时没有)运行`sanitize`方法?

问题描述 投票:3回答:1

我想在我的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)
ruby-on-rails ruby security ruby-on-rails-5 sanitization
1个回答
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

正如你所看到的那样,两个html都是用Erb中的一些工具来逃避,然后在最终返回之前还使用html_safe

© www.soinside.com 2019 - 2024. All rights reserved.