Rails安全性:完全避免批量分配
我倾向于不需要生产代码中的质量分配function。 (在我的测试代码中,我经常使用它,但在这些情况下,我确实想要设置任意列。)
因此,如果在我的生产代码中,我只是避免使用这些forms:
Article.new(params[:article]) # or create article.attributes = params[:article] article.update_attributes(params[:article])
而是始终手动枚举所有属性,如下所示:
Article.new(:title => params[:article][:title], :body => params[:article][:body], ...)
我是否从质量分配安全问题中attr_accessible
attr_protected
(即使不使用attr_accessible
/ attr_protected
)?
编辑:我不只是禁用批量分配的原因是,我希望能够编写Article.create!(:blog_id => @blog.id, ...)
,其中blog_id是一个“unsave”属性。
是的,使用第二种方法,您可以安全地从用户分配其他属性。
这是一种干燥的写作方式,但是:
Article.new(params[:article].slice(:title, :body))
-要么-
def article_params params[:article].slice(:title, :body) end Article.new(article_params) # or create article.attributes = article_params article.update_attributes(article_params)
在config/environments/production.rb
的末尾添加:
ActiveRecord::Base.send(:attr_accessible, nil)
我无法让John Douthat的方法适用于多个参数,所以我想出了以下替代方法(取自我的CommentsController):
def set_params @comment.parent_id = params[:blog_comment][:parent_id] @comment.ip_address = request.remote_ip @comment.commentator = current_user.username || "anonymous" @comment.name = params[:blog_comment][:name] @comment.email = params[:blog_comment][:email] @comment.url = params[:blog_comment][:url] end def create @comment = @post.comments.build(params[:blog_comment]) set_params if @comment.save ... end def update @comment = Blog::Comment.find(params[:id]) set_params if @comment.update_attributes(params[:blog_comment]) ... end