Summary
Mongoid resolves the id in a nested attributes hash against the parent's association. When it misses, it falls back to a collection wide lookup that also drops default scopes. The matched document is then mass assigned and pushed into the caller's association. An authenticated user of an application built on Mongoid can therefore overwrite a document belonging to another principal, and gain read access to it, by sending that document's id in a nested form field.
MONGOID-5930 added a feature flag intended to close this. The branch is still reached unconditionally for HABTM associations, so the flag does not protect them. Separately, the flag ships defaulting to on in the 9.0.x series, so has-many is also exposed there out of the box.
Reported by Product Security. See the linked SECBUG for the original finding and the validation record.
The code
The branch, on master:
# lib/mongoid/association/nested/many.rb:194 elsif association.is_a?(Association::Referenced::HasAndBelongsToMany) || Mongoid.allow_reparenting_via_nested_attributes? unless destroyable?(attrs) doc = association.klass.unscoped.find(converted) update_document(doc, attrs) existing.push(doc) end
The HABTM test short circuits the flag check, so the flag is never consulted for those associations.
Confirmed behaviour
Reproduced against master and against the released 9.0.11 gem, with two parent documents owned by different principals. This overwrites the victim's document and adds it to the attacker's association, after which the attacker reads it through ordinary authorized paths. The victim retains it as well.
attacker.update(tags_attributes: [{ id: victim_tag.id.to_s, body: 'overwritten' }])
When the attacker's association is empty, an operator hash survives id conversion and reaches the same collection wide lookup, so a known victim id is not required:
attacker.update(tags_attributes: [{ id: { '$ne' => nil }, body: 'overwritten' }])
When the association is not empty, an operator hash matches the caller's own document via the association scoped query instead, so that variant is narrower.
Two producers reach the builder: the setter generated by the nested attributes macro, and the second producer that needs no macro declaration, here:
lib/mongoid/attributes/processing.rb:155
Deletion is not reachable. The collection wide branch is guarded so a destroy flag does not apply to it. Destroy continues to work only for documents already in the association, which is correct.
A malformed object id hash raises an unhandled BSON error out of the same conversion path, surfacing as a 500 in a Rails application. Minor, but worth fixing alongside:
attacker.update(tags_attributes: [{ id: { '$oid' => 'junk' }, body: 'x' }])
Affected versions
# 8.1.x, and 9.0.10 and earlier: no guard at all; has-many and HABTM both affected # 9.0.11: flag added but declared default: true, so has-many affected out of the box; HABTM affected # 9.1.0 and master: has-many safe by default; HABTM still affected # 9.1.0 with Mongoid::Config.load_defaults('9.0') or earlier: has-many affected again, # because lib/mongoid/config/defaults.rb sets the flag back to true
Comparison with ActiveRecord
ActiveRecord resolves a nested attributes id only within the loaded association and raises a record not found error otherwise, at lines 529 and 545 of:
lib/active_record/nested_attributes.rb
Mongoid diverges from the framework it mirrors here.
Proposed fix
- Drop the unconditional HABTM test from the branch so HABTM honours the same flag as has-many, raising the not found error instead.
- Decide on the 9.0.x default. Either flip it to false in a patch release, or state explicitly that 9.0.x retains the insecure default and document that.
- Reject non scalar id values in the nested builder, so an operator hash cannot survive conversion into an id query, and a malformed object id hash produces a Mongoid error rather than an unhandled BSON error.