Summary
Mongoid's in-memory query contexts turn a caller-supplied field name into a Ruby method call instead of an attribute lookup. A field name of destroy, delete, _root.destroy, attributes or exit therefore performs a persisted delete, discloses a full attribute hash, or terminates the process. The database-backed contexts do not behave this way — the same application code is harmless against a top-level collection and destructive against an embedded association.
Fixes SECBUG-4349 (pluck / pick / tally / distinct) and SECBUG-4351 (sum / avg / min / max), which share this root cause at different sinks. Both from the aegis-ih scan campaign, both validated High.
Reproduction
Confirmed on the published mongoid 9.1.0 gem and on master (8c976ba1c), against a local replica set, default configuration, no dynamic attributes. Runs verbatim:
require 'mongoid' Mongoid.configure do |config| config.clients.default = { hosts: ['localhost:27017'], database: 'mongoid_repro' } end class Address include Mongoid::Document embedded_in :user field :street end class User include Mongoid::Document field :secret embeds_many :addresses end User.delete_all User.create!(secret: 'hunter2', addresses: [{ street: 'W 50th' }]) # 1. Disclosure: the parent document's whole attribute hash, including :secret. p User.first.addresses.pluck('_root.attributes') # 2. Deletion: pluck destroys the parent document. User.first.addresses.pluck('_root.destroy') puts "users remaining: #{User.count}" # 3. Aggregation sink: avg uses a bare send, so Kernel#exit is reachable. User.create!(secret: 'x', addresses: [{ street: 'a' }]) User.first.addresses.criteria.avg('exit') puts 'this line is never reached'
Actual output:
[{"_id" => BSON::ObjectId('...'), "secret" => "hunter2", "addresses" => [{"_id" => ..., "street" => "W 50th"}]}]
users remaining: 0
Step 1 leaks the secret field. Step 2 deletes the parent record — through destroy, so destroy callbacks and dependent-destroy cascades run. Step 3 exits the process, so the final line never prints.
In an application the field name is the attacker's input, e.g. an endpoint that lets the caller choose which field to list or group by: record.addresses.pluck(params[:field]) reached with ?field=_root.destroy
Sinks
- lib/mongoid/contextual/memory.rb:745 — document.try(meth) in retrieve_value_at_path. Segments come from splitting the caller string on dots (:732) and are used verbatim when absent from klass.aliased_associations (:744). Reached from pluck (:262), pick (:276), tally (:290) and distinct (:85). The recursion at :761/:763 dispatches later segments on the returned object, so _root and _parent escape the queried embedded collection. Released in 7.5.0; present on 8.0-stable, 8.1-stable, 9.0-stable and master.
- lib/mongoid/contextual/aggregable/memory.rb:31 — doc.send(field) in avg. Bare send, so private methods including Kernel#exit, #abort and #sleep are reachable. Fix this one first. Released in 8.0.0 (
MONGOID-4500); present on all four branches. - lib/mongoid/contextual/aggregable/memory.rb:117 — doc.public_send(field) in aggregate_by, used by sum, min and max. Dates to 2014, released in 4.0.1. aggregates (:18) fans one field name into all five operations.
- lib/mongoid/association/referenced/has_many/enumerable.rb:593 — doc.public_send(field) in field_values_for, the same problem for referenced (non-embedded) associations, behind sum, avg, min and max (:521, :535, :554, :570). New in 9.1.0, master only — worth fixing before it reaches a stable branch.
Note the dispatch is always zero-argument, so the gadget set is limited to existing no-arg methods. There is no arbitrary-code-execution path.
Approach
Mongoid already contains the safe version of this traversal. Mongoid::Pluckable (lib/mongoid/pluckable.rb) resolves each segment through document_class.traverse_association_tree and reads values with obj.try(:fetch, key, nil) — hash fetches against declared fields, never send. It is included by Contextual::Mongo (mongo.rb:24) and by HasMany::Enumerable (enumerable.rb:15), but Contextual::Memory carries its own retrieve_value_at_path instead, and field_values_for does not use it.
Two parts, so the backports stay low risk:
- Minimal and backportable: keep the existing traversal shape but gate the dispatch. Resolve each segment with document.class.traverse_association_tree(segment) and dispatch only when it resolves to a declared field or association, or matches a _translations name; otherwise read from the attribute hash; otherwise return nil. Apply the same gate in Aggregable::Memory — both avg's nil check and aggregate_by — and in field_values_for.
- Master only, optional follow-up: have Contextual::Memory include Mongoid::Pluckable and drop retrieve_value_at_path, so both contexts share one implementation.
Must be preserved
- The documented "unknown field name returns nil" contract, pinned by spec/mongoid/contextual/memory_spec.rb:1484-1514.
- Localized field and _translations handling (memory.rb:737-743).
- Array traversal with compact (memory.rb:759-761) and Hash-valued segments (:746-752).
- The klass.aliased_associations mapping (:744).
- Aggregable::Memory's nil-ignoring behaviour from
MONGOID-4500.
Tests
No existing spec passes a real method name as a field, so nothing in the suite would fail if dispatch were removed. Add specs asserting that a field name matching a real method returns nil or 0 and performs no dispatch — pluck('destroy'), pluck('_root.destroy'), pluck('attributes'), tally('_root.secret'), distinct('destroy'), sum('delete'), max('destroy'), avg('exit') — checking both that the value is nil and that the document still exists, for embedded and for loaded referenced associations.
Run: spec/mongoid/contextual/memory_spec.rb, spec/mongoid/contextual/aggregable/memory_spec.rb, and the has_many enumerable specs.
Release note
Behaviour change: a field name that happens to name a method now returns nil instead of invoking it.
Backports
9.0, 8.1 and 8.0 per the drivers maintenance policy. field_values_for exists only on master / 9.1.0, so that part does not backport.