百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

rails-html-sanitizer

> 编程语言
开源

Rails HTML Sanitizers 这个 gem 负责对 Rails 应用程序中的 HTML 片段进行清洗。具体来说,这是一组用于清洗 HTML 片段的函数:

342 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

Rails HTML Sanitizers 这个 gem 负责对 Rails 应用程序中的 HTML 片段进行清洗。具体来说,这是一组用于清洗 HTML 片段的函数:

Rails HTML Sanitizers

This gem is responsible for sanitizing HTML fragments in Rails applications. Specifically, this is the set of sanitizers used to implement the Action View SanitizerHelper methods sanitize, sanitize_css, strip_tags and strip_links.

Rails HTML Sanitizer is only intended to be used with Rails applications. If you need similar functionality but aren't using Rails, consider using the underlying sanitization library Loofah directly.

Usage

Sanitizers

All sanitizers respond to sanitize, and are available in variants that use either HTML4 or HTML5 parsing, under the Rails::HTML4 and Rails::HTML5 namespaces, respectively.

NOTE: The HTML5 sanitizers are not supported on JRuby. Users may programmatically check for support by calling Rails::HTML::Sanitizer.html5_support?.

FullSanitizer

ruby
full_sanitizer = Rails::HTML5::FullSanitizer.new
full_sanitizer.sanitize("Bold no more!  See more here...")
# => Bold no more!  See more here...

or, if you insist on parsing the content as HTML4:

ruby
full_sanitizer = Rails::HTML4::FullSanitizer.new
full_sanitizer.sanitize("Bold no more!  See more here...")
# => Bold no more!  See more here...

LinkSanitizer

ruby
link_sanitizer = Rails::HTML5::LinkSanitizer.new
link_sanitizer.sanitize('Only the link text will be kept.')
# => Only the link text will be kept.

or, if you insist on parsing the content as HTML4:

ruby
link_sanitizer = Rails::HTML4::LinkSanitizer.new
link_sanitizer.sanitize('Only the link text will be kept.')
# => Only the link text will be kept.

SafeListSanitizer

This sanitizer is also available as an HTML4 variant, but for simplicity we'll document only the HTML5 variant below.

…

Scrubbers

Scrubbers are objects responsible for removing nodes or attributes you don't want in your HTML document.

This gem includes two scrubbers Rails::HTML::PermitScrubber and Rails::HTML::TargetScrubber.

Rails::HTML::PermitScrubber

This scrubber allows you to permit only the tags and attributes you want.

ruby
scrubber = Rails::HTML::PermitScrubber.new
scrubber.tags = ['a']

html_fragment = Loofah.fragment('')
html_fragment.scrub!(scrubber)
html_fragment.to_s # => ""

By default, inner content is left, but it can be removed as well.

ruby
scrubber = Rails::HTML::PermitScrubber.new
scrubber.tags = ['a']

html_fragment = Loofah.fragment('text')
html_fragment.scrub!(scrubber)
html_fragment.to_s # => "text"

scrubber = Rails::HTML::PermitScrubber.new(prune: true)
scrubber.tags = ['a']

html_fragment = Loofah.fragment('text')
html_fragment.scrub!(scrubber)
html_fragment.to_s # => ""

Rails::HTML::TargetScrubber

Where PermitScrubber picks out tags and attributes to permit in sanitization, Rails::HTML::TargetScrubber targets them for removal. See https://github.com/flavorjones/loofah/blob/main/lib/loofah/html5/safelist.rb for the tag list.

Note: by default, it will scrub anything that is not part of the permitted tags from loofah HTML5::Scrub.allowed_element?.

ruby
scrubber = Rails::HTML::TargetScrubber.new
scrubber.tags = ['img']

html_fragment = Loofah.fragment('')
html_fragment.scrub!(scrubber)
html_fragment.to_s # => ""

Similarly to PermitScrubber, nodes can be fully pruned.

ruby
scrubber = Rails::HTML::TargetScrubber.new
scrubber.tags = ['span']

html_fragment = Loofah.fragment('text')
html_fragment.scrub!(scrubber)
html_fragment.to_s # => "text"

scrubber = Rails::HTML::TargetScrubber.new(prune: true)
scrubber.tags = ['span']

html_fragment = Loofah.fragment('text')
html_fragment.scrub!(scrubber)
html_fragment.to_s # => ""

Custom Scrubbers

You can also create custom scrubbers in your application if you want to.

ruby
class CommentScrubber 

A note on HTML entities

Rails HTML sanitizers are intended to be used by the view layer, at page-render time. They are not intended to sanitize persisted strings that will be sanitized again at page-render time.

Proper HTML sanitization will replace some characters with HTML entities. For example, text containing a `= 2.7 | yes | yes | | 1.5.x | >= 2.5 | no | while Rails 6.1 is in security support | | 1.4.x | >= 1.8.7 | no | no |

Read more

Loofah is what underlies the sanitizers and scrubbers of rails-html-sanitizer.

  • Loofah and Loofah Scrubbers

The node argument passed to some methods in a custom scrubber is an instance of Nokogiri::XML::Node.

  • Nokogiri::XML::Node
  • Nokogiri

Contributing to Rails HTML Sanitizers

Rails HTML Sanitizers is work of many contributors. You're encouraged to submit pull requests, propose features and discuss issues.

See CONTRIBUTING.

Security reports

Trying to report a possible security vulnerability in this project? Please check out the Rails project's security policy for instructions.

License

Rails HTML Sanitizers is released under the MIT License.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言