English 中文(简体)
Mongoid: 如何防止大规模转让造成未界定的领域?
原标题:Mongoid: How to prevent undefined fields from being created by mass assignment?

该法典:

class M
  include Mongoid::Document
  field :name
end

params = { name: "foo", age: 20 }
M.create(params)
#=> #<M name: "My Name", age: 20>

<代码><>>/代码>的通知作了界定,但予以保存。

这一点存在问题(可能是多功能系统的来源),因为恶意使用者可以在长官和未知的田中添加任何参数,大范围铺设可 s。 (例如name=foo&bogus=#{x*1000000})

迄今为止,我只找不到任何东西,但<代码>attr_accessible,但对于Mongoid来说,这并不真正大,因为你必须在<条码><<>/代码>和<条码>上保持相同的外地名称。 整个时间,所有模式。 不适用。

我想attr_accessible 有效的记录,是因为a.。 您没有在模型(DRY)和b.中明确界定这些领域,因此无法保证不存在的田地能够拯救到非洲开发银行。 但对于Mongoid而言,我认为应当比attr_accessible找到更好的解决办法。

请注意,有一套全球会议设置<代码>allow_dynamic_fields,但并不涉及大规模派任,因此不属于本次讨论的范围,但我认为这实际上应当是一种每模版的宏观办法,而且应当考虑到大规模派任。

How are you dealing with this problem?

问题回答

I m总使用attr_accessible。 我很少发现自己把所有领域都包括在内。 通常有几个领域可供大规模派任使用。 如果你经常需要包括每个属性,你则对重复表示关切:

attr_accessible *fields.keys

我已做些什么来解决这一问题,在我的模式中,这是在拯救之前使用的:

set_callback(:save, :before) do |doc|
    (doc.attributes.keys - fields.keys).each { |f| doc.unset(f) }
end

这样,即使有额外的属性,在被拯救之前,他们也会被拆除。





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签