Ruby on rails 如何最好地清理RubyonRails中的字段
我目前有一个控制器在前端从TinyMCE捕获一些html。如果我修补firebug,就可以提交脚本标签并在屏幕上插入警告消息等 编辑:目前我正在使用“清理”辅助对象在模型中修复此问题:Ruby on rails 如何最好地清理RubyonRails中的字段,ruby-on-rails,ruby-on-rails-3,activerecord,tinymce,sanitization,Ruby On Rails,Ruby On Rails 3,Activerecord,Tinymce,Sanitization,我目前有一个控制器在前端从TinyMCE捕获一些html。如果我修补firebug,就可以提交脚本标签并在屏幕上插入警告消息等 编辑:目前我正在使用“清理”辅助对象在模型中修复此问题: require 'action_view' class NotesController < AuthApplicationController include ActionView::Helpers::SanitizeHelper ... def update params[:note]
require 'action_view'
class NotesController < AuthApplicationController
include ActionView::Helpers::SanitizeHelper
...
def update
params[:note][:content] = sanitize(params[:note][:content],
:tags => %w(a object p param h1 h2 h3 h4 h5 h6 br hr ul li img),
:attributes => %w(href name src type value width height data) );
@note.update_attributes(params[:note])
然后在我的模型中,代码折叠为
class MyModel < ActiveRecord::Base
include ::SanitizeUtilities
...
before_save :sanitize_content
...
def sanitize_content
self.content = sanitize_tiny_mce(self.content)
end
end
classmymodel
这似乎可以去除不需要的标记,而不需要太多的麻烦
对rails来说是个新手,我很紧张,可能做错了什么。有人能看到这里潜在的缺点吗
再次感谢我认为您这样做很好,但是如果您在保存之前使用
,那么您仍然可能会失败验证(因为在验证之后调用在保存之前)。此外,您不必将它放入它自己的模块中,它可能只是类上的一个私有方法
比如:
class MyModel < ActiveRecord::Base
before_validation :sanitize_content, :on => :create
private
def sanitize_content
self.content = sanitize_tiny_mce(self.content)
end
def sanitize_tiny_mce(field)
ActionController::Base.helpers.sanitize(field,
:tags => %w(a b i strong em p param h1 h2 h3 h4 h5 h6 br hr ul li img),
:attributes => %w(href name src type value width height data) );
end
end
class MyModel < ActiveRecord::Base
def content= content
write_attribute(:content, sanitize_tiny_mce(content)
end
private
def sanitize_tiny_mce content
ActionController::Base.helpers.sanitize(field,
:tags => %w(a b i strong em p param h1 h2 h3 h4 h5 h6 br hr ul li img),
:attributes => %w(href name src type value width height data)
);
end
end
classmymodel:创建
私有的
def消毒内容
self.content=sanitize\u tiny\u mce(self.content)
结束
def消毒(现场)
ActionController::Base.helpers.sanitize(字段,
:tags=>%w(a b i强em p参数h1 h2 h3 h4 h5 h6 br hr ul li img),
:attributes=>%w(href name src type value width height data));
结束
结束
这个问题似乎是可以回答的,但是对于任何人来说,你可能想考虑使用定制的变异器来让这个更透明。比如:
class MyModel < ActiveRecord::Base
before_validation :sanitize_content, :on => :create
private
def sanitize_content
self.content = sanitize_tiny_mce(self.content)
end
def sanitize_tiny_mce(field)
ActionController::Base.helpers.sanitize(field,
:tags => %w(a b i strong em p param h1 h2 h3 h4 h5 h6 br hr ul li img),
:attributes => %w(href name src type value width height data) );
end
end
class MyModel < ActiveRecord::Base
def content= content
write_attribute(:content, sanitize_tiny_mce(content)
end
private
def sanitize_tiny_mce content
ActionController::Base.helpers.sanitize(field,
:tags => %w(a b i strong em p param h1 h2 h3 h4 h5 h6 br hr ul li img),
:attributes => %w(href name src type value width height data)
);
end
end
classmymodel%w(a b i强em p参数h1 h2 h3 h4 h5 h6 br hr ul li img),
:attributes=>%w(href name src type值宽度高度数据)
);
结束
结束
这将确保内容在任何时候被更改时都被清理干净。在rails中处理这一问题的更常见方法是接受客户端的旧垃圾并保存它(安全、小心地避免SQL注入)。然后,当需要显示时,在那里进行清理。这对我来说似乎很奇怪,为什么我要提交脏数据?如果我在多个位置读取数据,这将增加工作和丢失清理的机会。@noodl同意Chris的观点。提前剥离数据意味着只需执行此过程一次。不是吗剥离它意味着您每次显示数据时都必须执行此过程。正如Chris所说,您可能会忘记保护视图。想必,[:create,:update]
上的也是如此……还有其他最佳做法吗?