Rails sanitize

The SanitizeHelper module provides a set of methods for scrubbing text of undesired HTML elements. These helper methods extend Action View making them callable within your template files.

只允许 sanitize 方法中指定的标签和属性输出到页面,防止注入

sanitize(html, options = {})

Sanitizes HTML input, stripping all tags and attributes that aren‘t whitelisted.

It also strips href/src attributes with unsafe protocols like javascript:, while also protecting against attempts to use Unicode, ASCII, and hex character references to work around these protocol filters.

The default sanitizer is Rails::Html::WhiteListSanitizer. See Rails HTML Sanitizers for more information.

Custom sanitization rules can also be provided.

Please note that sanitizing user-provided text does not guarantee that the resulting markup is valid or even well-formed. For example, the output may still contain unescaped characters like <>, or &.

  • :tags - An array of allowed tags.
  • :attributes - An array of allowed attributes.
  • :scrubber - A Rails::Html scrubber or Loofah::Scrubber object that defines custom sanitization rules. A custom scrubber takes precedence over custom tags and attributes.
module AnnouncementsHelper
  def safe_content(content)
    sanitize(content, tags: %w(b br))
  end
end
<p>
  <strong><%= t ‘content‘ %></strong>
  <%= safe_content @announcement.content %>
</p>


http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html

时间: 2024-11-25 19:36:26

Rails sanitize的相关文章

Rails中的缓存

最近学习Rails. 看到如下代码: <% if notice %> <p id="notice"><%= notice %></p> <% end %> <h1>Your Pragmatic Catalog</h1> <!-- START_HIGHLIGHT --> <% cache ['store', Product.latest] do %> <!-- END_HIG

rails 字符串 转化为 html

simple_format http://apidock.com/rails/v4.0.2/ActionView/Helpers/TextHelper/simple_format http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html my_text = "Here is some basic text...\n...with a line break." simple_format(my_text)

rails中render 和 redirect_to的区别, each只能用在数组中,如果只有一个或者零个项,用each方法会报错undefined method `each&#39; for #...

在render中,即使有:action,那么也仅仅是取对应的view中的模板(html.erb)而已,所以这里即使浏览器中的url是/orders/xcreate,但是显示的界面是/app/views/orders/xshow的代码 def xcreate flash.now[:notice] = "bb" @order = Order.new(params[:order]) if @order.save flash[:notice] = "yes" render(

RailsCast26 Hackers Love Mass Assignment rails中按params创建、更新model时存在的安全隐患

Mass assignment是rails中常用的将表单数据存储起来的一种方式.不幸的是,它的简洁性成了黑客攻击的目标.下面将解释为什么及如何解决. 上述表单为一个简单的注册表单.当用户填入name,点击提交时,一个新用户被创建.用户模型被如下定义: ruby create_table :users do |t| t.string :name t.boolean :admin, :default => false, :null => false end 当用户点击提交时,如下的action被执

Rails 5 开发进阶

Rails 5 开发进阶:https://www.gitbook.com/book/kelby/rails-beginner-s-guide/details cancan : http://blog.xdite.net/posts/2012/07/30/cancan-rule-engine-authorization-based-library-1/ Ruby官方文档翻译(Ruby官方文档中文版) : http://blog.csdn.net/liuk10/article/details/509

nginx unicorn 来运行rails

一.安装nginx sudo apt-get install nginx 安装完成后查看一下:nginx -v 说明安装成功. ubuntu系统里的安装目录是在/etc/nginx/下,启动程序文件在/usr/sbin/nginx 二.新建项目 rails new app --skip-bundle 完成后修改Gemfile文件:vim Gemfile 把source 修改成taobao或者ruby-china的源. 在这个文件里加入:gem 'unicorn' 然后运行:bundle inst

rails中是如何实现定时任务的之sidetip

环境:ubuntu14.4,ruby2.1.5, rails4.2 sidetip 依赖于sidekiq,所以在Gemfile里要添加sidekiq. 一.新一个rais项目:rails new schedule_job --skip-bundle 进入项目文件夹: cd schedule_job 打开Gemfile文件:vim Gemfile 把source 'https://rubygems.org'改为source 'https://ruby.taobao.org'. 在文件里加入:gem

rails.vim环境安装(ubuntu)

学习和开发Ruby & Rails已经一年多了.但是正式全职Rails项目的开发只有可怜的4个月经验.这期间我一直使用Netbeans 6.x作为开发工具,到目前为止我依然认为它是开发Ruby & Rails程序的最佳开发工具(我没用过最新出来的RubyMine,另外这是一个付费产品,我也不会付费用它,但也不会用D版). 在网上可以看到很到开发Rails的同学使用Vim这个神器来开发的(就在JaveEye也有不少).但是由于我的vim水平只局限于只会使用有限的几个命令( i, :w, :x

how to change the ruby and rails version

changing ruby version checking ruby version run(if you had have install rvm to manage rubies: $rvm list $rvm use ruby-2.0.0-p643 --defualt changing rails version: changing the rails versions when running rails new to build a new app(installing rails