Rails Gem 清理 - 如何列入白名单和删除

发布于 2024-12-14 03:34:36 字数 348 浏览 8 评论 0原文

现在我们正在使用 sanitize gem: https://github.com/rgrove/sanitize

问题是如果您输入“hello & world”,清理会将其保存在数据库中:

hello & world 

如何将 & 列入白名单。我们希望通过清理来删除所有可能的恶意 html 和 JS/script 标签。但我们可以允许使用&符号。

有想法吗?谢谢

Right now we're using the sanitize gem: https://github.com/rgrove/sanitize

Problem is if you enter "hello & world" sanitize is saving that in the DB as:

hello & world 

How can you whitelist the & . We want sanitize to remove all possible malicious html and JS/script tags. but we're ok allowing the ampersand.

Ideas? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

寂寞陪衬 2024-12-21 03:34:36

Sanitize 始终会将输出内容转换为有效 html/xhtml 的 html 实体。

我可以确定的最好方法是过滤输出

Sanitize.fragment("hello & world").gsub('&','&') #=> "Hello & world"

Sanitize will always transform what is output into html entities for valid html/xhtml.

The best way I can determine is filter the output

Sanitize.fragment("hello & world").gsub('&','&') #=> "Hello & world"
温馨耳语 2024-12-21 03:34:36

UnixMonkey 的答案就是我们最终所做的。

def remove_markup(html_str)
    marked_up = Sanitize.clean html_str

    ESCAPE_SEQUENCES.each do |esc_seq, ascii_seq|
      marked_up = marked_up.gsub('&' + esc_seq + ';', ascii_seq.chr)
    end
    marked_up
  end

其中 ESCAPE_SEQUENCES 是我们不想转义的字符数组。

UnixMonkey's answer is what we ended up doing.

def remove_markup(html_str)
    marked_up = Sanitize.clean html_str

    ESCAPE_SEQUENCES.each do |esc_seq, ascii_seq|
      marked_up = marked_up.gsub('&' + esc_seq + ';', ascii_seq.chr)
    end
    marked_up
  end

Where ESCAPE_SEQUENCES was an array of the characters we didn't want escaped.

月依秋水 2024-12-21 03:34:36

其他答案都不适合我。我为我的用例找到的最佳方法是使用内置的 Loofah gem:

good = '&'
bad = "<script>alert('I am evil');</script>"
greater_than = '>' # << my use case

Loofah.fragment(good).text(encode_special_chars: false)
# => "&"
Loofah.fragment(greater_than).text(encode_special_chars: false)
# => ">"

Loofah.fragment(bad).text(encode_special_chars: false)
# => "alert('I am evil');"

# And just for clarity, without the option passed in:
Loofah.fragment(good).text
# => "&"

它并不是完美无缺的不过,所以要非常小心:

really_bad = "<script>alert('I am evil');</script>"
Loofah.fragment(really_bad).text(encode_special_chars: false)
# => "<script>alert('I am evil');</script>"

有关指定方法的更多信息 此处

绝对是我需要做的最有效的方法!

None of the other answers worked for me. The best approach I've found for my use case was using the built in Loofah gem:

good = '&'
bad = "<script>alert('I am evil');</script>"
greater_than = '>' # << my use case

Loofah.fragment(good).text(encode_special_chars: false)
# => "&"
Loofah.fragment(greater_than).text(encode_special_chars: false)
# => ">"

Loofah.fragment(bad).text(encode_special_chars: false)
# => "alert('I am evil');"

# And just for clarity, without the option passed in:
Loofah.fragment(good).text
# => "&"

It's not flawless though, so be incredibly careful:

really_bad = "<script>alert('I am evil');</script>"
Loofah.fragment(really_bad).text(encode_special_chars: false)
# => "<script>alert('I am evil');</script>"

More info on the specified method here.

Definitely the most efficient approach for what I needed to do!

笨死的猪 2024-12-21 03:34:36

从 Rails 4.2 开始,#strip_tags 不会取消编码 HTML 特殊字符,

strip_tags("fun & co")
  => "fun & co"

否则您会得到以下结果:

strip_tags("<script>")
  => "<script>"

如果您只想要 & 符号,我建议像 @Unixmonkey 建议的那样过滤输出,并将其保留为 <仅代码>&

strip_tags("<bold>Hello & World</bold>").gsub(/&/, "&")
  => "Hello & World"

As of Rails 4.2, #strip_tags does not unencode HTML special chars

strip_tags("fun & co")
  => "fun & co"

Otherwise you'd get the following:

strip_tags("<script>")
  => "<script>"

If you only want the ampersand I'd suggest filtering the output like @Unixmonkey suggested and keep it to & only

strip_tags("<bold>Hello & World</bold>").gsub(/&/, "&")
  => "Hello & World"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文